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