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