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