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