1 /*
2 * fs/f2fs/f2fs.h
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11 #ifndef _LINUX_F2FS_H
12 #define _LINUX_F2FS_H
13
14 #include <linux/types.h>
15 #include <linux/page-flags.h>
16 #include <linux/buffer_head.h>
17 #include <linux/slab.h>
18 #include <linux/crc32.h>
19 #include <linux/magic.h>
20 #include <linux/kobject.h>
21 #include <linux/sched.h>
22 #include <linux/cred.h>
23 #include <linux/vmalloc.h>
24 #include <linux/bio.h>
25 #include <linux/blkdev.h>
26 #include <linux/quotaops.h>
27 #include <crypto/hash.h>
28 #include <linux/writeback.h>
29
30 #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION)
31 #include <linux/fscrypt.h>
32
33 #ifdef CONFIG_F2FS_CHECK_FS
34 #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
35 #else
36 #define f2fs_bug_on(sbi, condition) \
37 do { \
38 if (unlikely(condition)) { \
39 WARN_ON(1); \
40 set_sbi_flag(sbi, SBI_NEED_FSCK); \
41 } \
42 } while (0)
43 #endif
44
45 #ifdef CONFIG_F2FS_FAULT_INJECTION
46 enum {
47 FAULT_KMALLOC,
48 FAULT_KVMALLOC,
49 FAULT_PAGE_ALLOC,
50 FAULT_PAGE_GET,
51 FAULT_ALLOC_BIO,
52 FAULT_ALLOC_NID,
53 FAULT_ORPHAN,
54 FAULT_BLOCK,
55 FAULT_DIR_DEPTH,
56 FAULT_EVICT_INODE,
57 FAULT_TRUNCATE,
58 FAULT_IO,
59 FAULT_CHECKPOINT,
60 FAULT_MAX,
61 };
62
63 struct f2fs_fault_info {
64 atomic_t inject_ops;
65 unsigned int inject_rate;
66 unsigned int inject_type;
67 };
68
69 extern char *fault_name[FAULT_MAX];
70 #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
71 #endif
72
73 /*
74 * For mount options
75 */
76 #define F2FS_MOUNT_BG_GC 0x00000001
77 #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
78 #define F2FS_MOUNT_DISCARD 0x00000004
79 #define F2FS_MOUNT_NOHEAP 0x00000008
80 #define F2FS_MOUNT_XATTR_USER 0x00000010
81 #define F2FS_MOUNT_POSIX_ACL 0x00000020
82 #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
83 #define F2FS_MOUNT_INLINE_XATTR 0x00000080
84 #define F2FS_MOUNT_INLINE_DATA 0x00000100
85 #define F2FS_MOUNT_INLINE_DENTRY 0x00000200
86 #define F2FS_MOUNT_FLUSH_MERGE 0x00000400
87 #define F2FS_MOUNT_NOBARRIER 0x00000800
88 #define F2FS_MOUNT_FASTBOOT 0x00001000
89 #define F2FS_MOUNT_EXTENT_CACHE 0x00002000
90 #define F2FS_MOUNT_FORCE_FG_GC 0x00004000
91 #define F2FS_MOUNT_DATA_FLUSH 0x00008000
92 #define F2FS_MOUNT_FAULT_INJECTION 0x00010000
93 #define F2FS_MOUNT_ADAPTIVE 0x00020000
94 #define F2FS_MOUNT_LFS 0x00040000
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
102 #define F2FS_OPTION(sbi) ((sbi)->mount_opt)
103 #define clear_opt(sbi, option) (F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
104 #define set_opt(sbi, option) (F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
105 #define test_opt(sbi, option) (F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
106
107 #define ver_after(a, b) (typecheck(unsigned long long, a) && \
108 typecheck(unsigned long long, b) && \
109 ((long long)((a) - (b)) > 0))
110
111 typedef u32 block_t; /*
112 * should not change u32, since it is the on-disk block
113 * address format, __le32.
114 */
115 typedef u32 nid_t;
116
117 struct f2fs_mount_info {
118 unsigned int opt;
119 int write_io_size_bits; /* Write IO size bits */
120 block_t root_reserved_blocks; /* root reserved blocks */
121 kuid_t s_resuid; /* reserved blocks for uid */
122 kgid_t s_resgid; /* reserved blocks for gid */
123 int active_logs; /* # of active logs */
124 int inline_xattr_size; /* inline xattr size */
125 #ifdef CONFIG_F2FS_FAULT_INJECTION
126 struct f2fs_fault_info fault_info; /* For fault injection */
127 #endif
128 #ifdef CONFIG_QUOTA
129 /* Names of quota files with journalled quota */
130 char *s_qf_names[MAXQUOTAS];
131 int s_jquota_fmt; /* Format of quota to use */
132 #endif
133 /* For which write hints are passed down to block layer */
134 int whint_mode;
135 int alloc_mode; /* segment allocation policy */
136 int fsync_mode; /* fsync policy */
137 bool test_dummy_encryption; /* test dummy encryption */
138 };
139
140 #define F2FS_FEATURE_ENCRYPT 0x0001
141 #define F2FS_FEATURE_BLKZONED 0x0002
142 #define F2FS_FEATURE_ATOMIC_WRITE 0x0004
143 #define F2FS_FEATURE_EXTRA_ATTR 0x0008
144 #define F2FS_FEATURE_PRJQUOTA 0x0010
145 #define F2FS_FEATURE_INODE_CHKSUM 0x0020
146 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x0040
147 #define F2FS_FEATURE_QUOTA_INO 0x0080
148 #define F2FS_FEATURE_INODE_CRTIME 0x0100
149 #define F2FS_FEATURE_LOST_FOUND 0x0200
150 #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
151
152 #define F2FS_HAS_FEATURE(sb, mask) \
153 ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
154 #define F2FS_SET_FEATURE(sb, mask) \
155 (F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
156 #define F2FS_CLEAR_FEATURE(sb, mask) \
157 (F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
158
159 /* bio stuffs */
160 #define REQ_OP_READ READ
161 #define REQ_OP_WRITE WRITE
162 #define bio_op(bio) ((bio)->bi_rw & 1)
163
bio_set_op_attrs(struct bio * bio,unsigned op,unsigned op_flags)164 static inline void bio_set_op_attrs(struct bio *bio, unsigned op,
165 unsigned op_flags)
166 {
167 bio->bi_rw = op | op_flags;
168 }
169
wbc_to_write_flags(struct writeback_control * wbc)170 static inline int wbc_to_write_flags(struct writeback_control *wbc)
171 {
172 if (wbc->sync_mode == WB_SYNC_ALL)
173 return REQ_SYNC | REQ_NOIDLE;
174 return 0;
175 }
176
177 /**
178 * wq_has_sleeper - check if there are any waiting processes
179 * @wq: wait queue head
180 *
181 * Returns true if wq has waiting processes
182 *
183 * Please refer to the comment for waitqueue_active.
184 */
wq_has_sleeper(wait_queue_head_t * wq)185 static inline bool wq_has_sleeper(wait_queue_head_t *wq)
186 {
187 /*
188 * We need to be sure we are in sync with the
189 * add_wait_queue modifications to the wait queue.
190 *
191 * This memory barrier should be paired with one on the
192 * waiting side.
193 */
194 smp_mb();
195 return waitqueue_active(wq);
196 }
197
198 /**
199 * current_time - Return FS time
200 * @inode: inode.
201 *
202 * Return the current time truncated to the time granularity supported by
203 * the fs.
204 *
205 * Note that inode and inode->sb cannot be NULL.
206 * Otherwise, the function warns and returns time without truncation.
207 */
current_time(struct inode * inode)208 static inline struct timespec current_time(struct inode *inode)
209 {
210 struct timespec now = current_kernel_time();
211
212 if (unlikely(!inode->i_sb)) {
213 WARN(1, "current_time() called with uninitialized super_block in the inode");
214 return now;
215 }
216
217 return timespec_trunc(now, inode->i_sb->s_time_gran);
218 }
219
220 /*
221 * Default values for user and/or group using reserved blocks
222 */
223 #define F2FS_DEF_RESUID 0
224 #define F2FS_DEF_RESGID 0
225
226 /*
227 * For checkpoint manager
228 */
229 enum {
230 NAT_BITMAP,
231 SIT_BITMAP
232 };
233
234 #define CP_UMOUNT 0x00000001
235 #define CP_FASTBOOT 0x00000002
236 #define CP_SYNC 0x00000004
237 #define CP_RECOVERY 0x00000008
238 #define CP_DISCARD 0x00000010
239 #define CP_TRIMMED 0x00000020
240
241 #define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
242 #define DEF_MAX_DISCARD_REQUEST 8 /* issue 8 discards per round */
243 #define DEF_MAX_DISCARD_LEN 512 /* Max. 2MB per discard */
244 #define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */
245 #define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */
246 #define DEF_DISCARD_URGENT_UTIL 80 /* do more discard over 80% */
247 #define DEF_CP_INTERVAL 60 /* 60 secs */
248 #define DEF_IDLE_INTERVAL 5 /* 5 secs */
249
250 struct cp_control {
251 int reason;
252 __u64 trim_start;
253 __u64 trim_end;
254 __u64 trim_minlen;
255 };
256
257 /*
258 * For CP/NAT/SIT/SSA readahead
259 */
260 enum {
261 META_CP,
262 META_NAT,
263 META_SIT,
264 META_SSA,
265 META_POR,
266 };
267
268 /* for the list of ino */
269 enum {
270 ORPHAN_INO, /* for orphan ino list */
271 APPEND_INO, /* for append ino list */
272 UPDATE_INO, /* for update ino list */
273 TRANS_DIR_INO, /* for trasactions dir ino list */
274 FLUSH_INO, /* for multiple device flushing */
275 MAX_INO_ENTRY, /* max. list */
276 };
277
278 struct ino_entry {
279 struct list_head list; /* list head */
280 nid_t ino; /* inode number */
281 unsigned int dirty_device; /* dirty device bitmap */
282 };
283
284 /* for the list of inodes to be GCed */
285 struct inode_entry {
286 struct list_head list; /* list head */
287 struct inode *inode; /* vfs inode pointer */
288 };
289
290 /* for the bitmap indicate blocks to be discarded */
291 struct discard_entry {
292 struct list_head list; /* list head */
293 block_t start_blkaddr; /* start blockaddr of current segment */
294 unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
295 };
296
297 /* default discard granularity of inner discard thread, unit: block count */
298 #define DEFAULT_DISCARD_GRANULARITY 16
299
300 /* max discard pend list number */
301 #define MAX_PLIST_NUM 512
302 #define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
303 (MAX_PLIST_NUM - 1) : (blk_num - 1))
304
305 enum {
306 D_PREP,
307 D_SUBMIT,
308 D_DONE,
309 };
310
311 struct discard_info {
312 block_t lstart; /* logical start address */
313 block_t len; /* length */
314 block_t start; /* actual start address in dev */
315 };
316
317 struct discard_cmd {
318 struct rb_node rb_node; /* rb node located in rb-tree */
319 union {
320 struct {
321 block_t lstart; /* logical start address */
322 block_t len; /* length */
323 block_t start; /* actual start address in dev */
324 };
325 struct discard_info di; /* discard info */
326
327 };
328 struct list_head list; /* command list */
329 struct completion wait; /* compleation */
330 struct block_device *bdev; /* bdev */
331 unsigned short ref; /* reference count */
332 unsigned char state; /* state */
333 int error; /* bio error */
334 };
335
336 enum {
337 DPOLICY_BG,
338 DPOLICY_FORCE,
339 DPOLICY_FSTRIM,
340 DPOLICY_UMOUNT,
341 MAX_DPOLICY,
342 };
343
344 struct discard_policy {
345 int type; /* type of discard */
346 unsigned int min_interval; /* used for candidates exist */
347 unsigned int max_interval; /* used for candidates not exist */
348 unsigned int max_requests; /* # of discards issued per round */
349 unsigned int io_aware_gran; /* minimum granularity discard not be aware of I/O */
350 bool io_aware; /* issue discard in idle time */
351 bool sync; /* submit discard with REQ_SYNC flag */
352 unsigned int granularity; /* discard granularity */
353 };
354
355 struct discard_cmd_control {
356 struct task_struct *f2fs_issue_discard; /* discard thread */
357 struct list_head entry_list; /* 4KB discard entry list */
358 struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
359 struct list_head wait_list; /* store on-flushing entries */
360 struct list_head fstrim_list; /* in-flight discard from fstrim */
361 wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
362 unsigned int discard_wake; /* to wake up discard thread */
363 struct mutex cmd_lock;
364 unsigned int nr_discards; /* # of discards in the list */
365 unsigned int max_discards; /* max. discards to be issued */
366 unsigned int discard_granularity; /* discard granularity */
367 unsigned int undiscard_blks; /* # of undiscard blocks */
368 atomic_t issued_discard; /* # of issued discard */
369 atomic_t issing_discard; /* # of issing discard */
370 atomic_t discard_cmd_cnt; /* # of cached cmd count */
371 struct rb_root root; /* root of discard rb-tree */
372 };
373
374 /* for the list of fsync inodes, used only during recovery */
375 struct fsync_inode_entry {
376 struct list_head list; /* list head */
377 struct inode *inode; /* vfs inode pointer */
378 block_t blkaddr; /* block address locating the last fsync */
379 block_t last_dentry; /* block address locating the last dentry */
380 };
381
382 #define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats))
383 #define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits))
384
385 #define nat_in_journal(jnl, i) ((jnl)->nat_j.entries[i].ne)
386 #define nid_in_journal(jnl, i) ((jnl)->nat_j.entries[i].nid)
387 #define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se)
388 #define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno)
389
390 #define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
391 #define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
392
update_nats_in_cursum(struct f2fs_journal * journal,int i)393 static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
394 {
395 int before = nats_in_cursum(journal);
396
397 journal->n_nats = cpu_to_le16(before + i);
398 return before;
399 }
400
update_sits_in_cursum(struct f2fs_journal * journal,int i)401 static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
402 {
403 int before = sits_in_cursum(journal);
404
405 journal->n_sits = cpu_to_le16(before + i);
406 return before;
407 }
408
__has_cursum_space(struct f2fs_journal * journal,int size,int type)409 static inline bool __has_cursum_space(struct f2fs_journal *journal,
410 int size, int type)
411 {
412 if (type == NAT_JOURNAL)
413 return size <= MAX_NAT_JENTRIES(journal);
414 return size <= MAX_SIT_JENTRIES(journal);
415 }
416
417 /*
418 * ioctl commands
419 */
420 #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
421 #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
422 #define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
423
424 #define F2FS_IOCTL_MAGIC 0xf5
425 #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
426 #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
427 #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
428 #define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
429 #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
430 #define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
431 #define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
432 #define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
433 struct f2fs_defragment)
434 #define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
435 struct f2fs_move_range)
436 #define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
437 struct f2fs_flush_device)
438 #define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
439 struct f2fs_gc_range)
440 #define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
441 #define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
442 #define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
443 #define F2FS_IOC_PRECACHE_EXTENTS _IO(F2FS_IOCTL_MAGIC, 15)
444
445 #define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
446 #define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
447 #define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
448
449 /*
450 * should be same as XFS_IOC_GOINGDOWN.
451 * Flags for going down operation used by FS_IOC_GOINGDOWN
452 */
453 #define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
454 #define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
455 #define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
456 #define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
457 #define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
458
459 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
460 /*
461 * ioctl commands in 32 bit emulation
462 */
463 #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
464 #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
465 #define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
466 #endif
467
468 struct f2fs_gc_range {
469 u32 sync;
470 u64 start;
471 u64 len;
472 };
473
474 struct f2fs_defragment {
475 u64 start;
476 u64 len;
477 };
478
479 struct f2fs_move_range {
480 u32 dst_fd; /* destination fd */
481 u64 pos_in; /* start position in src_fd */
482 u64 pos_out; /* start position in dst_fd */
483 u64 len; /* size to move */
484 };
485
486 struct f2fs_flush_device {
487 u32 dev_num; /* device number to flush */
488 u32 segments; /* # of segments to flush */
489 };
490
491 /* for inline stuff */
492 #define DEF_INLINE_RESERVED_SIZE 1
493 #define DEF_MIN_INLINE_SIZE 1
494 static inline int get_extra_isize(struct inode *inode);
495 static inline int get_inline_xattr_addrs(struct inode *inode);
496 #define MAX_INLINE_DATA(inode) (sizeof(__le32) * \
497 (CUR_ADDRS_PER_INODE(inode) - \
498 get_inline_xattr_addrs(inode) - \
499 DEF_INLINE_RESERVED_SIZE))
500
501 /* for inline dir */
502 #define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
503 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
504 BITS_PER_BYTE + 1))
505 #define INLINE_DENTRY_BITMAP_SIZE(inode) ((NR_INLINE_DENTRY(inode) + \
506 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
507 #define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \
508 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
509 NR_INLINE_DENTRY(inode) + \
510 INLINE_DENTRY_BITMAP_SIZE(inode)))
511
512 /*
513 * For INODE and NODE manager
514 */
515 /* for directory operations */
516 struct f2fs_dentry_ptr {
517 struct inode *inode;
518 void *bitmap;
519 struct f2fs_dir_entry *dentry;
520 __u8 (*filename)[F2FS_SLOT_LEN];
521 int max;
522 int nr_bitmap;
523 };
524
make_dentry_ptr_block(struct inode * inode,struct f2fs_dentry_ptr * d,struct f2fs_dentry_block * t)525 static inline void make_dentry_ptr_block(struct inode *inode,
526 struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
527 {
528 d->inode = inode;
529 d->max = NR_DENTRY_IN_BLOCK;
530 d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
531 d->bitmap = t->dentry_bitmap;
532 d->dentry = t->dentry;
533 d->filename = t->filename;
534 }
535
make_dentry_ptr_inline(struct inode * inode,struct f2fs_dentry_ptr * d,void * t)536 static inline void make_dentry_ptr_inline(struct inode *inode,
537 struct f2fs_dentry_ptr *d, void *t)
538 {
539 int entry_cnt = NR_INLINE_DENTRY(inode);
540 int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
541 int reserved_size = INLINE_RESERVED_SIZE(inode);
542
543 d->inode = inode;
544 d->max = entry_cnt;
545 d->nr_bitmap = bitmap_size;
546 d->bitmap = t;
547 d->dentry = t + bitmap_size + reserved_size;
548 d->filename = t + bitmap_size + reserved_size +
549 SIZE_OF_DIR_ENTRY * entry_cnt;
550 }
551
552 /*
553 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
554 * as its node offset to distinguish from index node blocks.
555 * But some bits are used to mark the node block.
556 */
557 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
558 >> OFFSET_BIT_SHIFT)
559 enum {
560 ALLOC_NODE, /* allocate a new node page if needed */
561 LOOKUP_NODE, /* look up a node without readahead */
562 LOOKUP_NODE_RA, /*
563 * look up a node with readahead called
564 * by get_data_block.
565 */
566 };
567
568 #define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
569
570 #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
571
572 /* vector size for gang look-up from extent cache that consists of radix tree */
573 #define EXT_TREE_VEC_SIZE 64
574
575 /* for in-memory extent cache entry */
576 #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
577
578 /* number of extent info in extent cache we try to shrink */
579 #define EXTENT_CACHE_SHRINK_NUMBER 128
580
581 struct rb_entry {
582 struct rb_node rb_node; /* rb node located in rb-tree */
583 unsigned int ofs; /* start offset of the entry */
584 unsigned int len; /* length of the entry */
585 };
586
587 struct extent_info {
588 unsigned int fofs; /* start offset in a file */
589 unsigned int len; /* length of the extent */
590 u32 blk; /* start block address of the extent */
591 };
592
593 struct extent_node {
594 struct rb_node rb_node;
595 union {
596 struct {
597 unsigned int fofs;
598 unsigned int len;
599 u32 blk;
600 };
601 struct extent_info ei; /* extent info */
602
603 };
604 struct list_head list; /* node in global extent list of sbi */
605 struct extent_tree *et; /* extent tree pointer */
606 };
607
608 struct extent_tree {
609 nid_t ino; /* inode number */
610 struct rb_root root; /* root of extent info rb-tree */
611 struct extent_node *cached_en; /* recently accessed extent node */
612 struct extent_info largest; /* largested extent info */
613 struct list_head list; /* to be used by sbi->zombie_list */
614 rwlock_t lock; /* protect extent info rb-tree */
615 atomic_t node_cnt; /* # of extent node in rb-tree*/
616 };
617
618 /*
619 * This structure is taken from ext4_map_blocks.
620 *
621 * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
622 */
623 #define F2FS_MAP_NEW (1 << BH_New)
624 #define F2FS_MAP_MAPPED (1 << BH_Mapped)
625 #define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
626 #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
627 F2FS_MAP_UNWRITTEN)
628
629 struct f2fs_map_blocks {
630 block_t m_pblk;
631 block_t m_lblk;
632 unsigned int m_len;
633 unsigned int m_flags;
634 pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
635 pgoff_t *m_next_extent; /* point to next possible extent */
636 int m_seg_type;
637 };
638
639 /* for flag in get_data_block */
640 enum {
641 F2FS_GET_BLOCK_DEFAULT,
642 F2FS_GET_BLOCK_FIEMAP,
643 F2FS_GET_BLOCK_BMAP,
644 F2FS_GET_BLOCK_PRE_DIO,
645 F2FS_GET_BLOCK_PRE_AIO,
646 F2FS_GET_BLOCK_PRECACHE,
647 };
648
649 /*
650 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
651 */
652 #define FADVISE_COLD_BIT 0x01
653 #define FADVISE_LOST_PINO_BIT 0x02
654 #define FADVISE_ENCRYPT_BIT 0x04
655 #define FADVISE_ENC_NAME_BIT 0x08
656 #define FADVISE_KEEP_SIZE_BIT 0x10
657 #define FADVISE_HOT_BIT 0x20
658 #define FADVISE_VERITY_BIT 0x40 /* reserved */
659
660 #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
661 #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
662 #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
663 #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
664 #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
665 #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
666 #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
667 #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
668 #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
669 #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
670 #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
671 #define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
672 #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
673 #define file_is_hot(inode) is_file(inode, FADVISE_HOT_BIT)
674 #define file_set_hot(inode) set_file(inode, FADVISE_HOT_BIT)
675 #define file_clear_hot(inode) clear_file(inode, FADVISE_HOT_BIT)
676
677 #define DEF_DIR_LEVEL 0
678
679 struct f2fs_inode_info {
680 struct inode vfs_inode; /* serve a vfs inode */
681 unsigned long i_flags; /* keep an inode flags for ioctl */
682 unsigned char i_advise; /* use to give file attribute hints */
683 unsigned char i_dir_level; /* use for dentry level for large dir */
684 union {
685 unsigned int i_current_depth; /* only for directory depth */
686 unsigned short i_gc_failures; /* only for regular file */
687 };
688 unsigned int i_pino; /* parent inode number */
689 umode_t i_acl_mode; /* keep file acl mode temporarily */
690
691 /* Use below internally in f2fs*/
692 unsigned long flags; /* use to pass per-file flags */
693 struct rw_semaphore i_sem; /* protect fi info */
694 atomic_t dirty_pages; /* # of dirty pages */
695 f2fs_hash_t chash; /* hash value of given file name */
696 unsigned int clevel; /* maximum level of given file name */
697 struct task_struct *task; /* lookup and create consistency */
698 struct task_struct *cp_task; /* separate cp/wb IO stats*/
699 nid_t i_xattr_nid; /* node id that contains xattrs */
700 loff_t last_disk_size; /* lastly written file size */
701
702 #ifdef CONFIG_QUOTA
703 struct dquot *i_dquot[MAXQUOTAS];
704
705 /* quota space reservation, managed internally by quota code */
706 qsize_t i_reserved_quota;
707 #endif
708 struct list_head dirty_list; /* dirty list for dirs and files */
709 struct list_head gdirty_list; /* linked in global dirty list */
710 struct list_head inmem_ilist; /* list for inmem inodes */
711 struct list_head inmem_pages; /* inmemory pages managed by f2fs */
712 struct task_struct *inmem_task; /* store inmemory task */
713 struct mutex inmem_lock; /* lock for inmemory pages */
714 struct extent_tree *extent_tree; /* cached extent_tree entry */
715 struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
716 struct rw_semaphore i_mmap_sem;
717 struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
718
719 int i_extra_isize; /* size of extra space located in i_addr */
720 kprojid_t i_projid; /* id for project quota */
721 int i_inline_xattr_size; /* inline xattr size */
722 struct timespec i_crtime; /* inode creation time */
723 struct timespec i_disk_time[4]; /* inode disk times */
724 };
725
get_extent_info(struct extent_info * ext,struct f2fs_extent * i_ext)726 static inline void get_extent_info(struct extent_info *ext,
727 struct f2fs_extent *i_ext)
728 {
729 ext->fofs = le32_to_cpu(i_ext->fofs);
730 ext->blk = le32_to_cpu(i_ext->blk);
731 ext->len = le32_to_cpu(i_ext->len);
732 }
733
set_raw_extent(struct extent_info * ext,struct f2fs_extent * i_ext)734 static inline void set_raw_extent(struct extent_info *ext,
735 struct f2fs_extent *i_ext)
736 {
737 i_ext->fofs = cpu_to_le32(ext->fofs);
738 i_ext->blk = cpu_to_le32(ext->blk);
739 i_ext->len = cpu_to_le32(ext->len);
740 }
741
set_extent_info(struct extent_info * ei,unsigned int fofs,u32 blk,unsigned int len)742 static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
743 u32 blk, unsigned int len)
744 {
745 ei->fofs = fofs;
746 ei->blk = blk;
747 ei->len = len;
748 }
749
__is_discard_mergeable(struct discard_info * back,struct discard_info * front)750 static inline bool __is_discard_mergeable(struct discard_info *back,
751 struct discard_info *front)
752 {
753 return (back->lstart + back->len == front->lstart) &&
754 (back->len + front->len < DEF_MAX_DISCARD_LEN);
755 }
756
__is_discard_back_mergeable(struct discard_info * cur,struct discard_info * back)757 static inline bool __is_discard_back_mergeable(struct discard_info *cur,
758 struct discard_info *back)
759 {
760 return __is_discard_mergeable(back, cur);
761 }
762
__is_discard_front_mergeable(struct discard_info * cur,struct discard_info * front)763 static inline bool __is_discard_front_mergeable(struct discard_info *cur,
764 struct discard_info *front)
765 {
766 return __is_discard_mergeable(cur, front);
767 }
768
__is_extent_mergeable(struct extent_info * back,struct extent_info * front)769 static inline bool __is_extent_mergeable(struct extent_info *back,
770 struct extent_info *front)
771 {
772 return (back->fofs + back->len == front->fofs &&
773 back->blk + back->len == front->blk);
774 }
775
__is_back_mergeable(struct extent_info * cur,struct extent_info * back)776 static inline bool __is_back_mergeable(struct extent_info *cur,
777 struct extent_info *back)
778 {
779 return __is_extent_mergeable(back, cur);
780 }
781
__is_front_mergeable(struct extent_info * cur,struct extent_info * front)782 static inline bool __is_front_mergeable(struct extent_info *cur,
783 struct extent_info *front)
784 {
785 return __is_extent_mergeable(cur, front);
786 }
787
788 extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
__try_update_largest_extent(struct inode * inode,struct extent_tree * et,struct extent_node * en)789 static inline void __try_update_largest_extent(struct inode *inode,
790 struct extent_tree *et, struct extent_node *en)
791 {
792 if (en->ei.len > et->largest.len) {
793 et->largest = en->ei;
794 f2fs_mark_inode_dirty_sync(inode, true);
795 }
796 }
797
798 /*
799 * For free nid management
800 */
801 enum nid_state {
802 FREE_NID, /* newly added to free nid list */
803 PREALLOC_NID, /* it is preallocated */
804 MAX_NID_STATE,
805 };
806
807 struct f2fs_nm_info {
808 block_t nat_blkaddr; /* base disk address of NAT */
809 nid_t max_nid; /* maximum possible node ids */
810 nid_t available_nids; /* # of available node ids */
811 nid_t next_scan_nid; /* the next nid to be scanned */
812 unsigned int ram_thresh; /* control the memory footprint */
813 unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
814 unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
815
816 /* NAT cache management */
817 struct radix_tree_root nat_root;/* root of the nat entry cache */
818 struct radix_tree_root nat_set_root;/* root of the nat set cache */
819 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
820 struct list_head nat_entries; /* cached nat entry list (clean) */
821 unsigned int nat_cnt; /* the # of cached nat entries */
822 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
823 unsigned int nat_blocks; /* # of nat blocks */
824
825 /* free node ids management */
826 struct radix_tree_root free_nid_root;/* root of the free_nid cache */
827 struct list_head free_nid_list; /* list for free nids excluding preallocated nids */
828 unsigned int nid_cnt[MAX_NID_STATE]; /* the number of free node id */
829 spinlock_t nid_list_lock; /* protect nid lists ops */
830 struct mutex build_lock; /* lock for build free nids */
831 unsigned char **free_nid_bitmap;
832 unsigned char *nat_block_bitmap;
833 unsigned short *free_nid_count; /* free nid count of NAT block */
834
835 /* for checkpoint */
836 char *nat_bitmap; /* NAT bitmap pointer */
837
838 unsigned int nat_bits_blocks; /* # of nat bits blocks */
839 unsigned char *nat_bits; /* NAT bits blocks */
840 unsigned char *full_nat_bits; /* full NAT pages */
841 unsigned char *empty_nat_bits; /* empty NAT pages */
842 #ifdef CONFIG_F2FS_CHECK_FS
843 char *nat_bitmap_mir; /* NAT bitmap mirror */
844 #endif
845 int bitmap_size; /* bitmap size */
846 };
847
848 /*
849 * this structure is used as one of function parameters.
850 * all the information are dedicated to a given direct node block determined
851 * by the data offset in a file.
852 */
853 struct dnode_of_data {
854 struct inode *inode; /* vfs inode pointer */
855 struct page *inode_page; /* its inode page, NULL is possible */
856 struct page *node_page; /* cached direct node page */
857 nid_t nid; /* node id of the direct node block */
858 unsigned int ofs_in_node; /* data offset in the node page */
859 bool inode_page_locked; /* inode page is locked or not */
860 bool node_changed; /* is node block changed */
861 char cur_level; /* level of hole node page */
862 char max_level; /* level of current page located */
863 block_t data_blkaddr; /* block address of the node block */
864 };
865
set_new_dnode(struct dnode_of_data * dn,struct inode * inode,struct page * ipage,struct page * npage,nid_t nid)866 static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
867 struct page *ipage, struct page *npage, nid_t nid)
868 {
869 memset(dn, 0, sizeof(*dn));
870 dn->inode = inode;
871 dn->inode_page = ipage;
872 dn->node_page = npage;
873 dn->nid = nid;
874 }
875
876 /*
877 * For SIT manager
878 *
879 * By default, there are 6 active log areas across the whole main area.
880 * When considering hot and cold data separation to reduce cleaning overhead,
881 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
882 * respectively.
883 * In the current design, you should not change the numbers intentionally.
884 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
885 * logs individually according to the underlying devices. (default: 6)
886 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
887 * data and 8 for node logs.
888 */
889 #define NR_CURSEG_DATA_TYPE (3)
890 #define NR_CURSEG_NODE_TYPE (3)
891 #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
892
893 enum {
894 CURSEG_HOT_DATA = 0, /* directory entry blocks */
895 CURSEG_WARM_DATA, /* data blocks */
896 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
897 CURSEG_HOT_NODE, /* direct node blocks of directory files */
898 CURSEG_WARM_NODE, /* direct node blocks of normal files */
899 CURSEG_COLD_NODE, /* indirect node blocks */
900 NO_CHECK_TYPE,
901 };
902
903 struct flush_cmd {
904 struct completion wait;
905 struct llist_node llnode;
906 nid_t ino;
907 int ret;
908 };
909
910 struct flush_cmd_control {
911 struct task_struct *f2fs_issue_flush; /* flush thread */
912 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
913 atomic_t issued_flush; /* # of issued flushes */
914 atomic_t issing_flush; /* # of issing flushes */
915 struct llist_head issue_list; /* list for command issue */
916 struct llist_node *dispatch_list; /* list for command dispatch */
917 };
918
919 struct f2fs_sm_info {
920 struct sit_info *sit_info; /* whole segment information */
921 struct free_segmap_info *free_info; /* free segment information */
922 struct dirty_seglist_info *dirty_info; /* dirty segment information */
923 struct curseg_info *curseg_array; /* active segment information */
924
925 struct rw_semaphore curseg_lock; /* for preventing curseg change */
926
927 block_t seg0_blkaddr; /* block address of 0'th segment */
928 block_t main_blkaddr; /* start block address of main area */
929 block_t ssa_blkaddr; /* start block address of SSA area */
930
931 unsigned int segment_count; /* total # of segments */
932 unsigned int main_segments; /* # of segments in main area */
933 unsigned int reserved_segments; /* # of reserved segments */
934 unsigned int ovp_segments; /* # of overprovision segments */
935
936 /* a threshold to reclaim prefree segments */
937 unsigned int rec_prefree_segments;
938
939 /* for batched trimming */
940 unsigned int trim_sections; /* # of sections to trim */
941
942 struct list_head sit_entry_set; /* sit entry set list */
943
944 unsigned int ipu_policy; /* in-place-update policy */
945 unsigned int min_ipu_util; /* in-place-update threshold */
946 unsigned int min_fsync_blocks; /* threshold for fsync */
947 unsigned int min_hot_blocks; /* threshold for hot block allocation */
948 unsigned int min_ssr_sections; /* threshold to trigger SSR allocation */
949
950 /* for flush command control */
951 struct flush_cmd_control *fcc_info;
952
953 /* for discard command control */
954 struct discard_cmd_control *dcc_info;
955 };
956
957 /*
958 * For superblock
959 */
960 /*
961 * COUNT_TYPE for monitoring
962 *
963 * f2fs monitors the number of several block types such as on-writeback,
964 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
965 */
966 #define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
967 enum count_type {
968 F2FS_DIRTY_DENTS,
969 F2FS_DIRTY_DATA,
970 F2FS_DIRTY_QDATA,
971 F2FS_DIRTY_NODES,
972 F2FS_DIRTY_META,
973 F2FS_INMEM_PAGES,
974 F2FS_DIRTY_IMETA,
975 F2FS_WB_CP_DATA,
976 F2FS_WB_DATA,
977 NR_COUNT_TYPE,
978 };
979
980 /*
981 * The below are the page types of bios used in submit_bio().
982 * The available types are:
983 * DATA User data pages. It operates as async mode.
984 * NODE Node pages. It operates as async mode.
985 * META FS metadata pages such as SIT, NAT, CP.
986 * NR_PAGE_TYPE The number of page types.
987 * META_FLUSH Make sure the previous pages are written
988 * with waiting the bio's completion
989 * ... Only can be used with META.
990 */
991 #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
992 enum page_type {
993 DATA,
994 NODE,
995 META,
996 NR_PAGE_TYPE,
997 META_FLUSH,
998 INMEM, /* the below types are used by tracepoints only. */
999 INMEM_DROP,
1000 INMEM_INVALIDATE,
1001 INMEM_REVOKE,
1002 IPU,
1003 OPU,
1004 };
1005
1006 enum temp_type {
1007 HOT = 0, /* must be zero for meta bio */
1008 WARM,
1009 COLD,
1010 NR_TEMP_TYPE,
1011 };
1012
1013 enum need_lock_type {
1014 LOCK_REQ = 0,
1015 LOCK_DONE,
1016 LOCK_RETRY,
1017 };
1018
1019 enum cp_reason_type {
1020 CP_NO_NEEDED,
1021 CP_NON_REGULAR,
1022 CP_HARDLINK,
1023 CP_SB_NEED_CP,
1024 CP_WRONG_PINO,
1025 CP_NO_SPC_ROLL,
1026 CP_NODE_NEED_CP,
1027 CP_FASTBOOT_MODE,
1028 CP_SPEC_LOG_NUM,
1029 CP_RECOVER_DIR,
1030 };
1031
1032 enum iostat_type {
1033 APP_DIRECT_IO, /* app direct IOs */
1034 APP_BUFFERED_IO, /* app buffered IOs */
1035 APP_WRITE_IO, /* app write IOs */
1036 APP_MAPPED_IO, /* app mapped IOs */
1037 FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */
1038 FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */
1039 FS_META_IO, /* meta IOs from kworker/reclaimer */
1040 FS_GC_DATA_IO, /* data IOs from forground gc */
1041 FS_GC_NODE_IO, /* node IOs from forground gc */
1042 FS_CP_DATA_IO, /* data IOs from checkpoint */
1043 FS_CP_NODE_IO, /* node IOs from checkpoint */
1044 FS_CP_META_IO, /* meta IOs from checkpoint */
1045 FS_DISCARD, /* discard */
1046 NR_IO_TYPE,
1047 };
1048
1049 struct f2fs_io_info {
1050 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
1051 nid_t ino; /* inode number */
1052 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
1053 enum temp_type temp; /* contains HOT/WARM/COLD */
1054 int op; /* contains REQ_OP_ */
1055 int op_flags; /* req_flag_bits */
1056 block_t new_blkaddr; /* new block address to be written */
1057 block_t old_blkaddr; /* old block address before Cow */
1058 struct page *page; /* page to be written */
1059 struct page *encrypted_page; /* encrypted page */
1060 struct list_head list; /* serialize IOs */
1061 bool submitted; /* indicate IO submission */
1062 int need_lock; /* indicate we need to lock cp_rwsem */
1063 bool in_list; /* indicate fio is in io_list */
1064 bool is_meta; /* indicate borrow meta inode mapping or not */
1065 enum iostat_type io_type; /* io type */
1066 struct writeback_control *io_wbc; /* writeback control */
1067 };
1068
1069 #define is_read_io(rw) ((rw) == READ)
1070 struct f2fs_bio_info {
1071 struct f2fs_sb_info *sbi; /* f2fs superblock */
1072 struct bio *bio; /* bios to merge */
1073 sector_t last_block_in_bio; /* last block number */
1074 struct f2fs_io_info fio; /* store buffered io info. */
1075 struct rw_semaphore io_rwsem; /* blocking op for bio */
1076 spinlock_t io_lock; /* serialize DATA/NODE IOs */
1077 struct list_head io_list; /* track fios */
1078 };
1079
1080 #define FDEV(i) (sbi->devs[i])
1081 #define RDEV(i) (raw_super->devs[i])
1082 struct f2fs_dev_info {
1083 struct block_device *bdev;
1084 char path[MAX_PATH_LEN];
1085 unsigned int total_segments;
1086 block_t start_blk;
1087 block_t end_blk;
1088 #ifdef CONFIG_BLK_DEV_ZONED
1089 unsigned int nr_blkz; /* Total number of zones */
1090 u8 *blkz_type; /* Array of zones type */
1091 #endif
1092 };
1093
1094 enum inode_type {
1095 DIR_INODE, /* for dirty dir inode */
1096 FILE_INODE, /* for dirty regular/symlink inode */
1097 DIRTY_META, /* for all dirtied inode metadata */
1098 ATOMIC_FILE, /* for all atomic files */
1099 NR_INODE_TYPE,
1100 };
1101
1102 /* for inner inode cache management */
1103 struct inode_management {
1104 struct radix_tree_root ino_root; /* ino entry array */
1105 spinlock_t ino_lock; /* for ino entry lock */
1106 struct list_head ino_list; /* inode list head */
1107 unsigned long ino_num; /* number of entries */
1108 };
1109
1110 /* For s_flag in struct f2fs_sb_info */
1111 enum {
1112 SBI_IS_DIRTY, /* dirty flag for checkpoint */
1113 SBI_IS_CLOSE, /* specify unmounting */
1114 SBI_NEED_FSCK, /* need fsck.f2fs to fix */
1115 SBI_POR_DOING, /* recovery is doing or not */
1116 SBI_NEED_SB_WRITE, /* need to recover superblock */
1117 SBI_NEED_CP, /* need to checkpoint */
1118 };
1119
1120 enum {
1121 CP_TIME,
1122 REQ_TIME,
1123 MAX_TIME,
1124 };
1125
1126 enum {
1127 WHINT_MODE_OFF, /* not pass down write hints */
1128 WHINT_MODE_USER, /* try to pass down hints given by users */
1129 WHINT_MODE_FS, /* pass down hints with F2FS policy */
1130 };
1131
1132 enum {
1133 ALLOC_MODE_DEFAULT, /* stay default */
1134 ALLOC_MODE_REUSE, /* reuse segments as much as possible */
1135 };
1136
1137 enum fsync_mode {
1138 FSYNC_MODE_POSIX, /* fsync follows posix semantics */
1139 FSYNC_MODE_STRICT, /* fsync behaves in line with ext4 */
1140 FSYNC_MODE_NOBARRIER, /* fsync behaves nobarrier based on posix */
1141 };
1142
1143 #ifdef CONFIG_F2FS_FS_ENCRYPTION
1144 #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1145 (unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1146 #else
1147 #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1148 #endif
1149
1150 struct f2fs_sb_info {
1151 struct super_block *sb; /* pointer to VFS super block */
1152 struct proc_dir_entry *s_proc; /* proc entry */
1153 struct f2fs_super_block *raw_super; /* raw super block pointer */
1154 struct rw_semaphore sb_lock; /* lock for raw super block */
1155 int valid_super_block; /* valid super block no */
1156 unsigned long s_flag; /* flags for sbi */
1157
1158 #ifdef CONFIG_BLK_DEV_ZONED
1159 unsigned int blocks_per_blkz; /* F2FS blocks per zone */
1160 unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
1161 #endif
1162
1163 /* for node-related operations */
1164 struct f2fs_nm_info *nm_info; /* node manager */
1165 struct inode *node_inode; /* cache node blocks */
1166
1167 /* for segment-related operations */
1168 struct f2fs_sm_info *sm_info; /* segment manager */
1169
1170 /* for bio operations */
1171 struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
1172 struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1173 /* bio ordering for NODE/DATA */
1174 mempool_t *write_io_dummy; /* Dummy pages */
1175
1176 /* for checkpoint */
1177 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
1178 int cur_cp_pack; /* remain current cp pack */
1179 spinlock_t cp_lock; /* for flag in ckpt */
1180 struct inode *meta_inode; /* cache meta blocks */
1181 struct mutex cp_mutex; /* checkpoint procedure lock */
1182 struct rw_semaphore cp_rwsem; /* blocking FS operations */
1183 struct rw_semaphore node_write; /* locking node writes */
1184 struct rw_semaphore node_change; /* locking node change */
1185 wait_queue_head_t cp_wait;
1186 unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
1187 long interval_time[MAX_TIME]; /* to store thresholds */
1188
1189 struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
1190
1191 /* for orphan inode, use 0'th array */
1192 unsigned int max_orphans; /* max orphan inodes */
1193
1194 /* for inode management */
1195 struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
1196 spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
1197
1198 /* for extent tree cache */
1199 struct radix_tree_root extent_tree_root;/* cache extent cache entries */
1200 struct mutex extent_tree_lock; /* locking extent radix tree */
1201 struct list_head extent_list; /* lru list for shrinker */
1202 spinlock_t extent_lock; /* locking extent lru list */
1203 atomic_t total_ext_tree; /* extent tree count */
1204 struct list_head zombie_list; /* extent zombie tree list */
1205 atomic_t total_zombie_tree; /* extent zombie tree count */
1206 atomic_t total_ext_node; /* extent info count */
1207
1208 /* basic filesystem units */
1209 unsigned int log_sectors_per_block; /* log2 sectors per block */
1210 unsigned int log_blocksize; /* log2 block size */
1211 unsigned int blocksize; /* block size */
1212 unsigned int root_ino_num; /* root inode number*/
1213 unsigned int node_ino_num; /* node inode number*/
1214 unsigned int meta_ino_num; /* meta inode number*/
1215 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
1216 unsigned int blocks_per_seg; /* blocks per segment */
1217 unsigned int segs_per_sec; /* segments per section */
1218 unsigned int secs_per_zone; /* sections per zone */
1219 unsigned int total_sections; /* total section count */
1220 unsigned int total_node_count; /* total node block count */
1221 unsigned int total_valid_node_count; /* valid node block count */
1222 loff_t max_file_blocks; /* max block index of file */
1223 int dir_level; /* directory level */
1224 unsigned int trigger_ssr_threshold; /* threshold to trigger ssr */
1225 int readdir_ra; /* readahead inode in readdir */
1226
1227 block_t user_block_count; /* # of user blocks */
1228 block_t total_valid_block_count; /* # of valid blocks */
1229 block_t discard_blks; /* discard command candidats */
1230 block_t last_valid_block_count; /* for recovery */
1231 block_t reserved_blocks; /* configurable reserved blocks */
1232 block_t current_reserved_blocks; /* current reserved blocks */
1233
1234 unsigned int nquota_files; /* # of quota sysfile */
1235
1236 u32 s_next_generation; /* for NFS support */
1237
1238 /* # of pages, see count_type */
1239 atomic_t nr_pages[NR_COUNT_TYPE];
1240 /* # of allocated blocks */
1241 struct percpu_counter alloc_valid_block_count;
1242
1243 /* writeback control */
1244 atomic_t wb_sync_req; /* count # of WB_SYNC threads */
1245
1246 /* valid inode count */
1247 struct percpu_counter total_valid_inode_count;
1248
1249 struct f2fs_mount_info mount_opt; /* mount options */
1250
1251 /* for cleaning operations */
1252 struct mutex gc_mutex; /* mutex for GC */
1253 struct f2fs_gc_kthread *gc_thread; /* GC thread */
1254 unsigned int cur_victim_sec; /* current victim section num */
1255
1256 /* threshold for converting bg victims for fg */
1257 u64 fggc_threshold;
1258
1259 /* threshold for gc trials on pinned files */
1260 u64 gc_pin_file_threshold;
1261
1262 /* maximum # of trials to find a victim segment for SSR and GC */
1263 unsigned int max_victim_search;
1264
1265 /*
1266 * for stat information.
1267 * one is for the LFS mode, and the other is for the SSR mode.
1268 */
1269 #ifdef CONFIG_F2FS_STAT_FS
1270 struct f2fs_stat_info *stat_info; /* FS status information */
1271 unsigned int segment_count[2]; /* # of allocated segments */
1272 unsigned int block_count[2]; /* # of allocated blocks */
1273 atomic_t inplace_count; /* # of inplace update */
1274 atomic64_t total_hit_ext; /* # of lookup extent cache */
1275 atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
1276 atomic64_t read_hit_largest; /* # of hit largest extent node */
1277 atomic64_t read_hit_cached; /* # of hit cached extent node */
1278 atomic_t inline_xattr; /* # of inline_xattr inodes */
1279 atomic_t inline_inode; /* # of inline_data inodes */
1280 atomic_t inline_dir; /* # of inline_dentry inodes */
1281 atomic_t aw_cnt; /* # of atomic writes */
1282 atomic_t vw_cnt; /* # of volatile writes */
1283 atomic_t max_aw_cnt; /* max # of atomic writes */
1284 atomic_t max_vw_cnt; /* max # of volatile writes */
1285 int bg_gc; /* background gc calls */
1286 unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
1287 #endif
1288 spinlock_t stat_lock; /* lock for stat operations */
1289
1290 /* For app/fs IO statistics */
1291 spinlock_t iostat_lock;
1292 unsigned long long write_iostat[NR_IO_TYPE];
1293 bool iostat_enable;
1294
1295 /* For sysfs suppport */
1296 struct kobject s_kobj;
1297 struct completion s_kobj_unregister;
1298
1299 /* For shrinker support */
1300 struct list_head s_list;
1301 int s_ndevs; /* number of devices */
1302 struct f2fs_dev_info *devs; /* for device list */
1303 unsigned int dirty_device; /* for checkpoint data flush */
1304 spinlock_t dev_lock; /* protect dirty_device */
1305 struct mutex umount_mutex;
1306 unsigned int shrinker_run_no;
1307
1308 /* For write statistics */
1309 u64 sectors_written_start;
1310 u64 kbytes_written;
1311
1312 /* Reference to checksum algorithm driver via cryptoapi */
1313 struct crypto_shash *s_chksum_driver;
1314
1315 /* Precomputed FS UUID checksum for seeding other checksums */
1316 __u32 s_chksum_seed;
1317 };
1318
1319 #ifdef CONFIG_F2FS_FAULT_INJECTION
1320 #define f2fs_show_injection_info(type) \
1321 printk("%sF2FS-fs : inject %s in %s of %pF\n", \
1322 KERN_INFO, fault_name[type], \
1323 __func__, __builtin_return_address(0))
time_to_inject(struct f2fs_sb_info * sbi,int type)1324 static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1325 {
1326 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
1327
1328 if (!ffi->inject_rate)
1329 return false;
1330
1331 if (!IS_FAULT_SET(ffi, type))
1332 return false;
1333
1334 atomic_inc(&ffi->inject_ops);
1335 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
1336 atomic_set(&ffi->inject_ops, 0);
1337 return true;
1338 }
1339 return false;
1340 }
1341 #endif
1342
1343 /* For write statistics. Suppose sector size is 512 bytes,
1344 * and the return value is in kbytes. s is of struct f2fs_sb_info.
1345 */
1346 #define BD_PART_WRITTEN(s) \
1347 (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) - \
1348 (s)->sectors_written_start) >> 1)
1349
f2fs_update_time(struct f2fs_sb_info * sbi,int type)1350 static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1351 {
1352 sbi->last_time[type] = jiffies;
1353 }
1354
f2fs_time_over(struct f2fs_sb_info * sbi,int type)1355 static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
1356 {
1357 unsigned long interval = sbi->interval_time[type] * HZ;
1358
1359 return time_after(jiffies, sbi->last_time[type] + interval);
1360 }
1361
is_idle(struct f2fs_sb_info * sbi)1362 static inline bool is_idle(struct f2fs_sb_info *sbi)
1363 {
1364 struct block_device *bdev = sbi->sb->s_bdev;
1365 struct request_queue *q = bdev_get_queue(bdev);
1366 struct request_list *rl = &q->root_rl;
1367
1368 if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1369 return 0;
1370
1371 return f2fs_time_over(sbi, REQ_TIME);
1372 }
1373
1374 /*
1375 * Inline functions
1376 */
__f2fs_crc32(struct f2fs_sb_info * sbi,u32 crc,const void * address,unsigned int length)1377 static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1378 const void *address, unsigned int length)
1379 {
1380 struct {
1381 struct shash_desc shash;
1382 char ctx[4];
1383 } desc;
1384 int err;
1385
1386 BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1387
1388 desc.shash.tfm = sbi->s_chksum_driver;
1389 desc.shash.flags = 0;
1390 *(u32 *)desc.ctx = crc;
1391
1392 err = crypto_shash_update(&desc.shash, address, length);
1393 BUG_ON(err);
1394
1395 return *(u32 *)desc.ctx;
1396 }
1397
f2fs_crc32(struct f2fs_sb_info * sbi,const void * address,unsigned int length)1398 static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1399 unsigned int length)
1400 {
1401 return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1402 }
1403
f2fs_crc_valid(struct f2fs_sb_info * sbi,__u32 blk_crc,void * buf,size_t buf_size)1404 static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1405 void *buf, size_t buf_size)
1406 {
1407 return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1408 }
1409
f2fs_chksum(struct f2fs_sb_info * sbi,u32 crc,const void * address,unsigned int length)1410 static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1411 const void *address, unsigned int length)
1412 {
1413 return __f2fs_crc32(sbi, crc, address, length);
1414 }
1415
F2FS_I(struct inode * inode)1416 static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1417 {
1418 return container_of(inode, struct f2fs_inode_info, vfs_inode);
1419 }
1420
F2FS_SB(struct super_block * sb)1421 static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
1422 {
1423 return sb->s_fs_info;
1424 }
1425
F2FS_I_SB(struct inode * inode)1426 static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
1427 {
1428 return F2FS_SB(inode->i_sb);
1429 }
1430
F2FS_M_SB(struct address_space * mapping)1431 static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
1432 {
1433 return F2FS_I_SB(mapping->host);
1434 }
1435
F2FS_P_SB(struct page * page)1436 static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
1437 {
1438 return F2FS_M_SB(page->mapping);
1439 }
1440
F2FS_RAW_SUPER(struct f2fs_sb_info * sbi)1441 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
1442 {
1443 return (struct f2fs_super_block *)(sbi->raw_super);
1444 }
1445
F2FS_CKPT(struct f2fs_sb_info * sbi)1446 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
1447 {
1448 return (struct f2fs_checkpoint *)(sbi->ckpt);
1449 }
1450
F2FS_NODE(struct page * page)1451 static inline struct f2fs_node *F2FS_NODE(struct page *page)
1452 {
1453 return (struct f2fs_node *)page_address(page);
1454 }
1455
F2FS_INODE(struct page * page)1456 static inline struct f2fs_inode *F2FS_INODE(struct page *page)
1457 {
1458 return &((struct f2fs_node *)page_address(page))->i;
1459 }
1460
NM_I(struct f2fs_sb_info * sbi)1461 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
1462 {
1463 return (struct f2fs_nm_info *)(sbi->nm_info);
1464 }
1465
SM_I(struct f2fs_sb_info * sbi)1466 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
1467 {
1468 return (struct f2fs_sm_info *)(sbi->sm_info);
1469 }
1470
SIT_I(struct f2fs_sb_info * sbi)1471 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
1472 {
1473 return (struct sit_info *)(SM_I(sbi)->sit_info);
1474 }
1475
FREE_I(struct f2fs_sb_info * sbi)1476 static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
1477 {
1478 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
1479 }
1480
DIRTY_I(struct f2fs_sb_info * sbi)1481 static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
1482 {
1483 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
1484 }
1485
META_MAPPING(struct f2fs_sb_info * sbi)1486 static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
1487 {
1488 return sbi->meta_inode->i_mapping;
1489 }
1490
NODE_MAPPING(struct f2fs_sb_info * sbi)1491 static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
1492 {
1493 return sbi->node_inode->i_mapping;
1494 }
1495
is_sbi_flag_set(struct f2fs_sb_info * sbi,unsigned int type)1496 static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
1497 {
1498 return test_bit(type, &sbi->s_flag);
1499 }
1500
set_sbi_flag(struct f2fs_sb_info * sbi,unsigned int type)1501 static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1502 {
1503 set_bit(type, &sbi->s_flag);
1504 }
1505
clear_sbi_flag(struct f2fs_sb_info * sbi,unsigned int type)1506 static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1507 {
1508 clear_bit(type, &sbi->s_flag);
1509 }
1510
cur_cp_version(struct f2fs_checkpoint * cp)1511 static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1512 {
1513 return le64_to_cpu(cp->checkpoint_ver);
1514 }
1515
f2fs_qf_ino(struct super_block * sb,int type)1516 static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1517 {
1518 if (type < F2FS_MAX_QUOTAS)
1519 return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1520 return 0;
1521 }
1522
cur_cp_crc(struct f2fs_checkpoint * cp)1523 static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1524 {
1525 size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1526 return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1527 }
1528
__is_set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)1529 static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1530 {
1531 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1532
1533 return ckpt_flags & f;
1534 }
1535
is_set_ckpt_flags(struct f2fs_sb_info * sbi,unsigned int f)1536 static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1537 {
1538 return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1539 }
1540
__set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)1541 static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1542 {
1543 unsigned int ckpt_flags;
1544
1545 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1546 ckpt_flags |= f;
1547 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1548 }
1549
set_ckpt_flags(struct f2fs_sb_info * sbi,unsigned int f)1550 static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1551 {
1552 unsigned long flags;
1553
1554 spin_lock_irqsave(&sbi->cp_lock, flags);
1555 __set_ckpt_flags(F2FS_CKPT(sbi), f);
1556 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1557 }
1558
__clear_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)1559 static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1560 {
1561 unsigned int ckpt_flags;
1562
1563 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1564 ckpt_flags &= (~f);
1565 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1566 }
1567
clear_ckpt_flags(struct f2fs_sb_info * sbi,unsigned int f)1568 static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1569 {
1570 unsigned long flags;
1571
1572 spin_lock_irqsave(&sbi->cp_lock, flags);
1573 __clear_ckpt_flags(F2FS_CKPT(sbi), f);
1574 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1575 }
1576
disable_nat_bits(struct f2fs_sb_info * sbi,bool lock)1577 static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
1578 {
1579 unsigned long flags;
1580
1581 set_sbi_flag(sbi, SBI_NEED_FSCK);
1582
1583 if (lock)
1584 spin_lock_irqsave(&sbi->cp_lock, flags);
1585 __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1586 kfree(NM_I(sbi)->nat_bits);
1587 NM_I(sbi)->nat_bits = NULL;
1588 if (lock)
1589 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1590 }
1591
enabled_nat_bits(struct f2fs_sb_info * sbi,struct cp_control * cpc)1592 static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
1593 struct cp_control *cpc)
1594 {
1595 bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1596
1597 return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
1598 }
1599
f2fs_lock_op(struct f2fs_sb_info * sbi)1600 static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
1601 {
1602 down_read(&sbi->cp_rwsem);
1603 }
1604
f2fs_trylock_op(struct f2fs_sb_info * sbi)1605 static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1606 {
1607 return down_read_trylock(&sbi->cp_rwsem);
1608 }
1609
f2fs_unlock_op(struct f2fs_sb_info * sbi)1610 static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
1611 {
1612 up_read(&sbi->cp_rwsem);
1613 }
1614
f2fs_lock_all(struct f2fs_sb_info * sbi)1615 static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
1616 {
1617 down_write(&sbi->cp_rwsem);
1618 }
1619
f2fs_unlock_all(struct f2fs_sb_info * sbi)1620 static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
1621 {
1622 up_write(&sbi->cp_rwsem);
1623 }
1624
__get_cp_reason(struct f2fs_sb_info * sbi)1625 static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1626 {
1627 int reason = CP_SYNC;
1628
1629 if (test_opt(sbi, FASTBOOT))
1630 reason = CP_FASTBOOT;
1631 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1632 reason = CP_UMOUNT;
1633 return reason;
1634 }
1635
__remain_node_summaries(int reason)1636 static inline bool __remain_node_summaries(int reason)
1637 {
1638 return (reason & (CP_UMOUNT | CP_FASTBOOT));
1639 }
1640
__exist_node_summaries(struct f2fs_sb_info * sbi)1641 static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1642 {
1643 return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1644 is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1645 }
1646
1647 /*
1648 * Check whether the given nid is within node id range.
1649 */
check_nid_range(struct f2fs_sb_info * sbi,nid_t nid)1650 static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
1651 {
1652 if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1653 return -EINVAL;
1654 if (unlikely(nid >= NM_I(sbi)->max_nid))
1655 return -EINVAL;
1656 return 0;
1657 }
1658
1659 /*
1660 * Check whether the inode has blocks or not
1661 */
F2FS_HAS_BLOCKS(struct inode * inode)1662 static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1663 {
1664 block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
1665
1666 return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
1667 }
1668
f2fs_has_xattr_block(unsigned int ofs)1669 static inline bool f2fs_has_xattr_block(unsigned int ofs)
1670 {
1671 return ofs == XATTR_NODE_OFFSET;
1672 }
1673
__allow_reserved_blocks(struct f2fs_sb_info * sbi,struct inode * inode,bool cap)1674 static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1675 struct inode *inode, bool cap)
1676 {
1677 if (!inode)
1678 return true;
1679 if (!test_opt(sbi, RESERVE_ROOT))
1680 return false;
1681 if (IS_NOQUOTA(inode))
1682 return true;
1683 if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
1684 return true;
1685 if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
1686 in_group_p(F2FS_OPTION(sbi).s_resgid))
1687 return true;
1688 if (cap && capable(CAP_SYS_RESOURCE))
1689 return true;
1690 return false;
1691 }
1692
1693 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)1694 static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
1695 struct inode *inode, blkcnt_t *count)
1696 {
1697 blkcnt_t diff = 0, release = 0;
1698 block_t avail_user_block_count;
1699 int ret;
1700
1701 ret = dquot_reserve_block(inode, *count);
1702 if (ret)
1703 return ret;
1704
1705 #ifdef CONFIG_F2FS_FAULT_INJECTION
1706 if (time_to_inject(sbi, FAULT_BLOCK)) {
1707 f2fs_show_injection_info(FAULT_BLOCK);
1708 release = *count;
1709 goto enospc;
1710 }
1711 #endif
1712 /*
1713 * let's increase this in prior to actual block count change in order
1714 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1715 */
1716 percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1717
1718 spin_lock(&sbi->stat_lock);
1719 sbi->total_valid_block_count += (block_t)(*count);
1720 avail_user_block_count = sbi->user_block_count -
1721 sbi->current_reserved_blocks;
1722
1723 if (!__allow_reserved_blocks(sbi, inode, true))
1724 avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
1725
1726 if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1727 diff = sbi->total_valid_block_count - avail_user_block_count;
1728 if (diff > *count)
1729 diff = *count;
1730 *count -= diff;
1731 release = diff;
1732 sbi->total_valid_block_count -= diff;
1733 if (!*count) {
1734 spin_unlock(&sbi->stat_lock);
1735 percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
1736 goto enospc;
1737 }
1738 }
1739 spin_unlock(&sbi->stat_lock);
1740
1741 if (unlikely(release))
1742 dquot_release_reservation_block(inode, release);
1743 f2fs_i_blocks_write(inode, *count, true, true);
1744 return 0;
1745
1746 enospc:
1747 dquot_release_reservation_block(inode, release);
1748 return -ENOSPC;
1749 }
1750
dec_valid_block_count(struct f2fs_sb_info * sbi,struct inode * inode,block_t count)1751 static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
1752 struct inode *inode,
1753 block_t count)
1754 {
1755 blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
1756
1757 spin_lock(&sbi->stat_lock);
1758 f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
1759 f2fs_bug_on(sbi, inode->i_blocks < sectors);
1760 sbi->total_valid_block_count -= (block_t)count;
1761 if (sbi->reserved_blocks &&
1762 sbi->current_reserved_blocks < sbi->reserved_blocks)
1763 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
1764 sbi->current_reserved_blocks + count);
1765 spin_unlock(&sbi->stat_lock);
1766 f2fs_i_blocks_write(inode, count, false, true);
1767 }
1768
inc_page_count(struct f2fs_sb_info * sbi,int count_type)1769 static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1770 {
1771 atomic_inc(&sbi->nr_pages[count_type]);
1772
1773 if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
1774 count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
1775 return;
1776
1777 set_sbi_flag(sbi, SBI_IS_DIRTY);
1778 }
1779
inode_inc_dirty_pages(struct inode * inode)1780 static inline void inode_inc_dirty_pages(struct inode *inode)
1781 {
1782 atomic_inc(&F2FS_I(inode)->dirty_pages);
1783 inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1784 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
1785 if (IS_NOQUOTA(inode))
1786 inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
1787 }
1788
dec_page_count(struct f2fs_sb_info * sbi,int count_type)1789 static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1790 {
1791 atomic_dec(&sbi->nr_pages[count_type]);
1792 }
1793
inode_dec_dirty_pages(struct inode * inode)1794 static inline void inode_dec_dirty_pages(struct inode *inode)
1795 {
1796 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1797 !S_ISLNK(inode->i_mode))
1798 return;
1799
1800 atomic_dec(&F2FS_I(inode)->dirty_pages);
1801 dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1802 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
1803 if (IS_NOQUOTA(inode))
1804 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
1805 }
1806
get_pages(struct f2fs_sb_info * sbi,int count_type)1807 static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
1808 {
1809 return atomic_read(&sbi->nr_pages[count_type]);
1810 }
1811
get_dirty_pages(struct inode * inode)1812 static inline int get_dirty_pages(struct inode *inode)
1813 {
1814 return atomic_read(&F2FS_I(inode)->dirty_pages);
1815 }
1816
get_blocktype_secs(struct f2fs_sb_info * sbi,int block_type)1817 static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1818 {
1819 unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1820 unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1821 sbi->log_blocks_per_seg;
1822
1823 return segs / sbi->segs_per_sec;
1824 }
1825
valid_user_blocks(struct f2fs_sb_info * sbi)1826 static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1827 {
1828 return sbi->total_valid_block_count;
1829 }
1830
discard_blocks(struct f2fs_sb_info * sbi)1831 static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1832 {
1833 return sbi->discard_blks;
1834 }
1835
__bitmap_size(struct f2fs_sb_info * sbi,int flag)1836 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1837 {
1838 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1839
1840 /* return NAT or SIT bitmap */
1841 if (flag == NAT_BITMAP)
1842 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1843 else if (flag == SIT_BITMAP)
1844 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1845
1846 return 0;
1847 }
1848
__cp_payload(struct f2fs_sb_info * sbi)1849 static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1850 {
1851 return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1852 }
1853
__bitmap_ptr(struct f2fs_sb_info * sbi,int flag)1854 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1855 {
1856 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1857 int offset;
1858
1859 if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1860 offset = (flag == SIT_BITMAP) ?
1861 le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1862 return &ckpt->sit_nat_version_bitmap + offset;
1863 }
1864
1865 if (__cp_payload(sbi) > 0) {
1866 if (flag == NAT_BITMAP)
1867 return &ckpt->sit_nat_version_bitmap;
1868 else
1869 return (unsigned char *)ckpt + F2FS_BLKSIZE;
1870 } else {
1871 offset = (flag == NAT_BITMAP) ?
1872 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
1873 return &ckpt->sit_nat_version_bitmap + offset;
1874 }
1875 }
1876
__start_cp_addr(struct f2fs_sb_info * sbi)1877 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1878 {
1879 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1880
1881 if (sbi->cur_cp_pack == 2)
1882 start_addr += sbi->blocks_per_seg;
1883 return start_addr;
1884 }
1885
__start_cp_next_addr(struct f2fs_sb_info * sbi)1886 static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
1887 {
1888 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1889
1890 if (sbi->cur_cp_pack == 1)
1891 start_addr += sbi->blocks_per_seg;
1892 return start_addr;
1893 }
1894
__set_cp_next_pack(struct f2fs_sb_info * sbi)1895 static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
1896 {
1897 sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
1898 }
1899
__start_sum_addr(struct f2fs_sb_info * sbi)1900 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
1901 {
1902 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
1903 }
1904
inc_valid_node_count(struct f2fs_sb_info * sbi,struct inode * inode,bool is_inode)1905 static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1906 struct inode *inode, bool is_inode)
1907 {
1908 block_t valid_block_count;
1909 unsigned int valid_node_count;
1910 bool quota = inode && !is_inode;
1911
1912 if (quota) {
1913 int ret = dquot_reserve_block(inode, 1);
1914 if (ret)
1915 return ret;
1916 }
1917
1918 #ifdef CONFIG_F2FS_FAULT_INJECTION
1919 if (time_to_inject(sbi, FAULT_BLOCK)) {
1920 f2fs_show_injection_info(FAULT_BLOCK);
1921 goto enospc;
1922 }
1923 #endif
1924
1925 spin_lock(&sbi->stat_lock);
1926
1927 valid_block_count = sbi->total_valid_block_count +
1928 sbi->current_reserved_blocks + 1;
1929
1930 if (!__allow_reserved_blocks(sbi, inode, false))
1931 valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
1932
1933 if (unlikely(valid_block_count > sbi->user_block_count)) {
1934 spin_unlock(&sbi->stat_lock);
1935 goto enospc;
1936 }
1937
1938 valid_node_count = sbi->total_valid_node_count + 1;
1939 if (unlikely(valid_node_count > sbi->total_node_count)) {
1940 spin_unlock(&sbi->stat_lock);
1941 goto enospc;
1942 }
1943
1944 sbi->total_valid_node_count++;
1945 sbi->total_valid_block_count++;
1946 spin_unlock(&sbi->stat_lock);
1947
1948 if (inode) {
1949 if (is_inode)
1950 f2fs_mark_inode_dirty_sync(inode, true);
1951 else
1952 f2fs_i_blocks_write(inode, 1, true, true);
1953 }
1954
1955 percpu_counter_inc(&sbi->alloc_valid_block_count);
1956 return 0;
1957
1958 enospc:
1959 if (quota)
1960 dquot_release_reservation_block(inode, 1);
1961 return -ENOSPC;
1962 }
1963
dec_valid_node_count(struct f2fs_sb_info * sbi,struct inode * inode,bool is_inode)1964 static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1965 struct inode *inode, bool is_inode)
1966 {
1967 spin_lock(&sbi->stat_lock);
1968
1969 f2fs_bug_on(sbi, !sbi->total_valid_block_count);
1970 f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1971 f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
1972
1973 sbi->total_valid_node_count--;
1974 sbi->total_valid_block_count--;
1975 if (sbi->reserved_blocks &&
1976 sbi->current_reserved_blocks < sbi->reserved_blocks)
1977 sbi->current_reserved_blocks++;
1978
1979 spin_unlock(&sbi->stat_lock);
1980
1981 if (!is_inode)
1982 f2fs_i_blocks_write(inode, 1, false, true);
1983 }
1984
valid_node_count(struct f2fs_sb_info * sbi)1985 static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
1986 {
1987 return sbi->total_valid_node_count;
1988 }
1989
inc_valid_inode_count(struct f2fs_sb_info * sbi)1990 static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
1991 {
1992 percpu_counter_inc(&sbi->total_valid_inode_count);
1993 }
1994
dec_valid_inode_count(struct f2fs_sb_info * sbi)1995 static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
1996 {
1997 percpu_counter_dec(&sbi->total_valid_inode_count);
1998 }
1999
valid_inode_count(struct f2fs_sb_info * sbi)2000 static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
2001 {
2002 return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
2003 }
2004
f2fs_grab_cache_page(struct address_space * mapping,pgoff_t index,bool for_write)2005 static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2006 pgoff_t index, bool for_write)
2007 {
2008 #ifdef CONFIG_F2FS_FAULT_INJECTION
2009 struct page *page = find_lock_page(mapping, index);
2010
2011 if (page)
2012 return page;
2013
2014 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2015 f2fs_show_injection_info(FAULT_PAGE_ALLOC);
2016 return NULL;
2017 }
2018 #endif
2019 if (!for_write)
2020 return grab_cache_page(mapping, index);
2021 return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2022 }
2023
f2fs_pagecache_get_page(struct address_space * mapping,pgoff_t index,int fgp_flags,gfp_t gfp_mask)2024 static inline struct page *f2fs_pagecache_get_page(
2025 struct address_space *mapping, pgoff_t index,
2026 int fgp_flags, gfp_t gfp_mask)
2027 {
2028 #ifdef CONFIG_F2FS_FAULT_INJECTION
2029 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2030 f2fs_show_injection_info(FAULT_PAGE_GET);
2031 return NULL;
2032 }
2033 #endif
2034 return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
2035 }
2036
f2fs_copy_page(struct page * src,struct page * dst)2037 static inline void f2fs_copy_page(struct page *src, struct page *dst)
2038 {
2039 char *src_kaddr = kmap(src);
2040 char *dst_kaddr = kmap(dst);
2041
2042 memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
2043 kunmap(dst);
2044 kunmap(src);
2045 }
2046
f2fs_put_page(struct page * page,int unlock)2047 static inline void f2fs_put_page(struct page *page, int unlock)
2048 {
2049 if (!page)
2050 return;
2051
2052 if (unlock) {
2053 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
2054 unlock_page(page);
2055 }
2056 put_page(page);
2057 }
2058
f2fs_put_dnode(struct dnode_of_data * dn)2059 static inline void f2fs_put_dnode(struct dnode_of_data *dn)
2060 {
2061 if (dn->node_page)
2062 f2fs_put_page(dn->node_page, 1);
2063 if (dn->inode_page && dn->node_page != dn->inode_page)
2064 f2fs_put_page(dn->inode_page, 0);
2065 dn->node_page = NULL;
2066 dn->inode_page = NULL;
2067 }
2068
f2fs_kmem_cache_create(const char * name,size_t size)2069 static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2070 size_t size)
2071 {
2072 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
2073 }
2074
f2fs_kmem_cache_alloc(struct kmem_cache * cachep,gfp_t flags)2075 static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
2076 gfp_t flags)
2077 {
2078 void *entry;
2079
2080 entry = kmem_cache_alloc(cachep, flags);
2081 if (!entry)
2082 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
2083 return entry;
2084 }
2085
f2fs_bio_alloc(struct f2fs_sb_info * sbi,int npages,bool no_fail)2086 static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2087 int npages, bool no_fail)
2088 {
2089 struct bio *bio;
2090
2091 if (no_fail) {
2092 /* No failure on bio allocation */
2093 bio = bio_alloc(GFP_NOIO, npages);
2094 if (!bio)
2095 bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2096 return bio;
2097 }
2098 #ifdef CONFIG_F2FS_FAULT_INJECTION
2099 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2100 f2fs_show_injection_info(FAULT_ALLOC_BIO);
2101 return NULL;
2102 }
2103 #endif
2104 return bio_alloc(GFP_KERNEL, npages);
2105 }
2106
f2fs_radix_tree_insert(struct radix_tree_root * root,unsigned long index,void * item)2107 static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
2108 unsigned long index, void *item)
2109 {
2110 while (radix_tree_insert(root, index, item))
2111 cond_resched();
2112 }
2113
2114 #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
2115
IS_INODE(struct page * page)2116 static inline bool IS_INODE(struct page *page)
2117 {
2118 struct f2fs_node *p = F2FS_NODE(page);
2119
2120 return RAW_IS_INODE(p);
2121 }
2122
offset_in_addr(struct f2fs_inode * i)2123 static inline int offset_in_addr(struct f2fs_inode *i)
2124 {
2125 return (i->i_inline & F2FS_EXTRA_ATTR) ?
2126 (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
2127 }
2128
blkaddr_in_node(struct f2fs_node * node)2129 static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
2130 {
2131 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
2132 }
2133
2134 static inline int f2fs_has_extra_attr(struct inode *inode);
datablock_addr(struct inode * inode,struct page * node_page,unsigned int offset)2135 static inline block_t datablock_addr(struct inode *inode,
2136 struct page *node_page, unsigned int offset)
2137 {
2138 struct f2fs_node *raw_node;
2139 __le32 *addr_array;
2140 int base = 0;
2141 bool is_inode = IS_INODE(node_page);
2142
2143 raw_node = F2FS_NODE(node_page);
2144
2145 /* from GC path only */
2146 if (is_inode) {
2147 if (!inode)
2148 base = offset_in_addr(&raw_node->i);
2149 else if (f2fs_has_extra_attr(inode))
2150 base = get_extra_isize(inode);
2151 }
2152
2153 addr_array = blkaddr_in_node(raw_node);
2154 return le32_to_cpu(addr_array[base + offset]);
2155 }
2156
f2fs_test_bit(unsigned int nr,char * addr)2157 static inline int f2fs_test_bit(unsigned int nr, char *addr)
2158 {
2159 int mask;
2160
2161 addr += (nr >> 3);
2162 mask = 1 << (7 - (nr & 0x07));
2163 return mask & *addr;
2164 }
2165
f2fs_set_bit(unsigned int nr,char * addr)2166 static inline void f2fs_set_bit(unsigned int nr, char *addr)
2167 {
2168 int mask;
2169
2170 addr += (nr >> 3);
2171 mask = 1 << (7 - (nr & 0x07));
2172 *addr |= mask;
2173 }
2174
f2fs_clear_bit(unsigned int nr,char * addr)2175 static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2176 {
2177 int mask;
2178
2179 addr += (nr >> 3);
2180 mask = 1 << (7 - (nr & 0x07));
2181 *addr &= ~mask;
2182 }
2183
f2fs_test_and_set_bit(unsigned int nr,char * addr)2184 static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
2185 {
2186 int mask;
2187 int ret;
2188
2189 addr += (nr >> 3);
2190 mask = 1 << (7 - (nr & 0x07));
2191 ret = mask & *addr;
2192 *addr |= mask;
2193 return ret;
2194 }
2195
f2fs_test_and_clear_bit(unsigned int nr,char * addr)2196 static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
2197 {
2198 int mask;
2199 int ret;
2200
2201 addr += (nr >> 3);
2202 mask = 1 << (7 - (nr & 0x07));
2203 ret = mask & *addr;
2204 *addr &= ~mask;
2205 return ret;
2206 }
2207
f2fs_change_bit(unsigned int nr,char * addr)2208 static inline void f2fs_change_bit(unsigned int nr, char *addr)
2209 {
2210 int mask;
2211
2212 addr += (nr >> 3);
2213 mask = 1 << (7 - (nr & 0x07));
2214 *addr ^= mask;
2215 }
2216
2217 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
2218 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
2219 #define F2FS_FL_INHERITED (FS_PROJINHERIT_FL)
2220
f2fs_mask_flags(umode_t mode,__u32 flags)2221 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
2222 {
2223 if (S_ISDIR(mode))
2224 return flags;
2225 else if (S_ISREG(mode))
2226 return flags & F2FS_REG_FLMASK;
2227 else
2228 return flags & F2FS_OTHER_FLMASK;
2229 }
2230
2231 /* used for f2fs_inode_info->flags */
2232 enum {
2233 FI_NEW_INODE, /* indicate newly allocated inode */
2234 FI_DIRTY_INODE, /* indicate inode is dirty or not */
2235 FI_AUTO_RECOVER, /* indicate inode is recoverable */
2236 FI_DIRTY_DIR, /* indicate directory has dirty pages */
2237 FI_INC_LINK, /* need to increment i_nlink */
2238 FI_ACL_MODE, /* indicate acl mode */
2239 FI_NO_ALLOC, /* should not allocate any blocks */
2240 FI_FREE_NID, /* free allocated nide */
2241 FI_NO_EXTENT, /* not to use the extent cache */
2242 FI_INLINE_XATTR, /* used for inline xattr */
2243 FI_INLINE_DATA, /* used for inline data*/
2244 FI_INLINE_DENTRY, /* used for inline dentry */
2245 FI_APPEND_WRITE, /* inode has appended data */
2246 FI_UPDATE_WRITE, /* inode has in-place-update data */
2247 FI_NEED_IPU, /* used for ipu per file */
2248 FI_ATOMIC_FILE, /* indicate atomic file */
2249 FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */
2250 FI_VOLATILE_FILE, /* indicate volatile file */
2251 FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
2252 FI_DROP_CACHE, /* drop dirty page cache */
2253 FI_DATA_EXIST, /* indicate data exists */
2254 FI_INLINE_DOTS, /* indicate inline dot dentries */
2255 FI_DO_DEFRAG, /* indicate defragment is running */
2256 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
2257 FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
2258 FI_HOT_DATA, /* indicate file is hot */
2259 FI_EXTRA_ATTR, /* indicate file has extra attribute */
2260 FI_PROJ_INHERIT, /* indicate file inherits projectid */
2261 FI_PIN_FILE, /* indicate file should not be gced */
2262 };
2263
__mark_inode_dirty_flag(struct inode * inode,int flag,bool set)2264 static inline void __mark_inode_dirty_flag(struct inode *inode,
2265 int flag, bool set)
2266 {
2267 switch (flag) {
2268 case FI_INLINE_XATTR:
2269 case FI_INLINE_DATA:
2270 case FI_INLINE_DENTRY:
2271 case FI_NEW_INODE:
2272 if (set)
2273 return;
2274 case FI_DATA_EXIST:
2275 case FI_INLINE_DOTS:
2276 case FI_PIN_FILE:
2277 f2fs_mark_inode_dirty_sync(inode, true);
2278 }
2279 }
2280
set_inode_flag(struct inode * inode,int flag)2281 static inline void set_inode_flag(struct inode *inode, int flag)
2282 {
2283 if (!test_bit(flag, &F2FS_I(inode)->flags))
2284 set_bit(flag, &F2FS_I(inode)->flags);
2285 __mark_inode_dirty_flag(inode, flag, true);
2286 }
2287
is_inode_flag_set(struct inode * inode,int flag)2288 static inline int is_inode_flag_set(struct inode *inode, int flag)
2289 {
2290 return test_bit(flag, &F2FS_I(inode)->flags);
2291 }
2292
clear_inode_flag(struct inode * inode,int flag)2293 static inline void clear_inode_flag(struct inode *inode, int flag)
2294 {
2295 if (test_bit(flag, &F2FS_I(inode)->flags))
2296 clear_bit(flag, &F2FS_I(inode)->flags);
2297 __mark_inode_dirty_flag(inode, flag, false);
2298 }
2299
set_acl_inode(struct inode * inode,umode_t mode)2300 static inline void set_acl_inode(struct inode *inode, umode_t mode)
2301 {
2302 F2FS_I(inode)->i_acl_mode = mode;
2303 set_inode_flag(inode, FI_ACL_MODE);
2304 f2fs_mark_inode_dirty_sync(inode, false);
2305 }
2306
f2fs_i_links_write(struct inode * inode,bool inc)2307 static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2308 {
2309 if (inc)
2310 inc_nlink(inode);
2311 else
2312 drop_nlink(inode);
2313 f2fs_mark_inode_dirty_sync(inode, true);
2314 }
2315
f2fs_i_blocks_write(struct inode * inode,block_t diff,bool add,bool claim)2316 static inline void f2fs_i_blocks_write(struct inode *inode,
2317 block_t diff, bool add, bool claim)
2318 {
2319 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2320 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2321
2322 /* add = 1, claim = 1 should be dquot_reserve_block in pair */
2323 if (add) {
2324 if (claim)
2325 dquot_claim_block(inode, diff);
2326 else
2327 dquot_alloc_block_nofail(inode, diff);
2328 } else {
2329 dquot_free_block(inode, diff);
2330 }
2331
2332 f2fs_mark_inode_dirty_sync(inode, true);
2333 if (clean || recover)
2334 set_inode_flag(inode, FI_AUTO_RECOVER);
2335 }
2336
f2fs_i_size_write(struct inode * inode,loff_t i_size)2337 static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2338 {
2339 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2340 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2341
2342 if (i_size_read(inode) == i_size)
2343 return;
2344
2345 i_size_write(inode, i_size);
2346 f2fs_mark_inode_dirty_sync(inode, true);
2347 if (clean || recover)
2348 set_inode_flag(inode, FI_AUTO_RECOVER);
2349 }
2350
f2fs_i_depth_write(struct inode * inode,unsigned int depth)2351 static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2352 {
2353 F2FS_I(inode)->i_current_depth = depth;
2354 f2fs_mark_inode_dirty_sync(inode, true);
2355 }
2356
f2fs_i_gc_failures_write(struct inode * inode,unsigned int count)2357 static inline void f2fs_i_gc_failures_write(struct inode *inode,
2358 unsigned int count)
2359 {
2360 F2FS_I(inode)->i_gc_failures = count;
2361 f2fs_mark_inode_dirty_sync(inode, true);
2362 }
2363
f2fs_i_xnid_write(struct inode * inode,nid_t xnid)2364 static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2365 {
2366 F2FS_I(inode)->i_xattr_nid = xnid;
2367 f2fs_mark_inode_dirty_sync(inode, true);
2368 }
2369
f2fs_i_pino_write(struct inode * inode,nid_t pino)2370 static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2371 {
2372 F2FS_I(inode)->i_pino = pino;
2373 f2fs_mark_inode_dirty_sync(inode, true);
2374 }
2375
get_inline_info(struct inode * inode,struct f2fs_inode * ri)2376 static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2377 {
2378 struct f2fs_inode_info *fi = F2FS_I(inode);
2379
2380 if (ri->i_inline & F2FS_INLINE_XATTR)
2381 set_bit(FI_INLINE_XATTR, &fi->flags);
2382 if (ri->i_inline & F2FS_INLINE_DATA)
2383 set_bit(FI_INLINE_DATA, &fi->flags);
2384 if (ri->i_inline & F2FS_INLINE_DENTRY)
2385 set_bit(FI_INLINE_DENTRY, &fi->flags);
2386 if (ri->i_inline & F2FS_DATA_EXIST)
2387 set_bit(FI_DATA_EXIST, &fi->flags);
2388 if (ri->i_inline & F2FS_INLINE_DOTS)
2389 set_bit(FI_INLINE_DOTS, &fi->flags);
2390 if (ri->i_inline & F2FS_EXTRA_ATTR)
2391 set_bit(FI_EXTRA_ATTR, &fi->flags);
2392 if (ri->i_inline & F2FS_PIN_FILE)
2393 set_bit(FI_PIN_FILE, &fi->flags);
2394 }
2395
set_raw_inline(struct inode * inode,struct f2fs_inode * ri)2396 static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2397 {
2398 ri->i_inline = 0;
2399
2400 if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2401 ri->i_inline |= F2FS_INLINE_XATTR;
2402 if (is_inode_flag_set(inode, FI_INLINE_DATA))
2403 ri->i_inline |= F2FS_INLINE_DATA;
2404 if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
2405 ri->i_inline |= F2FS_INLINE_DENTRY;
2406 if (is_inode_flag_set(inode, FI_DATA_EXIST))
2407 ri->i_inline |= F2FS_DATA_EXIST;
2408 if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2409 ri->i_inline |= F2FS_INLINE_DOTS;
2410 if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
2411 ri->i_inline |= F2FS_EXTRA_ATTR;
2412 if (is_inode_flag_set(inode, FI_PIN_FILE))
2413 ri->i_inline |= F2FS_PIN_FILE;
2414 }
2415
f2fs_has_extra_attr(struct inode * inode)2416 static inline int f2fs_has_extra_attr(struct inode *inode)
2417 {
2418 return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2419 }
2420
f2fs_has_inline_xattr(struct inode * inode)2421 static inline int f2fs_has_inline_xattr(struct inode *inode)
2422 {
2423 return is_inode_flag_set(inode, FI_INLINE_XATTR);
2424 }
2425
addrs_per_inode(struct inode * inode)2426 static inline unsigned int addrs_per_inode(struct inode *inode)
2427 {
2428 return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2429 }
2430
inline_xattr_addr(struct inode * inode,struct page * page)2431 static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
2432 {
2433 struct f2fs_inode *ri = F2FS_INODE(page);
2434
2435 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2436 get_inline_xattr_addrs(inode)]);
2437 }
2438
inline_xattr_size(struct inode * inode)2439 static inline int inline_xattr_size(struct inode *inode)
2440 {
2441 return get_inline_xattr_addrs(inode) * sizeof(__le32);
2442 }
2443
f2fs_has_inline_data(struct inode * inode)2444 static inline int f2fs_has_inline_data(struct inode *inode)
2445 {
2446 return is_inode_flag_set(inode, FI_INLINE_DATA);
2447 }
2448
f2fs_exist_data(struct inode * inode)2449 static inline int f2fs_exist_data(struct inode *inode)
2450 {
2451 return is_inode_flag_set(inode, FI_DATA_EXIST);
2452 }
2453
f2fs_has_inline_dots(struct inode * inode)2454 static inline int f2fs_has_inline_dots(struct inode *inode)
2455 {
2456 return is_inode_flag_set(inode, FI_INLINE_DOTS);
2457 }
2458
f2fs_is_pinned_file(struct inode * inode)2459 static inline bool f2fs_is_pinned_file(struct inode *inode)
2460 {
2461 return is_inode_flag_set(inode, FI_PIN_FILE);
2462 }
2463
f2fs_is_atomic_file(struct inode * inode)2464 static inline bool f2fs_is_atomic_file(struct inode *inode)
2465 {
2466 return is_inode_flag_set(inode, FI_ATOMIC_FILE);
2467 }
2468
f2fs_is_commit_atomic_write(struct inode * inode)2469 static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
2470 {
2471 return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
2472 }
2473
f2fs_is_volatile_file(struct inode * inode)2474 static inline bool f2fs_is_volatile_file(struct inode *inode)
2475 {
2476 return is_inode_flag_set(inode, FI_VOLATILE_FILE);
2477 }
2478
f2fs_is_first_block_written(struct inode * inode)2479 static inline bool f2fs_is_first_block_written(struct inode *inode)
2480 {
2481 return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
2482 }
2483
f2fs_is_drop_cache(struct inode * inode)2484 static inline bool f2fs_is_drop_cache(struct inode *inode)
2485 {
2486 return is_inode_flag_set(inode, FI_DROP_CACHE);
2487 }
2488
inline_data_addr(struct inode * inode,struct page * page)2489 static inline void *inline_data_addr(struct inode *inode, struct page *page)
2490 {
2491 struct f2fs_inode *ri = F2FS_INODE(page);
2492 int extra_size = get_extra_isize(inode);
2493
2494 return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
2495 }
2496
f2fs_has_inline_dentry(struct inode * inode)2497 static inline int f2fs_has_inline_dentry(struct inode *inode)
2498 {
2499 return is_inode_flag_set(inode, FI_INLINE_DENTRY);
2500 }
2501
is_file(struct inode * inode,int type)2502 static inline int is_file(struct inode *inode, int type)
2503 {
2504 return F2FS_I(inode)->i_advise & type;
2505 }
2506
set_file(struct inode * inode,int type)2507 static inline void set_file(struct inode *inode, int type)
2508 {
2509 F2FS_I(inode)->i_advise |= type;
2510 f2fs_mark_inode_dirty_sync(inode, true);
2511 }
2512
clear_file(struct inode * inode,int type)2513 static inline void clear_file(struct inode *inode, int type)
2514 {
2515 F2FS_I(inode)->i_advise &= ~type;
2516 f2fs_mark_inode_dirty_sync(inode, true);
2517 }
2518
f2fs_skip_inode_update(struct inode * inode,int dsync)2519 static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2520 {
2521 bool ret;
2522
2523 if (dsync) {
2524 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2525
2526 spin_lock(&sbi->inode_lock[DIRTY_META]);
2527 ret = list_empty(&F2FS_I(inode)->gdirty_list);
2528 spin_unlock(&sbi->inode_lock[DIRTY_META]);
2529 return ret;
2530 }
2531 if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2532 file_keep_isize(inode) ||
2533 i_size_read(inode) & ~PAGE_MASK)
2534 return false;
2535
2536 if (!timespec_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2537 return false;
2538 if (!timespec_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2539 return false;
2540 if (!timespec_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2541 return false;
2542 if (!timespec_equal(F2FS_I(inode)->i_disk_time + 3,
2543 &F2FS_I(inode)->i_crtime))
2544 return false;
2545
2546 down_read(&F2FS_I(inode)->i_sem);
2547 ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2548 up_read(&F2FS_I(inode)->i_sem);
2549
2550 return ret;
2551 }
2552
f2fs_readonly(struct super_block * sb)2553 static inline bool f2fs_readonly(struct super_block *sb)
2554 {
2555 return sb->s_flags & MS_RDONLY;
2556 }
2557
f2fs_cp_error(struct f2fs_sb_info * sbi)2558 static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2559 {
2560 return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2561 }
2562
is_dot_dotdot(const struct qstr * str)2563 static inline bool is_dot_dotdot(const struct qstr *str)
2564 {
2565 if (str->len == 1 && str->name[0] == '.')
2566 return true;
2567
2568 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2569 return true;
2570
2571 return false;
2572 }
2573
f2fs_may_extent_tree(struct inode * inode)2574 static inline bool f2fs_may_extent_tree(struct inode *inode)
2575 {
2576 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
2577 is_inode_flag_set(inode, FI_NO_EXTENT))
2578 return false;
2579
2580 return S_ISREG(inode->i_mode);
2581 }
2582
f2fs_kmalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)2583 static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2584 size_t size, gfp_t flags)
2585 {
2586 #ifdef CONFIG_F2FS_FAULT_INJECTION
2587 if (time_to_inject(sbi, FAULT_KMALLOC)) {
2588 f2fs_show_injection_info(FAULT_KMALLOC);
2589 return NULL;
2590 }
2591 #endif
2592 return kmalloc(size, flags);
2593 }
2594
kvmalloc(size_t size,gfp_t flags)2595 static inline void *kvmalloc(size_t size, gfp_t flags)
2596 {
2597 void *ret;
2598
2599 ret = kmalloc(size, flags | __GFP_NOWARN);
2600 if (!ret)
2601 ret = __vmalloc(size, flags, PAGE_KERNEL);
2602 return ret;
2603 }
2604
kvzalloc(size_t size,gfp_t flags)2605 static inline void *kvzalloc(size_t size, gfp_t flags)
2606 {
2607 void *ret;
2608
2609 ret = kzalloc(size, flags | __GFP_NOWARN);
2610 if (!ret)
2611 ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
2612 return ret;
2613 }
2614
f2fs_kzalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)2615 static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2616 size_t size, gfp_t flags)
2617 {
2618 return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2619 }
2620
f2fs_kvmalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)2621 static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2622 size_t size, gfp_t flags)
2623 {
2624 #ifdef CONFIG_F2FS_FAULT_INJECTION
2625 if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2626 f2fs_show_injection_info(FAULT_KVMALLOC);
2627 return NULL;
2628 }
2629 #endif
2630 return kvmalloc(size, flags);
2631 }
2632
f2fs_kvzalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)2633 static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2634 size_t size, gfp_t flags)
2635 {
2636 return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2637 }
2638
get_extra_isize(struct inode * inode)2639 static inline int get_extra_isize(struct inode *inode)
2640 {
2641 return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2642 }
2643
get_inline_xattr_addrs(struct inode * inode)2644 static inline int get_inline_xattr_addrs(struct inode *inode)
2645 {
2646 return F2FS_I(inode)->i_inline_xattr_size;
2647 }
2648
2649 #define get_inode_mode(i) \
2650 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2651 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2652
2653 #define F2FS_TOTAL_EXTRA_ATTR_SIZE \
2654 (offsetof(struct f2fs_inode, i_extra_end) - \
2655 offsetof(struct f2fs_inode, i_extra_isize)) \
2656
2657 #define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr))
2658 #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \
2659 ((offsetof(typeof(*f2fs_inode), field) + \
2660 sizeof((f2fs_inode)->field)) \
2661 <= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize)) \
2662
f2fs_reset_iostat(struct f2fs_sb_info * sbi)2663 static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2664 {
2665 int i;
2666
2667 spin_lock(&sbi->iostat_lock);
2668 for (i = 0; i < NR_IO_TYPE; i++)
2669 sbi->write_iostat[i] = 0;
2670 spin_unlock(&sbi->iostat_lock);
2671 }
2672
f2fs_update_iostat(struct f2fs_sb_info * sbi,enum iostat_type type,unsigned long long io_bytes)2673 static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2674 enum iostat_type type, unsigned long long io_bytes)
2675 {
2676 if (!sbi->iostat_enable)
2677 return;
2678 spin_lock(&sbi->iostat_lock);
2679 sbi->write_iostat[type] += io_bytes;
2680
2681 if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2682 sbi->write_iostat[APP_BUFFERED_IO] =
2683 sbi->write_iostat[APP_WRITE_IO] -
2684 sbi->write_iostat[APP_DIRECT_IO];
2685 spin_unlock(&sbi->iostat_lock);
2686 }
2687
2688 /*
2689 * file.c
2690 */
2691 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2692 void truncate_data_blocks(struct dnode_of_data *dn);
2693 int truncate_blocks(struct inode *inode, u64 from, bool lock);
2694 int f2fs_truncate(struct inode *inode);
2695 int f2fs_getattr(struct vfsmount *mnt, struct dentry *dentry,
2696 struct kstat *stat);
2697 int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2698 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2699 void truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2700 int f2fs_precache_extents(struct inode *inode);
2701 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2702 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
2703 int f2fs_pin_file_control(struct inode *inode, bool inc);
2704
2705 /*
2706 * inode.c
2707 */
2708 void f2fs_set_inode_flags(struct inode *inode);
2709 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2710 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2711 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2712 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2713 int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2714 void update_inode(struct inode *inode, struct page *node_page);
2715 void update_inode_page(struct inode *inode);
2716 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2717 void f2fs_evict_inode(struct inode *inode);
2718 void handle_failed_inode(struct inode *inode);
2719
2720 /*
2721 * namei.c
2722 */
2723 int update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2724 bool hot, bool set);
2725 struct dentry *f2fs_get_parent(struct dentry *child);
2726
2727 /*
2728 * dir.c
2729 */
2730 void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2731 unsigned char get_de_type(struct f2fs_dir_entry *de);
2732 struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2733 f2fs_hash_t namehash, int *max_slots,
2734 struct f2fs_dentry_ptr *d);
2735 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2736 unsigned int start_pos, struct fscrypt_str *fstr);
2737 void do_make_empty_dir(struct inode *inode, struct inode *parent,
2738 struct f2fs_dentry_ptr *d);
2739 struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2740 const struct qstr *new_name,
2741 const struct qstr *orig_name, struct page *dpage);
2742 void update_parent_metadata(struct inode *dir, struct inode *inode,
2743 unsigned int current_depth);
2744 int room_for_filename(const void *bitmap, int slots, int max_slots);
2745 void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2746 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2747 struct fscrypt_name *fname, struct page **res_page);
2748 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2749 const struct qstr *child, struct page **res_page);
2750 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2751 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2752 struct page **page);
2753 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2754 struct page *page, struct inode *inode);
2755 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2756 const struct qstr *name, f2fs_hash_t name_hash,
2757 unsigned int bit_pos);
2758 int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2759 const struct qstr *orig_name,
2760 struct inode *inode, nid_t ino, umode_t mode);
2761 int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2762 struct inode *inode, nid_t ino, umode_t mode);
2763 int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2764 struct inode *inode, nid_t ino, umode_t mode);
2765 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2766 struct inode *dir, struct inode *inode);
2767 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2768 bool f2fs_empty_dir(struct inode *dir);
2769
f2fs_add_link(struct dentry * dentry,struct inode * inode)2770 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2771 {
2772 return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2773 inode, inode->i_ino, inode->i_mode);
2774 }
2775
2776 /*
2777 * super.c
2778 */
2779 int f2fs_inode_dirtied(struct inode *inode, bool sync);
2780 void f2fs_inode_synced(struct inode *inode);
2781 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
2782 void f2fs_quota_off_umount(struct super_block *sb);
2783 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2784 int f2fs_sync_fs(struct super_block *sb, int sync);
2785 extern __printf(3, 4)
2786 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2787 int sanity_check_ckpt(struct f2fs_sb_info *sbi);
2788
2789 /*
2790 * hash.c
2791 */
2792 f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
2793 struct fscrypt_name *fname);
2794
2795 /*
2796 * node.c
2797 */
2798 struct dnode_of_data;
2799 struct node_info;
2800
2801 bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2802 int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2803 bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2804 bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2805 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2806 pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2807 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2808 int truncate_inode_blocks(struct inode *inode, pgoff_t from);
2809 int truncate_xattr_node(struct inode *inode);
2810 int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2811 int remove_inode_page(struct inode *inode);
2812 struct page *new_inode_page(struct inode *inode);
2813 struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2814 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2815 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2816 struct page *get_node_page_ra(struct page *parent, int start);
2817 void move_node_page(struct page *node_page, int gc_type);
2818 int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2819 struct writeback_control *wbc, bool atomic);
2820 int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2821 bool do_balance, enum iostat_type io_type);
2822 void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2823 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2824 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2825 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2826 int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2827 void recover_inline_xattr(struct inode *inode, struct page *page);
2828 int recover_xattr_data(struct inode *inode, struct page *page);
2829 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2830 void restore_node_summary(struct f2fs_sb_info *sbi,
2831 unsigned int segno, struct f2fs_summary_block *sum);
2832 void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2833 int build_node_manager(struct f2fs_sb_info *sbi);
2834 void destroy_node_manager(struct f2fs_sb_info *sbi);
2835 int __init create_node_manager_caches(void);
2836 void destroy_node_manager_caches(void);
2837
2838 /*
2839 * segment.c
2840 */
2841 bool need_SSR(struct f2fs_sb_info *sbi);
2842 void register_inmem_page(struct inode *inode, struct page *page);
2843 void drop_inmem_pages_all(struct f2fs_sb_info *sbi);
2844 void drop_inmem_pages(struct inode *inode);
2845 void drop_inmem_page(struct inode *inode, struct page *page);
2846 int commit_inmem_pages(struct inode *inode);
2847 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2848 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
2849 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
2850 int create_flush_cmd_control(struct f2fs_sb_info *sbi);
2851 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
2852 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2853 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2854 bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2855 void drop_discard_cmd(struct f2fs_sb_info *sbi);
2856 void stop_discard_thread(struct f2fs_sb_info *sbi);
2857 bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2858 void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2859 void release_discard_addrs(struct f2fs_sb_info *sbi);
2860 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2861 void allocate_new_segments(struct f2fs_sb_info *sbi);
2862 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2863 bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2864 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2865 void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2866 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2867 enum iostat_type io_type);
2868 void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2869 void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2870 int rewrite_data_page(struct f2fs_io_info *fio);
2871 void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2872 block_t old_blkaddr, block_t new_blkaddr,
2873 bool recover_curseg, bool recover_newaddr);
2874 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2875 block_t old_addr, block_t new_addr,
2876 unsigned char version, bool recover_curseg,
2877 bool recover_newaddr);
2878 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2879 block_t old_blkaddr, block_t *new_blkaddr,
2880 struct f2fs_summary *sum, int type,
2881 struct f2fs_io_info *fio, bool add_list);
2882 void f2fs_wait_on_page_writeback(struct page *page,
2883 enum page_type type, bool ordered);
2884 void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2885 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2886 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2887 int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2888 unsigned int val, int alloc);
2889 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2890 int build_segment_manager(struct f2fs_sb_info *sbi);
2891 void destroy_segment_manager(struct f2fs_sb_info *sbi);
2892 int __init create_segment_manager_caches(void);
2893 void destroy_segment_manager_caches(void);
2894 int rw_hint_to_seg_type(enum rw_hint hint);
2895 enum rw_hint io_type_to_rw_hint(struct f2fs_sb_info *sbi, enum page_type type,
2896 enum temp_type temp);
2897
2898 /*
2899 * checkpoint.c
2900 */
2901 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2902 struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2903 struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2904 struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2905 bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2906 int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2907 int type, bool sync);
2908 void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2909 long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2910 long nr_to_write, enum iostat_type io_type);
2911 void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2912 void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2913 void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2914 bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2915 void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
2916 unsigned int devidx, int type);
2917 bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
2918 unsigned int devidx, int type);
2919 int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2920 int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2921 void release_orphan_inode(struct f2fs_sb_info *sbi);
2922 void add_orphan_inode(struct inode *inode);
2923 void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2924 int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2925 int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2926 void update_dirty_page(struct inode *inode, struct page *page);
2927 void remove_dirty_inode(struct inode *inode);
2928 int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2929 int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2930 void init_ino_entry_info(struct f2fs_sb_info *sbi);
2931 int __init create_checkpoint_caches(void);
2932 void destroy_checkpoint_caches(void);
2933
2934 /*
2935 * data.c
2936 */
2937 int f2fs_init_post_read_processing(void);
2938 void f2fs_destroy_post_read_processing(void);
2939 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2940 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2941 struct inode *inode, nid_t ino, pgoff_t idx,
2942 enum page_type type);
2943 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2944 int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2945 int f2fs_submit_page_write(struct f2fs_io_info *fio);
2946 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2947 block_t blk_addr, struct bio *bio);
2948 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2949 void set_data_blkaddr(struct dnode_of_data *dn);
2950 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2951 int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2952 int reserve_new_block(struct dnode_of_data *dn);
2953 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2954 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2955 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2956 struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2957 int op_flags, bool for_write);
2958 struct page *find_data_page(struct inode *inode, pgoff_t index);
2959 struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2960 bool for_write);
2961 struct page *get_new_data_page(struct inode *inode,
2962 struct page *ipage, pgoff_t index, bool new_i_size);
2963 int do_write_data_page(struct f2fs_io_info *fio);
2964 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2965 int create, int flag);
2966 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2967 u64 start, u64 len);
2968 bool should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
2969 bool should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
2970 int __f2fs_write_data_pages(struct address_space *mapping,
2971 struct writeback_control *wbc,
2972 enum iostat_type io_type);
2973 void f2fs_invalidate_page(struct page *page, unsigned int offset,
2974 unsigned int length);
2975 int f2fs_release_page(struct page *page, gfp_t wait);
2976 #ifdef CONFIG_MIGRATION
2977 int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2978 struct page *page, enum migrate_mode mode);
2979 #endif
2980 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
2981
2982 /*
2983 * gc.c
2984 */
2985 int start_gc_thread(struct f2fs_sb_info *sbi);
2986 void stop_gc_thread(struct f2fs_sb_info *sbi);
2987 block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2988 int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2989 unsigned int segno);
2990 void build_gc_manager(struct f2fs_sb_info *sbi);
2991
2992 /*
2993 * recovery.c
2994 */
2995 int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2996 bool space_for_roll_forward(struct f2fs_sb_info *sbi);
2997
2998 /*
2999 * debug.c
3000 */
3001 #ifdef CONFIG_F2FS_STAT_FS
3002 struct f2fs_stat_info {
3003 struct list_head stat_list;
3004 struct f2fs_sb_info *sbi;
3005 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
3006 int main_area_segs, main_area_sections, main_area_zones;
3007 unsigned long long hit_largest, hit_cached, hit_rbtree;
3008 unsigned long long hit_total, total_ext;
3009 int ext_tree, zombie_tree, ext_node;
3010 int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
3011 int ndirty_data, ndirty_qdata;
3012 int inmem_pages;
3013 unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
3014 int nats, dirty_nats, sits, dirty_sits;
3015 int free_nids, avail_nids, alloc_nids;
3016 int total_count, utilization;
3017 int bg_gc, nr_wb_cp_data, nr_wb_data;
3018 int nr_flushing, nr_flushed, flush_list_empty;
3019 int nr_discarding, nr_discarded;
3020 int nr_discard_cmd;
3021 unsigned int undiscard_blks;
3022 int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3023 int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3024 unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
3025 unsigned int bimodal, avg_vblocks;
3026 int util_free, util_valid, util_invalid;
3027 int rsvd_segs, overp_segs;
3028 int dirty_count, node_pages, meta_pages;
3029 int prefree_count, call_count, cp_count, bg_cp_count;
3030 int tot_segs, node_segs, data_segs, free_segs, free_secs;
3031 int bg_node_segs, bg_data_segs;
3032 int tot_blks, data_blks, node_blks;
3033 int bg_data_blks, bg_node_blks;
3034 int curseg[NR_CURSEG_TYPE];
3035 int cursec[NR_CURSEG_TYPE];
3036 int curzone[NR_CURSEG_TYPE];
3037
3038 unsigned int segment_count[2];
3039 unsigned int block_count[2];
3040 unsigned int inplace_count;
3041 unsigned long long base_mem, cache_mem, page_mem;
3042 };
3043
F2FS_STAT(struct f2fs_sb_info * sbi)3044 static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3045 {
3046 return (struct f2fs_stat_info *)sbi->stat_info;
3047 }
3048
3049 #define stat_inc_cp_count(si) ((si)->cp_count++)
3050 #define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
3051 #define stat_inc_call_count(si) ((si)->call_count++)
3052 #define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
3053 #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
3054 #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
3055 #define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
3056 #define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
3057 #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
3058 #define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
3059 #define stat_inc_inline_xattr(inode) \
3060 do { \
3061 if (f2fs_has_inline_xattr(inode)) \
3062 (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
3063 } while (0)
3064 #define stat_dec_inline_xattr(inode) \
3065 do { \
3066 if (f2fs_has_inline_xattr(inode)) \
3067 (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
3068 } while (0)
3069 #define stat_inc_inline_inode(inode) \
3070 do { \
3071 if (f2fs_has_inline_data(inode)) \
3072 (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
3073 } while (0)
3074 #define stat_dec_inline_inode(inode) \
3075 do { \
3076 if (f2fs_has_inline_data(inode)) \
3077 (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
3078 } while (0)
3079 #define stat_inc_inline_dir(inode) \
3080 do { \
3081 if (f2fs_has_inline_dentry(inode)) \
3082 (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
3083 } while (0)
3084 #define stat_dec_inline_dir(inode) \
3085 do { \
3086 if (f2fs_has_inline_dentry(inode)) \
3087 (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
3088 } while (0)
3089 #define stat_inc_seg_type(sbi, curseg) \
3090 ((sbi)->segment_count[(curseg)->alloc_type]++)
3091 #define stat_inc_block_count(sbi, curseg) \
3092 ((sbi)->block_count[(curseg)->alloc_type]++)
3093 #define stat_inc_inplace_blocks(sbi) \
3094 (atomic_inc(&(sbi)->inplace_count))
3095 #define stat_inc_atomic_write(inode) \
3096 (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
3097 #define stat_dec_atomic_write(inode) \
3098 (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
3099 #define stat_update_max_atomic_write(inode) \
3100 do { \
3101 int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
3102 int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
3103 if (cur > max) \
3104 atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
3105 } while (0)
3106 #define stat_inc_volatile_write(inode) \
3107 (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3108 #define stat_dec_volatile_write(inode) \
3109 (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3110 #define stat_update_max_volatile_write(inode) \
3111 do { \
3112 int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
3113 int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
3114 if (cur > max) \
3115 atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
3116 } while (0)
3117 #define stat_inc_seg_count(sbi, type, gc_type) \
3118 do { \
3119 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
3120 si->tot_segs++; \
3121 if ((type) == SUM_TYPE_DATA) { \
3122 si->data_segs++; \
3123 si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
3124 } else { \
3125 si->node_segs++; \
3126 si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
3127 } \
3128 } while (0)
3129
3130 #define stat_inc_tot_blk_count(si, blks) \
3131 ((si)->tot_blks += (blks))
3132
3133 #define stat_inc_data_blk_count(sbi, blks, gc_type) \
3134 do { \
3135 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
3136 stat_inc_tot_blk_count(si, blks); \
3137 si->data_blks += (blks); \
3138 si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
3139 } while (0)
3140
3141 #define stat_inc_node_blk_count(sbi, blks, gc_type) \
3142 do { \
3143 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
3144 stat_inc_tot_blk_count(si, blks); \
3145 si->node_blks += (blks); \
3146 si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
3147 } while (0)
3148
3149 int f2fs_build_stats(struct f2fs_sb_info *sbi);
3150 void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3151 int __init f2fs_create_root_stats(void);
3152 void f2fs_destroy_root_stats(void);
3153 #else
3154 #define stat_inc_cp_count(si) do { } while (0)
3155 #define stat_inc_bg_cp_count(si) do { } while (0)
3156 #define stat_inc_call_count(si) do { } while (0)
3157 #define stat_inc_bggc_count(si) do { } while (0)
3158 #define stat_inc_dirty_inode(sbi, type) do { } while (0)
3159 #define stat_dec_dirty_inode(sbi, type) do { } while (0)
3160 #define stat_inc_total_hit(sb) do { } while (0)
3161 #define stat_inc_rbtree_node_hit(sb) do { } while (0)
3162 #define stat_inc_largest_node_hit(sbi) do { } while (0)
3163 #define stat_inc_cached_node_hit(sbi) do { } while (0)
3164 #define stat_inc_inline_xattr(inode) do { } while (0)
3165 #define stat_dec_inline_xattr(inode) do { } while (0)
3166 #define stat_inc_inline_inode(inode) do { } while (0)
3167 #define stat_dec_inline_inode(inode) do { } while (0)
3168 #define stat_inc_inline_dir(inode) do { } while (0)
3169 #define stat_dec_inline_dir(inode) do { } while (0)
3170 #define stat_inc_atomic_write(inode) do { } while (0)
3171 #define stat_dec_atomic_write(inode) do { } while (0)
3172 #define stat_update_max_atomic_write(inode) do { } while (0)
3173 #define stat_inc_volatile_write(inode) do { } while (0)
3174 #define stat_dec_volatile_write(inode) do { } while (0)
3175 #define stat_update_max_volatile_write(inode) do { } while (0)
3176 #define stat_inc_seg_type(sbi, curseg) do { } while (0)
3177 #define stat_inc_block_count(sbi, curseg) do { } while (0)
3178 #define stat_inc_inplace_blocks(sbi) do { } while (0)
3179 #define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
3180 #define stat_inc_tot_blk_count(si, blks) do { } while (0)
3181 #define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
3182 #define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
3183
f2fs_build_stats(struct f2fs_sb_info * sbi)3184 static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
f2fs_destroy_stats(struct f2fs_sb_info * sbi)3185 static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
f2fs_create_root_stats(void)3186 static inline int __init f2fs_create_root_stats(void) { return 0; }
f2fs_destroy_root_stats(void)3187 static inline void f2fs_destroy_root_stats(void) { }
3188 #endif
3189
3190 extern const struct file_operations f2fs_dir_operations;
3191 extern const struct file_operations f2fs_file_operations;
3192 extern const struct inode_operations f2fs_file_inode_operations;
3193 extern const struct address_space_operations f2fs_dblock_aops;
3194 extern const struct address_space_operations f2fs_node_aops;
3195 extern const struct address_space_operations f2fs_meta_aops;
3196 extern const struct inode_operations f2fs_dir_inode_operations;
3197 extern const struct inode_operations f2fs_symlink_inode_operations;
3198 extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
3199 extern const struct inode_operations f2fs_special_inode_operations;
3200 extern struct kmem_cache *inode_entry_slab;
3201
3202 /*
3203 * inline.c
3204 */
3205 bool f2fs_may_inline_data(struct inode *inode);
3206 bool f2fs_may_inline_dentry(struct inode *inode);
3207 void read_inline_data(struct page *page, struct page *ipage);
3208 void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
3209 int f2fs_read_inline_data(struct inode *inode, struct page *page);
3210 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3211 int f2fs_convert_inline_inode(struct inode *inode);
3212 int f2fs_write_inline_data(struct inode *inode, struct page *page);
3213 bool recover_inline_data(struct inode *inode, struct page *npage);
3214 struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
3215 struct fscrypt_name *fname, struct page **res_page);
3216 int make_empty_inline_dir(struct inode *inode, struct inode *parent,
3217 struct page *ipage);
3218 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3219 const struct qstr *orig_name,
3220 struct inode *inode, nid_t ino, umode_t mode);
3221 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
3222 struct inode *dir, struct inode *inode);
3223 bool f2fs_empty_inline_dir(struct inode *dir);
3224 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3225 struct fscrypt_str *fstr);
3226 int f2fs_inline_data_fiemap(struct inode *inode,
3227 struct fiemap_extent_info *fieinfo,
3228 __u64 start, __u64 len);
3229
3230 /*
3231 * shrinker.c
3232 */
3233 unsigned long f2fs_shrink_count(struct shrinker *shrink,
3234 struct shrink_control *sc);
3235 unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3236 struct shrink_control *sc);
3237 void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3238 void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
3239
3240 /*
3241 * extent_cache.c
3242 */
3243 struct rb_entry *__lookup_rb_tree(struct rb_root *root,
3244 struct rb_entry *cached_re, unsigned int ofs);
3245 struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3246 struct rb_root *root, struct rb_node **parent,
3247 unsigned int ofs);
3248 struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
3249 struct rb_entry *cached_re, unsigned int ofs,
3250 struct rb_entry **prev_entry, struct rb_entry **next_entry,
3251 struct rb_node ***insert_p, struct rb_node **insert_parent,
3252 bool force);
3253 bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3254 struct rb_root *root);
3255 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3256 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3257 void f2fs_drop_extent_tree(struct inode *inode);
3258 unsigned int f2fs_destroy_extent_node(struct inode *inode);
3259 void f2fs_destroy_extent_tree(struct inode *inode);
3260 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3261 struct extent_info *ei);
3262 void f2fs_update_extent_cache(struct dnode_of_data *dn);
3263 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3264 pgoff_t fofs, block_t blkaddr, unsigned int len);
3265 void init_extent_cache_info(struct f2fs_sb_info *sbi);
3266 int __init create_extent_cache(void);
3267 void destroy_extent_cache(void);
3268
3269 /*
3270 * sysfs.c
3271 */
3272 int __init f2fs_init_sysfs(void);
3273 void f2fs_exit_sysfs(void);
3274 int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3275 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
3276
3277 /*
3278 * crypto support
3279 */
f2fs_encrypted_inode(struct inode * inode)3280 static inline bool f2fs_encrypted_inode(struct inode *inode)
3281 {
3282 return file_is_encrypt(inode);
3283 }
3284
f2fs_encrypted_file(struct inode * inode)3285 static inline bool f2fs_encrypted_file(struct inode *inode)
3286 {
3287 return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
3288 }
3289
f2fs_set_encrypted_inode(struct inode * inode)3290 static inline void f2fs_set_encrypted_inode(struct inode *inode)
3291 {
3292 #ifdef CONFIG_F2FS_FS_ENCRYPTION
3293 file_set_encrypt(inode);
3294 inode->i_flags |= S_ENCRYPTED;
3295 #endif
3296 }
3297
3298 /*
3299 * Returns true if the reads of the inode's data need to undergo some
3300 * postprocessing step, like decryption or authenticity verification.
3301 */
f2fs_post_read_required(struct inode * inode)3302 static inline bool f2fs_post_read_required(struct inode *inode)
3303 {
3304 return f2fs_encrypted_file(inode);
3305 }
3306
3307 #define F2FS_FEATURE_FUNCS(name, flagname) \
3308 static inline int f2fs_sb_has_##name(struct super_block *sb) \
3309 { \
3310 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
3311 }
3312
3313 F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3314 F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3315 F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3316 F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3317 F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3318 F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3319 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3320 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3321 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
3322
3323 #ifdef CONFIG_BLK_DEV_ZONED
get_blkz_type(struct f2fs_sb_info * sbi,struct block_device * bdev,block_t blkaddr)3324 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
3325 struct block_device *bdev, block_t blkaddr)
3326 {
3327 unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
3328 int i;
3329
3330 for (i = 0; i < sbi->s_ndevs; i++)
3331 if (FDEV(i).bdev == bdev)
3332 return FDEV(i).blkz_type[zno];
3333 return -EINVAL;
3334 }
3335 #endif
3336
f2fs_discard_en(struct f2fs_sb_info * sbi)3337 static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
3338 {
3339 struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
3340
3341 return blk_queue_discard(q) || f2fs_sb_has_blkzoned(sbi->sb);
3342 }
3343
set_opt_mode(struct f2fs_sb_info * sbi,unsigned int mt)3344 static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
3345 {
3346 clear_opt(sbi, ADAPTIVE);
3347 clear_opt(sbi, LFS);
3348
3349 switch (mt) {
3350 case F2FS_MOUNT_ADAPTIVE:
3351 set_opt(sbi, ADAPTIVE);
3352 break;
3353 case F2FS_MOUNT_LFS:
3354 set_opt(sbi, LFS);
3355 break;
3356 }
3357 }
3358
f2fs_may_encrypt(struct inode * inode)3359 static inline bool f2fs_may_encrypt(struct inode *inode)
3360 {
3361 #ifdef CONFIG_F2FS_FS_ENCRYPTION
3362 umode_t mode = inode->i_mode;
3363
3364 return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3365 #else
3366 return 0;
3367 #endif
3368 }
3369
f2fs_force_buffered_io(struct inode * inode,int rw)3370 static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
3371 {
3372 return (f2fs_post_read_required(inode) ||
3373 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
3374 F2FS_I_SB(inode)->s_ndevs);
3375 }
3376
3377 #endif
3378