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