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