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