f2fs: use rw_sem instead of fs_lock(locks mutex)
[deliverable/linux.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/wait.h>
22 #include <linux/sched.h>
23
24 /*
25 * For mount options
26 */
27 #define F2FS_MOUNT_BG_GC 0x00000001
28 #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
29 #define F2FS_MOUNT_DISCARD 0x00000004
30 #define F2FS_MOUNT_NOHEAP 0x00000008
31 #define F2FS_MOUNT_XATTR_USER 0x00000010
32 #define F2FS_MOUNT_POSIX_ACL 0x00000020
33 #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
34 #define F2FS_MOUNT_INLINE_XATTR 0x00000080
35
36 #define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
37 #define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
38 #define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
39
40 #define ver_after(a, b) (typecheck(unsigned long long, a) && \
41 typecheck(unsigned long long, b) && \
42 ((long long)((a) - (b)) > 0))
43
44 typedef u32 block_t; /*
45 * should not change u32, since it is the on-disk block
46 * address format, __le32.
47 */
48 typedef u32 nid_t;
49
50 struct f2fs_mount_info {
51 unsigned int opt;
52 };
53
54 #define CRCPOLY_LE 0xedb88320
55
56 static inline __u32 f2fs_crc32(void *buf, size_t len)
57 {
58 unsigned char *p = (unsigned char *)buf;
59 __u32 crc = F2FS_SUPER_MAGIC;
60 int i;
61
62 while (len--) {
63 crc ^= *p++;
64 for (i = 0; i < 8; i++)
65 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
66 }
67 return crc;
68 }
69
70 static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
71 {
72 return f2fs_crc32(buf, buf_size) == blk_crc;
73 }
74
75 /*
76 * For checkpoint manager
77 */
78 enum {
79 NAT_BITMAP,
80 SIT_BITMAP
81 };
82
83 /* for the list of orphan inodes */
84 struct orphan_inode_entry {
85 struct list_head list; /* list head */
86 nid_t ino; /* inode number */
87 };
88
89 /* for the list of directory inodes */
90 struct dir_inode_entry {
91 struct list_head list; /* list head */
92 struct inode *inode; /* vfs inode pointer */
93 };
94
95 /* for the list of fsync inodes, used only during recovery */
96 struct fsync_inode_entry {
97 struct list_head list; /* list head */
98 struct inode *inode; /* vfs inode pointer */
99 block_t blkaddr; /* block address locating the last inode */
100 };
101
102 #define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
103 #define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
104
105 #define nat_in_journal(sum, i) (sum->nat_j.entries[i].ne)
106 #define nid_in_journal(sum, i) (sum->nat_j.entries[i].nid)
107 #define sit_in_journal(sum, i) (sum->sit_j.entries[i].se)
108 #define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno)
109
110 static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
111 {
112 int before = nats_in_cursum(rs);
113 rs->n_nats = cpu_to_le16(before + i);
114 return before;
115 }
116
117 static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
118 {
119 int before = sits_in_cursum(rs);
120 rs->n_sits = cpu_to_le16(before + i);
121 return before;
122 }
123
124 /*
125 * ioctl commands
126 */
127 #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
128 #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
129
130 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
131 /*
132 * ioctl commands in 32 bit emulation
133 */
134 #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
135 #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
136 #endif
137
138 /*
139 * For INODE and NODE manager
140 */
141 /*
142 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
143 * as its node offset to distinguish from index node blocks.
144 * But some bits are used to mark the node block.
145 */
146 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
147 >> OFFSET_BIT_SHIFT)
148 enum {
149 ALLOC_NODE, /* allocate a new node page if needed */
150 LOOKUP_NODE, /* look up a node without readahead */
151 LOOKUP_NODE_RA, /*
152 * look up a node with readahead called
153 * by get_datablock_ro.
154 */
155 };
156
157 #define F2FS_LINK_MAX 32000 /* maximum link count per file */
158
159 /* for in-memory extent cache entry */
160 struct extent_info {
161 rwlock_t ext_lock; /* rwlock for consistency */
162 unsigned int fofs; /* start offset in a file */
163 u32 blk_addr; /* start block address of the extent */
164 unsigned int len; /* length of the extent */
165 };
166
167 /*
168 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
169 */
170 #define FADVISE_COLD_BIT 0x01
171 #define FADVISE_LOST_PINO_BIT 0x02
172
173 struct f2fs_inode_info {
174 struct inode vfs_inode; /* serve a vfs inode */
175 unsigned long i_flags; /* keep an inode flags for ioctl */
176 unsigned char i_advise; /* use to give file attribute hints */
177 unsigned int i_current_depth; /* use only in directory structure */
178 unsigned int i_pino; /* parent inode number */
179 umode_t i_acl_mode; /* keep file acl mode temporarily */
180
181 /* Use below internally in f2fs*/
182 unsigned long flags; /* use to pass per-file flags */
183 atomic_t dirty_dents; /* # of dirty dentry pages */
184 f2fs_hash_t chash; /* hash value of given file name */
185 unsigned int clevel; /* maximum level of given file name */
186 nid_t i_xattr_nid; /* node id that contains xattrs */
187 unsigned long long xattr_ver; /* cp version of xattr modification */
188 struct extent_info ext; /* in-memory extent cache entry */
189 };
190
191 static inline void get_extent_info(struct extent_info *ext,
192 struct f2fs_extent i_ext)
193 {
194 write_lock(&ext->ext_lock);
195 ext->fofs = le32_to_cpu(i_ext.fofs);
196 ext->blk_addr = le32_to_cpu(i_ext.blk_addr);
197 ext->len = le32_to_cpu(i_ext.len);
198 write_unlock(&ext->ext_lock);
199 }
200
201 static inline void set_raw_extent(struct extent_info *ext,
202 struct f2fs_extent *i_ext)
203 {
204 read_lock(&ext->ext_lock);
205 i_ext->fofs = cpu_to_le32(ext->fofs);
206 i_ext->blk_addr = cpu_to_le32(ext->blk_addr);
207 i_ext->len = cpu_to_le32(ext->len);
208 read_unlock(&ext->ext_lock);
209 }
210
211 struct f2fs_nm_info {
212 block_t nat_blkaddr; /* base disk address of NAT */
213 nid_t max_nid; /* maximum possible node ids */
214 nid_t next_scan_nid; /* the next nid to be scanned */
215
216 /* NAT cache management */
217 struct radix_tree_root nat_root;/* root of the nat entry cache */
218 rwlock_t nat_tree_lock; /* protect nat_tree_lock */
219 unsigned int nat_cnt; /* the # of cached nat entries */
220 struct list_head nat_entries; /* cached nat entry list (clean) */
221 struct list_head dirty_nat_entries; /* cached nat entry list (dirty) */
222
223 /* free node ids management */
224 struct list_head free_nid_list; /* a list for free nids */
225 spinlock_t free_nid_list_lock; /* protect free nid list */
226 unsigned int fcnt; /* the number of free node id */
227 struct mutex build_lock; /* lock for build free nids */
228
229 /* for checkpoint */
230 char *nat_bitmap; /* NAT bitmap pointer */
231 int bitmap_size; /* bitmap size */
232 };
233
234 /*
235 * this structure is used as one of function parameters.
236 * all the information are dedicated to a given direct node block determined
237 * by the data offset in a file.
238 */
239 struct dnode_of_data {
240 struct inode *inode; /* vfs inode pointer */
241 struct page *inode_page; /* its inode page, NULL is possible */
242 struct page *node_page; /* cached direct node page */
243 nid_t nid; /* node id of the direct node block */
244 unsigned int ofs_in_node; /* data offset in the node page */
245 bool inode_page_locked; /* inode page is locked or not */
246 block_t data_blkaddr; /* block address of the node block */
247 };
248
249 static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
250 struct page *ipage, struct page *npage, nid_t nid)
251 {
252 memset(dn, 0, sizeof(*dn));
253 dn->inode = inode;
254 dn->inode_page = ipage;
255 dn->node_page = npage;
256 dn->nid = nid;
257 }
258
259 /*
260 * For SIT manager
261 *
262 * By default, there are 6 active log areas across the whole main area.
263 * When considering hot and cold data separation to reduce cleaning overhead,
264 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
265 * respectively.
266 * In the current design, you should not change the numbers intentionally.
267 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
268 * logs individually according to the underlying devices. (default: 6)
269 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
270 * data and 8 for node logs.
271 */
272 #define NR_CURSEG_DATA_TYPE (3)
273 #define NR_CURSEG_NODE_TYPE (3)
274 #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
275
276 enum {
277 CURSEG_HOT_DATA = 0, /* directory entry blocks */
278 CURSEG_WARM_DATA, /* data blocks */
279 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
280 CURSEG_HOT_NODE, /* direct node blocks of directory files */
281 CURSEG_WARM_NODE, /* direct node blocks of normal files */
282 CURSEG_COLD_NODE, /* indirect node blocks */
283 NO_CHECK_TYPE
284 };
285
286 struct f2fs_sm_info {
287 struct sit_info *sit_info; /* whole segment information */
288 struct free_segmap_info *free_info; /* free segment information */
289 struct dirty_seglist_info *dirty_info; /* dirty segment information */
290 struct curseg_info *curseg_array; /* active segment information */
291
292 struct list_head wblist_head; /* list of under-writeback pages */
293 spinlock_t wblist_lock; /* lock for checkpoint */
294
295 block_t seg0_blkaddr; /* block address of 0'th segment */
296 block_t main_blkaddr; /* start block address of main area */
297 block_t ssa_blkaddr; /* start block address of SSA area */
298
299 unsigned int segment_count; /* total # of segments */
300 unsigned int main_segments; /* # of segments in main area */
301 unsigned int reserved_segments; /* # of reserved segments */
302 unsigned int ovp_segments; /* # of overprovision segments */
303 };
304
305 /*
306 * For superblock
307 */
308 /*
309 * COUNT_TYPE for monitoring
310 *
311 * f2fs monitors the number of several block types such as on-writeback,
312 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
313 */
314 enum count_type {
315 F2FS_WRITEBACK,
316 F2FS_DIRTY_DENTS,
317 F2FS_DIRTY_NODES,
318 F2FS_DIRTY_META,
319 NR_COUNT_TYPE,
320 };
321
322 /*
323 * The below are the page types of bios used in submti_bio().
324 * The available types are:
325 * DATA User data pages. It operates as async mode.
326 * NODE Node pages. It operates as async mode.
327 * META FS metadata pages such as SIT, NAT, CP.
328 * NR_PAGE_TYPE The number of page types.
329 * META_FLUSH Make sure the previous pages are written
330 * with waiting the bio's completion
331 * ... Only can be used with META.
332 */
333 enum page_type {
334 DATA,
335 NODE,
336 META,
337 NR_PAGE_TYPE,
338 META_FLUSH,
339 };
340
341 struct f2fs_sb_info {
342 struct super_block *sb; /* pointer to VFS super block */
343 struct proc_dir_entry *s_proc; /* proc entry */
344 struct buffer_head *raw_super_buf; /* buffer head of raw sb */
345 struct f2fs_super_block *raw_super; /* raw super block pointer */
346 int s_dirty; /* dirty flag for checkpoint */
347
348 /* for node-related operations */
349 struct f2fs_nm_info *nm_info; /* node manager */
350 struct inode *node_inode; /* cache node blocks */
351
352 /* for segment-related operations */
353 struct f2fs_sm_info *sm_info; /* segment manager */
354 struct bio *bio[NR_PAGE_TYPE]; /* bios to merge */
355 sector_t last_block_in_bio[NR_PAGE_TYPE]; /* last block number */
356 struct rw_semaphore bio_sem; /* IO semaphore */
357
358 /* for checkpoint */
359 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
360 struct inode *meta_inode; /* cache meta blocks */
361 struct mutex cp_mutex; /* checkpoint procedure lock */
362 struct rw_semaphore cp_rwsem; /* blocking FS operations */
363 wait_queue_head_t cp_wait; /* checkpoint wait queue */
364 struct mutex node_write; /* locking node writes */
365 struct mutex writepages; /* mutex for writepages() */
366 int por_doing; /* recovery is doing or not */
367 int on_build_free_nids; /* build_free_nids is doing */
368
369 /* for orphan inode management */
370 struct list_head orphan_inode_list; /* orphan inode list */
371 struct mutex orphan_inode_mutex; /* for orphan inode list */
372 unsigned int n_orphans; /* # of orphan inodes */
373
374 /* for directory inode management */
375 struct list_head dir_inode_list; /* dir inode list */
376 spinlock_t dir_inode_lock; /* for dir inode list lock */
377
378 /* basic file system units */
379 unsigned int log_sectors_per_block; /* log2 sectors per block */
380 unsigned int log_blocksize; /* log2 block size */
381 unsigned int blocksize; /* block size */
382 unsigned int root_ino_num; /* root inode number*/
383 unsigned int node_ino_num; /* node inode number*/
384 unsigned int meta_ino_num; /* meta inode number*/
385 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
386 unsigned int blocks_per_seg; /* blocks per segment */
387 unsigned int segs_per_sec; /* segments per section */
388 unsigned int secs_per_zone; /* sections per zone */
389 unsigned int total_sections; /* total section count */
390 unsigned int total_node_count; /* total node block count */
391 unsigned int total_valid_node_count; /* valid node block count */
392 unsigned int total_valid_inode_count; /* valid inode count */
393 int active_logs; /* # of active logs */
394
395 block_t user_block_count; /* # of user blocks */
396 block_t total_valid_block_count; /* # of valid blocks */
397 block_t alloc_valid_block_count; /* # of allocated blocks */
398 block_t last_valid_block_count; /* for recovery */
399 u32 s_next_generation; /* for NFS support */
400 atomic_t nr_pages[NR_COUNT_TYPE]; /* # of pages, see count_type */
401
402 struct f2fs_mount_info mount_opt; /* mount options */
403
404 /* for cleaning operations */
405 struct mutex gc_mutex; /* mutex for GC */
406 struct f2fs_gc_kthread *gc_thread; /* GC thread */
407 unsigned int cur_victim_sec; /* current victim section num */
408
409 /*
410 * for stat information.
411 * one is for the LFS mode, and the other is for the SSR mode.
412 */
413 #ifdef CONFIG_F2FS_STAT_FS
414 struct f2fs_stat_info *stat_info; /* FS status information */
415 unsigned int segment_count[2]; /* # of allocated segments */
416 unsigned int block_count[2]; /* # of allocated blocks */
417 int total_hit_ext, read_hit_ext; /* extent cache hit ratio */
418 int bg_gc; /* background gc calls */
419 unsigned int n_dirty_dirs; /* # of dir inodes */
420 #endif
421 unsigned int last_victim[2]; /* last victim segment # */
422 spinlock_t stat_lock; /* lock for stat operations */
423
424 /* For sysfs suppport */
425 struct kobject s_kobj;
426 struct completion s_kobj_unregister;
427 };
428
429 /*
430 * Inline functions
431 */
432 static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
433 {
434 return container_of(inode, struct f2fs_inode_info, vfs_inode);
435 }
436
437 static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
438 {
439 return sb->s_fs_info;
440 }
441
442 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
443 {
444 return (struct f2fs_super_block *)(sbi->raw_super);
445 }
446
447 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
448 {
449 return (struct f2fs_checkpoint *)(sbi->ckpt);
450 }
451
452 static inline struct f2fs_node *F2FS_NODE(struct page *page)
453 {
454 return (struct f2fs_node *)page_address(page);
455 }
456
457 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
458 {
459 return (struct f2fs_nm_info *)(sbi->nm_info);
460 }
461
462 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
463 {
464 return (struct f2fs_sm_info *)(sbi->sm_info);
465 }
466
467 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
468 {
469 return (struct sit_info *)(SM_I(sbi)->sit_info);
470 }
471
472 static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
473 {
474 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
475 }
476
477 static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
478 {
479 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
480 }
481
482 static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info *sbi)
483 {
484 sbi->s_dirty = 1;
485 }
486
487 static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info *sbi)
488 {
489 sbi->s_dirty = 0;
490 }
491
492 static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
493 {
494 return le64_to_cpu(cp->checkpoint_ver);
495 }
496
497 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
498 {
499 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
500 return ckpt_flags & f;
501 }
502
503 static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
504 {
505 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
506 ckpt_flags |= f;
507 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
508 }
509
510 static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
511 {
512 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
513 ckpt_flags &= (~f);
514 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
515 }
516
517 static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
518 {
519 /*
520 * If the checkpoint thread is waiting for cp_rwsem, add cuurent task
521 * into wait list to avoid the checkpoint thread starvation
522 */
523 while (!list_empty(&sbi->cp_rwsem.wait_list))
524 wait_event_interruptible(sbi->cp_wait,
525 list_empty(&sbi->cp_rwsem.wait_list));
526 down_read(&sbi->cp_rwsem);
527 }
528
529 static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
530 {
531 up_read(&sbi->cp_rwsem);
532 }
533
534 static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
535 {
536 down_write_nest_lock(&sbi->cp_rwsem, &sbi->cp_mutex);
537 }
538
539 static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
540 {
541 up_write(&sbi->cp_rwsem);
542
543 /* wake up all tasks blocked by checkpoint */
544 wake_up_all(&sbi->cp_wait);
545 }
546
547 /*
548 * Check whether the given nid is within node id range.
549 */
550 static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
551 {
552 WARN_ON((nid >= NM_I(sbi)->max_nid));
553 if (nid >= NM_I(sbi)->max_nid)
554 return -EINVAL;
555 return 0;
556 }
557
558 #define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
559
560 /*
561 * Check whether the inode has blocks or not
562 */
563 static inline int F2FS_HAS_BLOCKS(struct inode *inode)
564 {
565 if (F2FS_I(inode)->i_xattr_nid)
566 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1);
567 else
568 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS);
569 }
570
571 static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
572 struct inode *inode, blkcnt_t count)
573 {
574 block_t valid_block_count;
575
576 spin_lock(&sbi->stat_lock);
577 valid_block_count =
578 sbi->total_valid_block_count + (block_t)count;
579 if (valid_block_count > sbi->user_block_count) {
580 spin_unlock(&sbi->stat_lock);
581 return false;
582 }
583 inode->i_blocks += count;
584 sbi->total_valid_block_count = valid_block_count;
585 sbi->alloc_valid_block_count += (block_t)count;
586 spin_unlock(&sbi->stat_lock);
587 return true;
588 }
589
590 static inline int dec_valid_block_count(struct f2fs_sb_info *sbi,
591 struct inode *inode,
592 blkcnt_t count)
593 {
594 spin_lock(&sbi->stat_lock);
595 BUG_ON(sbi->total_valid_block_count < (block_t) count);
596 BUG_ON(inode->i_blocks < count);
597 inode->i_blocks -= count;
598 sbi->total_valid_block_count -= (block_t)count;
599 spin_unlock(&sbi->stat_lock);
600 return 0;
601 }
602
603 static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
604 {
605 atomic_inc(&sbi->nr_pages[count_type]);
606 F2FS_SET_SB_DIRT(sbi);
607 }
608
609 static inline void inode_inc_dirty_dents(struct inode *inode)
610 {
611 atomic_inc(&F2FS_I(inode)->dirty_dents);
612 }
613
614 static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
615 {
616 atomic_dec(&sbi->nr_pages[count_type]);
617 }
618
619 static inline void inode_dec_dirty_dents(struct inode *inode)
620 {
621 atomic_dec(&F2FS_I(inode)->dirty_dents);
622 }
623
624 static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
625 {
626 return atomic_read(&sbi->nr_pages[count_type]);
627 }
628
629 static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
630 {
631 unsigned int pages_per_sec = sbi->segs_per_sec *
632 (1 << sbi->log_blocks_per_seg);
633 return ((get_pages(sbi, block_type) + pages_per_sec - 1)
634 >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
635 }
636
637 static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
638 {
639 block_t ret;
640 spin_lock(&sbi->stat_lock);
641 ret = sbi->total_valid_block_count;
642 spin_unlock(&sbi->stat_lock);
643 return ret;
644 }
645
646 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
647 {
648 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
649
650 /* return NAT or SIT bitmap */
651 if (flag == NAT_BITMAP)
652 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
653 else if (flag == SIT_BITMAP)
654 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
655
656 return 0;
657 }
658
659 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
660 {
661 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
662 int offset = (flag == NAT_BITMAP) ?
663 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
664 return &ckpt->sit_nat_version_bitmap + offset;
665 }
666
667 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
668 {
669 block_t start_addr;
670 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
671 unsigned long long ckpt_version = cur_cp_version(ckpt);
672
673 start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
674
675 /*
676 * odd numbered checkpoint should at cp segment 0
677 * and even segent must be at cp segment 1
678 */
679 if (!(ckpt_version & 1))
680 start_addr += sbi->blocks_per_seg;
681
682 return start_addr;
683 }
684
685 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
686 {
687 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
688 }
689
690 static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
691 struct inode *inode,
692 unsigned int count)
693 {
694 block_t valid_block_count;
695 unsigned int valid_node_count;
696
697 spin_lock(&sbi->stat_lock);
698
699 valid_block_count = sbi->total_valid_block_count + (block_t)count;
700 sbi->alloc_valid_block_count += (block_t)count;
701 valid_node_count = sbi->total_valid_node_count + count;
702
703 if (valid_block_count > sbi->user_block_count) {
704 spin_unlock(&sbi->stat_lock);
705 return false;
706 }
707
708 if (valid_node_count > sbi->total_node_count) {
709 spin_unlock(&sbi->stat_lock);
710 return false;
711 }
712
713 if (inode)
714 inode->i_blocks += count;
715 sbi->total_valid_node_count = valid_node_count;
716 sbi->total_valid_block_count = valid_block_count;
717 spin_unlock(&sbi->stat_lock);
718
719 return true;
720 }
721
722 static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
723 struct inode *inode,
724 unsigned int count)
725 {
726 spin_lock(&sbi->stat_lock);
727
728 BUG_ON(sbi->total_valid_block_count < count);
729 BUG_ON(sbi->total_valid_node_count < count);
730 BUG_ON(inode->i_blocks < count);
731
732 inode->i_blocks -= count;
733 sbi->total_valid_node_count -= count;
734 sbi->total_valid_block_count -= (block_t)count;
735
736 spin_unlock(&sbi->stat_lock);
737 }
738
739 static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
740 {
741 unsigned int ret;
742 spin_lock(&sbi->stat_lock);
743 ret = sbi->total_valid_node_count;
744 spin_unlock(&sbi->stat_lock);
745 return ret;
746 }
747
748 static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
749 {
750 spin_lock(&sbi->stat_lock);
751 BUG_ON(sbi->total_valid_inode_count == sbi->total_node_count);
752 sbi->total_valid_inode_count++;
753 spin_unlock(&sbi->stat_lock);
754 }
755
756 static inline int dec_valid_inode_count(struct f2fs_sb_info *sbi)
757 {
758 spin_lock(&sbi->stat_lock);
759 BUG_ON(!sbi->total_valid_inode_count);
760 sbi->total_valid_inode_count--;
761 spin_unlock(&sbi->stat_lock);
762 return 0;
763 }
764
765 static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
766 {
767 unsigned int ret;
768 spin_lock(&sbi->stat_lock);
769 ret = sbi->total_valid_inode_count;
770 spin_unlock(&sbi->stat_lock);
771 return ret;
772 }
773
774 static inline void f2fs_put_page(struct page *page, int unlock)
775 {
776 if (!page || IS_ERR(page))
777 return;
778
779 if (unlock) {
780 BUG_ON(!PageLocked(page));
781 unlock_page(page);
782 }
783 page_cache_release(page);
784 }
785
786 static inline void f2fs_put_dnode(struct dnode_of_data *dn)
787 {
788 if (dn->node_page)
789 f2fs_put_page(dn->node_page, 1);
790 if (dn->inode_page && dn->node_page != dn->inode_page)
791 f2fs_put_page(dn->inode_page, 0);
792 dn->node_page = NULL;
793 dn->inode_page = NULL;
794 }
795
796 static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
797 size_t size, void (*ctor)(void *))
798 {
799 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, ctor);
800 }
801
802 #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
803
804 static inline bool IS_INODE(struct page *page)
805 {
806 struct f2fs_node *p = F2FS_NODE(page);
807 return RAW_IS_INODE(p);
808 }
809
810 static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
811 {
812 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
813 }
814
815 static inline block_t datablock_addr(struct page *node_page,
816 unsigned int offset)
817 {
818 struct f2fs_node *raw_node;
819 __le32 *addr_array;
820 raw_node = F2FS_NODE(node_page);
821 addr_array = blkaddr_in_node(raw_node);
822 return le32_to_cpu(addr_array[offset]);
823 }
824
825 static inline int f2fs_test_bit(unsigned int nr, char *addr)
826 {
827 int mask;
828
829 addr += (nr >> 3);
830 mask = 1 << (7 - (nr & 0x07));
831 return mask & *addr;
832 }
833
834 static inline int f2fs_set_bit(unsigned int nr, char *addr)
835 {
836 int mask;
837 int ret;
838
839 addr += (nr >> 3);
840 mask = 1 << (7 - (nr & 0x07));
841 ret = mask & *addr;
842 *addr |= mask;
843 return ret;
844 }
845
846 static inline int f2fs_clear_bit(unsigned int nr, char *addr)
847 {
848 int mask;
849 int ret;
850
851 addr += (nr >> 3);
852 mask = 1 << (7 - (nr & 0x07));
853 ret = mask & *addr;
854 *addr &= ~mask;
855 return ret;
856 }
857
858 /* used for f2fs_inode_info->flags */
859 enum {
860 FI_NEW_INODE, /* indicate newly allocated inode */
861 FI_DIRTY_INODE, /* indicate inode is dirty or not */
862 FI_INC_LINK, /* need to increment i_nlink */
863 FI_ACL_MODE, /* indicate acl mode */
864 FI_NO_ALLOC, /* should not allocate any blocks */
865 FI_UPDATE_DIR, /* should update inode block for consistency */
866 FI_DELAY_IPUT, /* used for the recovery */
867 FI_INLINE_XATTR, /* used for inline xattr */
868 };
869
870 static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
871 {
872 set_bit(flag, &fi->flags);
873 }
874
875 static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
876 {
877 return test_bit(flag, &fi->flags);
878 }
879
880 static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
881 {
882 clear_bit(flag, &fi->flags);
883 }
884
885 static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
886 {
887 fi->i_acl_mode = mode;
888 set_inode_flag(fi, FI_ACL_MODE);
889 }
890
891 static inline int cond_clear_inode_flag(struct f2fs_inode_info *fi, int flag)
892 {
893 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
894 clear_inode_flag(fi, FI_ACL_MODE);
895 return 1;
896 }
897 return 0;
898 }
899
900 static inline void get_inline_info(struct f2fs_inode_info *fi,
901 struct f2fs_inode *ri)
902 {
903 if (ri->i_inline & F2FS_INLINE_XATTR)
904 set_inode_flag(fi, FI_INLINE_XATTR);
905 }
906
907 static inline void set_raw_inline(struct f2fs_inode_info *fi,
908 struct f2fs_inode *ri)
909 {
910 ri->i_inline = 0;
911
912 if (is_inode_flag_set(fi, FI_INLINE_XATTR))
913 ri->i_inline |= F2FS_INLINE_XATTR;
914 }
915
916 static inline unsigned int addrs_per_inode(struct f2fs_inode_info *fi)
917 {
918 if (is_inode_flag_set(fi, FI_INLINE_XATTR))
919 return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
920 return DEF_ADDRS_PER_INODE;
921 }
922
923 static inline void *inline_xattr_addr(struct page *page)
924 {
925 struct f2fs_inode *ri;
926 ri = (struct f2fs_inode *)page_address(page);
927 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
928 F2FS_INLINE_XATTR_ADDRS]);
929 }
930
931 static inline int inline_xattr_size(struct inode *inode)
932 {
933 if (is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR))
934 return F2FS_INLINE_XATTR_ADDRS << 2;
935 else
936 return 0;
937 }
938
939 static inline int f2fs_readonly(struct super_block *sb)
940 {
941 return sb->s_flags & MS_RDONLY;
942 }
943
944 /*
945 * file.c
946 */
947 int f2fs_sync_file(struct file *, loff_t, loff_t, int);
948 void truncate_data_blocks(struct dnode_of_data *);
949 void f2fs_truncate(struct inode *);
950 int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
951 int f2fs_setattr(struct dentry *, struct iattr *);
952 int truncate_hole(struct inode *, pgoff_t, pgoff_t);
953 int truncate_data_blocks_range(struct dnode_of_data *, int);
954 long f2fs_ioctl(struct file *, unsigned int, unsigned long);
955 long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
956
957 /*
958 * inode.c
959 */
960 void f2fs_set_inode_flags(struct inode *);
961 struct inode *f2fs_iget(struct super_block *, unsigned long);
962 void update_inode(struct inode *, struct page *);
963 int update_inode_page(struct inode *);
964 int f2fs_write_inode(struct inode *, struct writeback_control *);
965 void f2fs_evict_inode(struct inode *);
966
967 /*
968 * namei.c
969 */
970 struct dentry *f2fs_get_parent(struct dentry *child);
971
972 /*
973 * dir.c
974 */
975 struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
976 struct page **);
977 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
978 ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
979 void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
980 struct page *, struct inode *);
981 int update_dent_inode(struct inode *, const struct qstr *);
982 int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *);
983 void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *);
984 int f2fs_make_empty(struct inode *, struct inode *);
985 bool f2fs_empty_dir(struct inode *);
986
987 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
988 {
989 return __f2fs_add_link(dentry->d_parent->d_inode, &dentry->d_name,
990 inode);
991 }
992
993 /*
994 * super.c
995 */
996 int f2fs_sync_fs(struct super_block *, int);
997 extern __printf(3, 4)
998 void f2fs_msg(struct super_block *, const char *, const char *, ...);
999
1000 /*
1001 * hash.c
1002 */
1003 f2fs_hash_t f2fs_dentry_hash(const char *, size_t);
1004
1005 /*
1006 * node.c
1007 */
1008 struct dnode_of_data;
1009 struct node_info;
1010
1011 int is_checkpointed_node(struct f2fs_sb_info *, nid_t);
1012 void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
1013 int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
1014 int truncate_inode_blocks(struct inode *, pgoff_t);
1015 int truncate_xattr_node(struct inode *, struct page *);
1016 int remove_inode_page(struct inode *);
1017 struct page *new_inode_page(struct inode *, const struct qstr *);
1018 struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
1019 void ra_node_page(struct f2fs_sb_info *, nid_t);
1020 struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
1021 struct page *get_node_page_ra(struct page *, int);
1022 void sync_inode_page(struct dnode_of_data *);
1023 int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
1024 bool alloc_nid(struct f2fs_sb_info *, nid_t *);
1025 void alloc_nid_done(struct f2fs_sb_info *, nid_t);
1026 void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
1027 void recover_node_page(struct f2fs_sb_info *, struct page *,
1028 struct f2fs_summary *, struct node_info *, block_t);
1029 int recover_inode_page(struct f2fs_sb_info *, struct page *);
1030 int restore_node_summary(struct f2fs_sb_info *, unsigned int,
1031 struct f2fs_summary_block *);
1032 void flush_nat_entries(struct f2fs_sb_info *);
1033 int build_node_manager(struct f2fs_sb_info *);
1034 void destroy_node_manager(struct f2fs_sb_info *);
1035 int __init create_node_manager_caches(void);
1036 void destroy_node_manager_caches(void);
1037
1038 /*
1039 * segment.c
1040 */
1041 void f2fs_balance_fs(struct f2fs_sb_info *);
1042 void invalidate_blocks(struct f2fs_sb_info *, block_t);
1043 void clear_prefree_segments(struct f2fs_sb_info *);
1044 int npages_for_summary_flush(struct f2fs_sb_info *);
1045 void allocate_new_segments(struct f2fs_sb_info *);
1046 struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
1047 struct bio *f2fs_bio_alloc(struct block_device *, int);
1048 void f2fs_submit_bio(struct f2fs_sb_info *, enum page_type, bool);
1049 void f2fs_wait_on_page_writeback(struct page *, enum page_type, bool);
1050 void write_meta_page(struct f2fs_sb_info *, struct page *);
1051 void write_node_page(struct f2fs_sb_info *, struct page *, unsigned int,
1052 block_t, block_t *);
1053 void write_data_page(struct inode *, struct page *, struct dnode_of_data*,
1054 block_t, block_t *);
1055 void rewrite_data_page(struct f2fs_sb_info *, struct page *, block_t);
1056 void recover_data_page(struct f2fs_sb_info *, struct page *,
1057 struct f2fs_summary *, block_t, block_t);
1058 void rewrite_node_page(struct f2fs_sb_info *, struct page *,
1059 struct f2fs_summary *, block_t, block_t);
1060 void write_data_summaries(struct f2fs_sb_info *, block_t);
1061 void write_node_summaries(struct f2fs_sb_info *, block_t);
1062 int lookup_journal_in_cursum(struct f2fs_summary_block *,
1063 int, unsigned int, int);
1064 void flush_sit_entries(struct f2fs_sb_info *);
1065 int build_segment_manager(struct f2fs_sb_info *);
1066 void destroy_segment_manager(struct f2fs_sb_info *);
1067
1068 /*
1069 * checkpoint.c
1070 */
1071 struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
1072 struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
1073 long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
1074 int acquire_orphan_inode(struct f2fs_sb_info *);
1075 void release_orphan_inode(struct f2fs_sb_info *);
1076 void add_orphan_inode(struct f2fs_sb_info *, nid_t);
1077 void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
1078 int recover_orphan_inodes(struct f2fs_sb_info *);
1079 int get_valid_checkpoint(struct f2fs_sb_info *);
1080 void set_dirty_dir_page(struct inode *, struct page *);
1081 void add_dirty_dir_inode(struct inode *);
1082 void remove_dirty_dir_inode(struct inode *);
1083 struct inode *check_dirty_dir_inode(struct f2fs_sb_info *, nid_t);
1084 void sync_dirty_dir_inodes(struct f2fs_sb_info *);
1085 void write_checkpoint(struct f2fs_sb_info *, bool);
1086 void init_orphan_info(struct f2fs_sb_info *);
1087 int __init create_checkpoint_caches(void);
1088 void destroy_checkpoint_caches(void);
1089
1090 /*
1091 * data.c
1092 */
1093 int reserve_new_block(struct dnode_of_data *);
1094 void update_extent_cache(block_t, struct dnode_of_data *);
1095 struct page *find_data_page(struct inode *, pgoff_t, bool);
1096 struct page *get_lock_data_page(struct inode *, pgoff_t);
1097 struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
1098 int f2fs_readpage(struct f2fs_sb_info *, struct page *, block_t, int);
1099 int do_write_data_page(struct page *);
1100
1101 /*
1102 * gc.c
1103 */
1104 int start_gc_thread(struct f2fs_sb_info *);
1105 void stop_gc_thread(struct f2fs_sb_info *);
1106 block_t start_bidx_of_node(unsigned int, struct f2fs_inode_info *);
1107 int f2fs_gc(struct f2fs_sb_info *);
1108 void build_gc_manager(struct f2fs_sb_info *);
1109 int __init create_gc_caches(void);
1110 void destroy_gc_caches(void);
1111
1112 /*
1113 * recovery.c
1114 */
1115 int recover_fsync_data(struct f2fs_sb_info *);
1116 bool space_for_roll_forward(struct f2fs_sb_info *);
1117
1118 /*
1119 * debug.c
1120 */
1121 #ifdef CONFIG_F2FS_STAT_FS
1122 struct f2fs_stat_info {
1123 struct list_head stat_list;
1124 struct f2fs_sb_info *sbi;
1125 struct mutex stat_lock;
1126 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
1127 int main_area_segs, main_area_sections, main_area_zones;
1128 int hit_ext, total_ext;
1129 int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
1130 int nats, sits, fnids;
1131 int total_count, utilization;
1132 int bg_gc;
1133 unsigned int valid_count, valid_node_count, valid_inode_count;
1134 unsigned int bimodal, avg_vblocks;
1135 int util_free, util_valid, util_invalid;
1136 int rsvd_segs, overp_segs;
1137 int dirty_count, node_pages, meta_pages;
1138 int prefree_count, call_count;
1139 int tot_segs, node_segs, data_segs, free_segs, free_secs;
1140 int tot_blks, data_blks, node_blks;
1141 int curseg[NR_CURSEG_TYPE];
1142 int cursec[NR_CURSEG_TYPE];
1143 int curzone[NR_CURSEG_TYPE];
1144
1145 unsigned int segment_count[2];
1146 unsigned int block_count[2];
1147 unsigned base_mem, cache_mem;
1148 };
1149
1150 static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
1151 {
1152 return (struct f2fs_stat_info*)sbi->stat_info;
1153 }
1154
1155 #define stat_inc_call_count(si) ((si)->call_count++)
1156
1157 #define stat_inc_seg_count(sbi, type) \
1158 do { \
1159 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
1160 (si)->tot_segs++; \
1161 if (type == SUM_TYPE_DATA) \
1162 si->data_segs++; \
1163 else \
1164 si->node_segs++; \
1165 } while (0)
1166
1167 #define stat_inc_tot_blk_count(si, blks) \
1168 (si->tot_blks += (blks))
1169
1170 #define stat_inc_data_blk_count(sbi, blks) \
1171 do { \
1172 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
1173 stat_inc_tot_blk_count(si, blks); \
1174 si->data_blks += (blks); \
1175 } while (0)
1176
1177 #define stat_inc_node_blk_count(sbi, blks) \
1178 do { \
1179 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
1180 stat_inc_tot_blk_count(si, blks); \
1181 si->node_blks += (blks); \
1182 } while (0)
1183
1184 int f2fs_build_stats(struct f2fs_sb_info *);
1185 void f2fs_destroy_stats(struct f2fs_sb_info *);
1186 void __init f2fs_create_root_stats(void);
1187 void f2fs_destroy_root_stats(void);
1188 #else
1189 #define stat_inc_call_count(si)
1190 #define stat_inc_seg_count(si, type)
1191 #define stat_inc_tot_blk_count(si, blks)
1192 #define stat_inc_data_blk_count(si, blks)
1193 #define stat_inc_node_blk_count(sbi, blks)
1194
1195 static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
1196 static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
1197 static inline void __init f2fs_create_root_stats(void) { }
1198 static inline void f2fs_destroy_root_stats(void) { }
1199 #endif
1200
1201 extern const struct file_operations f2fs_dir_operations;
1202 extern const struct file_operations f2fs_file_operations;
1203 extern const struct inode_operations f2fs_file_inode_operations;
1204 extern const struct address_space_operations f2fs_dblock_aops;
1205 extern const struct address_space_operations f2fs_node_aops;
1206 extern const struct address_space_operations f2fs_meta_aops;
1207 extern const struct inode_operations f2fs_dir_inode_operations;
1208 extern const struct inode_operations f2fs_symlink_inode_operations;
1209 extern const struct inode_operations f2fs_special_inode_operations;
1210 #endif
This page took 0.056672 seconds and 6 git commands to generate.