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/slab.h> 15 #include <linux/crc32.h> 16 #include <linux/magic.h> 17 #include <linux/kobject.h> 18 #include <linux/sched.h> 19 #include <linux/cred.h> 20 #include <linux/sched/mm.h> 21 #include <linux/vmalloc.h> 22 #include <linux/bio.h> 23 #include <linux/blkdev.h> 24 #include <linux/quotaops.h> 25 #include <linux/part_stat.h> 26 #include <linux/rw_hint.h> 27 28 #include <linux/fscrypt.h> 29 #include <linux/fsverity.h> 30 31 struct pagevec; 32 33 #ifdef CONFIG_F2FS_CHECK_FS 34 #define f2fs_bug_on(sbi, condition) BUG_ON(condition) 35 #else 36 #define f2fs_bug_on(sbi, condition) \ 37 do { \ 38 if (WARN_ON(condition)) \ 39 set_sbi_flag(sbi, SBI_NEED_FSCK); \ 40 } while (0) 41 #endif 42 43 enum { 44 FAULT_KMALLOC, 45 FAULT_KVMALLOC, 46 FAULT_PAGE_ALLOC, 47 FAULT_PAGE_GET, 48 FAULT_ALLOC_BIO, /* it's obsolete due to bio_alloc() will never fail */ 49 FAULT_ALLOC_NID, 50 FAULT_ORPHAN, 51 FAULT_BLOCK, 52 FAULT_DIR_DEPTH, 53 FAULT_EVICT_INODE, 54 FAULT_TRUNCATE, 55 FAULT_READ_IO, 56 FAULT_CHECKPOINT, 57 FAULT_DISCARD, /* it's obsolete due to __blkdev_issue_discard() will never fail */ 58 FAULT_WRITE_IO, 59 FAULT_SLAB_ALLOC, 60 FAULT_DQUOT_INIT, 61 FAULT_LOCK_OP, 62 FAULT_BLKADDR_VALIDITY, 63 FAULT_BLKADDR_CONSISTENCE, 64 FAULT_NO_SEGMENT, 65 FAULT_INCONSISTENT_FOOTER, 66 FAULT_ATOMIC_TIMEOUT, 67 FAULT_VMALLOC, 68 FAULT_LOCK_TIMEOUT, 69 FAULT_SKIP_WRITE, 70 FAULT_MAX, 71 }; 72 73 /* indicate which option to update */ 74 enum fault_option { 75 FAULT_RATE = 1, /* only update fault rate */ 76 FAULT_TYPE = 2, /* only update fault type */ 77 FAULT_TIMEOUT = 4, /* only update fault timeout type */ 78 FAULT_ALL = 8, /* reset all fault injection options/stats */ 79 }; 80 81 #ifdef CONFIG_F2FS_FAULT_INJECTION 82 struct f2fs_fault_info { 83 atomic_t inject_ops; 84 int inject_rate; 85 unsigned int inject_type; 86 /* Used to account total count of injection for each type */ 87 unsigned int inject_count[FAULT_MAX]; 88 unsigned int inject_lock_timeout; /* inject lock timeout */ 89 }; 90 91 extern const char *f2fs_fault_name[FAULT_MAX]; 92 #define IS_FAULT_SET(fi, type) ((fi)->inject_type & BIT(type)) 93 94 /* maximum retry count for injected failure */ 95 #define DEFAULT_FAILURE_RETRY_COUNT 8 96 #else 97 #define DEFAULT_FAILURE_RETRY_COUNT 1 98 #endif 99 100 /* 101 * For mount options 102 */ 103 enum f2fs_mount_opt { 104 F2FS_MOUNT_DISABLE_ROLL_FORWARD, 105 F2FS_MOUNT_DISCARD, 106 F2FS_MOUNT_NOHEAP, 107 F2FS_MOUNT_XATTR_USER, 108 F2FS_MOUNT_POSIX_ACL, 109 F2FS_MOUNT_DISABLE_EXT_IDENTIFY, 110 F2FS_MOUNT_INLINE_XATTR, 111 F2FS_MOUNT_INLINE_DATA, 112 F2FS_MOUNT_INLINE_DENTRY, 113 F2FS_MOUNT_FLUSH_MERGE, 114 F2FS_MOUNT_NOBARRIER, 115 F2FS_MOUNT_FASTBOOT, 116 F2FS_MOUNT_READ_EXTENT_CACHE, 117 F2FS_MOUNT_DATA_FLUSH, 118 F2FS_MOUNT_FAULT_INJECTION, 119 F2FS_MOUNT_USRQUOTA, 120 F2FS_MOUNT_GRPQUOTA, 121 F2FS_MOUNT_PRJQUOTA, 122 F2FS_MOUNT_QUOTA, 123 F2FS_MOUNT_INLINE_XATTR_SIZE, 124 F2FS_MOUNT_RESERVE_ROOT, 125 F2FS_MOUNT_DISABLE_CHECKPOINT, 126 F2FS_MOUNT_NORECOVERY, 127 F2FS_MOUNT_ATGC, 128 F2FS_MOUNT_MERGE_CHECKPOINT, 129 F2FS_MOUNT_GC_MERGE, 130 F2FS_MOUNT_COMPRESS_CACHE, 131 F2FS_MOUNT_AGE_EXTENT_CACHE, 132 F2FS_MOUNT_NAT_BITS, 133 F2FS_MOUNT_INLINECRYPT, 134 /* 135 * Some f2fs environments expect to be able to pass the "lazytime" option 136 * string rather than using the MS_LAZYTIME flag, so this must remain. 137 */ 138 F2FS_MOUNT_LAZYTIME, 139 F2FS_MOUNT_RESERVE_NODE, 140 }; 141 142 #define F2FS_OPTION(sbi) ((sbi)->mount_opt) 143 #define clear_opt(sbi, option) \ 144 (F2FS_OPTION(sbi).opt &= ~BIT(F2FS_MOUNT_##option)) 145 #define set_opt(sbi, option) \ 146 (F2FS_OPTION(sbi).opt |= BIT(F2FS_MOUNT_##option)) 147 #define test_opt(sbi, option) \ 148 (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option)) 149 150 #define ver_after(a, b) (typecheck(unsigned long long, a) && \ 151 typecheck(unsigned long long, b) && \ 152 ((long long)((a) - (b)) > 0)) 153 154 typedef u32 block_t; /* 155 * should not change u32, since it is the on-disk block 156 * address format, __le32. 157 */ 158 typedef u32 nid_t; 159 160 #define COMPRESS_EXT_NUM 16 161 162 enum blkzone_allocation_policy { 163 BLKZONE_ALLOC_PRIOR_SEQ, /* Prioritize writing to sequential zones */ 164 BLKZONE_ALLOC_ONLY_SEQ, /* Only allow writing to sequential zones */ 165 BLKZONE_ALLOC_PRIOR_CONV, /* Prioritize writing to conventional zones */ 166 }; 167 168 enum bggc_io_aware_policy { 169 AWARE_ALL_IO, /* skip background GC if there is any kind of pending IO */ 170 AWARE_READ_IO, /* skip background GC if there is pending read IO */ 171 AWARE_NONE, /* don't aware IO for background GC */ 172 }; 173 174 enum device_allocation_policy { 175 ALLOCATE_FORWARD_NOHINT, 176 ALLOCATE_FORWARD_WITHIN_HINT, 177 ALLOCATE_FORWARD_FROM_HINT, 178 }; 179 180 enum f2fs_lock_name { 181 LOCK_NAME_NONE, 182 LOCK_NAME_CP_RWSEM, 183 LOCK_NAME_NODE_CHANGE, 184 LOCK_NAME_NODE_WRITE, 185 LOCK_NAME_GC_LOCK, 186 LOCK_NAME_CP_GLOBAL, 187 LOCK_NAME_IO_RWSEM, 188 LOCK_NAME_MAX, 189 }; 190 191 enum f2fs_timeout_type { 192 TIMEOUT_TYPE_NONE, 193 TIMEOUT_TYPE_RUNNING, 194 TIMEOUT_TYPE_IO_SLEEP, 195 TIMEOUT_TYPE_NONIO_SLEEP, 196 TIMEOUT_TYPE_RUNNABLE, 197 TIMEOUT_TYPE_MAX, 198 }; 199 200 /* 201 * An implementation of an rwsem that is explicitly unfair to readers. This 202 * prevents priority inversion when a low-priority reader acquires the read lock 203 * while sleeping on the write lock but the write lock is needed by 204 * higher-priority clients. 205 */ 206 207 struct f2fs_rwsem { 208 struct f2fs_sb_info *sbi; 209 enum f2fs_lock_name name; 210 struct rw_semaphore internal_rwsem; 211 #ifdef CONFIG_F2FS_UNFAIR_RWSEM 212 wait_queue_head_t read_waiters; 213 #endif 214 }; 215 216 struct f2fs_mount_info { 217 unsigned long long opt; 218 block_t root_reserved_blocks; /* root reserved blocks */ 219 block_t root_reserved_nodes; /* root reserved nodes */ 220 kuid_t s_resuid; /* reserved blocks for uid */ 221 kgid_t s_resgid; /* reserved blocks for gid */ 222 int active_logs; /* # of active logs */ 223 int inline_xattr_size; /* inline xattr size */ 224 #ifdef CONFIG_F2FS_FAULT_INJECTION 225 struct f2fs_fault_info fault_info; /* For fault injection */ 226 #endif 227 #ifdef CONFIG_QUOTA 228 /* Names of quota files with journalled quota */ 229 char *s_qf_names[MAXQUOTAS]; 230 int s_jquota_fmt; /* Format of quota to use */ 231 #endif 232 /* For which write hints are passed down to block layer */ 233 int alloc_mode; /* segment allocation policy */ 234 int fsync_mode; /* fsync policy */ 235 int fs_mode; /* fs mode: LFS or ADAPTIVE */ 236 int bggc_mode; /* bggc mode: off, on or sync */ 237 int memory_mode; /* memory mode */ 238 int errors; /* errors parameter */ 239 int discard_unit; /* 240 * discard command's offset/size should 241 * be aligned to this unit: block, 242 * segment or section 243 */ 244 struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */ 245 block_t unusable_cap_perc; /* percentage for cap */ 246 block_t unusable_cap; /* Amount of space allowed to be 247 * unusable when disabling checkpoint 248 */ 249 250 /* For compression */ 251 unsigned char compress_algorithm; /* algorithm type */ 252 unsigned char compress_log_size; /* cluster log size */ 253 unsigned char compress_level; /* compress level */ 254 bool compress_chksum; /* compressed data chksum */ 255 unsigned char compress_ext_cnt; /* extension count */ 256 unsigned char nocompress_ext_cnt; /* nocompress extension count */ 257 int compress_mode; /* compression mode */ 258 unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */ 259 unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */ 260 unsigned int lookup_mode; 261 }; 262 263 #define F2FS_FEATURE_ENCRYPT 0x00000001 264 #define F2FS_FEATURE_BLKZONED 0x00000002 265 #define F2FS_FEATURE_ATOMIC_WRITE 0x00000004 266 #define F2FS_FEATURE_EXTRA_ATTR 0x00000008 267 #define F2FS_FEATURE_PRJQUOTA 0x00000010 268 #define F2FS_FEATURE_INODE_CHKSUM 0x00000020 269 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x00000040 270 #define F2FS_FEATURE_QUOTA_INO 0x00000080 271 #define F2FS_FEATURE_INODE_CRTIME 0x00000100 272 #define F2FS_FEATURE_LOST_FOUND 0x00000200 273 #define F2FS_FEATURE_VERITY 0x00000400 274 #define F2FS_FEATURE_SB_CHKSUM 0x00000800 275 #define F2FS_FEATURE_CASEFOLD 0x00001000 276 #define F2FS_FEATURE_COMPRESSION 0x00002000 277 #define F2FS_FEATURE_RO 0x00004000 278 #define F2FS_FEATURE_DEVICE_ALIAS 0x00008000 279 #define F2FS_FEATURE_PACKED_SSA 0x00010000 280 281 #define __F2FS_HAS_FEATURE(raw_super, mask) \ 282 ((raw_super->feature & cpu_to_le32(mask)) != 0) 283 #define F2FS_HAS_FEATURE(sbi, mask) __F2FS_HAS_FEATURE(sbi->raw_super, mask) 284 285 /* 286 * Default values for user and/or group using reserved blocks 287 */ 288 #define F2FS_DEF_RESUID 0 289 #define F2FS_DEF_RESGID 0 290 291 /* 292 * For checkpoint manager 293 */ 294 enum { 295 NAT_BITMAP, 296 SIT_BITMAP 297 }; 298 299 #define CP_UMOUNT 0x00000001 300 #define CP_FASTBOOT 0x00000002 301 #define CP_SYNC 0x00000004 302 #define CP_RECOVERY 0x00000008 303 #define CP_DISCARD 0x00000010 304 #define CP_TRIMMED 0x00000020 305 #define CP_PAUSE 0x00000040 306 #define CP_RESIZE 0x00000080 307 308 #define DEF_MAX_DISCARD_REQUEST 8 /* issue 8 discards per round */ 309 #define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */ 310 #define DEF_MID_DISCARD_ISSUE_TIME 500 /* 500 ms, if device busy */ 311 #define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */ 312 #define DEF_DISCARD_URGENT_UTIL 80 /* do more discard over 80% */ 313 #define DEF_CP_INTERVAL 60 /* 60 secs */ 314 #define DEF_IDLE_INTERVAL 5 /* 5 secs */ 315 #define DEF_DISABLE_INTERVAL 5 /* 5 secs */ 316 #define DEF_DISABLE_QUICK_INTERVAL 1 /* 1 secs */ 317 #define DEF_UMOUNT_DISCARD_TIMEOUT 5 /* 5 secs */ 318 319 enum cp_time { 320 CP_TIME_START, /* begin */ 321 CP_TIME_LOCK, /* after cp_global_sem */ 322 CP_TIME_OP_LOCK, /* after block_operation */ 323 CP_TIME_MERGE_WRITE, /* after flush DATA/NODE/META */ 324 CP_TIME_FLUSH_NAT, /* after flush nat */ 325 CP_TIME_FLUSH_SIT, /* after flush sit */ 326 CP_TIME_SYNC_META, /* after sync_meta_pages */ 327 CP_TIME_SYNC_CP_META, /* after sync cp meta pages */ 328 CP_TIME_WAIT_DIRTY_META,/* after wait on dirty meta */ 329 CP_TIME_WAIT_CP_DATA, /* after wait on cp data */ 330 CP_TIME_FLUSH_DEVICE, /* after flush device cache */ 331 CP_TIME_WAIT_LAST_CP, /* after wait on last cp pack */ 332 CP_TIME_END, /* after unblock_operation */ 333 CP_TIME_MAX, 334 }; 335 336 /* time cost stats of checkpoint */ 337 struct cp_stats { 338 ktime_t times[CP_TIME_MAX]; 339 }; 340 341 struct cp_control { 342 int reason; 343 __u64 trim_start; 344 __u64 trim_end; 345 __u64 trim_minlen; 346 struct cp_stats stats; 347 }; 348 349 enum f2fs_cp_phase { 350 CP_PHASE_START_BLOCK_OPS, 351 CP_PHASE_FINISH_BLOCK_OPS, 352 CP_PHASE_FINISH_CHECKPOINT, 353 }; 354 355 /* 356 * indicate meta/data type 357 */ 358 enum { 359 META_CP, 360 META_NAT, 361 META_SIT, 362 META_SSA, 363 META_MAX, 364 META_POR, 365 DATA_GENERIC, /* check range only */ 366 DATA_GENERIC_ENHANCE, /* strong check on range and segment bitmap */ 367 DATA_GENERIC_ENHANCE_READ, /* 368 * strong check on range and segment 369 * bitmap but no warning due to race 370 * condition of read on truncated area 371 * by extent_cache 372 */ 373 DATA_GENERIC_ENHANCE_UPDATE, /* 374 * strong check on range and segment 375 * bitmap for update case 376 */ 377 META_GENERIC, 378 }; 379 380 /* for the list of ino */ 381 enum { 382 ORPHAN_INO, /* for orphan ino list */ 383 APPEND_INO, /* for append ino list */ 384 UPDATE_INO, /* for update ino list */ 385 TRANS_DIR_INO, /* for transactions dir ino list */ 386 XATTR_DIR_INO, /* for xattr updated dir ino list */ 387 FLUSH_INO, /* for multiple device flushing */ 388 MAX_INO_ENTRY, /* max. list */ 389 }; 390 391 struct ino_entry { 392 struct list_head list; /* list head */ 393 nid_t ino; /* inode number */ 394 unsigned int dirty_device; /* dirty device bitmap */ 395 }; 396 397 /* for the list of inodes to be GCed */ 398 struct inode_entry { 399 struct list_head list; /* list head */ 400 struct inode *inode; /* vfs inode pointer */ 401 }; 402 403 struct fsync_node_entry { 404 struct list_head list; /* list head */ 405 struct folio *folio; /* warm node folio pointer */ 406 unsigned int seq_id; /* sequence id */ 407 }; 408 409 struct ckpt_req { 410 struct completion wait; /* completion for checkpoint done */ 411 struct llist_node llnode; /* llist_node to be linked in wait queue */ 412 int ret; /* return code of checkpoint */ 413 union { 414 ktime_t queue_time; /* request queued time */ 415 ktime_t delta_time; /* time in queue */ 416 }; 417 }; 418 419 struct ckpt_req_control { 420 struct task_struct *f2fs_issue_ckpt; /* checkpoint task */ 421 int ckpt_thread_ioprio; /* checkpoint merge thread ioprio */ 422 wait_queue_head_t ckpt_wait_queue; /* waiting queue for wake-up */ 423 atomic_t issued_ckpt; /* # of actually issued ckpts */ 424 atomic_t total_ckpt; /* # of total ckpts */ 425 atomic_t queued_ckpt; /* # of queued ckpts */ 426 struct llist_head issue_list; /* list for command issue */ 427 spinlock_t stat_lock; /* lock for below checkpoint time stats */ 428 unsigned int cur_time; /* cur wait time in msec for currently issued checkpoint */ 429 unsigned int peak_time; /* peak wait time in msec until now */ 430 }; 431 432 /* a time threshold that checkpoint was blocked for, unit: ms */ 433 #define CP_LONG_LATENCY_THRESHOLD 5000 434 435 /* for the bitmap indicate blocks to be discarded */ 436 struct discard_entry { 437 struct list_head list; /* list head */ 438 block_t start_blkaddr; /* start blockaddr of current segment */ 439 unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */ 440 }; 441 442 /* minimum discard granularity, unit: block count */ 443 #define MIN_DISCARD_GRANULARITY 1 444 /* default discard granularity of inner discard thread, unit: block count */ 445 #define DEFAULT_DISCARD_GRANULARITY 16 446 /* default maximum discard granularity of ordered discard, unit: block count */ 447 #define DEFAULT_MAX_ORDERED_DISCARD_GRANULARITY 16 448 /* default interval of periodical discard submission */ 449 #define DEFAULT_DISCARD_INTERVAL (msecs_to_jiffies(20)) 450 451 /* max discard pend list number */ 452 #define MAX_PLIST_NUM 512 453 #define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \ 454 (MAX_PLIST_NUM - 1) : ((blk_num) - 1)) 455 456 enum { 457 D_PREP, /* initial */ 458 D_PARTIAL, /* partially submitted */ 459 D_SUBMIT, /* all submitted */ 460 D_DONE, /* finished */ 461 }; 462 463 struct discard_info { 464 block_t lstart; /* logical start address */ 465 block_t len; /* length */ 466 block_t start; /* actual start address in dev */ 467 }; 468 469 struct discard_cmd { 470 struct rb_node rb_node; /* rb node located in rb-tree */ 471 struct discard_info di; /* discard info */ 472 struct list_head list; /* command list */ 473 struct completion wait; /* completion */ 474 struct block_device *bdev; /* bdev */ 475 unsigned short ref; /* reference count */ 476 unsigned char state; /* state */ 477 unsigned char queued; /* queued discard */ 478 int error; /* bio error */ 479 spinlock_t lock; /* for state/bio_ref updating */ 480 unsigned short bio_ref; /* bio reference count */ 481 }; 482 483 enum { 484 DPOLICY_BG, 485 DPOLICY_FORCE, 486 DPOLICY_FSTRIM, 487 DPOLICY_UMOUNT, 488 MAX_DPOLICY, 489 }; 490 491 enum { 492 DPOLICY_IO_AWARE_DISABLE, /* force to not be aware of IO */ 493 DPOLICY_IO_AWARE_ENABLE, /* force to be aware of IO */ 494 DPOLICY_IO_AWARE_MAX, 495 }; 496 497 struct discard_policy { 498 int type; /* type of discard */ 499 unsigned int min_interval; /* used for candidates exist */ 500 unsigned int mid_interval; /* used for device busy */ 501 unsigned int max_interval; /* used for candidates not exist */ 502 unsigned int max_requests; /* # of discards issued per round */ 503 unsigned int io_aware_gran; /* minimum granularity discard not be aware of I/O */ 504 bool io_aware; /* issue discard in idle time */ 505 bool sync; /* submit discard with REQ_SYNC flag */ 506 bool ordered; /* issue discard by lba order */ 507 bool timeout; /* discard timeout for put_super */ 508 unsigned int granularity; /* discard granularity */ 509 }; 510 511 struct discard_cmd_control { 512 struct task_struct *f2fs_issue_discard; /* discard thread */ 513 struct list_head entry_list; /* 4KB discard entry list */ 514 struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */ 515 struct list_head wait_list; /* store on-flushing entries */ 516 struct list_head fstrim_list; /* in-flight discard from fstrim */ 517 wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */ 518 struct mutex cmd_lock; 519 unsigned int nr_discards; /* # of discards in the list */ 520 unsigned int max_discards; /* max. discards to be issued */ 521 unsigned int max_discard_request; /* max. discard request per round */ 522 unsigned int min_discard_issue_time; /* min. interval between discard issue */ 523 unsigned int mid_discard_issue_time; /* mid. interval between discard issue */ 524 unsigned int max_discard_issue_time; /* max. interval between discard issue */ 525 unsigned int discard_io_aware_gran; /* minimum discard granularity not be aware of I/O */ 526 unsigned int discard_urgent_util; /* utilization which issue discard proactively */ 527 unsigned int discard_granularity; /* discard granularity */ 528 unsigned int max_ordered_discard; /* maximum discard granularity issued by lba order */ 529 unsigned int discard_io_aware; /* io_aware policy */ 530 unsigned int undiscard_blks; /* # of undiscard blocks */ 531 unsigned int next_pos; /* next discard position */ 532 atomic_t issued_discard; /* # of issued discard */ 533 atomic_t queued_discard; /* # of queued discard */ 534 atomic_t discard_cmd_cnt; /* # of cached cmd count */ 535 struct rb_root_cached root; /* root of discard rb-tree */ 536 bool rbtree_check; /* config for consistence check */ 537 bool discard_wake; /* to wake up discard thread */ 538 }; 539 540 /* for the list of fsync inodes, used only during recovery */ 541 struct fsync_inode_entry { 542 struct list_head list; /* list head */ 543 struct inode *inode; /* vfs inode pointer */ 544 block_t blkaddr; /* block address locating the last fsync */ 545 block_t last_dentry; /* block address locating the last dentry */ 546 }; 547 548 #define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats)) 549 #define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits)) 550 551 #define nat_in_journal(jnl, i) \ 552 (((struct nat_journal_entry *)(jnl)->nat_j.entries)[i].ne) 553 #define nid_in_journal(jnl, i) \ 554 (((struct nat_journal_entry *)(jnl)->nat_j.entries)[i].nid) 555 #define sit_in_journal(jnl, i) \ 556 (((struct sit_journal_entry *)(jnl)->sit_j.entries)[i].se) 557 #define segno_in_journal(jnl, i) \ 558 (((struct sit_journal_entry *)(jnl)->sit_j.entries)[i].segno) 559 560 #define sum_entries(sum) ((struct f2fs_summary *)(sum)) 561 #define sum_journal(sbi, sum) \ 562 ((struct f2fs_journal *)((char *)(sum) + \ 563 ((sbi)->entries_in_sum * sizeof(struct f2fs_summary)))) 564 #define sum_footer(sbi, sum) \ 565 ((struct summary_footer *)((char *)(sum) + (sbi)->sum_blocksize - \ 566 sizeof(struct summary_footer))) 567 568 #define MAX_NAT_JENTRIES(sbi, jnl) ((sbi)->nat_journal_entries - nats_in_cursum(jnl)) 569 #define MAX_SIT_JENTRIES(sbi, jnl) ((sbi)->sit_journal_entries - sits_in_cursum(jnl)) 570 571 static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i) 572 { 573 int before = nats_in_cursum(journal); 574 575 journal->n_nats = cpu_to_le16(before + i); 576 return before; 577 } 578 579 static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i) 580 { 581 int before = sits_in_cursum(journal); 582 583 journal->n_sits = cpu_to_le16(before + i); 584 return before; 585 } 586 587 /* for inline stuff */ 588 #define DEF_INLINE_RESERVED_SIZE 1 589 static inline int get_extra_isize(struct inode *inode); 590 static inline int get_inline_xattr_addrs(struct inode *inode); 591 #define MAX_INLINE_DATA(inode) (sizeof(__le32) * \ 592 (CUR_ADDRS_PER_INODE(inode) - \ 593 get_inline_xattr_addrs(inode) - \ 594 DEF_INLINE_RESERVED_SIZE)) 595 596 /* for inline dir */ 597 #define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \ 598 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ 599 BITS_PER_BYTE + 1)) 600 #define INLINE_DENTRY_BITMAP_SIZE(inode) \ 601 DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE) 602 #define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \ 603 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ 604 NR_INLINE_DENTRY(inode) + \ 605 INLINE_DENTRY_BITMAP_SIZE(inode))) 606 607 /* 608 * For INODE and NODE manager 609 */ 610 /* for directory operations */ 611 612 struct f2fs_filename { 613 /* 614 * The filename the user specified. This is NULL for some 615 * filesystem-internal operations, e.g. converting an inline directory 616 * to a non-inline one, or roll-forward recovering an encrypted dentry. 617 */ 618 const struct qstr *usr_fname; 619 620 /* 621 * The on-disk filename. For encrypted directories, this is encrypted. 622 * This may be NULL for lookups in an encrypted dir without the key. 623 */ 624 struct fscrypt_str disk_name; 625 626 /* The dirhash of this filename */ 627 f2fs_hash_t hash; 628 629 #ifdef CONFIG_FS_ENCRYPTION 630 /* 631 * For lookups in encrypted directories: either the buffer backing 632 * disk_name, or a buffer that holds the decoded no-key name. 633 */ 634 struct fscrypt_str crypto_buf; 635 #endif 636 #if IS_ENABLED(CONFIG_UNICODE) 637 /* 638 * For casefolded directories: the casefolded name, but it's left NULL 639 * if the original name is not valid Unicode, if the original name is 640 * "." or "..", if the directory is both casefolded and encrypted and 641 * its encryption key is unavailable, or if the filesystem is doing an 642 * internal operation where usr_fname is also NULL. In all these cases 643 * we fall back to treating the name as an opaque byte sequence. 644 */ 645 struct qstr cf_name; 646 #endif 647 }; 648 649 struct f2fs_dentry_ptr { 650 struct inode *inode; 651 void *bitmap; 652 struct f2fs_dir_entry *dentry; 653 __u8 (*filename)[F2FS_SLOT_LEN]; 654 int max; 655 int nr_bitmap; 656 }; 657 658 static inline void make_dentry_ptr_block(struct inode *inode, 659 struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t) 660 { 661 d->inode = inode; 662 d->max = NR_DENTRY_IN_BLOCK; 663 d->nr_bitmap = SIZE_OF_DENTRY_BITMAP; 664 d->bitmap = t->dentry_bitmap; 665 d->dentry = t->dentry; 666 d->filename = t->filename; 667 } 668 669 static inline void make_dentry_ptr_inline(struct inode *inode, 670 struct f2fs_dentry_ptr *d, void *t) 671 { 672 int entry_cnt = NR_INLINE_DENTRY(inode); 673 int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode); 674 int reserved_size = INLINE_RESERVED_SIZE(inode); 675 676 d->inode = inode; 677 d->max = entry_cnt; 678 d->nr_bitmap = bitmap_size; 679 d->bitmap = t; 680 d->dentry = t + bitmap_size + reserved_size; 681 d->filename = t + bitmap_size + reserved_size + 682 SIZE_OF_DIR_ENTRY * entry_cnt; 683 } 684 685 /* 686 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1 687 * as its node offset to distinguish from index node blocks. 688 * But some bits are used to mark the node block. 689 */ 690 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \ 691 >> OFFSET_BIT_SHIFT) 692 enum { 693 ALLOC_NODE, /* allocate a new node page if needed */ 694 LOOKUP_NODE, /* look up a node without readahead */ 695 LOOKUP_NODE_RA, /* 696 * look up a node with readahead called 697 * by get_data_block. 698 */ 699 }; 700 701 #define DEFAULT_RETRY_IO_COUNT 8 /* maximum retry read IO or flush count */ 702 703 #define MAX_FLUSH_RETRY_COUNT 3 /* maximum flush retry count in f2fs_enable_checkpoint() */ 704 705 /* IO/non-IO congestion wait timeout value, default: 1 jiffies */ 706 #define DEFAULT_SCHEDULE_TIMEOUT 1 707 708 /* timeout value injected, default: 1000ms */ 709 #define DEFAULT_FAULT_TIMEOUT (msecs_to_jiffies(1000)) 710 711 /* maximum retry quota flush count */ 712 #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT 8 713 714 /* maximum retry of EIO'ed page */ 715 #define MAX_RETRY_PAGE_EIO 100 716 717 #define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */ 718 719 #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */ 720 721 /* dirty segments threshold for triggering CP */ 722 #define DEFAULT_DIRTY_THRESHOLD 4 723 724 #define RECOVERY_MAX_RA_BLOCKS BIO_MAX_VECS 725 #define RECOVERY_MIN_RA_BLOCKS 1 726 727 #define F2FS_ONSTACK_PAGES 16 /* nr of onstack pages */ 728 729 /* for in-memory extent cache entry */ 730 #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */ 731 732 /* number of extent info in extent cache we try to shrink */ 733 #define READ_EXTENT_CACHE_SHRINK_NUMBER 128 734 735 /* number of age extent info in extent cache we try to shrink */ 736 #define AGE_EXTENT_CACHE_SHRINK_NUMBER 128 737 #define LAST_AGE_WEIGHT 30 738 #define SAME_AGE_REGION 1024 739 740 /* 741 * Define data block with age less than 1GB as hot data 742 * define data block with age less than 10GB but more than 1GB as warm data 743 */ 744 #define DEF_HOT_DATA_AGE_THRESHOLD 262144 745 #define DEF_WARM_DATA_AGE_THRESHOLD 2621440 746 747 /* default max read extent count per inode */ 748 #define DEF_MAX_READ_EXTENT_COUNT 10240 749 750 /* extent cache type */ 751 enum extent_type { 752 EX_READ, 753 EX_BLOCK_AGE, 754 NR_EXTENT_CACHES, 755 }; 756 757 /* 758 * Reserved value to mark invalid age extents, hence valid block range 759 * from 0 to ULLONG_MAX-1 760 */ 761 #define F2FS_EXTENT_AGE_INVALID ULLONG_MAX 762 763 struct extent_info { 764 unsigned int fofs; /* start offset in a file */ 765 unsigned int len; /* length of the extent */ 766 union { 767 /* read extent_cache */ 768 struct { 769 /* start block address of the extent */ 770 block_t blk; 771 #ifdef CONFIG_F2FS_FS_COMPRESSION 772 /* physical extent length of compressed blocks */ 773 unsigned int c_len; 774 #endif 775 }; 776 /* block age extent_cache */ 777 struct { 778 /* block age of the extent */ 779 unsigned long long age; 780 /* last total blocks allocated */ 781 unsigned long long last_blocks; 782 }; 783 }; 784 }; 785 786 struct extent_node { 787 struct rb_node rb_node; /* rb node located in rb-tree */ 788 struct extent_info ei; /* extent info */ 789 struct list_head list; /* node in global extent list of sbi */ 790 struct extent_tree *et; /* extent tree pointer */ 791 }; 792 793 struct extent_tree { 794 nid_t ino; /* inode number */ 795 enum extent_type type; /* keep the extent tree type */ 796 struct rb_root_cached root; /* root of extent info rb-tree */ 797 struct extent_node *cached_en; /* recently accessed extent node */ 798 struct list_head list; /* to be used by sbi->zombie_list */ 799 rwlock_t lock; /* protect extent info rb-tree */ 800 atomic_t node_cnt; /* # of extent node in rb-tree*/ 801 bool largest_updated; /* largest extent updated */ 802 struct extent_info largest; /* largest cached extent for EX_READ */ 803 }; 804 805 struct extent_tree_info { 806 struct radix_tree_root extent_tree_root;/* cache extent cache entries */ 807 struct mutex extent_tree_lock; /* locking extent radix tree */ 808 struct list_head extent_list; /* lru list for shrinker */ 809 spinlock_t extent_lock; /* locking extent lru list */ 810 atomic_t total_ext_tree; /* extent tree count */ 811 struct list_head zombie_list; /* extent zombie tree list */ 812 atomic_t total_zombie_tree; /* extent zombie tree count */ 813 atomic_t total_ext_node; /* extent info count */ 814 }; 815 816 /* 817 * State of block returned by f2fs_map_blocks. 818 */ 819 #define F2FS_MAP_NEW (1U << 0) 820 #define F2FS_MAP_MAPPED (1U << 1) 821 #define F2FS_MAP_DELALLOC (1U << 2) 822 #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\ 823 F2FS_MAP_DELALLOC) 824 825 struct f2fs_map_blocks { 826 struct block_device *m_bdev; /* for multi-device dio */ 827 block_t m_pblk; 828 block_t m_lblk; 829 unsigned int m_len; 830 unsigned int m_flags; 831 unsigned long m_last_pblk; /* last allocated block, only used for DIO in LFS mode */ 832 pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */ 833 pgoff_t *m_next_extent; /* point to next possible extent */ 834 int m_seg_type; 835 bool m_may_create; /* indicate it is from write path */ 836 bool m_multidev_dio; /* indicate it allows multi-device dio */ 837 }; 838 839 /* for flag in get_data_block */ 840 enum { 841 F2FS_GET_BLOCK_DEFAULT, 842 F2FS_GET_BLOCK_FIEMAP, 843 F2FS_GET_BLOCK_BMAP, 844 F2FS_GET_BLOCK_DIO, 845 F2FS_GET_BLOCK_PRE_DIO, 846 F2FS_GET_BLOCK_PRE_AIO, 847 F2FS_GET_BLOCK_PRECACHE, 848 }; 849 850 /* 851 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later. 852 */ 853 #define FADVISE_COLD_BIT 0x01 854 #define FADVISE_LOST_PINO_BIT 0x02 855 #define FADVISE_ENCRYPT_BIT 0x04 856 #define FADVISE_ENC_NAME_BIT 0x08 857 #define FADVISE_KEEP_SIZE_BIT 0x10 858 #define FADVISE_HOT_BIT 0x20 859 #define FADVISE_VERITY_BIT 0x40 860 #define FADVISE_TRUNC_BIT 0x80 861 862 #define FADVISE_MODIFIABLE_BITS (FADVISE_COLD_BIT | FADVISE_HOT_BIT) 863 864 #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT) 865 #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT) 866 #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT) 867 868 #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT) 869 #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT) 870 #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT) 871 872 #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT) 873 #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT) 874 875 #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT) 876 #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT) 877 878 #define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT) 879 #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT) 880 881 #define file_is_hot(inode) is_file(inode, FADVISE_HOT_BIT) 882 #define file_set_hot(inode) set_file(inode, FADVISE_HOT_BIT) 883 #define file_clear_hot(inode) clear_file(inode, FADVISE_HOT_BIT) 884 885 #define file_is_verity(inode) is_file(inode, FADVISE_VERITY_BIT) 886 #define file_set_verity(inode) set_file(inode, FADVISE_VERITY_BIT) 887 888 #define file_should_truncate(inode) is_file(inode, FADVISE_TRUNC_BIT) 889 #define file_need_truncate(inode) set_file(inode, FADVISE_TRUNC_BIT) 890 #define file_dont_truncate(inode) clear_file(inode, FADVISE_TRUNC_BIT) 891 892 #define DEF_DIR_LEVEL 0 893 894 /* used for f2fs_inode_info->flags */ 895 enum { 896 FI_NEW_INODE, /* indicate newly allocated inode */ 897 FI_DIRTY_INODE, /* indicate inode is dirty or not */ 898 FI_AUTO_RECOVER, /* indicate inode is recoverable */ 899 FI_DIRTY_DIR, /* indicate directory has dirty pages */ 900 FI_INC_LINK, /* need to increment i_nlink */ 901 FI_ACL_MODE, /* indicate acl mode */ 902 FI_NO_ALLOC, /* should not allocate any blocks */ 903 FI_FREE_NID, /* free allocated nide */ 904 FI_NO_EXTENT, /* not to use the extent cache */ 905 FI_INLINE_XATTR, /* used for inline xattr */ 906 FI_INLINE_DATA, /* used for inline data*/ 907 FI_INLINE_DENTRY, /* used for inline dentry */ 908 FI_APPEND_WRITE, /* inode has appended data */ 909 FI_UPDATE_WRITE, /* inode has in-place-update data */ 910 FI_NEED_IPU, /* used for ipu per file */ 911 FI_ATOMIC_FILE, /* indicate atomic file */ 912 FI_DATA_EXIST, /* indicate data exists */ 913 FI_SKIP_WRITES, /* should skip data page writeback */ 914 FI_OPU_WRITE, /* used for opu per file */ 915 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */ 916 FI_PREALLOCATED_ALL, /* all blocks for write were preallocated */ 917 FI_HOT_DATA, /* indicate file is hot */ 918 FI_EXTRA_ATTR, /* indicate file has extra attribute */ 919 FI_PROJ_INHERIT, /* indicate file inherits projectid */ 920 FI_PIN_FILE, /* indicate file should not be gced */ 921 FI_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */ 922 FI_COMPRESSED_FILE, /* indicate file's data can be compressed */ 923 FI_COMPRESS_CORRUPT, /* indicate compressed cluster is corrupted */ 924 FI_MMAP_FILE, /* indicate file was mmapped */ 925 FI_ENABLE_COMPRESS, /* enable compression in "user" compression mode */ 926 FI_COMPRESS_RELEASED, /* compressed blocks were released */ 927 FI_ALIGNED_WRITE, /* enable aligned write */ 928 FI_COW_FILE, /* indicate COW file */ 929 FI_ATOMIC_COMMITTED, /* indicate atomic commit completed except disk sync */ 930 FI_ATOMIC_DIRTIED, /* indicate atomic file is dirtied */ 931 FI_ATOMIC_REPLACE, /* indicate atomic replace */ 932 FI_OPENED_FILE, /* indicate file has been opened */ 933 FI_DONATE_FINISHED, /* indicate page donation of file has been finished */ 934 FI_MAX, /* max flag, never be used */ 935 }; 936 937 struct f2fs_inode_info { 938 struct inode vfs_inode; /* serve a vfs inode */ 939 unsigned long i_flags; /* keep an inode flags for ioctl */ 940 unsigned char i_advise; /* use to give file attribute hints */ 941 unsigned char i_dir_level; /* use for dentry level for large dir */ 942 union { 943 unsigned int i_current_depth; /* only for directory depth */ 944 unsigned short i_gc_failures; /* for gc failure statistic */ 945 }; 946 unsigned int i_pino; /* parent inode number */ 947 umode_t i_acl_mode; /* keep file acl mode temporarily */ 948 949 /* Use below internally in f2fs*/ 950 unsigned long flags[BITS_TO_LONGS(FI_MAX)]; /* use to pass per-file flags */ 951 unsigned int ioprio_hint; /* hint for IO priority */ 952 struct f2fs_rwsem i_sem; /* protect fi info */ 953 atomic_t dirty_pages; /* # of dirty pages */ 954 f2fs_hash_t chash; /* hash value of given file name */ 955 unsigned int clevel; /* maximum level of given file name */ 956 struct task_struct *task; /* lookup and create consistency */ 957 struct task_struct *cp_task; /* separate cp/wb IO stats*/ 958 struct task_struct *wb_task; /* indicate inode is in context of writeback */ 959 nid_t i_xattr_nid; /* node id that contains xattrs */ 960 loff_t last_disk_size; /* lastly written file size */ 961 spinlock_t i_size_lock; /* protect last_disk_size */ 962 963 #ifdef CONFIG_QUOTA 964 struct dquot __rcu *i_dquot[MAXQUOTAS]; 965 966 /* quota space reservation, managed internally by quota code */ 967 qsize_t i_reserved_quota; 968 #endif 969 struct list_head dirty_list; /* dirty list for dirs and files */ 970 struct list_head gdirty_list; /* linked in global dirty list */ 971 972 /* linked in global inode list for cache donation */ 973 struct list_head gdonate_list; 974 pgoff_t donate_start, donate_end; /* inclusive */ 975 atomic_t open_count; /* # of open files */ 976 977 struct task_struct *atomic_write_task; /* store atomic write task */ 978 struct extent_tree *extent_tree[NR_EXTENT_CACHES]; 979 /* cached extent_tree entry */ 980 union { 981 struct inode *cow_inode; /* copy-on-write inode for atomic write */ 982 struct inode *atomic_inode; 983 /* point to atomic_inode, available only for cow_inode */ 984 }; 985 986 /* avoid racing between foreground op and gc */ 987 struct f2fs_rwsem i_gc_rwsem[2]; 988 struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */ 989 990 int i_extra_isize; /* size of extra space located in i_addr */ 991 kprojid_t i_projid; /* id for project quota */ 992 int i_inline_xattr_size; /* inline xattr size */ 993 struct timespec64 i_crtime; /* inode creation time */ 994 struct timespec64 i_disk_time[3];/* inode disk times */ 995 996 /* for file compress */ 997 atomic_t i_compr_blocks; /* # of compressed blocks */ 998 unsigned char i_compress_algorithm; /* algorithm type */ 999 unsigned char i_log_cluster_size; /* log of cluster size */ 1000 unsigned char i_compress_level; /* compress level (lz4hc,zstd) */ 1001 unsigned char i_compress_flag; /* compress flag */ 1002 unsigned int i_cluster_size; /* cluster size */ 1003 atomic_t writeback; /* count # of writeback thread */ 1004 1005 unsigned int atomic_write_cnt; 1006 loff_t original_i_size; /* original i_size before atomic write */ 1007 #ifdef CONFIG_FS_ENCRYPTION 1008 struct fscrypt_inode_info *i_crypt_info; /* filesystem encryption info */ 1009 #endif 1010 }; 1011 1012 static inline void get_read_extent_info(struct extent_info *ext, 1013 struct f2fs_extent *i_ext) 1014 { 1015 ext->fofs = le32_to_cpu(i_ext->fofs); 1016 ext->blk = le32_to_cpu(i_ext->blk); 1017 ext->len = le32_to_cpu(i_ext->len); 1018 } 1019 1020 static inline void set_raw_read_extent(struct extent_info *ext, 1021 struct f2fs_extent *i_ext) 1022 { 1023 i_ext->fofs = cpu_to_le32(ext->fofs); 1024 i_ext->blk = cpu_to_le32(ext->blk); 1025 i_ext->len = cpu_to_le32(ext->len); 1026 } 1027 1028 static inline bool __is_discard_mergeable(struct discard_info *back, 1029 struct discard_info *front, unsigned int max_len) 1030 { 1031 return (back->lstart + back->len == front->lstart) && 1032 (back->len + front->len <= max_len); 1033 } 1034 1035 static inline bool __is_discard_back_mergeable(struct discard_info *cur, 1036 struct discard_info *back, unsigned int max_len) 1037 { 1038 return __is_discard_mergeable(back, cur, max_len); 1039 } 1040 1041 static inline bool __is_discard_front_mergeable(struct discard_info *cur, 1042 struct discard_info *front, unsigned int max_len) 1043 { 1044 return __is_discard_mergeable(cur, front, max_len); 1045 } 1046 1047 /* 1048 * For free nid management 1049 */ 1050 enum nid_state { 1051 FREE_NID, /* newly added to free nid list */ 1052 PREALLOC_NID, /* it is preallocated */ 1053 MAX_NID_STATE, 1054 }; 1055 1056 enum nat_state { 1057 TOTAL_NAT, 1058 DIRTY_NAT, 1059 RECLAIMABLE_NAT, 1060 MAX_NAT_STATE, 1061 }; 1062 1063 struct f2fs_nm_info { 1064 block_t nat_blkaddr; /* base disk address of NAT */ 1065 nid_t max_nid; /* maximum possible node ids */ 1066 nid_t available_nids; /* # of available node ids */ 1067 nid_t next_scan_nid; /* the next nid to be scanned */ 1068 nid_t max_rf_node_blocks; /* max # of nodes for recovery */ 1069 unsigned int ram_thresh; /* control the memory footprint */ 1070 unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */ 1071 unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */ 1072 1073 /* NAT cache management */ 1074 struct radix_tree_root nat_root;/* root of the nat entry cache */ 1075 struct radix_tree_root nat_set_root;/* root of the nat set cache */ 1076 struct f2fs_rwsem nat_tree_lock; /* protect nat entry tree */ 1077 struct list_head nat_entries; /* cached nat entry list (clean) */ 1078 spinlock_t nat_list_lock; /* protect clean nat entry list */ 1079 unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */ 1080 unsigned int nat_blocks; /* # of nat blocks */ 1081 1082 /* free node ids management */ 1083 struct radix_tree_root free_nid_root;/* root of the free_nid cache */ 1084 struct list_head free_nid_list; /* list for free nids excluding preallocated nids */ 1085 unsigned int nid_cnt[MAX_NID_STATE]; /* the number of free node id */ 1086 spinlock_t nid_list_lock; /* protect nid lists ops */ 1087 struct mutex build_lock; /* lock for build free nids */ 1088 unsigned char **free_nid_bitmap; 1089 unsigned char *nat_block_bitmap; 1090 unsigned short *free_nid_count; /* free nid count of NAT block */ 1091 1092 /* for checkpoint */ 1093 char *nat_bitmap; /* NAT bitmap pointer */ 1094 1095 unsigned int nat_bits_blocks; /* # of nat bits blocks */ 1096 unsigned char *nat_bits; /* NAT bits blocks */ 1097 unsigned char *full_nat_bits; /* full NAT pages */ 1098 unsigned char *empty_nat_bits; /* empty NAT pages */ 1099 #ifdef CONFIG_F2FS_CHECK_FS 1100 char *nat_bitmap_mir; /* NAT bitmap mirror */ 1101 #endif 1102 int bitmap_size; /* bitmap size */ 1103 }; 1104 1105 /* 1106 * this structure is used as one of function parameters. 1107 * all the information are dedicated to a given direct node block determined 1108 * by the data offset in a file. 1109 */ 1110 struct dnode_of_data { 1111 struct inode *inode; /* vfs inode pointer */ 1112 struct folio *inode_folio; /* its inode folio, NULL is possible */ 1113 struct folio *node_folio; /* cached direct node folio */ 1114 nid_t nid; /* node id of the direct node block */ 1115 unsigned int ofs_in_node; /* data offset in the node page */ 1116 bool inode_folio_locked; /* inode folio is locked or not */ 1117 bool node_changed; /* is node block changed */ 1118 char cur_level; /* level of hole node page */ 1119 char max_level; /* level of current page located */ 1120 block_t data_blkaddr; /* block address of the node block */ 1121 }; 1122 1123 static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode, 1124 struct folio *ifolio, struct folio *nfolio, nid_t nid) 1125 { 1126 memset(dn, 0, sizeof(*dn)); 1127 dn->inode = inode; 1128 dn->inode_folio = ifolio; 1129 dn->node_folio = nfolio; 1130 dn->nid = nid; 1131 } 1132 1133 /* 1134 * For SIT manager 1135 * 1136 * By default, there are 6 active log areas across the whole main area. 1137 * When considering hot and cold data separation to reduce cleaning overhead, 1138 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types, 1139 * respectively. 1140 * In the current design, you should not change the numbers intentionally. 1141 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6 1142 * logs individually according to the underlying devices. (default: 6) 1143 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for 1144 * data and 8 for node logs. 1145 */ 1146 #define NR_CURSEG_DATA_TYPE (3) 1147 #define NR_CURSEG_NODE_TYPE (3) 1148 #define NR_CURSEG_INMEM_TYPE (2) 1149 #define NR_CURSEG_RO_TYPE (2) 1150 #define NR_CURSEG_PERSIST_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE) 1151 #define NR_CURSEG_TYPE (NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE) 1152 1153 enum log_type { 1154 CURSEG_HOT_DATA = 0, /* directory entry blocks */ 1155 CURSEG_WARM_DATA, /* data blocks */ 1156 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */ 1157 CURSEG_HOT_NODE, /* direct node blocks of directory files */ 1158 CURSEG_WARM_NODE, /* direct node blocks of normal files */ 1159 CURSEG_COLD_NODE, /* indirect node blocks */ 1160 NR_PERSISTENT_LOG, /* number of persistent log */ 1161 CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG, 1162 /* pinned file that needs consecutive block address */ 1163 CURSEG_ALL_DATA_ATGC, /* SSR alloctor in hot/warm/cold data area */ 1164 NO_CHECK_TYPE, /* number of persistent & inmem log */ 1165 }; 1166 1167 struct flush_cmd { 1168 struct completion wait; 1169 struct llist_node llnode; 1170 nid_t ino; 1171 int ret; 1172 }; 1173 1174 struct flush_cmd_control { 1175 struct task_struct *f2fs_issue_flush; /* flush thread */ 1176 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */ 1177 atomic_t issued_flush; /* # of issued flushes */ 1178 atomic_t queued_flush; /* # of queued flushes */ 1179 struct llist_head issue_list; /* list for command issue */ 1180 struct llist_node *dispatch_list; /* list for command dispatch */ 1181 }; 1182 1183 struct f2fs_sm_info { 1184 struct sit_info *sit_info; /* whole segment information */ 1185 struct free_segmap_info *free_info; /* free segment information */ 1186 struct dirty_seglist_info *dirty_info; /* dirty segment information */ 1187 struct curseg_info *curseg_array; /* active segment information */ 1188 1189 struct f2fs_rwsem curseg_lock; /* for preventing curseg change */ 1190 1191 block_t seg0_blkaddr; /* block address of 0'th segment */ 1192 block_t main_blkaddr; /* start block address of main area */ 1193 block_t ssa_blkaddr; /* start block address of SSA area */ 1194 1195 unsigned int segment_count; /* total # of segments */ 1196 unsigned int main_segments; /* # of segments in main area */ 1197 unsigned int reserved_segments; /* # of reserved segments */ 1198 unsigned int ovp_segments; /* # of overprovision segments */ 1199 1200 /* a threshold to reclaim prefree segments */ 1201 unsigned int rec_prefree_segments; 1202 1203 struct list_head sit_entry_set; /* sit entry set list */ 1204 1205 unsigned int ipu_policy; /* in-place-update policy */ 1206 unsigned int min_ipu_util; /* in-place-update threshold */ 1207 unsigned int min_fsync_blocks; /* threshold for fsync */ 1208 unsigned int min_seq_blocks; /* threshold for sequential blocks */ 1209 unsigned int min_hot_blocks; /* threshold for hot block allocation */ 1210 unsigned int min_ssr_sections; /* threshold to trigger SSR allocation */ 1211 1212 /* for flush command control */ 1213 struct flush_cmd_control *fcc_info; 1214 1215 /* for discard command control */ 1216 struct discard_cmd_control *dcc_info; 1217 }; 1218 1219 /* 1220 * For superblock 1221 */ 1222 /* 1223 * COUNT_TYPE for monitoring 1224 * 1225 * f2fs monitors the number of several block types such as on-writeback, 1226 * dirty dentry blocks, dirty node blocks, and dirty meta blocks. 1227 */ 1228 #define WB_DATA_TYPE(folio, f) \ 1229 (f || f2fs_is_cp_guaranteed(folio) ? F2FS_WB_CP_DATA : F2FS_WB_DATA) 1230 enum count_type { 1231 F2FS_DIRTY_DENTS, 1232 F2FS_DIRTY_DATA, 1233 F2FS_DIRTY_QDATA, 1234 F2FS_DIRTY_NODES, 1235 F2FS_DIRTY_META, 1236 F2FS_DIRTY_IMETA, 1237 F2FS_WB_CP_DATA, 1238 F2FS_WB_DATA, 1239 F2FS_RD_DATA, 1240 F2FS_RD_NODE, 1241 F2FS_RD_META, 1242 F2FS_DIO_WRITE, 1243 F2FS_DIO_READ, 1244 F2FS_SKIPPED_WRITE, /* skip or fail during f2fs_enable_checkpoint() */ 1245 NR_COUNT_TYPE, 1246 }; 1247 1248 /* 1249 * The below are the page types of bios used in submit_bio(). 1250 * The available types are: 1251 * DATA User data pages. It operates as async mode. 1252 * NODE Node pages. It operates as async mode. 1253 * META FS metadata pages such as SIT, NAT, CP. 1254 * NR_PAGE_TYPE The number of page types. 1255 * META_FLUSH Make sure the previous pages are written 1256 * with waiting the bio's completion 1257 * ... Only can be used with META. 1258 */ 1259 #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type)) 1260 #define PAGE_TYPE_ON_MAIN(type) ((type) == DATA || (type) == NODE) 1261 enum page_type { 1262 DATA = 0, 1263 NODE = 1, /* should not change this */ 1264 META, 1265 NR_PAGE_TYPE, 1266 META_FLUSH, 1267 IPU, /* the below types are used by tracepoints only. */ 1268 OPU, 1269 }; 1270 1271 enum temp_type { 1272 HOT = 0, /* must be zero for meta bio */ 1273 WARM, 1274 COLD, 1275 NR_TEMP_TYPE, 1276 }; 1277 1278 enum need_lock_type { 1279 LOCK_REQ = 0, 1280 LOCK_DONE, 1281 LOCK_RETRY, 1282 }; 1283 1284 enum cp_reason_type { 1285 CP_NO_NEEDED, 1286 CP_NON_REGULAR, 1287 CP_COMPRESSED, 1288 CP_HARDLINK, 1289 CP_SB_NEED_CP, 1290 CP_WRONG_PINO, 1291 CP_NO_SPC_ROLL, 1292 CP_NODE_NEED_CP, 1293 CP_FASTBOOT_MODE, 1294 CP_SPEC_LOG_NUM, 1295 CP_RECOVER_DIR, 1296 CP_XATTR_DIR, 1297 }; 1298 1299 enum iostat_type { 1300 /* WRITE IO */ 1301 APP_DIRECT_IO, /* app direct write IOs */ 1302 APP_BUFFERED_IO, /* app buffered write IOs */ 1303 APP_WRITE_IO, /* app write IOs */ 1304 APP_MAPPED_IO, /* app mapped IOs */ 1305 APP_BUFFERED_CDATA_IO, /* app buffered write IOs on compressed file */ 1306 APP_MAPPED_CDATA_IO, /* app mapped write IOs on compressed file */ 1307 FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */ 1308 FS_CDATA_IO, /* data IOs from kworker/fsync/reclaimer on compressed file */ 1309 FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */ 1310 FS_META_IO, /* meta IOs from kworker/reclaimer */ 1311 FS_GC_DATA_IO, /* data IOs from forground gc */ 1312 FS_GC_NODE_IO, /* node IOs from forground gc */ 1313 FS_CP_DATA_IO, /* data IOs from checkpoint */ 1314 FS_CP_NODE_IO, /* node IOs from checkpoint */ 1315 FS_CP_META_IO, /* meta IOs from checkpoint */ 1316 1317 /* READ IO */ 1318 APP_DIRECT_READ_IO, /* app direct read IOs */ 1319 APP_BUFFERED_READ_IO, /* app buffered read IOs */ 1320 APP_READ_IO, /* app read IOs */ 1321 APP_MAPPED_READ_IO, /* app mapped read IOs */ 1322 APP_BUFFERED_CDATA_READ_IO, /* app buffered read IOs on compressed file */ 1323 APP_MAPPED_CDATA_READ_IO, /* app mapped read IOs on compressed file */ 1324 FS_DATA_READ_IO, /* data read IOs */ 1325 FS_GDATA_READ_IO, /* data read IOs from background gc */ 1326 FS_CDATA_READ_IO, /* compressed data read IOs */ 1327 FS_NODE_READ_IO, /* node read IOs */ 1328 FS_META_READ_IO, /* meta read IOs */ 1329 1330 /* other */ 1331 FS_DISCARD_IO, /* discard */ 1332 FS_FLUSH_IO, /* flush */ 1333 FS_ZONE_RESET_IO, /* zone reset */ 1334 NR_IO_TYPE, 1335 }; 1336 1337 struct f2fs_io_info { 1338 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */ 1339 nid_t ino; /* inode number */ 1340 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */ 1341 enum temp_type temp; /* contains HOT/WARM/COLD */ 1342 enum req_op op; /* contains REQ_OP_ */ 1343 blk_opf_t op_flags; /* req_flag_bits */ 1344 block_t new_blkaddr; /* new block address to be written */ 1345 block_t old_blkaddr; /* old block address before Cow */ 1346 union { 1347 struct page *page; /* page to be written */ 1348 struct folio *folio; 1349 }; 1350 struct page *encrypted_page; /* encrypted page */ 1351 struct page *compressed_page; /* compressed page */ 1352 struct list_head list; /* serialize IOs */ 1353 unsigned int compr_blocks; /* # of compressed block addresses */ 1354 unsigned int need_lock:8; /* indicate we need to lock cp_rwsem */ 1355 unsigned int version:8; /* version of the node */ 1356 unsigned int submitted:1; /* indicate IO submission */ 1357 unsigned int in_list:1; /* indicate fio is in io_list */ 1358 unsigned int is_por:1; /* indicate IO is from recovery or not */ 1359 unsigned int encrypted:1; /* indicate file is encrypted */ 1360 unsigned int meta_gc:1; /* require meta inode GC */ 1361 enum iostat_type io_type; /* io type */ 1362 struct writeback_control *io_wbc; /* writeback control */ 1363 struct bio **bio; /* bio for ipu */ 1364 sector_t *last_block; /* last block number in bio */ 1365 }; 1366 1367 struct bio_entry { 1368 struct bio *bio; 1369 struct list_head list; 1370 }; 1371 1372 #define is_read_io(rw) ((rw) == READ) 1373 struct f2fs_bio_info { 1374 struct f2fs_sb_info *sbi; /* f2fs superblock */ 1375 struct bio *bio; /* bios to merge */ 1376 sector_t last_block_in_bio; /* last block number */ 1377 struct f2fs_io_info fio; /* store buffered io info. */ 1378 #ifdef CONFIG_BLK_DEV_ZONED 1379 struct completion zone_wait; /* condition value for the previous open zone to close */ 1380 struct bio *zone_pending_bio; /* pending bio for the previous zone */ 1381 void *bi_private; /* previous bi_private for pending bio */ 1382 #endif 1383 struct f2fs_rwsem io_rwsem; /* blocking op for bio */ 1384 spinlock_t io_lock; /* serialize DATA/NODE IOs */ 1385 struct list_head io_list; /* track fios */ 1386 struct list_head bio_list; /* bio entry list head */ 1387 struct f2fs_rwsem bio_list_lock; /* lock to protect bio entry list */ 1388 }; 1389 1390 #define FDEV(i) (sbi->devs[i]) 1391 #define RDEV(i) (raw_super->devs[i]) 1392 struct f2fs_dev_info { 1393 struct file *bdev_file; 1394 struct block_device *bdev; 1395 char path[MAX_PATH_LEN + 1]; 1396 unsigned int total_segments; 1397 block_t start_blk; 1398 block_t end_blk; 1399 #ifdef CONFIG_BLK_DEV_ZONED 1400 unsigned int nr_blkz; /* Total number of zones */ 1401 unsigned long *blkz_seq; /* Bitmap indicating sequential zones */ 1402 #endif 1403 }; 1404 1405 enum inode_type { 1406 DIR_INODE, /* for dirty dir inode */ 1407 FILE_INODE, /* for dirty regular/symlink inode */ 1408 DIRTY_META, /* for all dirtied inode metadata */ 1409 DONATE_INODE, /* for all inode to donate pages */ 1410 NR_INODE_TYPE, 1411 }; 1412 1413 /* for inner inode cache management */ 1414 struct inode_management { 1415 struct radix_tree_root ino_root; /* ino entry array */ 1416 spinlock_t ino_lock; /* for ino entry lock */ 1417 struct list_head ino_list; /* inode list head */ 1418 unsigned long ino_num; /* number of entries */ 1419 }; 1420 1421 /* for GC_AT */ 1422 struct atgc_management { 1423 bool atgc_enabled; /* ATGC is enabled or not */ 1424 struct rb_root_cached root; /* root of victim rb-tree */ 1425 struct list_head victim_list; /* linked with all victim entries */ 1426 unsigned int victim_count; /* victim count in rb-tree */ 1427 unsigned int candidate_ratio; /* candidate ratio */ 1428 unsigned int max_candidate_count; /* max candidate count */ 1429 unsigned int age_weight; /* age weight, vblock_weight = 100 - age_weight */ 1430 unsigned long long age_threshold; /* age threshold */ 1431 }; 1432 1433 struct f2fs_time_stat { 1434 unsigned long long total_time; /* total wall clock time */ 1435 #ifdef CONFIG_64BIT 1436 unsigned long long running_time; /* running time */ 1437 #endif 1438 #if defined(CONFIG_SCHED_INFO) && defined(CONFIG_SCHEDSTATS) 1439 unsigned long long runnable_time; /* runnable(including preempted) time */ 1440 #endif 1441 #ifdef CONFIG_TASK_DELAY_ACCT 1442 unsigned long long io_sleep_time; /* IO sleep time */ 1443 #endif 1444 }; 1445 1446 struct f2fs_lock_context { 1447 struct f2fs_time_stat ts; 1448 int orig_nice; 1449 int new_nice; 1450 bool lock_trace; 1451 bool need_restore; 1452 }; 1453 1454 struct f2fs_gc_control { 1455 unsigned int victim_segno; /* target victim segment number */ 1456 int init_gc_type; /* FG_GC or BG_GC */ 1457 bool no_bg_gc; /* check the space and stop bg_gc */ 1458 bool should_migrate_blocks; /* should migrate blocks */ 1459 bool err_gc_skipped; /* return EAGAIN if GC skipped */ 1460 bool one_time; /* require one time GC in one migration unit */ 1461 unsigned int nr_free_secs; /* # of free sections to do GC */ 1462 struct f2fs_lock_context lc; /* lock context for gc_lock */ 1463 }; 1464 1465 /* 1466 * For s_flag in struct f2fs_sb_info 1467 * Modification on enum should be synchronized with s_flag array 1468 */ 1469 enum { 1470 SBI_IS_DIRTY, /* dirty flag for checkpoint */ 1471 SBI_IS_CLOSE, /* specify unmounting */ 1472 SBI_NEED_FSCK, /* need fsck.f2fs to fix */ 1473 SBI_POR_DOING, /* recovery is doing or not */ 1474 SBI_NEED_SB_WRITE, /* need to recover superblock */ 1475 SBI_NEED_CP, /* need to checkpoint */ 1476 SBI_IS_SHUTDOWN, /* shutdown by ioctl */ 1477 SBI_IS_RECOVERED, /* recovered orphan/data */ 1478 SBI_CP_DISABLED, /* CP was disabled last mount */ 1479 SBI_CP_DISABLED_QUICK, /* CP was disabled quickly */ 1480 SBI_QUOTA_NEED_FLUSH, /* need to flush quota info in CP */ 1481 SBI_QUOTA_SKIP_FLUSH, /* skip flushing quota in current CP */ 1482 SBI_QUOTA_NEED_REPAIR, /* quota file may be corrupted */ 1483 SBI_IS_RESIZEFS, /* resizefs is in process */ 1484 SBI_IS_FREEZING, /* freezefs is in process */ 1485 SBI_IS_WRITABLE, /* remove ro mountoption transiently */ 1486 SBI_ENABLE_CHECKPOINT, /* indicate it's during f2fs_enable_checkpoint() */ 1487 MAX_SBI_FLAG, 1488 }; 1489 1490 enum { 1491 CP_TIME, 1492 REQ_TIME, 1493 DISCARD_TIME, 1494 GC_TIME, 1495 DISABLE_TIME, 1496 UMOUNT_DISCARD_TIMEOUT, 1497 MAX_TIME, 1498 }; 1499 1500 /* Note that you need to keep synchronization with this gc_mode_names array */ 1501 enum { 1502 GC_NORMAL, 1503 GC_IDLE_CB, 1504 GC_IDLE_GREEDY, 1505 GC_IDLE_AT, 1506 GC_URGENT_HIGH, 1507 GC_URGENT_LOW, 1508 GC_URGENT_MID, 1509 MAX_GC_MODE, 1510 }; 1511 1512 enum { 1513 BGGC_MODE_ON, /* background gc is on */ 1514 BGGC_MODE_OFF, /* background gc is off */ 1515 BGGC_MODE_SYNC, /* 1516 * background gc is on, migrating blocks 1517 * like foreground gc 1518 */ 1519 }; 1520 1521 enum { 1522 FS_MODE_ADAPTIVE, /* use both lfs/ssr allocation */ 1523 FS_MODE_LFS, /* use lfs allocation only */ 1524 FS_MODE_FRAGMENT_SEG, /* segment fragmentation mode */ 1525 FS_MODE_FRAGMENT_BLK, /* block fragmentation mode */ 1526 }; 1527 1528 enum { 1529 ALLOC_MODE_DEFAULT, /* stay default */ 1530 ALLOC_MODE_REUSE, /* reuse segments as much as possible */ 1531 }; 1532 1533 enum fsync_mode { 1534 FSYNC_MODE_POSIX, /* fsync follows posix semantics */ 1535 FSYNC_MODE_STRICT, /* fsync behaves in line with ext4 */ 1536 FSYNC_MODE_NOBARRIER, /* fsync behaves nobarrier based on posix */ 1537 }; 1538 1539 enum { 1540 COMPR_MODE_FS, /* 1541 * automatically compress compression 1542 * enabled files 1543 */ 1544 COMPR_MODE_USER, /* 1545 * automatical compression is disabled. 1546 * user can control the file compression 1547 * using ioctls 1548 */ 1549 }; 1550 1551 enum { 1552 DISCARD_UNIT_BLOCK, /* basic discard unit is block */ 1553 DISCARD_UNIT_SEGMENT, /* basic discard unit is segment */ 1554 DISCARD_UNIT_SECTION, /* basic discard unit is section */ 1555 }; 1556 1557 enum { 1558 MEMORY_MODE_NORMAL, /* memory mode for normal devices */ 1559 MEMORY_MODE_LOW, /* memory mode for low memory devices */ 1560 }; 1561 1562 enum errors_option { 1563 MOUNT_ERRORS_READONLY, /* remount fs ro on errors */ 1564 MOUNT_ERRORS_CONTINUE, /* continue on errors */ 1565 MOUNT_ERRORS_PANIC, /* panic on errors */ 1566 }; 1567 1568 enum { 1569 BACKGROUND, 1570 FOREGROUND, 1571 MAX_CALL_TYPE, 1572 TOTAL_CALL = FOREGROUND, 1573 }; 1574 1575 enum f2fs_lookup_mode { 1576 LOOKUP_PERF, 1577 LOOKUP_COMPAT, 1578 LOOKUP_AUTO, 1579 }; 1580 1581 /* For node type in __get_node_folio() */ 1582 enum node_type { 1583 NODE_TYPE_REGULAR, 1584 NODE_TYPE_INODE, 1585 NODE_TYPE_XATTR, 1586 NODE_TYPE_NON_INODE, 1587 }; 1588 1589 /* a threshold of maximum elapsed time in critical region to print tracepoint */ 1590 #define MAX_LOCK_ELAPSED_TIME 500 1591 1592 #define F2FS_DEFAULT_TASK_PRIORITY (DEFAULT_PRIO) 1593 #define F2FS_CRITICAL_TASK_PRIORITY NICE_TO_PRIO(0) 1594 1595 static inline int f2fs_test_bit(unsigned int nr, char *addr); 1596 static inline void f2fs_set_bit(unsigned int nr, char *addr); 1597 static inline void f2fs_clear_bit(unsigned int nr, char *addr); 1598 1599 /* 1600 * Layout of f2fs page.private: 1601 * 1602 * Layout A: lowest bit should be 1 1603 * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... | 1604 * bit 0 PAGE_PRIVATE_NOT_POINTER 1605 * bit 1 PAGE_PRIVATE_ONGOING_MIGRATION 1606 * bit 2 PAGE_PRIVATE_INLINE_INODE 1607 * bit 3 PAGE_PRIVATE_REF_RESOURCE 1608 * bit 4 PAGE_PRIVATE_ATOMIC_WRITE 1609 * bit 5- f2fs private data 1610 * 1611 * Layout B: lowest bit should be 0 1612 * page.private is a wrapped pointer. 1613 */ 1614 enum { 1615 PAGE_PRIVATE_NOT_POINTER, /* private contains non-pointer data */ 1616 PAGE_PRIVATE_ONGOING_MIGRATION, /* data page which is on-going migrating */ 1617 PAGE_PRIVATE_INLINE_INODE, /* inode page contains inline data */ 1618 PAGE_PRIVATE_REF_RESOURCE, /* dirty page has referenced resources */ 1619 PAGE_PRIVATE_ATOMIC_WRITE, /* data page from atomic write path */ 1620 PAGE_PRIVATE_MAX 1621 }; 1622 1623 /* For compression */ 1624 enum compress_algorithm_type { 1625 COMPRESS_LZO, 1626 COMPRESS_LZ4, 1627 COMPRESS_ZSTD, 1628 COMPRESS_LZORLE, 1629 COMPRESS_MAX, 1630 }; 1631 1632 enum compress_flag { 1633 COMPRESS_CHKSUM, 1634 COMPRESS_MAX_FLAG, 1635 }; 1636 1637 #define COMPRESS_WATERMARK 20 1638 #define COMPRESS_PERCENT 20 1639 1640 #define COMPRESS_DATA_RESERVED_SIZE 4 1641 struct compress_data { 1642 __le32 clen; /* compressed data size */ 1643 __le32 chksum; /* compressed data checksum */ 1644 __le32 reserved[COMPRESS_DATA_RESERVED_SIZE]; /* reserved */ 1645 u8 cdata[]; /* compressed data */ 1646 }; 1647 1648 #define COMPRESS_HEADER_SIZE (sizeof(struct compress_data)) 1649 1650 #define F2FS_COMPRESSED_PAGE_MAGIC 0xF5F2C000 1651 1652 #define F2FS_ZSTD_DEFAULT_CLEVEL 1 1653 1654 #define COMPRESS_LEVEL_OFFSET 8 1655 1656 /* compress context */ 1657 struct compress_ctx { 1658 struct inode *inode; /* inode the context belong to */ 1659 pgoff_t cluster_idx; /* cluster index number */ 1660 unsigned int cluster_size; /* page count in cluster */ 1661 unsigned int log_cluster_size; /* log of cluster size */ 1662 struct page **rpages; /* pages store raw data in cluster */ 1663 unsigned int nr_rpages; /* total page number in rpages */ 1664 struct page **cpages; /* pages store compressed data in cluster */ 1665 unsigned int nr_cpages; /* total page number in cpages */ 1666 unsigned int valid_nr_cpages; /* valid page number in cpages */ 1667 void *rbuf; /* virtual mapped address on rpages */ 1668 struct compress_data *cbuf; /* virtual mapped address on cpages */ 1669 size_t rlen; /* valid data length in rbuf */ 1670 size_t clen; /* valid data length in cbuf */ 1671 void *private; /* payload buffer for specified compression algorithm */ 1672 void *private2; /* extra payload buffer */ 1673 struct fsverity_info *vi; /* verity info if needed */ 1674 }; 1675 1676 /* compress context for write IO path */ 1677 struct compress_io_ctx { 1678 u32 magic; /* magic number to indicate page is compressed */ 1679 struct inode *inode; /* inode the context belong to */ 1680 struct page **rpages; /* pages store raw data in cluster */ 1681 unsigned int nr_rpages; /* total page number in rpages */ 1682 atomic_t pending_pages; /* in-flight compressed page count */ 1683 }; 1684 1685 /* Context for decompressing one cluster on the read IO path */ 1686 struct decompress_io_ctx { 1687 u32 magic; /* magic number to indicate page is compressed */ 1688 struct inode *inode; /* inode the context belong to */ 1689 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */ 1690 pgoff_t cluster_idx; /* cluster index number */ 1691 unsigned int cluster_size; /* page count in cluster */ 1692 unsigned int log_cluster_size; /* log of cluster size */ 1693 struct page **rpages; /* pages store raw data in cluster */ 1694 unsigned int nr_rpages; /* total page number in rpages */ 1695 struct page **cpages; /* pages store compressed data in cluster */ 1696 unsigned int nr_cpages; /* total page number in cpages */ 1697 struct page **tpages; /* temp pages to pad holes in cluster */ 1698 void *rbuf; /* virtual mapped address on rpages */ 1699 struct compress_data *cbuf; /* virtual mapped address on cpages */ 1700 size_t rlen; /* valid data length in rbuf */ 1701 size_t clen; /* valid data length in cbuf */ 1702 1703 /* 1704 * The number of compressed pages remaining to be read in this cluster. 1705 * This is initially nr_cpages. It is decremented by 1 each time a page 1706 * has been read (or failed to be read). When it reaches 0, the cluster 1707 * is decompressed (or an error is reported). 1708 * 1709 * If an error occurs before all the pages have been submitted for I/O, 1710 * then this will never reach 0. In this case the I/O submitter is 1711 * responsible for calling f2fs_decompress_end_io() instead. 1712 */ 1713 atomic_t remaining_pages; 1714 1715 /* 1716 * Number of references to this decompress_io_ctx. 1717 * 1718 * One reference is held for I/O completion. This reference is dropped 1719 * after the pagecache pages are updated and unlocked -- either after 1720 * decompression (and verity if enabled), or after an error. 1721 * 1722 * In addition, each compressed page holds a reference while it is in a 1723 * bio. These references are necessary prevent compressed pages from 1724 * being freed while they are still in a bio. 1725 */ 1726 refcount_t refcnt; 1727 1728 bool failed; /* IO error occurred before decompression? */ 1729 struct fsverity_info *vi; /* fs-verity context if needed */ 1730 unsigned char compress_algorithm; /* backup algorithm type */ 1731 void *private; /* payload buffer for specified decompression algorithm */ 1732 void *private2; /* extra payload buffer */ 1733 struct work_struct verity_work; /* work to verify the decompressed pages */ 1734 struct work_struct free_work; /* work for late free this structure itself */ 1735 }; 1736 1737 #define NULL_CLUSTER ((unsigned int)(~0)) 1738 #define MIN_COMPRESS_LOG_SIZE 2 1739 #define MAX_COMPRESS_LOG_SIZE 8 1740 #define MAX_COMPRESS_WINDOW_SIZE(log_size) ((PAGE_SIZE) << (log_size)) 1741 1742 struct f2fs_sb_info { 1743 struct super_block *sb; /* pointer to VFS super block */ 1744 struct proc_dir_entry *s_proc; /* proc entry */ 1745 struct f2fs_super_block *raw_super; /* raw super block pointer */ 1746 struct f2fs_rwsem sb_lock; /* lock for raw super block */ 1747 int valid_super_block; /* valid super block no */ 1748 unsigned long s_flag; /* flags for sbi */ 1749 struct mutex writepages; /* mutex for writepages() */ 1750 1751 #ifdef CONFIG_BLK_DEV_ZONED 1752 unsigned int blocks_per_blkz; /* F2FS blocks per zone */ 1753 unsigned int unusable_blocks_per_sec; /* unusable blocks per section */ 1754 unsigned int max_open_zones; /* max open zone resources of the zoned device */ 1755 /* For adjust the priority writing position of data in zone UFS */ 1756 unsigned int blkzone_alloc_policy; 1757 #endif 1758 1759 /* for node-related operations */ 1760 struct f2fs_nm_info *nm_info; /* node manager */ 1761 struct inode *node_inode; /* cache node blocks */ 1762 1763 /* for segment-related operations */ 1764 struct f2fs_sm_info *sm_info; /* segment manager */ 1765 1766 /* for bio operations */ 1767 struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */ 1768 /* keep migration IO order for LFS mode */ 1769 struct f2fs_rwsem io_order_lock; 1770 pgoff_t page_eio_ofs[NR_PAGE_TYPE]; /* EIO page offset */ 1771 int page_eio_cnt[NR_PAGE_TYPE]; /* EIO count */ 1772 1773 /* for checkpoint */ 1774 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */ 1775 int cur_cp_pack; /* remain current cp pack */ 1776 spinlock_t cp_lock; /* for flag in ckpt */ 1777 struct inode *meta_inode; /* cache meta blocks */ 1778 struct f2fs_rwsem cp_global_sem; /* checkpoint procedure lock */ 1779 struct f2fs_rwsem cp_rwsem; /* blocking FS operations */ 1780 struct f2fs_rwsem node_write; /* locking node writes */ 1781 struct f2fs_rwsem node_change; /* locking node change */ 1782 wait_queue_head_t cp_wait; 1783 unsigned long last_time[MAX_TIME]; /* to store time in jiffies */ 1784 long interval_time[MAX_TIME]; /* to store thresholds */ 1785 struct ckpt_req_control cprc_info; /* for checkpoint request control */ 1786 struct cp_stats cp_stats; /* for time stat of checkpoint */ 1787 1788 struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */ 1789 1790 spinlock_t fsync_node_lock; /* for node entry lock */ 1791 struct list_head fsync_node_list; /* node list head */ 1792 unsigned int fsync_seg_id; /* sequence id */ 1793 unsigned int fsync_node_num; /* number of node entries */ 1794 1795 /* for orphan inode, use 0'th array */ 1796 unsigned int max_orphans; /* max orphan inodes */ 1797 1798 /* for inode management */ 1799 struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */ 1800 spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */ 1801 struct mutex flush_lock; /* for flush exclusion */ 1802 1803 /* for extent tree cache */ 1804 struct extent_tree_info extent_tree[NR_EXTENT_CACHES]; 1805 atomic64_t allocated_data_blocks; /* for block age extent_cache */ 1806 unsigned int max_read_extent_count; /* max read extent count per inode */ 1807 1808 /* The threshold used for hot and warm data seperation*/ 1809 unsigned int hot_data_age_threshold; 1810 unsigned int warm_data_age_threshold; 1811 unsigned int last_age_weight; 1812 1813 /* control donate caches */ 1814 unsigned int donate_files; 1815 1816 /* basic filesystem units */ 1817 unsigned int log_sectors_per_block; /* log2 sectors per block */ 1818 unsigned int log_blocksize; /* log2 block size */ 1819 unsigned int blocksize; /* block size */ 1820 unsigned int root_ino_num; /* root inode number*/ 1821 unsigned int node_ino_num; /* node inode number*/ 1822 unsigned int meta_ino_num; /* meta inode number*/ 1823 unsigned int log_blocks_per_seg; /* log2 blocks per segment */ 1824 unsigned int blocks_per_seg; /* blocks per segment */ 1825 unsigned int segs_per_sec; /* segments per section */ 1826 unsigned int secs_per_zone; /* sections per zone */ 1827 unsigned int total_sections; /* total section count */ 1828 unsigned int total_node_count; /* total node block count */ 1829 unsigned int total_valid_node_count; /* valid node block count */ 1830 int dir_level; /* directory level */ 1831 bool readdir_ra; /* readahead inode in readdir */ 1832 unsigned int max_io_bytes; /* max io bytes to merge IOs */ 1833 1834 /* variable summary block units */ 1835 unsigned int sum_blocksize; /* sum block size */ 1836 unsigned int sums_per_block; /* sum block count per block */ 1837 unsigned int entries_in_sum; /* entry count in sum block */ 1838 unsigned int sum_entry_size; /* total entry size in sum block */ 1839 unsigned int sum_journal_size; /* journal size in sum block */ 1840 unsigned int nat_journal_entries; /* nat journal entry count in the journal */ 1841 unsigned int sit_journal_entries; /* sit journal entry count in the journal */ 1842 1843 block_t user_block_count; /* # of user blocks */ 1844 block_t total_valid_block_count; /* # of valid blocks */ 1845 block_t discard_blks; /* discard command candidats */ 1846 block_t last_valid_block_count; /* for recovery */ 1847 block_t reserved_blocks; /* configurable reserved blocks */ 1848 block_t current_reserved_blocks; /* current reserved blocks */ 1849 1850 /* Additional tracking for no checkpoint mode */ 1851 block_t unusable_block_count; /* # of blocks saved by last cp */ 1852 1853 unsigned int nquota_files; /* # of quota sysfile */ 1854 struct f2fs_rwsem quota_sem; /* blocking cp for flags */ 1855 struct task_struct *umount_lock_holder; /* s_umount lock holder */ 1856 1857 /* # of pages, see count_type */ 1858 atomic_t nr_pages[NR_COUNT_TYPE]; 1859 /* # of allocated blocks */ 1860 struct percpu_counter alloc_valid_block_count; 1861 /* # of node block writes as roll forward recovery */ 1862 struct percpu_counter rf_node_block_count; 1863 1864 /* writeback control */ 1865 atomic_t wb_sync_req[META]; /* count # of WB_SYNC threads */ 1866 1867 /* valid inode count */ 1868 struct percpu_counter total_valid_inode_count; 1869 1870 struct f2fs_mount_info mount_opt; /* mount options */ 1871 1872 /* for cleaning operations */ 1873 struct f2fs_rwsem gc_lock; /* 1874 * semaphore for GC, avoid 1875 * race between GC and GC or CP 1876 */ 1877 struct f2fs_gc_kthread *gc_thread; /* GC thread */ 1878 struct atgc_management am; /* atgc management */ 1879 unsigned int cur_victim_sec; /* current victim section num */ 1880 unsigned int gc_mode; /* current GC state */ 1881 unsigned int next_victim_seg[2]; /* next segment in victim section */ 1882 spinlock_t gc_remaining_trials_lock; 1883 /* remaining trial count for GC_URGENT_* and GC_IDLE_* */ 1884 unsigned int gc_remaining_trials; 1885 1886 /* for skip statistic */ 1887 unsigned long long skipped_gc_rwsem; /* FG_GC only */ 1888 1889 /* free sections reserved for pinned file */ 1890 unsigned int reserved_pin_section; 1891 1892 /* threshold for gc trials on pinned files */ 1893 unsigned short gc_pin_file_threshold; 1894 struct f2fs_rwsem pin_sem; 1895 1896 /* maximum # of trials to find a victim segment for SSR and GC */ 1897 unsigned int max_victim_search; 1898 /* migration granularity of garbage collection, unit: segment */ 1899 unsigned int migration_granularity; 1900 /* migration window granularity of garbage collection, unit: segment */ 1901 unsigned int migration_window_granularity; 1902 1903 /* 1904 * for stat information. 1905 * one is for the LFS mode, and the other is for the SSR mode. 1906 */ 1907 #ifdef CONFIG_F2FS_STAT_FS 1908 struct f2fs_stat_info *stat_info; /* FS status information */ 1909 atomic_t meta_count[META_MAX]; /* # of meta blocks */ 1910 unsigned int segment_count[2]; /* # of allocated segments */ 1911 unsigned int block_count[2]; /* # of allocated blocks */ 1912 atomic_t inplace_count; /* # of inplace update */ 1913 /* # of lookup extent cache */ 1914 atomic64_t total_hit_ext[NR_EXTENT_CACHES]; 1915 /* # of hit rbtree extent node */ 1916 atomic64_t read_hit_rbtree[NR_EXTENT_CACHES]; 1917 /* # of hit cached extent node */ 1918 atomic64_t read_hit_cached[NR_EXTENT_CACHES]; 1919 /* # of hit largest extent node in read extent cache */ 1920 atomic64_t read_hit_largest; 1921 atomic_t inline_xattr; /* # of inline_xattr inodes */ 1922 atomic_t inline_inode; /* # of inline_data inodes */ 1923 atomic_t inline_dir; /* # of inline_dentry inodes */ 1924 atomic_t compr_inode; /* # of compressed inodes */ 1925 atomic64_t compr_blocks; /* # of compressed blocks */ 1926 atomic_t swapfile_inode; /* # of swapfile inodes */ 1927 atomic_t atomic_files; /* # of opened atomic file */ 1928 atomic_t max_aw_cnt; /* max # of atomic writes */ 1929 unsigned int io_skip_bggc; /* skip background gc for in-flight IO */ 1930 unsigned int other_skip_bggc; /* skip background gc for other reasons */ 1931 unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */ 1932 atomic_t cp_call_count[MAX_CALL_TYPE]; /* # of cp call */ 1933 #endif 1934 spinlock_t stat_lock; /* lock for stat operations */ 1935 1936 /* to attach REQ_META|REQ_FUA flags */ 1937 unsigned int data_io_flag; 1938 unsigned int node_io_flag; 1939 1940 /* For sysfs support */ 1941 struct kobject s_kobj; /* /sys/fs/f2fs/<devname> */ 1942 struct completion s_kobj_unregister; 1943 1944 struct kobject s_stat_kobj; /* /sys/fs/f2fs/<devname>/stat */ 1945 struct completion s_stat_kobj_unregister; 1946 1947 struct kobject s_feature_list_kobj; /* /sys/fs/f2fs/<devname>/feature_list */ 1948 struct completion s_feature_list_kobj_unregister; 1949 1950 /* For shrinker support */ 1951 struct list_head s_list; 1952 struct mutex umount_mutex; 1953 unsigned int shrinker_run_no; 1954 1955 /* For multi devices */ 1956 int s_ndevs; /* number of devices */ 1957 struct f2fs_dev_info *devs; /* for device list */ 1958 unsigned int dirty_device; /* for checkpoint data flush */ 1959 spinlock_t dev_lock; /* protect dirty_device */ 1960 bool aligned_blksize; /* all devices has the same logical blksize */ 1961 unsigned int first_seq_zone_segno; /* first segno in sequential zone */ 1962 unsigned int bggc_io_aware; /* For adjust the BG_GC priority when pending IO */ 1963 unsigned int allocate_section_hint; /* the boundary position between devices */ 1964 unsigned int allocate_section_policy; /* determine the section writing priority */ 1965 1966 /* For write statistics */ 1967 u64 sectors_written_start; 1968 u64 kbytes_written; 1969 1970 /* Precomputed FS UUID checksum for seeding other checksums */ 1971 __u32 s_chksum_seed; 1972 1973 struct workqueue_struct *post_read_wq; /* post read workqueue */ 1974 1975 /* 1976 * If we are in irq context, let's update error information into 1977 * on-disk superblock in the work. 1978 */ 1979 struct work_struct s_error_work; 1980 unsigned char errors[MAX_F2FS_ERRORS]; /* error flags */ 1981 unsigned char stop_reason[MAX_STOP_REASON]; /* stop reason */ 1982 spinlock_t error_lock; /* protect errors/stop_reason array */ 1983 bool error_dirty; /* errors of sb is dirty */ 1984 1985 /* For reclaimed segs statistics per each GC mode */ 1986 unsigned int gc_segment_mode; /* GC state for reclaimed segments */ 1987 unsigned int gc_reclaimed_segs[MAX_GC_MODE]; /* Reclaimed segs for each mode */ 1988 1989 unsigned int seq_file_ra_mul; /* multiplier for ra_pages of seq. files in fadvise */ 1990 1991 int max_fragment_chunk; /* max chunk size for block fragmentation mode */ 1992 int max_fragment_hole; /* max hole size for block fragmentation mode */ 1993 1994 /* For atomic write statistics */ 1995 atomic64_t current_atomic_write; 1996 s64 peak_atomic_write; 1997 u64 committed_atomic_block; 1998 u64 revoked_atomic_block; 1999 2000 /* carve out reserved_blocks from total blocks */ 2001 bool carve_out; 2002 2003 /* max elapsed time threshold in critical region that lock covered */ 2004 unsigned long long max_lock_elapsed_time; 2005 2006 /* enable/disable to adjust task priority in critical region covered by lock */ 2007 unsigned int adjust_lock_priority; 2008 2009 /* adjust priority for task which is in critical region covered by lock */ 2010 unsigned int lock_duration_priority; 2011 2012 /* priority for critical task, e.g. f2fs_ckpt, f2fs_gc threads */ 2013 long critical_task_priority; 2014 2015 #ifdef CONFIG_F2FS_FS_COMPRESSION 2016 struct kmem_cache *page_array_slab; /* page array entry */ 2017 unsigned int page_array_slab_size; /* default page array slab size */ 2018 2019 /* For runtime compression statistics */ 2020 u64 compr_written_block; 2021 u64 compr_saved_block; 2022 u32 compr_new_inode; 2023 2024 /* For compressed block cache */ 2025 struct inode *compress_inode; /* cache compressed blocks */ 2026 unsigned int compress_percent; /* cache page percentage */ 2027 unsigned int compress_watermark; /* cache page watermark */ 2028 atomic_t compress_page_hit; /* cache hit count */ 2029 #endif 2030 2031 #ifdef CONFIG_F2FS_IOSTAT 2032 /* For app/fs IO statistics */ 2033 spinlock_t iostat_lock; 2034 unsigned long long iostat_count[NR_IO_TYPE]; 2035 unsigned long long iostat_bytes[NR_IO_TYPE]; 2036 unsigned long long prev_iostat_bytes[NR_IO_TYPE]; 2037 bool iostat_enable; 2038 unsigned long iostat_next_period; 2039 unsigned int iostat_period_ms; 2040 2041 /* For io latency related statistics info in one iostat period */ 2042 spinlock_t iostat_lat_lock; 2043 struct iostat_lat_info *iostat_io_lat; 2044 #endif 2045 }; 2046 2047 /* Definitions to access f2fs_sb_info */ 2048 #define SEGS_TO_BLKS(sbi, segs) \ 2049 ((segs) << (sbi)->log_blocks_per_seg) 2050 #define BLKS_TO_SEGS(sbi, blks) \ 2051 ((blks) >> (sbi)->log_blocks_per_seg) 2052 2053 #define BLKS_PER_SEG(sbi) ((sbi)->blocks_per_seg) 2054 #define BLKS_PER_SEC(sbi) (SEGS_TO_BLKS(sbi, (sbi)->segs_per_sec)) 2055 #define SEGS_PER_SEC(sbi) ((sbi)->segs_per_sec) 2056 2057 __printf(3, 4) 2058 void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate, const char *fmt, ...); 2059 2060 #define f2fs_err(sbi, fmt, ...) \ 2061 f2fs_printk(sbi, false, KERN_ERR fmt, ##__VA_ARGS__) 2062 #define f2fs_warn(sbi, fmt, ...) \ 2063 f2fs_printk(sbi, false, KERN_WARNING fmt, ##__VA_ARGS__) 2064 #define f2fs_notice(sbi, fmt, ...) \ 2065 f2fs_printk(sbi, false, KERN_NOTICE fmt, ##__VA_ARGS__) 2066 #define f2fs_info(sbi, fmt, ...) \ 2067 f2fs_printk(sbi, false, KERN_INFO fmt, ##__VA_ARGS__) 2068 #define f2fs_debug(sbi, fmt, ...) \ 2069 f2fs_printk(sbi, false, KERN_DEBUG fmt, ##__VA_ARGS__) 2070 2071 #define f2fs_err_ratelimited(sbi, fmt, ...) \ 2072 f2fs_printk(sbi, true, KERN_ERR fmt, ##__VA_ARGS__) 2073 #define f2fs_warn_ratelimited(sbi, fmt, ...) \ 2074 f2fs_printk(sbi, true, KERN_WARNING fmt, ##__VA_ARGS__) 2075 #define f2fs_info_ratelimited(sbi, fmt, ...) \ 2076 f2fs_printk(sbi, true, KERN_INFO fmt, ##__VA_ARGS__) 2077 2078 #ifdef CONFIG_F2FS_FAULT_INJECTION 2079 #define time_to_inject(sbi, type) __time_to_inject(sbi, type, __func__, \ 2080 __builtin_return_address(0)) 2081 static inline bool __time_to_inject(struct f2fs_sb_info *sbi, int type, 2082 const char *func, const char *parent_func) 2083 { 2084 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info; 2085 2086 if (!ffi->inject_rate) 2087 return false; 2088 2089 if (!IS_FAULT_SET(ffi, type)) 2090 return false; 2091 2092 atomic_inc(&ffi->inject_ops); 2093 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) { 2094 atomic_set(&ffi->inject_ops, 0); 2095 ffi->inject_count[type]++; 2096 f2fs_info_ratelimited(sbi, "inject %s in %s of %pS", 2097 f2fs_fault_name[type], func, parent_func); 2098 return true; 2099 } 2100 return false; 2101 } 2102 #else 2103 static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type) 2104 { 2105 return false; 2106 } 2107 #endif 2108 2109 /* 2110 * Test if the mounted volume is a multi-device volume. 2111 * - For a single regular disk volume, sbi->s_ndevs is 0. 2112 * - For a single zoned disk volume, sbi->s_ndevs is 1. 2113 * - For a multi-device volume, sbi->s_ndevs is always 2 or more. 2114 */ 2115 static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi) 2116 { 2117 return sbi->s_ndevs > 1; 2118 } 2119 2120 static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type) 2121 { 2122 unsigned long now = jiffies; 2123 2124 sbi->last_time[type] = now; 2125 2126 /* DISCARD_TIME and GC_TIME are based on REQ_TIME */ 2127 if (type == REQ_TIME) { 2128 sbi->last_time[DISCARD_TIME] = now; 2129 sbi->last_time[GC_TIME] = now; 2130 } 2131 } 2132 2133 static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type) 2134 { 2135 unsigned long interval = sbi->interval_time[type] * HZ; 2136 2137 return time_after(jiffies, sbi->last_time[type] + interval); 2138 } 2139 2140 static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi, 2141 int type) 2142 { 2143 unsigned long interval = sbi->interval_time[type] * HZ; 2144 unsigned int wait_ms = 0; 2145 long delta; 2146 2147 delta = (sbi->last_time[type] + interval) - jiffies; 2148 if (delta > 0) 2149 wait_ms = jiffies_to_msecs(delta); 2150 2151 return wait_ms; 2152 } 2153 2154 /* 2155 * Inline functions 2156 */ 2157 static inline u32 __f2fs_crc32(u32 crc, const void *address, 2158 unsigned int length) 2159 { 2160 return crc32(crc, address, length); 2161 } 2162 2163 static inline u32 f2fs_crc32(const void *address, unsigned int length) 2164 { 2165 return __f2fs_crc32(F2FS_SUPER_MAGIC, address, length); 2166 } 2167 2168 static inline u32 f2fs_chksum(u32 crc, const void *address, unsigned int length) 2169 { 2170 return __f2fs_crc32(crc, address, length); 2171 } 2172 2173 static inline struct f2fs_inode_info *F2FS_I(struct inode *inode) 2174 { 2175 return container_of(inode, struct f2fs_inode_info, vfs_inode); 2176 } 2177 2178 static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb) 2179 { 2180 return sb->s_fs_info; 2181 } 2182 2183 static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode) 2184 { 2185 return F2FS_SB(inode->i_sb); 2186 } 2187 2188 static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping) 2189 { 2190 return F2FS_I_SB(mapping->host); 2191 } 2192 2193 static inline struct f2fs_sb_info *F2FS_F_SB(const struct folio *folio) 2194 { 2195 return F2FS_M_SB(folio->mapping); 2196 } 2197 2198 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi) 2199 { 2200 return (struct f2fs_super_block *)(sbi->raw_super); 2201 } 2202 2203 static inline struct f2fs_super_block *F2FS_SUPER_BLOCK(struct folio *folio, 2204 pgoff_t index) 2205 { 2206 pgoff_t idx_in_folio = index % folio_nr_pages(folio); 2207 2208 return (struct f2fs_super_block *) 2209 (page_address(folio_page(folio, idx_in_folio)) + 2210 F2FS_SUPER_OFFSET); 2211 } 2212 2213 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi) 2214 { 2215 return (struct f2fs_checkpoint *)(sbi->ckpt); 2216 } 2217 2218 static inline struct f2fs_node *F2FS_NODE(const struct folio *folio) 2219 { 2220 return (struct f2fs_node *)folio_address(folio); 2221 } 2222 2223 static inline struct f2fs_inode *F2FS_INODE(const struct folio *folio) 2224 { 2225 return &((struct f2fs_node *)folio_address(folio))->i; 2226 } 2227 2228 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi) 2229 { 2230 return (struct f2fs_nm_info *)(sbi->nm_info); 2231 } 2232 2233 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi) 2234 { 2235 return (struct f2fs_sm_info *)(sbi->sm_info); 2236 } 2237 2238 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi) 2239 { 2240 return (struct sit_info *)(SM_I(sbi)->sit_info); 2241 } 2242 2243 static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi) 2244 { 2245 return (struct free_segmap_info *)(SM_I(sbi)->free_info); 2246 } 2247 2248 static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi) 2249 { 2250 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info); 2251 } 2252 2253 static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi) 2254 { 2255 return sbi->meta_inode->i_mapping; 2256 } 2257 2258 static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi) 2259 { 2260 return sbi->node_inode->i_mapping; 2261 } 2262 2263 static inline bool is_meta_folio(struct folio *folio) 2264 { 2265 return folio->mapping == META_MAPPING(F2FS_F_SB(folio)); 2266 } 2267 2268 static inline bool is_node_folio(struct folio *folio) 2269 { 2270 return folio->mapping == NODE_MAPPING(F2FS_F_SB(folio)); 2271 } 2272 2273 static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type) 2274 { 2275 return test_bit(type, &sbi->s_flag); 2276 } 2277 2278 static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type) 2279 { 2280 set_bit(type, &sbi->s_flag); 2281 } 2282 2283 static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type) 2284 { 2285 clear_bit(type, &sbi->s_flag); 2286 } 2287 2288 static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp) 2289 { 2290 return le64_to_cpu(cp->checkpoint_ver); 2291 } 2292 2293 static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type) 2294 { 2295 if (type < F2FS_MAX_QUOTAS) 2296 return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]); 2297 return 0; 2298 } 2299 2300 static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp) 2301 { 2302 size_t crc_offset = le32_to_cpu(cp->checksum_offset); 2303 return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset))); 2304 } 2305 2306 static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f) 2307 { 2308 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags); 2309 2310 return ckpt_flags & f; 2311 } 2312 2313 static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f) 2314 { 2315 return __is_set_ckpt_flags(F2FS_CKPT(sbi), f); 2316 } 2317 2318 static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f) 2319 { 2320 unsigned int ckpt_flags; 2321 2322 ckpt_flags = le32_to_cpu(cp->ckpt_flags); 2323 ckpt_flags |= f; 2324 cp->ckpt_flags = cpu_to_le32(ckpt_flags); 2325 } 2326 2327 static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f) 2328 { 2329 unsigned long flags; 2330 2331 spin_lock_irqsave(&sbi->cp_lock, flags); 2332 __set_ckpt_flags(F2FS_CKPT(sbi), f); 2333 spin_unlock_irqrestore(&sbi->cp_lock, flags); 2334 } 2335 2336 static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f) 2337 { 2338 unsigned int ckpt_flags; 2339 2340 ckpt_flags = le32_to_cpu(cp->ckpt_flags); 2341 ckpt_flags &= (~f); 2342 cp->ckpt_flags = cpu_to_le32(ckpt_flags); 2343 } 2344 2345 static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f) 2346 { 2347 unsigned long flags; 2348 2349 spin_lock_irqsave(&sbi->cp_lock, flags); 2350 __clear_ckpt_flags(F2FS_CKPT(sbi), f); 2351 spin_unlock_irqrestore(&sbi->cp_lock, flags); 2352 } 2353 2354 #define init_f2fs_rwsem(sem) __init_f2fs_rwsem(sem, NULL, LOCK_NAME_NONE) 2355 #define init_f2fs_rwsem_trace __init_f2fs_rwsem 2356 2357 #define __init_f2fs_rwsem(sem, sbi, name) \ 2358 do { \ 2359 static struct lock_class_key __key; \ 2360 \ 2361 do_init_f2fs_rwsem((sem), #sem, &__key, sbi, name); \ 2362 } while (0) 2363 2364 static inline void do_init_f2fs_rwsem(struct f2fs_rwsem *sem, 2365 const char *sem_name, struct lock_class_key *key, 2366 struct f2fs_sb_info *sbi, enum f2fs_lock_name name) 2367 { 2368 sem->sbi = sbi; 2369 sem->name = name; 2370 __init_rwsem(&sem->internal_rwsem, sem_name, key); 2371 #ifdef CONFIG_F2FS_UNFAIR_RWSEM 2372 init_waitqueue_head(&sem->read_waiters); 2373 #endif 2374 } 2375 2376 static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem) 2377 { 2378 return rwsem_is_locked(&sem->internal_rwsem); 2379 } 2380 2381 static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem) 2382 { 2383 return rwsem_is_contended(&sem->internal_rwsem); 2384 } 2385 2386 static inline void f2fs_down_read(struct f2fs_rwsem *sem) 2387 { 2388 #ifdef CONFIG_F2FS_UNFAIR_RWSEM 2389 wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem)); 2390 #else 2391 down_read(&sem->internal_rwsem); 2392 #endif 2393 } 2394 2395 static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem) 2396 { 2397 return down_read_trylock(&sem->internal_rwsem); 2398 } 2399 2400 static inline void f2fs_up_read(struct f2fs_rwsem *sem) 2401 { 2402 up_read(&sem->internal_rwsem); 2403 } 2404 2405 static inline void f2fs_down_write(struct f2fs_rwsem *sem) 2406 { 2407 down_write(&sem->internal_rwsem); 2408 } 2409 2410 #ifdef CONFIG_DEBUG_LOCK_ALLOC 2411 static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass) 2412 { 2413 down_read_nested(&sem->internal_rwsem, subclass); 2414 } 2415 2416 static inline void f2fs_down_write_nested(struct f2fs_rwsem *sem, int subclass) 2417 { 2418 down_write_nested(&sem->internal_rwsem, subclass); 2419 } 2420 #else 2421 #define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem) 2422 #define f2fs_down_write_nested(sem, subclass) f2fs_down_write(sem) 2423 #endif 2424 2425 static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem) 2426 { 2427 return down_write_trylock(&sem->internal_rwsem); 2428 } 2429 2430 static inline void f2fs_up_write(struct f2fs_rwsem *sem) 2431 { 2432 up_write(&sem->internal_rwsem); 2433 #ifdef CONFIG_F2FS_UNFAIR_RWSEM 2434 wake_up_all(&sem->read_waiters); 2435 #endif 2436 } 2437 2438 void f2fs_down_read_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc); 2439 int f2fs_down_read_trylock_trace(struct f2fs_rwsem *sem, 2440 struct f2fs_lock_context *lc); 2441 void f2fs_up_read_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc); 2442 void f2fs_down_write_trace(struct f2fs_rwsem *sem, 2443 struct f2fs_lock_context *lc); 2444 int f2fs_down_write_trylock_trace(struct f2fs_rwsem *sem, 2445 struct f2fs_lock_context *lc); 2446 void f2fs_up_write_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc); 2447 2448 static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock) 2449 { 2450 unsigned long flags; 2451 unsigned char *nat_bits; 2452 2453 /* 2454 * In order to re-enable nat_bits we need to call fsck.f2fs by 2455 * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost, 2456 * so let's rely on regular fsck or unclean shutdown. 2457 */ 2458 2459 if (lock) 2460 spin_lock_irqsave(&sbi->cp_lock, flags); 2461 __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG); 2462 nat_bits = NM_I(sbi)->nat_bits; 2463 NM_I(sbi)->nat_bits = NULL; 2464 if (lock) 2465 spin_unlock_irqrestore(&sbi->cp_lock, flags); 2466 2467 kvfree(nat_bits); 2468 } 2469 2470 static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi, 2471 struct cp_control *cpc) 2472 { 2473 bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 2474 2475 return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set; 2476 } 2477 2478 static inline int __get_cp_reason(struct f2fs_sb_info *sbi) 2479 { 2480 int reason = CP_SYNC; 2481 2482 if (test_opt(sbi, FASTBOOT)) 2483 reason = CP_FASTBOOT; 2484 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) 2485 reason = CP_UMOUNT; 2486 return reason; 2487 } 2488 2489 static inline bool __remain_node_summaries(int reason) 2490 { 2491 return (reason & (CP_UMOUNT | CP_FASTBOOT)); 2492 } 2493 2494 static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi) 2495 { 2496 return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) || 2497 is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG)); 2498 } 2499 2500 /* 2501 * Check whether the inode has blocks or not 2502 */ 2503 static inline int F2FS_HAS_BLOCKS(struct inode *inode) 2504 { 2505 block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0; 2506 2507 return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block; 2508 } 2509 2510 static inline bool f2fs_has_xattr_block(unsigned int ofs) 2511 { 2512 return ofs == XATTR_NODE_OFFSET; 2513 } 2514 2515 static inline bool __allow_reserved_root(struct f2fs_sb_info *sbi, 2516 struct inode *inode, bool cap) 2517 { 2518 if (!inode) 2519 return true; 2520 if (IS_NOQUOTA(inode)) 2521 return true; 2522 if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid())) 2523 return true; 2524 if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) && 2525 in_group_p(F2FS_OPTION(sbi).s_resgid)) 2526 return true; 2527 if (cap && capable(CAP_SYS_RESOURCE)) 2528 return true; 2529 return false; 2530 } 2531 2532 static inline unsigned int get_available_block_count(struct f2fs_sb_info *sbi, 2533 struct inode *inode, bool cap) 2534 { 2535 block_t avail_user_block_count; 2536 2537 avail_user_block_count = sbi->user_block_count - 2538 sbi->current_reserved_blocks; 2539 2540 if (test_opt(sbi, RESERVE_ROOT) && !__allow_reserved_root(sbi, inode, cap)) 2541 avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks; 2542 2543 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 2544 if (avail_user_block_count > sbi->unusable_block_count) 2545 avail_user_block_count -= sbi->unusable_block_count; 2546 else 2547 avail_user_block_count = 0; 2548 } 2549 2550 return avail_user_block_count; 2551 } 2552 2553 static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool); 2554 static inline int inc_valid_block_count(struct f2fs_sb_info *sbi, 2555 struct inode *inode, blkcnt_t *count, bool partial) 2556 { 2557 long long diff = 0, release = 0; 2558 block_t avail_user_block_count; 2559 int ret; 2560 2561 ret = dquot_reserve_block(inode, *count); 2562 if (ret) 2563 return ret; 2564 2565 if (time_to_inject(sbi, FAULT_BLOCK)) { 2566 release = *count; 2567 goto release_quota; 2568 } 2569 2570 /* 2571 * let's increase this in prior to actual block count change in order 2572 * for f2fs_sync_file to avoid data races when deciding checkpoint. 2573 */ 2574 percpu_counter_add(&sbi->alloc_valid_block_count, (*count)); 2575 2576 spin_lock(&sbi->stat_lock); 2577 2578 avail_user_block_count = get_available_block_count(sbi, inode, true); 2579 diff = (long long)sbi->total_valid_block_count + *count - 2580 avail_user_block_count; 2581 if (unlikely(diff > 0)) { 2582 if (!partial) { 2583 spin_unlock(&sbi->stat_lock); 2584 release = *count; 2585 goto enospc; 2586 } 2587 if (diff > *count) 2588 diff = *count; 2589 *count -= diff; 2590 release = diff; 2591 if (!*count) { 2592 spin_unlock(&sbi->stat_lock); 2593 goto enospc; 2594 } 2595 } 2596 sbi->total_valid_block_count += (block_t)(*count); 2597 2598 spin_unlock(&sbi->stat_lock); 2599 2600 if (unlikely(release)) { 2601 percpu_counter_sub(&sbi->alloc_valid_block_count, release); 2602 dquot_release_reservation_block(inode, release); 2603 } 2604 f2fs_i_blocks_write(inode, *count, true, true); 2605 return 0; 2606 2607 enospc: 2608 percpu_counter_sub(&sbi->alloc_valid_block_count, release); 2609 release_quota: 2610 dquot_release_reservation_block(inode, release); 2611 return -ENOSPC; 2612 } 2613 2614 #define PAGE_PRIVATE_GET_FUNC(name, flagname) \ 2615 static inline bool folio_test_f2fs_##name(const struct folio *folio) \ 2616 { \ 2617 unsigned long priv = (unsigned long)folio->private; \ 2618 unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \ 2619 (1UL << PAGE_PRIVATE_##flagname); \ 2620 return (priv & v) == v; \ 2621 } \ 2622 static inline bool page_private_##name(struct page *page) \ 2623 { \ 2624 return PagePrivate(page) && \ 2625 test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \ 2626 test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \ 2627 } 2628 2629 #define PAGE_PRIVATE_SET_FUNC(name, flagname) \ 2630 static inline void folio_set_f2fs_##name(struct folio *folio) \ 2631 { \ 2632 unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \ 2633 (1UL << PAGE_PRIVATE_##flagname); \ 2634 if (!folio->private) \ 2635 folio_attach_private(folio, (void *)v); \ 2636 else { \ 2637 v |= (unsigned long)folio->private; \ 2638 folio->private = (void *)v; \ 2639 } \ 2640 } \ 2641 static inline void set_page_private_##name(struct page *page) \ 2642 { \ 2643 if (!PagePrivate(page)) \ 2644 attach_page_private(page, (void *)0); \ 2645 set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \ 2646 set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \ 2647 } 2648 2649 #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \ 2650 static inline void folio_clear_f2fs_##name(struct folio *folio) \ 2651 { \ 2652 unsigned long v = (unsigned long)folio->private; \ 2653 \ 2654 v &= ~(1UL << PAGE_PRIVATE_##flagname); \ 2655 if (v == (1UL << PAGE_PRIVATE_NOT_POINTER)) \ 2656 folio_detach_private(folio); \ 2657 else \ 2658 folio->private = (void *)v; \ 2659 } \ 2660 static inline void clear_page_private_##name(struct page *page) \ 2661 { \ 2662 clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \ 2663 if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) \ 2664 detach_page_private(page); \ 2665 } 2666 2667 PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER); 2668 PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE); 2669 PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION); 2670 PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE); 2671 2672 PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE); 2673 PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE); 2674 PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION); 2675 PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE); 2676 2677 PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE); 2678 PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE); 2679 PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION); 2680 PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE); 2681 2682 static inline unsigned long folio_get_f2fs_data(struct folio *folio) 2683 { 2684 unsigned long data = (unsigned long)folio->private; 2685 2686 if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data)) 2687 return 0; 2688 return data >> PAGE_PRIVATE_MAX; 2689 } 2690 2691 static inline void folio_set_f2fs_data(struct folio *folio, unsigned long data) 2692 { 2693 data = (1UL << PAGE_PRIVATE_NOT_POINTER) | (data << PAGE_PRIVATE_MAX); 2694 2695 if (!folio_test_private(folio)) 2696 folio_attach_private(folio, (void *)data); 2697 else 2698 folio->private = (void *)((unsigned long)folio->private | data); 2699 } 2700 2701 static inline void dec_valid_block_count(struct f2fs_sb_info *sbi, 2702 struct inode *inode, 2703 block_t count) 2704 { 2705 blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK; 2706 2707 spin_lock(&sbi->stat_lock); 2708 if (unlikely(sbi->total_valid_block_count < count)) { 2709 f2fs_warn(sbi, "Inconsistent total_valid_block_count:%u, ino:%lu, count:%u", 2710 sbi->total_valid_block_count, inode->i_ino, count); 2711 sbi->total_valid_block_count = 0; 2712 set_sbi_flag(sbi, SBI_NEED_FSCK); 2713 } else { 2714 sbi->total_valid_block_count -= count; 2715 } 2716 if (sbi->reserved_blocks && 2717 sbi->current_reserved_blocks < sbi->reserved_blocks) 2718 sbi->current_reserved_blocks = min(sbi->reserved_blocks, 2719 sbi->current_reserved_blocks + count); 2720 spin_unlock(&sbi->stat_lock); 2721 if (unlikely(inode->i_blocks < sectors)) { 2722 f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu", 2723 inode->i_ino, 2724 (unsigned long long)inode->i_blocks, 2725 (unsigned long long)sectors); 2726 set_sbi_flag(sbi, SBI_NEED_FSCK); 2727 return; 2728 } 2729 f2fs_i_blocks_write(inode, count, false, true); 2730 } 2731 2732 static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type) 2733 { 2734 atomic_inc(&sbi->nr_pages[count_type]); 2735 2736 if (count_type == F2FS_DIRTY_DENTS || 2737 count_type == F2FS_DIRTY_NODES || 2738 count_type == F2FS_DIRTY_META || 2739 count_type == F2FS_DIRTY_QDATA || 2740 count_type == F2FS_DIRTY_IMETA) 2741 set_sbi_flag(sbi, SBI_IS_DIRTY); 2742 } 2743 2744 static inline void inode_inc_dirty_pages(struct inode *inode) 2745 { 2746 atomic_inc(&F2FS_I(inode)->dirty_pages); 2747 inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ? 2748 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA); 2749 if (IS_NOQUOTA(inode)) 2750 inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA); 2751 } 2752 2753 static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type) 2754 { 2755 atomic_dec(&sbi->nr_pages[count_type]); 2756 } 2757 2758 static inline void inode_dec_dirty_pages(struct inode *inode) 2759 { 2760 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 2761 !S_ISLNK(inode->i_mode)) 2762 return; 2763 2764 atomic_dec(&F2FS_I(inode)->dirty_pages); 2765 dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ? 2766 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA); 2767 if (IS_NOQUOTA(inode)) 2768 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA); 2769 } 2770 2771 static inline void inc_atomic_write_cnt(struct inode *inode) 2772 { 2773 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2774 struct f2fs_inode_info *fi = F2FS_I(inode); 2775 u64 current_write; 2776 2777 fi->atomic_write_cnt++; 2778 atomic64_inc(&sbi->current_atomic_write); 2779 current_write = atomic64_read(&sbi->current_atomic_write); 2780 if (current_write > sbi->peak_atomic_write) 2781 sbi->peak_atomic_write = current_write; 2782 } 2783 2784 static inline void release_atomic_write_cnt(struct inode *inode) 2785 { 2786 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2787 struct f2fs_inode_info *fi = F2FS_I(inode); 2788 2789 atomic64_sub(fi->atomic_write_cnt, &sbi->current_atomic_write); 2790 fi->atomic_write_cnt = 0; 2791 } 2792 2793 static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type) 2794 { 2795 return atomic_read(&sbi->nr_pages[count_type]); 2796 } 2797 2798 static inline int get_dirty_pages(struct inode *inode) 2799 { 2800 return atomic_read(&F2FS_I(inode)->dirty_pages); 2801 } 2802 2803 static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type) 2804 { 2805 return div_u64(get_pages(sbi, block_type) + BLKS_PER_SEC(sbi) - 1, 2806 BLKS_PER_SEC(sbi)); 2807 } 2808 2809 static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi) 2810 { 2811 return sbi->total_valid_block_count; 2812 } 2813 2814 static inline block_t discard_blocks(struct f2fs_sb_info *sbi) 2815 { 2816 return sbi->discard_blks; 2817 } 2818 2819 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag) 2820 { 2821 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 2822 2823 /* return NAT or SIT bitmap */ 2824 if (flag == NAT_BITMAP) 2825 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize); 2826 else if (flag == SIT_BITMAP) 2827 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize); 2828 2829 return 0; 2830 } 2831 2832 static inline block_t __cp_payload(struct f2fs_sb_info *sbi) 2833 { 2834 return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload); 2835 } 2836 2837 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag) 2838 { 2839 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 2840 void *tmp_ptr = &ckpt->sit_nat_version_bitmap; 2841 int offset; 2842 2843 if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) { 2844 offset = (flag == SIT_BITMAP) ? 2845 le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0; 2846 /* 2847 * if large_nat_bitmap feature is enabled, leave checksum 2848 * protection for all nat/sit bitmaps. 2849 */ 2850 return tmp_ptr + offset + sizeof(__le32); 2851 } 2852 2853 if (__cp_payload(sbi) > 0) { 2854 if (flag == NAT_BITMAP) 2855 return tmp_ptr; 2856 else 2857 return (unsigned char *)ckpt + F2FS_BLKSIZE; 2858 } else { 2859 offset = (flag == NAT_BITMAP) ? 2860 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0; 2861 return tmp_ptr + offset; 2862 } 2863 } 2864 2865 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi) 2866 { 2867 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr); 2868 2869 if (sbi->cur_cp_pack == 2) 2870 start_addr += BLKS_PER_SEG(sbi); 2871 return start_addr; 2872 } 2873 2874 static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi) 2875 { 2876 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr); 2877 2878 if (sbi->cur_cp_pack == 1) 2879 start_addr += BLKS_PER_SEG(sbi); 2880 return start_addr; 2881 } 2882 2883 static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi) 2884 { 2885 sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1; 2886 } 2887 2888 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi) 2889 { 2890 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum); 2891 } 2892 2893 static inline bool __has_cursum_space(struct f2fs_sb_info *sbi, 2894 struct f2fs_journal *journal, unsigned int size, int type) 2895 { 2896 if (type == NAT_JOURNAL) 2897 return size <= MAX_NAT_JENTRIES(sbi, journal); 2898 return size <= MAX_SIT_JENTRIES(sbi, journal); 2899 } 2900 2901 extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync); 2902 static inline int inc_valid_node_count(struct f2fs_sb_info *sbi, 2903 struct inode *inode, bool is_inode) 2904 { 2905 block_t valid_block_count; 2906 unsigned int valid_node_count, avail_user_node_count; 2907 unsigned int avail_user_block_count; 2908 int err; 2909 2910 if (is_inode) { 2911 if (inode) { 2912 err = dquot_alloc_inode(inode); 2913 if (err) 2914 return err; 2915 } 2916 } else { 2917 err = dquot_reserve_block(inode, 1); 2918 if (err) 2919 return err; 2920 } 2921 2922 if (time_to_inject(sbi, FAULT_BLOCK)) 2923 goto enospc; 2924 2925 spin_lock(&sbi->stat_lock); 2926 2927 valid_block_count = sbi->total_valid_block_count + 1; 2928 avail_user_block_count = get_available_block_count(sbi, inode, 2929 test_opt(sbi, RESERVE_NODE)); 2930 2931 if (unlikely(valid_block_count > avail_user_block_count)) { 2932 spin_unlock(&sbi->stat_lock); 2933 goto enospc; 2934 } 2935 2936 avail_user_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM; 2937 if (test_opt(sbi, RESERVE_NODE) && 2938 !__allow_reserved_root(sbi, inode, true)) 2939 avail_user_node_count -= F2FS_OPTION(sbi).root_reserved_nodes; 2940 valid_node_count = sbi->total_valid_node_count + 1; 2941 if (unlikely(valid_node_count > avail_user_node_count)) { 2942 spin_unlock(&sbi->stat_lock); 2943 goto enospc; 2944 } 2945 2946 sbi->total_valid_node_count++; 2947 sbi->total_valid_block_count++; 2948 spin_unlock(&sbi->stat_lock); 2949 2950 if (inode) { 2951 if (is_inode) 2952 f2fs_mark_inode_dirty_sync(inode, true); 2953 else 2954 f2fs_i_blocks_write(inode, 1, true, true); 2955 } 2956 2957 percpu_counter_inc(&sbi->alloc_valid_block_count); 2958 return 0; 2959 2960 enospc: 2961 if (is_inode) { 2962 if (inode) 2963 dquot_free_inode(inode); 2964 } else { 2965 dquot_release_reservation_block(inode, 1); 2966 } 2967 return -ENOSPC; 2968 } 2969 2970 static inline void dec_valid_node_count(struct f2fs_sb_info *sbi, 2971 struct inode *inode, bool is_inode) 2972 { 2973 spin_lock(&sbi->stat_lock); 2974 2975 if (unlikely(!sbi->total_valid_block_count || 2976 !sbi->total_valid_node_count)) { 2977 f2fs_warn(sbi, "dec_valid_node_count: inconsistent block counts, total_valid_block:%u, total_valid_node:%u", 2978 sbi->total_valid_block_count, 2979 sbi->total_valid_node_count); 2980 set_sbi_flag(sbi, SBI_NEED_FSCK); 2981 } else { 2982 sbi->total_valid_block_count--; 2983 sbi->total_valid_node_count--; 2984 } 2985 2986 if (sbi->reserved_blocks && 2987 sbi->current_reserved_blocks < sbi->reserved_blocks) 2988 sbi->current_reserved_blocks++; 2989 2990 spin_unlock(&sbi->stat_lock); 2991 2992 if (is_inode) { 2993 dquot_free_inode(inode); 2994 } else { 2995 if (unlikely(inode->i_blocks == 0)) { 2996 f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu", 2997 inode->i_ino, 2998 (unsigned long long)inode->i_blocks); 2999 set_sbi_flag(sbi, SBI_NEED_FSCK); 3000 return; 3001 } 3002 f2fs_i_blocks_write(inode, 1, false, true); 3003 } 3004 } 3005 3006 static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi) 3007 { 3008 return sbi->total_valid_node_count; 3009 } 3010 3011 static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi) 3012 { 3013 percpu_counter_inc(&sbi->total_valid_inode_count); 3014 } 3015 3016 static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi) 3017 { 3018 percpu_counter_dec(&sbi->total_valid_inode_count); 3019 } 3020 3021 static inline s64 valid_inode_count(struct f2fs_sb_info *sbi) 3022 { 3023 return percpu_counter_sum_positive(&sbi->total_valid_inode_count); 3024 } 3025 3026 static inline struct folio *f2fs_grab_cache_folio(struct address_space *mapping, 3027 pgoff_t index, bool for_write) 3028 { 3029 struct folio *folio; 3030 unsigned int flags; 3031 3032 if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) { 3033 fgf_t fgf_flags; 3034 3035 if (!for_write) 3036 fgf_flags = FGP_LOCK | FGP_ACCESSED; 3037 else 3038 fgf_flags = FGP_LOCK; 3039 folio = __filemap_get_folio(mapping, index, fgf_flags, 0); 3040 if (!IS_ERR(folio)) 3041 return folio; 3042 3043 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) 3044 return ERR_PTR(-ENOMEM); 3045 } 3046 3047 if (!for_write) 3048 return filemap_grab_folio(mapping, index); 3049 3050 flags = memalloc_nofs_save(); 3051 folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN, 3052 mapping_gfp_mask(mapping)); 3053 memalloc_nofs_restore(flags); 3054 3055 return folio; 3056 } 3057 3058 static inline struct folio *f2fs_filemap_get_folio( 3059 struct address_space *mapping, pgoff_t index, 3060 fgf_t fgp_flags, gfp_t gfp_mask) 3061 { 3062 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) 3063 return ERR_PTR(-ENOMEM); 3064 3065 return __filemap_get_folio(mapping, index, fgp_flags, gfp_mask); 3066 } 3067 3068 static inline void f2fs_folio_put(struct folio *folio, bool unlock) 3069 { 3070 if (IS_ERR_OR_NULL(folio)) 3071 return; 3072 3073 if (unlock) { 3074 f2fs_bug_on(F2FS_F_SB(folio), !folio_test_locked(folio)); 3075 folio_unlock(folio); 3076 } 3077 folio_put(folio); 3078 } 3079 3080 static inline void f2fs_put_page(struct page *page, bool unlock) 3081 { 3082 if (!page) 3083 return; 3084 f2fs_folio_put(page_folio(page), unlock); 3085 } 3086 3087 static inline void f2fs_put_dnode(struct dnode_of_data *dn) 3088 { 3089 if (dn->node_folio) 3090 f2fs_folio_put(dn->node_folio, true); 3091 if (dn->inode_folio && dn->node_folio != dn->inode_folio) 3092 f2fs_folio_put(dn->inode_folio, false); 3093 dn->node_folio = NULL; 3094 dn->inode_folio = NULL; 3095 } 3096 3097 static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name, 3098 size_t size) 3099 { 3100 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL); 3101 } 3102 3103 static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep, 3104 gfp_t flags) 3105 { 3106 void *entry; 3107 3108 entry = kmem_cache_alloc(cachep, flags); 3109 if (!entry) 3110 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL); 3111 return entry; 3112 } 3113 3114 static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep, 3115 gfp_t flags, bool nofail, struct f2fs_sb_info *sbi) 3116 { 3117 if (nofail) 3118 return f2fs_kmem_cache_alloc_nofail(cachep, flags); 3119 3120 if (time_to_inject(sbi, FAULT_SLAB_ALLOC)) 3121 return NULL; 3122 3123 return kmem_cache_alloc(cachep, flags); 3124 } 3125 3126 static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type) 3127 { 3128 if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) || 3129 get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) || 3130 get_pages(sbi, F2FS_WB_CP_DATA) || 3131 get_pages(sbi, F2FS_DIO_READ) || 3132 get_pages(sbi, F2FS_DIO_WRITE)) 3133 return true; 3134 3135 if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info && 3136 atomic_read(&SM_I(sbi)->dcc_info->queued_discard)) 3137 return true; 3138 3139 if (SM_I(sbi) && SM_I(sbi)->fcc_info && 3140 atomic_read(&SM_I(sbi)->fcc_info->queued_flush)) 3141 return true; 3142 return false; 3143 } 3144 3145 static inline bool is_inflight_read_io(struct f2fs_sb_info *sbi) 3146 { 3147 return get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_DIO_READ); 3148 } 3149 3150 static inline bool is_idle(struct f2fs_sb_info *sbi, int type) 3151 { 3152 bool zoned_gc = (type == GC_TIME && 3153 F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_BLKZONED)); 3154 3155 if (sbi->gc_mode == GC_URGENT_HIGH) 3156 return true; 3157 3158 if (sbi->bggc_io_aware == AWARE_READ_IO && is_inflight_read_io(sbi)) 3159 return false; 3160 if (sbi->bggc_io_aware == AWARE_ALL_IO && is_inflight_io(sbi, type)) 3161 return false; 3162 3163 if (sbi->gc_mode == GC_URGENT_MID) 3164 return true; 3165 3166 if (sbi->gc_mode == GC_URGENT_LOW && 3167 (type == DISCARD_TIME || type == GC_TIME)) 3168 return true; 3169 3170 if (zoned_gc) 3171 return true; 3172 3173 return f2fs_time_over(sbi, type); 3174 } 3175 3176 static inline void f2fs_radix_tree_insert(struct radix_tree_root *root, 3177 unsigned long index, void *item) 3178 { 3179 while (radix_tree_insert(root, index, item)) 3180 cond_resched(); 3181 } 3182 3183 #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino) 3184 3185 static inline bool IS_INODE(const struct folio *folio) 3186 { 3187 struct f2fs_node *p = F2FS_NODE(folio); 3188 3189 return RAW_IS_INODE(p); 3190 } 3191 3192 static inline int offset_in_addr(struct f2fs_inode *i) 3193 { 3194 return (i->i_inline & F2FS_EXTRA_ATTR) ? 3195 (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0; 3196 } 3197 3198 static inline __le32 *blkaddr_in_node(struct f2fs_node *node) 3199 { 3200 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr; 3201 } 3202 3203 static inline int f2fs_has_extra_attr(struct inode *inode); 3204 static inline unsigned int get_dnode_base(struct inode *inode, 3205 struct folio *node_folio) 3206 { 3207 if (!IS_INODE(node_folio)) 3208 return 0; 3209 3210 return inode ? get_extra_isize(inode) : 3211 offset_in_addr(&F2FS_NODE(node_folio)->i); 3212 } 3213 3214 static inline __le32 *get_dnode_addr(struct inode *inode, 3215 struct folio *node_folio) 3216 { 3217 return blkaddr_in_node(F2FS_NODE(node_folio)) + 3218 get_dnode_base(inode, node_folio); 3219 } 3220 3221 static inline block_t data_blkaddr(struct inode *inode, 3222 struct folio *node_folio, unsigned int offset) 3223 { 3224 return le32_to_cpu(*(get_dnode_addr(inode, node_folio) + offset)); 3225 } 3226 3227 static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn) 3228 { 3229 return data_blkaddr(dn->inode, dn->node_folio, dn->ofs_in_node); 3230 } 3231 3232 static inline int f2fs_test_bit(unsigned int nr, char *addr) 3233 { 3234 int mask; 3235 3236 addr += (nr >> 3); 3237 mask = BIT(7 - (nr & 0x07)); 3238 return mask & *addr; 3239 } 3240 3241 static inline void f2fs_set_bit(unsigned int nr, char *addr) 3242 { 3243 int mask; 3244 3245 addr += (nr >> 3); 3246 mask = BIT(7 - (nr & 0x07)); 3247 *addr |= mask; 3248 } 3249 3250 static inline void f2fs_clear_bit(unsigned int nr, char *addr) 3251 { 3252 int mask; 3253 3254 addr += (nr >> 3); 3255 mask = BIT(7 - (nr & 0x07)); 3256 *addr &= ~mask; 3257 } 3258 3259 static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr) 3260 { 3261 int mask; 3262 int ret; 3263 3264 addr += (nr >> 3); 3265 mask = BIT(7 - (nr & 0x07)); 3266 ret = mask & *addr; 3267 *addr |= mask; 3268 return ret; 3269 } 3270 3271 static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr) 3272 { 3273 int mask; 3274 int ret; 3275 3276 addr += (nr >> 3); 3277 mask = BIT(7 - (nr & 0x07)); 3278 ret = mask & *addr; 3279 *addr &= ~mask; 3280 return ret; 3281 } 3282 3283 static inline void f2fs_change_bit(unsigned int nr, char *addr) 3284 { 3285 int mask; 3286 3287 addr += (nr >> 3); 3288 mask = BIT(7 - (nr & 0x07)); 3289 *addr ^= mask; 3290 } 3291 3292 /* 3293 * On-disk inode flags (f2fs_inode::i_flags) 3294 */ 3295 #define F2FS_COMPR_FL 0x00000004 /* Compress file */ 3296 #define F2FS_SYNC_FL 0x00000008 /* Synchronous updates */ 3297 #define F2FS_IMMUTABLE_FL 0x00000010 /* Immutable file */ 3298 #define F2FS_APPEND_FL 0x00000020 /* writes to file may only append */ 3299 #define F2FS_NODUMP_FL 0x00000040 /* do not dump file */ 3300 #define F2FS_NOATIME_FL 0x00000080 /* do not update atime */ 3301 #define F2FS_NOCOMP_FL 0x00000400 /* Don't compress */ 3302 #define F2FS_INDEX_FL 0x00001000 /* hash-indexed directory */ 3303 #define F2FS_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */ 3304 #define F2FS_PROJINHERIT_FL 0x20000000 /* Create with parents projid */ 3305 #define F2FS_CASEFOLD_FL 0x40000000 /* Casefolded file */ 3306 #define F2FS_DEVICE_ALIAS_FL 0x80000000 /* File for aliasing a device */ 3307 3308 #define F2FS_QUOTA_DEFAULT_FL (F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL) 3309 3310 /* Flags that should be inherited by new inodes from their parent. */ 3311 #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \ 3312 F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \ 3313 F2FS_CASEFOLD_FL) 3314 3315 /* Flags that are appropriate for regular files (all but dir-specific ones). */ 3316 #define F2FS_REG_FLMASK (~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \ 3317 F2FS_CASEFOLD_FL)) 3318 3319 /* Flags that are appropriate for non-directories/regular files. */ 3320 #define F2FS_OTHER_FLMASK (F2FS_NODUMP_FL | F2FS_NOATIME_FL) 3321 3322 #define IS_DEVICE_ALIASING(inode) (F2FS_I(inode)->i_flags & F2FS_DEVICE_ALIAS_FL) 3323 3324 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) 3325 { 3326 if (S_ISDIR(mode)) 3327 return flags; 3328 else if (S_ISREG(mode)) 3329 return flags & F2FS_REG_FLMASK; 3330 else 3331 return flags & F2FS_OTHER_FLMASK; 3332 } 3333 3334 static inline void __mark_inode_dirty_flag(struct inode *inode, 3335 int flag, bool set) 3336 { 3337 switch (flag) { 3338 case FI_INLINE_XATTR: 3339 case FI_INLINE_DATA: 3340 case FI_INLINE_DENTRY: 3341 case FI_NEW_INODE: 3342 if (set) 3343 return; 3344 fallthrough; 3345 case FI_DATA_EXIST: 3346 case FI_PIN_FILE: 3347 case FI_COMPRESS_RELEASED: 3348 f2fs_mark_inode_dirty_sync(inode, true); 3349 } 3350 } 3351 3352 static inline void set_inode_flag(struct inode *inode, int flag) 3353 { 3354 set_bit(flag, F2FS_I(inode)->flags); 3355 __mark_inode_dirty_flag(inode, flag, true); 3356 } 3357 3358 static inline int is_inode_flag_set(struct inode *inode, int flag) 3359 { 3360 return test_bit(flag, F2FS_I(inode)->flags); 3361 } 3362 3363 static inline void clear_inode_flag(struct inode *inode, int flag) 3364 { 3365 clear_bit(flag, F2FS_I(inode)->flags); 3366 __mark_inode_dirty_flag(inode, flag, false); 3367 } 3368 3369 static inline bool f2fs_verity_in_progress(struct inode *inode) 3370 { 3371 return IS_ENABLED(CONFIG_FS_VERITY) && 3372 is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS); 3373 } 3374 3375 static inline void set_acl_inode(struct inode *inode, umode_t mode) 3376 { 3377 F2FS_I(inode)->i_acl_mode = mode; 3378 set_inode_flag(inode, FI_ACL_MODE); 3379 f2fs_mark_inode_dirty_sync(inode, false); 3380 } 3381 3382 static inline void f2fs_i_links_write(struct inode *inode, bool inc) 3383 { 3384 if (inc) 3385 inc_nlink(inode); 3386 else 3387 drop_nlink(inode); 3388 f2fs_mark_inode_dirty_sync(inode, true); 3389 } 3390 3391 static inline void f2fs_i_blocks_write(struct inode *inode, 3392 block_t diff, bool add, bool claim) 3393 { 3394 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE); 3395 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER); 3396 3397 /* add = 1, claim = 1 should be dquot_reserve_block in pair */ 3398 if (add) { 3399 if (claim) 3400 dquot_claim_block(inode, diff); 3401 else 3402 dquot_alloc_block_nofail(inode, diff); 3403 } else { 3404 dquot_free_block(inode, diff); 3405 } 3406 3407 f2fs_mark_inode_dirty_sync(inode, true); 3408 if (clean || recover) 3409 set_inode_flag(inode, FI_AUTO_RECOVER); 3410 } 3411 3412 static inline bool f2fs_is_atomic_file(struct inode *inode); 3413 3414 static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size) 3415 { 3416 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE); 3417 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER); 3418 3419 if (i_size_read(inode) == i_size) 3420 return; 3421 3422 i_size_write(inode, i_size); 3423 3424 if (f2fs_is_atomic_file(inode)) 3425 return; 3426 3427 f2fs_mark_inode_dirty_sync(inode, true); 3428 if (clean || recover) 3429 set_inode_flag(inode, FI_AUTO_RECOVER); 3430 } 3431 3432 static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth) 3433 { 3434 F2FS_I(inode)->i_current_depth = depth; 3435 f2fs_mark_inode_dirty_sync(inode, true); 3436 } 3437 3438 static inline void f2fs_i_gc_failures_write(struct inode *inode, 3439 unsigned int count) 3440 { 3441 F2FS_I(inode)->i_gc_failures = count; 3442 f2fs_mark_inode_dirty_sync(inode, true); 3443 } 3444 3445 static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid) 3446 { 3447 F2FS_I(inode)->i_xattr_nid = xnid; 3448 f2fs_mark_inode_dirty_sync(inode, true); 3449 } 3450 3451 static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino) 3452 { 3453 F2FS_I(inode)->i_pino = pino; 3454 f2fs_mark_inode_dirty_sync(inode, true); 3455 } 3456 3457 static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri) 3458 { 3459 struct f2fs_inode_info *fi = F2FS_I(inode); 3460 3461 if (ri->i_inline & F2FS_INLINE_XATTR) 3462 set_bit(FI_INLINE_XATTR, fi->flags); 3463 if (ri->i_inline & F2FS_INLINE_DATA) 3464 set_bit(FI_INLINE_DATA, fi->flags); 3465 if (ri->i_inline & F2FS_INLINE_DENTRY) 3466 set_bit(FI_INLINE_DENTRY, fi->flags); 3467 if (ri->i_inline & F2FS_DATA_EXIST) 3468 set_bit(FI_DATA_EXIST, fi->flags); 3469 if (ri->i_inline & F2FS_EXTRA_ATTR) 3470 set_bit(FI_EXTRA_ATTR, fi->flags); 3471 if (ri->i_inline & F2FS_PIN_FILE) 3472 set_bit(FI_PIN_FILE, fi->flags); 3473 if (ri->i_inline & F2FS_COMPRESS_RELEASED) 3474 set_bit(FI_COMPRESS_RELEASED, fi->flags); 3475 } 3476 3477 static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri) 3478 { 3479 ri->i_inline = 0; 3480 3481 if (is_inode_flag_set(inode, FI_INLINE_XATTR)) 3482 ri->i_inline |= F2FS_INLINE_XATTR; 3483 if (is_inode_flag_set(inode, FI_INLINE_DATA)) 3484 ri->i_inline |= F2FS_INLINE_DATA; 3485 if (is_inode_flag_set(inode, FI_INLINE_DENTRY)) 3486 ri->i_inline |= F2FS_INLINE_DENTRY; 3487 if (is_inode_flag_set(inode, FI_DATA_EXIST)) 3488 ri->i_inline |= F2FS_DATA_EXIST; 3489 if (is_inode_flag_set(inode, FI_EXTRA_ATTR)) 3490 ri->i_inline |= F2FS_EXTRA_ATTR; 3491 if (is_inode_flag_set(inode, FI_PIN_FILE)) 3492 ri->i_inline |= F2FS_PIN_FILE; 3493 if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) 3494 ri->i_inline |= F2FS_COMPRESS_RELEASED; 3495 } 3496 3497 static inline int f2fs_has_extra_attr(struct inode *inode) 3498 { 3499 return is_inode_flag_set(inode, FI_EXTRA_ATTR); 3500 } 3501 3502 static inline int f2fs_has_inline_xattr(struct inode *inode) 3503 { 3504 return is_inode_flag_set(inode, FI_INLINE_XATTR); 3505 } 3506 3507 static inline int f2fs_compressed_file(struct inode *inode) 3508 { 3509 return S_ISREG(inode->i_mode) && 3510 is_inode_flag_set(inode, FI_COMPRESSED_FILE); 3511 } 3512 3513 static inline bool f2fs_need_compress_data(struct inode *inode) 3514 { 3515 int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode; 3516 3517 if (!f2fs_compressed_file(inode)) 3518 return false; 3519 3520 if (compress_mode == COMPR_MODE_FS) 3521 return true; 3522 else if (compress_mode == COMPR_MODE_USER && 3523 is_inode_flag_set(inode, FI_ENABLE_COMPRESS)) 3524 return true; 3525 3526 return false; 3527 } 3528 3529 static inline unsigned int addrs_per_page(struct inode *inode, 3530 bool is_inode) 3531 { 3532 unsigned int addrs = is_inode ? (CUR_ADDRS_PER_INODE(inode) - 3533 get_inline_xattr_addrs(inode)) : DEF_ADDRS_PER_BLOCK; 3534 3535 if (f2fs_compressed_file(inode)) 3536 return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size); 3537 return addrs; 3538 } 3539 3540 static inline 3541 void *inline_xattr_addr(struct inode *inode, const struct folio *folio) 3542 { 3543 struct f2fs_inode *ri = F2FS_INODE(folio); 3544 3545 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE - 3546 get_inline_xattr_addrs(inode)]); 3547 } 3548 3549 static inline int inline_xattr_size(struct inode *inode) 3550 { 3551 if (f2fs_has_inline_xattr(inode)) 3552 return get_inline_xattr_addrs(inode) * sizeof(__le32); 3553 return 0; 3554 } 3555 3556 /* 3557 * Notice: check inline_data flag without inode page lock is unsafe. 3558 * It could change at any time by f2fs_convert_inline_folio(). 3559 */ 3560 static inline int f2fs_has_inline_data(struct inode *inode) 3561 { 3562 return is_inode_flag_set(inode, FI_INLINE_DATA); 3563 } 3564 3565 static inline int f2fs_exist_data(struct inode *inode) 3566 { 3567 return is_inode_flag_set(inode, FI_DATA_EXIST); 3568 } 3569 3570 static inline int f2fs_is_mmap_file(struct inode *inode) 3571 { 3572 return is_inode_flag_set(inode, FI_MMAP_FILE); 3573 } 3574 3575 static inline bool f2fs_is_pinned_file(struct inode *inode) 3576 { 3577 return is_inode_flag_set(inode, FI_PIN_FILE); 3578 } 3579 3580 static inline bool f2fs_is_atomic_file(struct inode *inode) 3581 { 3582 return is_inode_flag_set(inode, FI_ATOMIC_FILE); 3583 } 3584 3585 static inline bool f2fs_is_cow_file(struct inode *inode) 3586 { 3587 return is_inode_flag_set(inode, FI_COW_FILE); 3588 } 3589 3590 static inline void *inline_data_addr(struct inode *inode, struct folio *folio) 3591 { 3592 __le32 *addr = get_dnode_addr(inode, folio); 3593 3594 return (void *)(addr + DEF_INLINE_RESERVED_SIZE); 3595 } 3596 3597 static inline int f2fs_has_inline_dentry(struct inode *inode) 3598 { 3599 return is_inode_flag_set(inode, FI_INLINE_DENTRY); 3600 } 3601 3602 static inline int is_file(struct inode *inode, int type) 3603 { 3604 return F2FS_I(inode)->i_advise & type; 3605 } 3606 3607 static inline void set_file(struct inode *inode, int type) 3608 { 3609 if (is_file(inode, type)) 3610 return; 3611 F2FS_I(inode)->i_advise |= type; 3612 f2fs_mark_inode_dirty_sync(inode, true); 3613 } 3614 3615 static inline void clear_file(struct inode *inode, int type) 3616 { 3617 if (!is_file(inode, type)) 3618 return; 3619 F2FS_I(inode)->i_advise &= ~type; 3620 f2fs_mark_inode_dirty_sync(inode, true); 3621 } 3622 3623 static inline bool f2fs_is_time_consistent(struct inode *inode) 3624 { 3625 struct timespec64 ts = inode_get_atime(inode); 3626 3627 if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &ts)) 3628 return false; 3629 ts = inode_get_ctime(inode); 3630 if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &ts)) 3631 return false; 3632 ts = inode_get_mtime(inode); 3633 if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &ts)) 3634 return false; 3635 return true; 3636 } 3637 3638 static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync) 3639 { 3640 bool ret; 3641 3642 if (dsync) { 3643 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3644 3645 spin_lock(&sbi->inode_lock[DIRTY_META]); 3646 ret = list_empty(&F2FS_I(inode)->gdirty_list); 3647 spin_unlock(&sbi->inode_lock[DIRTY_META]); 3648 return ret; 3649 } 3650 if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) || 3651 file_keep_isize(inode) || 3652 i_size_read(inode) & ~PAGE_MASK) 3653 return false; 3654 3655 if (!f2fs_is_time_consistent(inode)) 3656 return false; 3657 3658 spin_lock(&F2FS_I(inode)->i_size_lock); 3659 ret = F2FS_I(inode)->last_disk_size == i_size_read(inode); 3660 spin_unlock(&F2FS_I(inode)->i_size_lock); 3661 3662 return ret; 3663 } 3664 3665 static inline bool f2fs_readonly(struct super_block *sb) 3666 { 3667 return sb_rdonly(sb); 3668 } 3669 3670 static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi) 3671 { 3672 return is_set_ckpt_flags(sbi, CP_ERROR_FLAG); 3673 } 3674 3675 static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi, 3676 size_t size, gfp_t flags) 3677 { 3678 if (time_to_inject(sbi, FAULT_KMALLOC)) 3679 return NULL; 3680 3681 return kmalloc(size, flags); 3682 } 3683 3684 static inline void *f2fs_getname(struct f2fs_sb_info *sbi) 3685 { 3686 if (time_to_inject(sbi, FAULT_KMALLOC)) 3687 return NULL; 3688 3689 return __getname(); 3690 } 3691 3692 static inline void f2fs_putname(char *buf) 3693 { 3694 __putname(buf); 3695 } 3696 3697 static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi, 3698 size_t size, gfp_t flags) 3699 { 3700 return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO); 3701 } 3702 3703 static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi, 3704 size_t size, gfp_t flags) 3705 { 3706 if (time_to_inject(sbi, FAULT_KVMALLOC)) 3707 return NULL; 3708 3709 return kvmalloc(size, flags); 3710 } 3711 3712 static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi, 3713 size_t size, gfp_t flags) 3714 { 3715 return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO); 3716 } 3717 3718 static inline void *f2fs_vmalloc(struct f2fs_sb_info *sbi, size_t size) 3719 { 3720 if (time_to_inject(sbi, FAULT_VMALLOC)) 3721 return NULL; 3722 3723 return vmalloc(size); 3724 } 3725 3726 static inline int get_extra_isize(struct inode *inode) 3727 { 3728 return F2FS_I(inode)->i_extra_isize / sizeof(__le32); 3729 } 3730 3731 static inline int get_inline_xattr_addrs(struct inode *inode) 3732 { 3733 return F2FS_I(inode)->i_inline_xattr_size; 3734 } 3735 3736 #define f2fs_get_inode_mode(i) \ 3737 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \ 3738 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode)) 3739 3740 #define F2FS_MIN_EXTRA_ATTR_SIZE (sizeof(__le32)) 3741 3742 #define F2FS_TOTAL_EXTRA_ATTR_SIZE \ 3743 (offsetof(struct f2fs_inode, i_extra_end) - \ 3744 offsetof(struct f2fs_inode, i_extra_isize)) \ 3745 3746 #define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr)) 3747 #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \ 3748 ((offsetof(typeof(*(f2fs_inode)), field) + \ 3749 sizeof((f2fs_inode)->field)) \ 3750 <= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize))) \ 3751 3752 #define __is_large_section(sbi) (SEGS_PER_SEC(sbi) > 1) 3753 3754 #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META) 3755 3756 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 3757 block_t blkaddr, int type); 3758 static inline void verify_blkaddr(struct f2fs_sb_info *sbi, 3759 block_t blkaddr, int type) 3760 { 3761 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) 3762 f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.", 3763 blkaddr, type); 3764 } 3765 3766 static inline bool __is_valid_data_blkaddr(block_t blkaddr) 3767 { 3768 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR || 3769 blkaddr == COMPRESS_ADDR) 3770 return false; 3771 return true; 3772 } 3773 3774 /* 3775 * file.c 3776 */ 3777 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync); 3778 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock); 3779 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock); 3780 int f2fs_truncate(struct inode *inode); 3781 int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path, 3782 struct kstat *stat, u32 request_mask, unsigned int flags); 3783 int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 3784 struct iattr *attr); 3785 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end); 3786 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count); 3787 int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag, 3788 bool readonly, bool need_lock); 3789 int f2fs_precache_extents(struct inode *inode); 3790 int f2fs_fileattr_get(struct dentry *dentry, struct file_kattr *fa); 3791 int f2fs_fileattr_set(struct mnt_idmap *idmap, 3792 struct dentry *dentry, struct file_kattr *fa); 3793 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); 3794 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 3795 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid); 3796 int f2fs_pin_file_control(struct inode *inode, bool inc); 3797 3798 /* 3799 * inode.c 3800 */ 3801 void f2fs_set_inode_flags(struct inode *inode); 3802 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct folio *folio); 3803 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct folio *folio); 3804 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino); 3805 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino); 3806 int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink); 3807 void f2fs_update_inode(struct inode *inode, struct folio *node_folio); 3808 void f2fs_update_inode_page(struct inode *inode); 3809 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc); 3810 void f2fs_remove_donate_inode(struct inode *inode); 3811 void f2fs_evict_inode(struct inode *inode); 3812 void f2fs_handle_failed_inode(struct inode *inode, struct f2fs_lock_context *lc); 3813 3814 /* 3815 * namei.c 3816 */ 3817 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, 3818 bool hot, bool set); 3819 struct dentry *f2fs_get_parent(struct dentry *child); 3820 int f2fs_get_tmpfile(struct mnt_idmap *idmap, struct inode *dir, 3821 struct inode **new_inode); 3822 3823 /* 3824 * dir.c 3825 */ 3826 #if IS_ENABLED(CONFIG_UNICODE) 3827 int f2fs_init_casefolded_name(const struct inode *dir, 3828 struct f2fs_filename *fname); 3829 void f2fs_free_casefolded_name(struct f2fs_filename *fname); 3830 #else 3831 static inline int f2fs_init_casefolded_name(const struct inode *dir, 3832 struct f2fs_filename *fname) 3833 { 3834 return 0; 3835 } 3836 3837 static inline void f2fs_free_casefolded_name(struct f2fs_filename *fname) 3838 { 3839 } 3840 #endif /* CONFIG_UNICODE */ 3841 3842 int f2fs_setup_filename(struct inode *dir, const struct qstr *iname, 3843 int lookup, struct f2fs_filename *fname); 3844 int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry, 3845 struct f2fs_filename *fname); 3846 void f2fs_free_filename(struct f2fs_filename *fname); 3847 struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d, 3848 const struct f2fs_filename *fname, int *max_slots, 3849 bool use_hash); 3850 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, 3851 unsigned int start_pos, struct fscrypt_str *fstr); 3852 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent, 3853 struct f2fs_dentry_ptr *d); 3854 struct folio *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir, 3855 const struct f2fs_filename *fname, struct folio *dfolio); 3856 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode, 3857 unsigned int current_depth); 3858 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots); 3859 void f2fs_drop_nlink(struct inode *dir, struct inode *inode); 3860 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir, 3861 const struct f2fs_filename *fname, struct folio **res_folio); 3862 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir, 3863 const struct qstr *child, struct folio **res_folio); 3864 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct folio **f); 3865 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr, 3866 struct folio **folio); 3867 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de, 3868 struct folio *folio, struct inode *inode); 3869 bool f2fs_has_enough_room(struct inode *dir, struct folio *ifolio, 3870 const struct f2fs_filename *fname); 3871 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d, 3872 const struct fscrypt_str *name, f2fs_hash_t name_hash, 3873 unsigned int bit_pos); 3874 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname, 3875 struct inode *inode, nid_t ino, umode_t mode); 3876 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname, 3877 struct inode *inode, nid_t ino, umode_t mode); 3878 int f2fs_do_add_link(struct inode *dir, const struct qstr *name, 3879 struct inode *inode, nid_t ino, umode_t mode); 3880 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct folio *folio, 3881 struct inode *dir, struct inode *inode); 3882 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir, 3883 struct f2fs_filename *fname); 3884 bool f2fs_empty_dir(struct inode *dir); 3885 3886 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode) 3887 { 3888 if (fscrypt_is_nokey_name(dentry)) 3889 return -ENOKEY; 3890 return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name, 3891 inode, inode->i_ino, inode->i_mode); 3892 } 3893 3894 /* 3895 * super.c 3896 */ 3897 int f2fs_inode_dirtied(struct inode *inode, bool sync); 3898 void f2fs_inode_synced(struct inode *inode); 3899 int f2fs_dquot_initialize(struct inode *inode); 3900 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly); 3901 int f2fs_do_quota_sync(struct super_block *sb, int type); 3902 loff_t max_file_blocks(struct inode *inode); 3903 void f2fs_quota_off_umount(struct super_block *sb); 3904 void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag); 3905 void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason); 3906 void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error); 3907 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover); 3908 int f2fs_sync_fs(struct super_block *sb, int sync); 3909 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi); 3910 3911 /* 3912 * hash.c 3913 */ 3914 void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname); 3915 3916 /* 3917 * node.c 3918 */ 3919 struct node_info; 3920 enum node_type; 3921 3922 int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid); 3923 bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type); 3924 bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct folio *folio); 3925 void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi); 3926 void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct folio *folio); 3927 void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi); 3928 int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid); 3929 bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid); 3930 bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino); 3931 int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid, 3932 struct node_info *ni, bool checkpoint_context); 3933 pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs); 3934 int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode); 3935 int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from); 3936 int f2fs_truncate_xattr_node(struct inode *inode); 3937 int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, 3938 unsigned int seq_id); 3939 int f2fs_remove_inode_page(struct inode *inode); 3940 struct folio *f2fs_new_inode_folio(struct inode *inode); 3941 struct folio *f2fs_new_node_folio(struct dnode_of_data *dn, unsigned int ofs); 3942 void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid); 3943 struct folio *f2fs_get_node_folio(struct f2fs_sb_info *sbi, pgoff_t nid, 3944 enum node_type node_type); 3945 int f2fs_sanity_check_node_footer(struct f2fs_sb_info *sbi, 3946 struct folio *folio, pgoff_t nid, 3947 enum node_type ntype, bool in_irq); 3948 struct folio *f2fs_get_inode_folio(struct f2fs_sb_info *sbi, pgoff_t ino); 3949 struct folio *f2fs_get_xnode_folio(struct f2fs_sb_info *sbi, pgoff_t xnid); 3950 int f2fs_move_node_folio(struct folio *node_folio, int gc_type); 3951 void f2fs_flush_inline_data(struct f2fs_sb_info *sbi); 3952 int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode, 3953 struct writeback_control *wbc, bool atomic, 3954 unsigned int *seq_id); 3955 int f2fs_sync_node_pages(struct f2fs_sb_info *sbi, 3956 struct writeback_control *wbc, 3957 bool do_balance, enum iostat_type io_type); 3958 int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount); 3959 bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid); 3960 void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid); 3961 void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid); 3962 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink); 3963 int f2fs_recover_inline_xattr(struct inode *inode, struct folio *folio); 3964 int f2fs_recover_xattr_data(struct inode *inode, struct folio *folio); 3965 int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct folio *folio); 3966 int f2fs_restore_node_summary(struct f2fs_sb_info *sbi, 3967 unsigned int segno, struct f2fs_summary_block *sum); 3968 int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc); 3969 int f2fs_build_node_manager(struct f2fs_sb_info *sbi); 3970 void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi); 3971 int __init f2fs_create_node_manager_caches(void); 3972 void f2fs_destroy_node_manager_caches(void); 3973 3974 /* 3975 * segment.c 3976 */ 3977 bool f2fs_need_SSR(struct f2fs_sb_info *sbi); 3978 int f2fs_commit_atomic_write(struct inode *inode); 3979 void f2fs_abort_atomic_write(struct inode *inode, bool clean); 3980 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need); 3981 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg); 3982 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino); 3983 int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi); 3984 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi); 3985 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free); 3986 void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr, 3987 unsigned int len); 3988 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr); 3989 int f2fs_start_discard_thread(struct f2fs_sb_info *sbi); 3990 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi); 3991 void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi); 3992 bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi); 3993 void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi, 3994 struct cp_control *cpc); 3995 void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi); 3996 block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi); 3997 int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable); 3998 void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi); 3999 int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra); 4000 bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno); 4001 int f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi); 4002 int f2fs_reinit_atgc_curseg(struct f2fs_sb_info *sbi); 4003 void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi); 4004 void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi); 4005 int f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type, 4006 unsigned int start, unsigned int end); 4007 int f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force); 4008 int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi); 4009 int f2fs_allocate_new_segments(struct f2fs_sb_info *sbi); 4010 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range); 4011 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi, 4012 struct cp_control *cpc); 4013 struct folio *f2fs_get_sum_folio(struct f2fs_sb_info *sbi, unsigned int segno); 4014 void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src, 4015 block_t blk_addr); 4016 void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct folio *folio, 4017 enum iostat_type io_type); 4018 void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio); 4019 void f2fs_outplace_write_data(struct dnode_of_data *dn, 4020 struct f2fs_io_info *fio); 4021 int f2fs_inplace_write_data(struct f2fs_io_info *fio); 4022 void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, 4023 block_t old_blkaddr, block_t new_blkaddr, 4024 bool recover_curseg, bool recover_newaddr, 4025 bool from_gc); 4026 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn, 4027 block_t old_addr, block_t new_addr, 4028 unsigned char version, bool recover_curseg, 4029 bool recover_newaddr); 4030 enum temp_type f2fs_get_segment_temp(struct f2fs_sb_info *sbi, 4031 enum log_type seg_type); 4032 int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct folio *folio, 4033 block_t old_blkaddr, block_t *new_blkaddr, 4034 struct f2fs_summary *sum, int type, 4035 struct f2fs_io_info *fio); 4036 void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino, 4037 block_t blkaddr, unsigned int blkcnt); 4038 void f2fs_folio_wait_writeback(struct folio *folio, enum page_type type, 4039 bool ordered, bool locked); 4040 #define f2fs_wait_on_page_writeback(page, type, ordered, locked) \ 4041 f2fs_folio_wait_writeback(page_folio(page), type, ordered, locked) 4042 void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr); 4043 void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr, 4044 block_t len); 4045 void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk); 4046 void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk); 4047 int f2fs_lookup_journal_in_cursum(struct f2fs_sb_info *sbi, 4048 struct f2fs_journal *journal, int type, 4049 unsigned int val, int alloc); 4050 void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc); 4051 int f2fs_check_and_fix_write_pointer(struct f2fs_sb_info *sbi); 4052 int f2fs_build_segment_manager(struct f2fs_sb_info *sbi); 4053 void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi); 4054 int __init f2fs_create_segment_manager_caches(void); 4055 void f2fs_destroy_segment_manager_caches(void); 4056 int f2fs_rw_hint_to_seg_type(struct f2fs_sb_info *sbi, enum rw_hint hint); 4057 enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi, 4058 enum page_type type, enum temp_type temp); 4059 unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi); 4060 unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi, 4061 unsigned int segno); 4062 unsigned long long f2fs_get_section_mtime(struct f2fs_sb_info *sbi, 4063 unsigned int segno); 4064 4065 static inline struct inode *fio_inode(struct f2fs_io_info *fio) 4066 { 4067 return fio->folio->mapping->host; 4068 } 4069 4070 #define DEF_FRAGMENT_SIZE 4 4071 #define MIN_FRAGMENT_SIZE 1 4072 #define MAX_FRAGMENT_SIZE 512 4073 4074 static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi) 4075 { 4076 return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG || 4077 F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK; 4078 } 4079 4080 /* 4081 * checkpoint.c 4082 */ 4083 void f2fs_lock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc); 4084 int f2fs_trylock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc); 4085 void f2fs_unlock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc); 4086 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io, 4087 unsigned char reason); 4088 void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi); 4089 struct folio *f2fs_grab_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index); 4090 struct folio *f2fs_get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index); 4091 struct folio *f2fs_get_meta_folio_retry(struct f2fs_sb_info *sbi, pgoff_t index); 4092 struct folio *f2fs_get_tmp_folio(struct f2fs_sb_info *sbi, pgoff_t index); 4093 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 4094 block_t blkaddr, int type); 4095 bool f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info *sbi, 4096 block_t blkaddr, int type); 4097 int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 4098 int type, bool sync); 4099 void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index, 4100 unsigned int ra_blocks); 4101 long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, long nr_to_write, 4102 enum iostat_type io_type); 4103 void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type); 4104 void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type); 4105 void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all); 4106 bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode); 4107 void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 4108 unsigned int devidx, int type); 4109 bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 4110 unsigned int devidx, int type); 4111 int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi); 4112 void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi); 4113 void f2fs_add_orphan_inode(struct inode *inode); 4114 void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino); 4115 int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi); 4116 int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi); 4117 void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio); 4118 void f2fs_remove_dirty_inode(struct inode *inode); 4119 int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type, 4120 bool from_cp); 4121 void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type); 4122 u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi); 4123 int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc); 4124 void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi); 4125 int __init f2fs_create_checkpoint_caches(void); 4126 void f2fs_destroy_checkpoint_caches(void); 4127 int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi); 4128 int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi); 4129 void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi); 4130 void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi); 4131 4132 /* 4133 * data.c 4134 */ 4135 int __init f2fs_init_bioset(void); 4136 void f2fs_destroy_bioset(void); 4137 bool f2fs_is_cp_guaranteed(const struct folio *folio); 4138 int f2fs_init_bio_entry_cache(void); 4139 void f2fs_destroy_bio_entry_cache(void); 4140 void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio, 4141 enum page_type type); 4142 int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi); 4143 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type); 4144 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi, 4145 struct inode *inode, struct folio *folio, 4146 nid_t ino, enum page_type type); 4147 void f2fs_submit_merged_write_folio(struct f2fs_sb_info *sbi, 4148 struct folio *folio, enum page_type type); 4149 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi, 4150 struct bio **bio, struct folio *folio); 4151 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi); 4152 int f2fs_submit_page_bio(struct f2fs_io_info *fio); 4153 int f2fs_merge_page_bio(struct f2fs_io_info *fio); 4154 void f2fs_submit_page_write(struct f2fs_io_info *fio); 4155 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, 4156 block_t blk_addr, sector_t *sector); 4157 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr); 4158 void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr); 4159 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr); 4160 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count); 4161 int f2fs_reserve_new_block(struct dnode_of_data *dn); 4162 int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index); 4163 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index); 4164 struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index, 4165 blk_opf_t op_flags, bool for_write, pgoff_t *next_pgofs); 4166 struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index, 4167 pgoff_t *next_pgofs); 4168 struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index, 4169 bool for_write); 4170 struct folio *f2fs_get_new_data_folio(struct inode *inode, 4171 struct folio *ifolio, pgoff_t index, bool new_i_size); 4172 int f2fs_do_write_data_page(struct f2fs_io_info *fio); 4173 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag); 4174 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 4175 u64 start, u64 len); 4176 int f2fs_encrypt_one_page(struct f2fs_io_info *fio); 4177 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio); 4178 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio); 4179 int f2fs_write_single_data_page(struct folio *folio, int *submitted, 4180 struct bio **bio, sector_t *last_block, 4181 struct writeback_control *wbc, 4182 enum iostat_type io_type, 4183 int compr_blocks, bool allow_balance); 4184 void f2fs_write_failed(struct inode *inode, loff_t to); 4185 void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length); 4186 bool f2fs_release_folio(struct folio *folio, gfp_t wait); 4187 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len); 4188 void f2fs_clear_page_cache_dirty_tag(struct folio *folio); 4189 int f2fs_init_post_read_processing(void); 4190 void f2fs_destroy_post_read_processing(void); 4191 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi); 4192 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi); 4193 extern const struct iomap_ops f2fs_iomap_ops; 4194 4195 /* 4196 * gc.c 4197 */ 4198 int f2fs_start_gc_thread(struct f2fs_sb_info *sbi); 4199 void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi); 4200 block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode); 4201 int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control); 4202 void f2fs_build_gc_manager(struct f2fs_sb_info *sbi); 4203 int f2fs_gc_range(struct f2fs_sb_info *sbi, 4204 unsigned int start_seg, unsigned int end_seg, 4205 bool dry_run, unsigned int dry_run_sections); 4206 int f2fs_resize_fs(struct file *filp, __u64 block_count); 4207 int __init f2fs_create_garbage_collection_cache(void); 4208 void f2fs_destroy_garbage_collection_cache(void); 4209 /* victim selection function for cleaning and SSR */ 4210 int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result, 4211 int gc_type, int type, char alloc_mode, 4212 unsigned long long age, bool one_time); 4213 4214 /* 4215 * recovery.c 4216 */ 4217 int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only); 4218 bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi); 4219 int __init f2fs_create_recovery_cache(void); 4220 void f2fs_destroy_recovery_cache(void); 4221 4222 /* 4223 * debug.c 4224 */ 4225 #ifdef CONFIG_F2FS_STAT_FS 4226 enum { 4227 DEVSTAT_INUSE, 4228 DEVSTAT_DIRTY, 4229 DEVSTAT_FULL, 4230 DEVSTAT_FREE, 4231 DEVSTAT_PREFREE, 4232 DEVSTAT_MAX, 4233 }; 4234 4235 struct f2fs_dev_stats { 4236 unsigned int devstats[2][DEVSTAT_MAX]; /* 0: segs, 1: secs */ 4237 }; 4238 4239 struct f2fs_stat_info { 4240 struct list_head stat_list; 4241 struct f2fs_sb_info *sbi; 4242 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs; 4243 int main_area_segs, main_area_sections, main_area_zones; 4244 unsigned long long hit_cached[NR_EXTENT_CACHES]; 4245 unsigned long long hit_rbtree[NR_EXTENT_CACHES]; 4246 unsigned long long total_ext[NR_EXTENT_CACHES]; 4247 unsigned long long hit_total[NR_EXTENT_CACHES]; 4248 int ext_tree[NR_EXTENT_CACHES]; 4249 int zombie_tree[NR_EXTENT_CACHES]; 4250 int ext_node[NR_EXTENT_CACHES]; 4251 /* to count memory footprint */ 4252 unsigned long long ext_mem[NR_EXTENT_CACHES]; 4253 /* for read extent cache */ 4254 unsigned long long hit_largest; 4255 /* for block age extent cache */ 4256 unsigned long long allocated_data_blocks; 4257 int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta; 4258 int ndirty_data, ndirty_qdata; 4259 unsigned int ndirty_dirs, ndirty_files, ndirty_all; 4260 unsigned int nquota_files, ndonate_files; 4261 int nats, dirty_nats, sits, dirty_sits; 4262 int free_nids, avail_nids, alloc_nids; 4263 int total_count, utilization; 4264 int nr_wb_cp_data, nr_wb_data; 4265 int nr_rd_data, nr_rd_node, nr_rd_meta; 4266 int nr_dio_read, nr_dio_write; 4267 unsigned int io_skip_bggc, other_skip_bggc; 4268 int nr_flushing, nr_flushed, flush_list_empty; 4269 int nr_discarding, nr_discarded; 4270 int nr_discard_cmd; 4271 unsigned int undiscard_blks; 4272 int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt; 4273 unsigned int cur_ckpt_time, peak_ckpt_time; 4274 int inline_xattr, inline_inode, inline_dir, append, update, orphans; 4275 int compr_inode, swapfile_inode; 4276 unsigned long long compr_blocks; 4277 int aw_cnt, max_aw_cnt; 4278 unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks; 4279 unsigned int bimodal, avg_vblocks; 4280 int util_free, util_valid, util_invalid; 4281 int rsvd_segs, overp_segs; 4282 int dirty_count, node_pages, meta_pages, compress_pages; 4283 int compress_page_hit; 4284 int prefree_count, free_segs, free_secs; 4285 int cp_call_count[MAX_CALL_TYPE], cp_count; 4286 int gc_call_count[MAX_CALL_TYPE]; 4287 int gc_segs[2][2]; 4288 int gc_secs[2][2]; 4289 int tot_blks, data_blks, node_blks; 4290 int bg_data_blks, bg_node_blks; 4291 int blkoff[NR_CURSEG_TYPE]; 4292 int curseg[NR_CURSEG_TYPE]; 4293 int cursec[NR_CURSEG_TYPE]; 4294 int curzone[NR_CURSEG_TYPE]; 4295 unsigned int dirty_seg[NR_CURSEG_TYPE]; 4296 unsigned int full_seg[NR_CURSEG_TYPE]; 4297 unsigned int valid_blks[NR_CURSEG_TYPE]; 4298 4299 unsigned int meta_count[META_MAX]; 4300 unsigned int segment_count[2]; 4301 unsigned int block_count[2]; 4302 unsigned int inplace_count; 4303 unsigned long long base_mem, cache_mem, page_mem; 4304 struct f2fs_dev_stats *dev_stats; 4305 }; 4306 4307 static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi) 4308 { 4309 return (struct f2fs_stat_info *)sbi->stat_info; 4310 } 4311 4312 #define stat_inc_cp_call_count(sbi, foreground) \ 4313 atomic_inc(&sbi->cp_call_count[(foreground)]) 4314 #define stat_inc_cp_count(sbi) (F2FS_STAT(sbi)->cp_count++) 4315 #define stat_io_skip_bggc_count(sbi) ((sbi)->io_skip_bggc++) 4316 #define stat_other_skip_bggc_count(sbi) ((sbi)->other_skip_bggc++) 4317 #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++) 4318 #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--) 4319 #define stat_inc_total_hit(sbi, type) (atomic64_inc(&(sbi)->total_hit_ext[type])) 4320 #define stat_inc_rbtree_node_hit(sbi, type) (atomic64_inc(&(sbi)->read_hit_rbtree[type])) 4321 #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest)) 4322 #define stat_inc_cached_node_hit(sbi, type) (atomic64_inc(&(sbi)->read_hit_cached[type])) 4323 #define stat_inc_inline_xattr(inode) \ 4324 do { \ 4325 if (f2fs_has_inline_xattr(inode)) \ 4326 (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \ 4327 } while (0) 4328 #define stat_dec_inline_xattr(inode) \ 4329 do { \ 4330 if (f2fs_has_inline_xattr(inode)) \ 4331 (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \ 4332 } while (0) 4333 #define stat_inc_inline_inode(inode) \ 4334 do { \ 4335 if (f2fs_has_inline_data(inode)) \ 4336 (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \ 4337 } while (0) 4338 #define stat_dec_inline_inode(inode) \ 4339 do { \ 4340 if (f2fs_has_inline_data(inode)) \ 4341 (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \ 4342 } while (0) 4343 #define stat_inc_inline_dir(inode) \ 4344 do { \ 4345 if (f2fs_has_inline_dentry(inode)) \ 4346 (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \ 4347 } while (0) 4348 #define stat_dec_inline_dir(inode) \ 4349 do { \ 4350 if (f2fs_has_inline_dentry(inode)) \ 4351 (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \ 4352 } while (0) 4353 #define stat_inc_compr_inode(inode) \ 4354 do { \ 4355 if (f2fs_compressed_file(inode)) \ 4356 (atomic_inc(&F2FS_I_SB(inode)->compr_inode)); \ 4357 } while (0) 4358 #define stat_dec_compr_inode(inode) \ 4359 do { \ 4360 if (f2fs_compressed_file(inode)) \ 4361 (atomic_dec(&F2FS_I_SB(inode)->compr_inode)); \ 4362 } while (0) 4363 #define stat_add_compr_blocks(inode, blocks) \ 4364 (atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks)) 4365 #define stat_sub_compr_blocks(inode, blocks) \ 4366 (atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks)) 4367 #define stat_inc_swapfile_inode(inode) \ 4368 (atomic_inc(&F2FS_I_SB(inode)->swapfile_inode)) 4369 #define stat_dec_swapfile_inode(inode) \ 4370 (atomic_dec(&F2FS_I_SB(inode)->swapfile_inode)) 4371 #define stat_inc_atomic_inode(inode) \ 4372 (atomic_inc(&F2FS_I_SB(inode)->atomic_files)) 4373 #define stat_dec_atomic_inode(inode) \ 4374 (atomic_dec(&F2FS_I_SB(inode)->atomic_files)) 4375 #define stat_inc_meta_count(sbi, blkaddr) \ 4376 do { \ 4377 if (blkaddr < SIT_I(sbi)->sit_base_addr) \ 4378 atomic_inc(&(sbi)->meta_count[META_CP]); \ 4379 else if (blkaddr < NM_I(sbi)->nat_blkaddr) \ 4380 atomic_inc(&(sbi)->meta_count[META_SIT]); \ 4381 else if (blkaddr < SM_I(sbi)->ssa_blkaddr) \ 4382 atomic_inc(&(sbi)->meta_count[META_NAT]); \ 4383 else if (blkaddr < SM_I(sbi)->main_blkaddr) \ 4384 atomic_inc(&(sbi)->meta_count[META_SSA]); \ 4385 } while (0) 4386 #define stat_inc_seg_type(sbi, curseg) \ 4387 ((sbi)->segment_count[(curseg)->alloc_type]++) 4388 #define stat_inc_block_count(sbi, curseg) \ 4389 ((sbi)->block_count[(curseg)->alloc_type]++) 4390 #define stat_inc_inplace_blocks(sbi) \ 4391 (atomic_inc(&(sbi)->inplace_count)) 4392 #define stat_update_max_atomic_write(inode) \ 4393 do { \ 4394 int cur = atomic_read(&F2FS_I_SB(inode)->atomic_files); \ 4395 int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \ 4396 if (cur > max) \ 4397 atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \ 4398 } while (0) 4399 #define stat_inc_gc_call_count(sbi, foreground) \ 4400 (F2FS_STAT(sbi)->gc_call_count[(foreground)]++) 4401 #define stat_inc_gc_sec_count(sbi, type, gc_type) \ 4402 (F2FS_STAT(sbi)->gc_secs[(type)][(gc_type)]++) 4403 #define stat_inc_gc_seg_count(sbi, type, gc_type) \ 4404 (F2FS_STAT(sbi)->gc_segs[(type)][(gc_type)]++) 4405 4406 #define stat_inc_tot_blk_count(si, blks) \ 4407 ((si)->tot_blks += (blks)) 4408 4409 #define stat_inc_data_blk_count(sbi, blks, gc_type) \ 4410 do { \ 4411 struct f2fs_stat_info *si = F2FS_STAT(sbi); \ 4412 stat_inc_tot_blk_count(si, blks); \ 4413 si->data_blks += (blks); \ 4414 si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \ 4415 } while (0) 4416 4417 #define stat_inc_node_blk_count(sbi, blks, gc_type) \ 4418 do { \ 4419 struct f2fs_stat_info *si = F2FS_STAT(sbi); \ 4420 stat_inc_tot_blk_count(si, blks); \ 4421 si->node_blks += (blks); \ 4422 si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \ 4423 } while (0) 4424 4425 int f2fs_build_stats(struct f2fs_sb_info *sbi); 4426 void f2fs_destroy_stats(struct f2fs_sb_info *sbi); 4427 void __init f2fs_create_root_stats(void); 4428 void f2fs_destroy_root_stats(void); 4429 void f2fs_update_sit_info(struct f2fs_sb_info *sbi); 4430 #else 4431 #define stat_inc_cp_call_count(sbi, foreground) do { } while (0) 4432 #define stat_inc_cp_count(sbi) do { } while (0) 4433 #define stat_io_skip_bggc_count(sbi) do { } while (0) 4434 #define stat_other_skip_bggc_count(sbi) do { } while (0) 4435 #define stat_inc_dirty_inode(sbi, type) do { } while (0) 4436 #define stat_dec_dirty_inode(sbi, type) do { } while (0) 4437 #define stat_inc_total_hit(sbi, type) do { } while (0) 4438 #define stat_inc_rbtree_node_hit(sbi, type) do { } while (0) 4439 #define stat_inc_largest_node_hit(sbi) do { } while (0) 4440 #define stat_inc_cached_node_hit(sbi, type) do { } while (0) 4441 #define stat_inc_inline_xattr(inode) do { } while (0) 4442 #define stat_dec_inline_xattr(inode) do { } while (0) 4443 #define stat_inc_inline_inode(inode) do { } while (0) 4444 #define stat_dec_inline_inode(inode) do { } while (0) 4445 #define stat_inc_inline_dir(inode) do { } while (0) 4446 #define stat_dec_inline_dir(inode) do { } while (0) 4447 #define stat_inc_compr_inode(inode) do { } while (0) 4448 #define stat_dec_compr_inode(inode) do { } while (0) 4449 #define stat_add_compr_blocks(inode, blocks) do { } while (0) 4450 #define stat_sub_compr_blocks(inode, blocks) do { } while (0) 4451 #define stat_inc_swapfile_inode(inode) do { } while (0) 4452 #define stat_dec_swapfile_inode(inode) do { } while (0) 4453 #define stat_inc_atomic_inode(inode) do { } while (0) 4454 #define stat_dec_atomic_inode(inode) do { } while (0) 4455 #define stat_update_max_atomic_write(inode) do { } while (0) 4456 #define stat_inc_meta_count(sbi, blkaddr) do { } while (0) 4457 #define stat_inc_seg_type(sbi, curseg) do { } while (0) 4458 #define stat_inc_block_count(sbi, curseg) do { } while (0) 4459 #define stat_inc_inplace_blocks(sbi) do { } while (0) 4460 #define stat_inc_gc_call_count(sbi, foreground) do { } while (0) 4461 #define stat_inc_gc_sec_count(sbi, type, gc_type) do { } while (0) 4462 #define stat_inc_gc_seg_count(sbi, type, gc_type) do { } while (0) 4463 #define stat_inc_tot_blk_count(si, blks) do { } while (0) 4464 #define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0) 4465 #define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0) 4466 4467 static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; } 4468 static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { } 4469 static inline void __init f2fs_create_root_stats(void) { } 4470 static inline void f2fs_destroy_root_stats(void) { } 4471 static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {} 4472 #endif 4473 4474 extern const struct file_operations f2fs_dir_operations; 4475 extern const struct file_operations f2fs_file_operations; 4476 extern const struct inode_operations f2fs_file_inode_operations; 4477 extern const struct address_space_operations f2fs_dblock_aops; 4478 extern const struct address_space_operations f2fs_node_aops; 4479 extern const struct address_space_operations f2fs_meta_aops; 4480 extern const struct inode_operations f2fs_dir_inode_operations; 4481 extern const struct inode_operations f2fs_symlink_inode_operations; 4482 extern const struct inode_operations f2fs_encrypted_symlink_inode_operations; 4483 extern const struct inode_operations f2fs_special_inode_operations; 4484 extern struct kmem_cache *f2fs_inode_entry_slab; 4485 4486 /* 4487 * inline.c 4488 */ 4489 bool f2fs_may_inline_data(struct inode *inode); 4490 bool f2fs_sanity_check_inline_data(struct inode *inode, struct folio *ifolio); 4491 bool f2fs_may_inline_dentry(struct inode *inode); 4492 void f2fs_do_read_inline_data(struct folio *folio, struct folio *ifolio); 4493 void f2fs_truncate_inline_inode(struct inode *inode, struct folio *ifolio, 4494 u64 from); 4495 int f2fs_read_inline_data(struct inode *inode, struct folio *folio); 4496 int f2fs_convert_inline_folio(struct dnode_of_data *dn, struct folio *folio); 4497 int f2fs_convert_inline_inode(struct inode *inode); 4498 int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry); 4499 int f2fs_write_inline_data(struct inode *inode, struct folio *folio); 4500 int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio); 4501 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir, 4502 const struct f2fs_filename *fname, struct folio **res_folio, 4503 bool use_hash); 4504 int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent, 4505 struct folio *ifolio); 4506 int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname, 4507 struct inode *inode, nid_t ino, umode_t mode); 4508 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, 4509 struct folio *folio, struct inode *dir, struct inode *inode); 4510 bool f2fs_empty_inline_dir(struct inode *dir); 4511 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx, 4512 struct fscrypt_str *fstr); 4513 int f2fs_inline_data_fiemap(struct inode *inode, 4514 struct fiemap_extent_info *fieinfo, 4515 __u64 start, __u64 len); 4516 4517 /* 4518 * shrinker.c 4519 */ 4520 unsigned long f2fs_shrink_count(struct shrinker *shrink, 4521 struct shrink_control *sc); 4522 unsigned long f2fs_shrink_scan(struct shrinker *shrink, 4523 struct shrink_control *sc); 4524 unsigned int f2fs_donate_files(void); 4525 void f2fs_reclaim_caches(unsigned int reclaim_caches_kb); 4526 void f2fs_join_shrinker(struct f2fs_sb_info *sbi); 4527 void f2fs_leave_shrinker(struct f2fs_sb_info *sbi); 4528 4529 /* 4530 * extent_cache.c 4531 */ 4532 bool sanity_check_extent_cache(struct inode *inode, struct folio *ifolio); 4533 void f2fs_init_extent_tree(struct inode *inode); 4534 void f2fs_drop_extent_tree(struct inode *inode); 4535 void f2fs_destroy_extent_node(struct inode *inode); 4536 void f2fs_destroy_extent_tree(struct inode *inode); 4537 void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi); 4538 int __init f2fs_create_extent_cache(void); 4539 void f2fs_destroy_extent_cache(void); 4540 4541 /* read extent cache ops */ 4542 void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio); 4543 bool f2fs_lookup_read_extent_cache(struct inode *inode, pgoff_t pgofs, 4544 struct extent_info *ei); 4545 bool f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index, 4546 block_t *blkaddr); 4547 void f2fs_update_read_extent_cache(struct dnode_of_data *dn); 4548 void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn, 4549 pgoff_t fofs, block_t blkaddr, unsigned int len); 4550 unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi, 4551 int nr_shrink); 4552 4553 /* block age extent cache ops */ 4554 void f2fs_init_age_extent_tree(struct inode *inode); 4555 bool f2fs_lookup_age_extent_cache(struct inode *inode, pgoff_t pgofs, 4556 struct extent_info *ei); 4557 void f2fs_update_age_extent_cache(struct dnode_of_data *dn); 4558 void f2fs_update_age_extent_cache_range(struct dnode_of_data *dn, 4559 pgoff_t fofs, unsigned int len); 4560 unsigned int f2fs_shrink_age_extent_tree(struct f2fs_sb_info *sbi, 4561 int nr_shrink); 4562 4563 /* 4564 * sysfs.c 4565 */ 4566 #define MIN_RA_MUL 2 4567 #define MAX_RA_MUL 256 4568 4569 int __init f2fs_init_sysfs(void); 4570 void f2fs_exit_sysfs(void); 4571 int f2fs_register_sysfs(struct f2fs_sb_info *sbi); 4572 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi); 4573 4574 /* verity.c */ 4575 extern const struct fsverity_operations f2fs_verityops; 4576 4577 /* 4578 * crypto support 4579 */ 4580 static inline bool f2fs_encrypted_file(struct inode *inode) 4581 { 4582 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode); 4583 } 4584 4585 static inline void f2fs_set_encrypted_inode(struct inode *inode) 4586 { 4587 #ifdef CONFIG_FS_ENCRYPTION 4588 file_set_encrypt(inode); 4589 f2fs_set_inode_flags(inode); 4590 #endif 4591 } 4592 4593 /* 4594 * Returns true if the reads of the inode's data need to undergo some 4595 * postprocessing step, like decryption or authenticity verification. 4596 */ 4597 static inline bool f2fs_post_read_required(struct inode *inode) 4598 { 4599 return f2fs_encrypted_file(inode) || fsverity_active(inode) || 4600 f2fs_compressed_file(inode); 4601 } 4602 4603 static inline bool f2fs_used_in_atomic_write(struct inode *inode) 4604 { 4605 return f2fs_is_atomic_file(inode) || f2fs_is_cow_file(inode); 4606 } 4607 4608 static inline bool f2fs_meta_inode_gc_required(struct inode *inode) 4609 { 4610 return f2fs_post_read_required(inode) || f2fs_used_in_atomic_write(inode); 4611 } 4612 4613 /* 4614 * compress.c 4615 */ 4616 #ifdef CONFIG_F2FS_FS_COMPRESSION 4617 enum cluster_check_type { 4618 CLUSTER_IS_COMPR, /* check only if compressed cluster */ 4619 CLUSTER_COMPR_BLKS, /* return # of compressed blocks in a cluster */ 4620 CLUSTER_RAW_BLKS /* return # of raw blocks in a cluster */ 4621 }; 4622 bool f2fs_is_compressed_page(struct folio *folio); 4623 struct folio *f2fs_compress_control_folio(struct folio *folio); 4624 int f2fs_prepare_compress_overwrite(struct inode *inode, 4625 struct page **pagep, pgoff_t index, void **fsdata); 4626 bool f2fs_compress_write_end(struct inode *inode, void *fsdata, 4627 pgoff_t index, unsigned copied); 4628 int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock); 4629 void f2fs_compress_write_end_io(struct bio *bio, struct folio *folio); 4630 bool f2fs_is_compress_backend_ready(struct inode *inode); 4631 bool f2fs_is_compress_level_valid(int alg, int lvl); 4632 int __init f2fs_init_compress_mempool(void); 4633 void f2fs_destroy_compress_mempool(void); 4634 void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task); 4635 void f2fs_end_read_compressed_page(struct folio *folio, bool failed, 4636 block_t blkaddr, bool in_task); 4637 bool f2fs_cluster_is_empty(struct compress_ctx *cc); 4638 bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index); 4639 bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages, 4640 int index, int nr_pages, bool uptodate); 4641 bool f2fs_sanity_check_cluster(struct dnode_of_data *dn); 4642 void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct folio *folio); 4643 int f2fs_write_multi_pages(struct compress_ctx *cc, 4644 int *submitted, 4645 struct writeback_control *wbc, 4646 enum iostat_type io_type); 4647 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index); 4648 bool f2fs_is_sparse_cluster(struct inode *inode, pgoff_t index); 4649 void f2fs_update_read_extent_tree_range_compressed(struct inode *inode, 4650 pgoff_t fofs, block_t blkaddr, 4651 unsigned int llen, unsigned int c_len); 4652 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret, 4653 unsigned nr_pages, sector_t *last_block_in_bio, 4654 struct readahead_control *rac, bool for_write); 4655 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc); 4656 void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed, 4657 bool in_task); 4658 void f2fs_put_folio_dic(struct folio *folio, bool in_task); 4659 unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn, 4660 unsigned int ofs_in_node); 4661 int f2fs_init_compress_ctx(struct compress_ctx *cc); 4662 void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse); 4663 void f2fs_init_compress_info(struct f2fs_sb_info *sbi); 4664 int f2fs_init_compress_inode(struct f2fs_sb_info *sbi); 4665 void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi); 4666 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi); 4667 void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi); 4668 int __init f2fs_init_compress_cache(void); 4669 void f2fs_destroy_compress_cache(void); 4670 struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi); 4671 void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi, 4672 block_t blkaddr, unsigned int len); 4673 bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio, 4674 block_t blkaddr); 4675 void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino); 4676 #define inc_compr_inode_stat(inode) \ 4677 do { \ 4678 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); \ 4679 sbi->compr_new_inode++; \ 4680 } while (0) 4681 #define add_compr_block_stat(inode, blocks) \ 4682 do { \ 4683 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); \ 4684 int diff = F2FS_I(inode)->i_cluster_size - blocks; \ 4685 sbi->compr_written_block += blocks; \ 4686 sbi->compr_saved_block += diff; \ 4687 } while (0) 4688 #else 4689 static inline bool f2fs_is_compressed_page(struct folio *folio) { return false; } 4690 static inline bool f2fs_is_compress_backend_ready(struct inode *inode) 4691 { 4692 if (!f2fs_compressed_file(inode)) 4693 return true; 4694 /* not support compression */ 4695 return false; 4696 } 4697 static inline bool f2fs_is_compress_level_valid(int alg, int lvl) { return false; } 4698 static inline struct folio *f2fs_compress_control_folio(struct folio *folio) 4699 { 4700 WARN_ON_ONCE(1); 4701 return ERR_PTR(-EINVAL); 4702 } 4703 static inline int __init f2fs_init_compress_mempool(void) { return 0; } 4704 static inline void f2fs_destroy_compress_mempool(void) { } 4705 static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic, 4706 bool in_task) { } 4707 static inline void f2fs_end_read_compressed_page(struct folio *folio, 4708 bool failed, block_t blkaddr, bool in_task) 4709 { 4710 WARN_ON_ONCE(1); 4711 } 4712 static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task) 4713 { 4714 WARN_ON_ONCE(1); 4715 } 4716 static inline unsigned int f2fs_cluster_blocks_are_contiguous( 4717 struct dnode_of_data *dn, unsigned int ofs_in_node) { return 0; } 4718 static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; } 4719 static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; } 4720 static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { } 4721 static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; } 4722 static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { } 4723 static inline int __init f2fs_init_compress_cache(void) { return 0; } 4724 static inline void f2fs_destroy_compress_cache(void) { } 4725 static inline void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi, 4726 block_t blkaddr, unsigned int len) { } 4727 static inline bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, 4728 struct folio *folio, block_t blkaddr) { return false; } 4729 static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, 4730 nid_t ino) { } 4731 #define inc_compr_inode_stat(inode) do { } while (0) 4732 static inline int f2fs_is_compressed_cluster( 4733 struct inode *inode, 4734 pgoff_t index) { return 0; } 4735 static inline bool f2fs_is_sparse_cluster( 4736 struct inode *inode, 4737 pgoff_t index) { return true; } 4738 static inline void f2fs_update_read_extent_tree_range_compressed( 4739 struct inode *inode, 4740 pgoff_t fofs, block_t blkaddr, 4741 unsigned int llen, unsigned int c_len) { } 4742 #endif 4743 4744 static inline int set_compress_context(struct inode *inode) 4745 { 4746 #ifdef CONFIG_F2FS_FS_COMPRESSION 4747 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4748 struct f2fs_inode_info *fi = F2FS_I(inode); 4749 4750 fi->i_compress_algorithm = F2FS_OPTION(sbi).compress_algorithm; 4751 fi->i_log_cluster_size = F2FS_OPTION(sbi).compress_log_size; 4752 fi->i_compress_flag = F2FS_OPTION(sbi).compress_chksum ? 4753 BIT(COMPRESS_CHKSUM) : 0; 4754 fi->i_cluster_size = BIT(fi->i_log_cluster_size); 4755 if ((fi->i_compress_algorithm == COMPRESS_LZ4 || 4756 fi->i_compress_algorithm == COMPRESS_ZSTD) && 4757 F2FS_OPTION(sbi).compress_level) 4758 fi->i_compress_level = F2FS_OPTION(sbi).compress_level; 4759 fi->i_flags |= F2FS_COMPR_FL; 4760 set_inode_flag(inode, FI_COMPRESSED_FILE); 4761 stat_inc_compr_inode(inode); 4762 inc_compr_inode_stat(inode); 4763 f2fs_mark_inode_dirty_sync(inode, true); 4764 return 0; 4765 #else 4766 return -EOPNOTSUPP; 4767 #endif 4768 } 4769 4770 static inline bool f2fs_disable_compressed_file(struct inode *inode) 4771 { 4772 struct f2fs_inode_info *fi = F2FS_I(inode); 4773 4774 f2fs_down_write(&fi->i_sem); 4775 4776 if (!f2fs_compressed_file(inode)) { 4777 f2fs_up_write(&fi->i_sem); 4778 return true; 4779 } 4780 if (f2fs_is_mmap_file(inode) || atomic_read(&fi->writeback) || 4781 (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))) { 4782 f2fs_up_write(&fi->i_sem); 4783 return false; 4784 } 4785 4786 fi->i_flags &= ~F2FS_COMPR_FL; 4787 stat_dec_compr_inode(inode); 4788 clear_inode_flag(inode, FI_COMPRESSED_FILE); 4789 f2fs_mark_inode_dirty_sync(inode, true); 4790 4791 f2fs_up_write(&fi->i_sem); 4792 return true; 4793 } 4794 4795 #define F2FS_FEATURE_FUNCS(name, flagname) \ 4796 static inline bool f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \ 4797 { \ 4798 return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \ 4799 } 4800 4801 F2FS_FEATURE_FUNCS(encrypt, ENCRYPT); 4802 F2FS_FEATURE_FUNCS(blkzoned, BLKZONED); 4803 F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR); 4804 F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA); 4805 F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM); 4806 F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR); 4807 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO); 4808 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME); 4809 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND); 4810 F2FS_FEATURE_FUNCS(verity, VERITY); 4811 F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM); 4812 F2FS_FEATURE_FUNCS(casefold, CASEFOLD); 4813 F2FS_FEATURE_FUNCS(compression, COMPRESSION); 4814 F2FS_FEATURE_FUNCS(readonly, RO); 4815 F2FS_FEATURE_FUNCS(device_alias, DEVICE_ALIAS); 4816 F2FS_FEATURE_FUNCS(packed_ssa, PACKED_SSA); 4817 4818 #ifdef CONFIG_BLK_DEV_ZONED 4819 static inline bool f2fs_zone_is_seq(struct f2fs_sb_info *sbi, int devi, 4820 unsigned int zone) 4821 { 4822 return test_bit(zone, FDEV(devi).blkz_seq); 4823 } 4824 4825 static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi, 4826 block_t blkaddr) 4827 { 4828 return f2fs_zone_is_seq(sbi, devi, blkaddr / sbi->blocks_per_blkz); 4829 } 4830 #endif 4831 4832 static inline int f2fs_bdev_index(struct f2fs_sb_info *sbi, 4833 struct block_device *bdev) 4834 { 4835 int i; 4836 4837 if (!f2fs_is_multi_device(sbi)) 4838 return 0; 4839 4840 for (i = 0; i < sbi->s_ndevs; i++) 4841 if (FDEV(i).bdev == bdev) 4842 return i; 4843 4844 WARN_ON(1); 4845 return -1; 4846 } 4847 4848 static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi) 4849 { 4850 return f2fs_sb_has_blkzoned(sbi); 4851 } 4852 4853 static inline bool f2fs_bdev_support_discard(struct block_device *bdev) 4854 { 4855 return bdev_max_discard_sectors(bdev) || bdev_is_zoned(bdev); 4856 } 4857 4858 static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi) 4859 { 4860 int i; 4861 4862 if (!f2fs_is_multi_device(sbi)) 4863 return f2fs_bdev_support_discard(sbi->sb->s_bdev); 4864 4865 for (i = 0; i < sbi->s_ndevs; i++) 4866 if (f2fs_bdev_support_discard(FDEV(i).bdev)) 4867 return true; 4868 return false; 4869 } 4870 4871 static inline unsigned int f2fs_hw_discard_granularity(struct f2fs_sb_info *sbi) 4872 { 4873 int i = 1; 4874 unsigned int discard_granularity = bdev_discard_granularity(sbi->sb->s_bdev); 4875 4876 if (f2fs_is_multi_device(sbi)) 4877 for (; i < sbi->s_ndevs && !bdev_is_zoned(FDEV(i).bdev); i++) 4878 discard_granularity = max_t(unsigned int, discard_granularity, 4879 bdev_discard_granularity(FDEV(i).bdev)); 4880 return discard_granularity; 4881 } 4882 4883 static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi) 4884 { 4885 return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) || 4886 f2fs_hw_should_discard(sbi); 4887 } 4888 4889 static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi) 4890 { 4891 int i; 4892 4893 if (!f2fs_is_multi_device(sbi)) 4894 return bdev_read_only(sbi->sb->s_bdev); 4895 4896 for (i = 0; i < sbi->s_ndevs; i++) 4897 if (bdev_read_only(FDEV(i).bdev)) 4898 return true; 4899 return false; 4900 } 4901 4902 static inline bool f2fs_dev_is_readonly(struct f2fs_sb_info *sbi) 4903 { 4904 return f2fs_sb_has_readonly(sbi) || f2fs_hw_is_readonly(sbi); 4905 } 4906 4907 static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi) 4908 { 4909 return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS; 4910 } 4911 4912 static inline bool f2fs_is_sequential_zone_area(struct f2fs_sb_info *sbi, 4913 block_t blkaddr) 4914 { 4915 if (f2fs_sb_has_blkzoned(sbi)) { 4916 #ifdef CONFIG_BLK_DEV_ZONED 4917 int devi = f2fs_target_device_index(sbi, blkaddr); 4918 4919 if (!bdev_is_zoned(FDEV(devi).bdev)) 4920 return false; 4921 4922 if (f2fs_is_multi_device(sbi)) { 4923 if (blkaddr < FDEV(devi).start_blk || 4924 blkaddr > FDEV(devi).end_blk) { 4925 f2fs_err(sbi, "Invalid block %x", blkaddr); 4926 return false; 4927 } 4928 blkaddr -= FDEV(devi).start_blk; 4929 } 4930 4931 return f2fs_blkz_is_seq(sbi, devi, blkaddr); 4932 #else 4933 return false; 4934 #endif 4935 } 4936 return false; 4937 } 4938 4939 static inline bool f2fs_low_mem_mode(struct f2fs_sb_info *sbi) 4940 { 4941 return F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW; 4942 } 4943 4944 static inline bool f2fs_may_compress(struct inode *inode) 4945 { 4946 if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) || 4947 f2fs_is_atomic_file(inode) || f2fs_has_inline_data(inode) || 4948 f2fs_is_mmap_file(inode)) 4949 return false; 4950 return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode); 4951 } 4952 4953 static inline void f2fs_i_compr_blocks_update(struct inode *inode, 4954 u64 blocks, bool add) 4955 { 4956 struct f2fs_inode_info *fi = F2FS_I(inode); 4957 int diff = fi->i_cluster_size - blocks; 4958 4959 /* don't update i_compr_blocks if saved blocks were released */ 4960 if (!add && !atomic_read(&fi->i_compr_blocks)) 4961 return; 4962 4963 if (add) { 4964 atomic_add(diff, &fi->i_compr_blocks); 4965 stat_add_compr_blocks(inode, diff); 4966 } else { 4967 atomic_sub(diff, &fi->i_compr_blocks); 4968 stat_sub_compr_blocks(inode, diff); 4969 } 4970 f2fs_mark_inode_dirty_sync(inode, true); 4971 } 4972 4973 static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi, 4974 int flag) 4975 { 4976 if (!f2fs_is_multi_device(sbi)) 4977 return false; 4978 if (flag != F2FS_GET_BLOCK_DIO) 4979 return false; 4980 return sbi->aligned_blksize; 4981 } 4982 4983 #ifdef CONFIG_F2FS_FAULT_INJECTION 4984 extern int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate, 4985 unsigned long type, enum fault_option fo); 4986 extern void f2fs_simulate_lock_timeout(struct f2fs_sb_info *sbi); 4987 #else 4988 static inline int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, 4989 unsigned long rate, unsigned long type, 4990 enum fault_option fo) 4991 { 4992 return 0; 4993 } 4994 static inline void f2fs_simulate_lock_timeout(struct f2fs_sb_info *sbi) 4995 { 4996 return; 4997 } 4998 #endif 4999 5000 static inline bool is_journalled_quota(struct f2fs_sb_info *sbi) 5001 { 5002 #ifdef CONFIG_QUOTA 5003 if (f2fs_sb_has_quota_ino(sbi)) 5004 return true; 5005 if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] || 5006 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] || 5007 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) 5008 return true; 5009 #endif 5010 return false; 5011 } 5012 5013 static inline bool f2fs_quota_file(struct f2fs_sb_info *sbi, nid_t ino) 5014 { 5015 #ifdef CONFIG_QUOTA 5016 int i; 5017 5018 if (!f2fs_sb_has_quota_ino(sbi)) 5019 return false; 5020 5021 for (i = 0; i < MAXQUOTAS; i++) { 5022 if (f2fs_qf_ino(sbi->sb, i) == ino) 5023 return true; 5024 } 5025 #endif 5026 return false; 5027 } 5028 5029 static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi) 5030 { 5031 return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK; 5032 } 5033 5034 static inline void __f2fs_schedule_timeout(long timeout, bool io) 5035 { 5036 set_current_state(TASK_UNINTERRUPTIBLE); 5037 if (io) 5038 io_schedule_timeout(timeout); 5039 else 5040 schedule_timeout(timeout); 5041 } 5042 5043 #define f2fs_io_schedule_timeout(timeout) \ 5044 __f2fs_schedule_timeout(timeout, true) 5045 #define f2fs_schedule_timeout(timeout) \ 5046 __f2fs_schedule_timeout(timeout, false) 5047 5048 static inline void f2fs_schedule_timeout_killable(long timeout, bool io) 5049 { 5050 unsigned long last_time = jiffies + timeout; 5051 5052 while (jiffies < last_time) { 5053 if (fatal_signal_pending(current)) 5054 return; 5055 __f2fs_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT, io); 5056 } 5057 } 5058 5059 static inline void f2fs_handle_page_eio(struct f2fs_sb_info *sbi, 5060 struct folio *folio, enum page_type type) 5061 { 5062 pgoff_t ofs = folio->index; 5063 5064 if (unlikely(f2fs_cp_error(sbi))) 5065 return; 5066 5067 if (ofs == sbi->page_eio_ofs[type]) { 5068 if (sbi->page_eio_cnt[type]++ == MAX_RETRY_PAGE_EIO) 5069 set_ckpt_flags(sbi, CP_ERROR_FLAG); 5070 } else { 5071 sbi->page_eio_ofs[type] = ofs; 5072 sbi->page_eio_cnt[type] = 0; 5073 } 5074 } 5075 5076 static inline bool f2fs_is_readonly(struct f2fs_sb_info *sbi) 5077 { 5078 return f2fs_sb_has_readonly(sbi) || f2fs_readonly(sbi->sb); 5079 } 5080 5081 static inline void f2fs_truncate_meta_inode_pages(struct f2fs_sb_info *sbi, 5082 block_t blkaddr, unsigned int cnt) 5083 { 5084 bool need_submit = false; 5085 int i = 0; 5086 5087 do { 5088 struct folio *folio; 5089 5090 folio = filemap_get_folio(META_MAPPING(sbi), blkaddr + i); 5091 if (!IS_ERR(folio)) { 5092 if (folio_test_writeback(folio)) 5093 need_submit = true; 5094 f2fs_folio_put(folio, false); 5095 } 5096 } while (++i < cnt && !need_submit); 5097 5098 if (need_submit) 5099 f2fs_submit_merged_write_cond(sbi, sbi->meta_inode, 5100 NULL, 0, DATA); 5101 5102 truncate_inode_pages_range(META_MAPPING(sbi), 5103 F2FS_BLK_TO_BYTES((loff_t)blkaddr), 5104 F2FS_BLK_END_BYTES((loff_t)(blkaddr + cnt - 1))); 5105 } 5106 5107 static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi, 5108 block_t blkaddr, unsigned int len) 5109 { 5110 f2fs_truncate_meta_inode_pages(sbi, blkaddr, len); 5111 f2fs_invalidate_compress_pages_range(sbi, blkaddr, len); 5112 } 5113 5114 #endif /* _LINUX_F2FS_H */ 5115