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