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