Debian Codegraph @

fs/f2fs/f2fs.h

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