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