f2fs.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. #include <linux/types.h>
  14. #include <linux/page-flags.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/slab.h>
  17. #include <linux/crc32.h>
  18. #include <linux/magic.h>
  19. /*
  20. * For mount options
  21. */
  22. #define F2FS_MOUNT_BG_GC 0x00000001
  23. #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
  24. #define F2FS_MOUNT_DISCARD 0x00000004
  25. #define F2FS_MOUNT_NOHEAP 0x00000008
  26. #define F2FS_MOUNT_XATTR_USER 0x00000010
  27. #define F2FS_MOUNT_POSIX_ACL 0x00000020
  28. #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
  29. #define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
  30. #define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
  31. #define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
  32. #define ver_after(a, b) (typecheck(unsigned long long, a) && \
  33. typecheck(unsigned long long, b) && \
  34. ((long long)((a) - (b)) > 0))
  35. typedef u64 block_t;
  36. typedef u32 nid_t;
  37. struct f2fs_mount_info {
  38. unsigned int opt;
  39. };
  40. static inline __u32 f2fs_crc32(void *buff, size_t len)
  41. {
  42. return crc32_le(F2FS_SUPER_MAGIC, buff, len);
  43. }
  44. static inline bool f2fs_crc_valid(__u32 blk_crc, void *buff, size_t buff_size)
  45. {
  46. return f2fs_crc32(buff, buff_size) == blk_crc;
  47. }
  48. /*
  49. * For checkpoint manager
  50. */
  51. enum {
  52. NAT_BITMAP,
  53. SIT_BITMAP
  54. };
  55. /* for the list of orphan inodes */
  56. struct orphan_inode_entry {
  57. struct list_head list; /* list head */
  58. nid_t ino; /* inode number */
  59. };
  60. /* for the list of directory inodes */
  61. struct dir_inode_entry {
  62. struct list_head list; /* list head */
  63. struct inode *inode; /* vfs inode pointer */
  64. };
  65. /* for the list of fsync inodes, used only during recovery */
  66. struct fsync_inode_entry {
  67. struct list_head list; /* list head */
  68. struct inode *inode; /* vfs inode pointer */
  69. block_t blkaddr; /* block address locating the last inode */
  70. };
  71. #define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
  72. #define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
  73. #define nat_in_journal(sum, i) (sum->nat_j.entries[i].ne)
  74. #define nid_in_journal(sum, i) (sum->nat_j.entries[i].nid)
  75. #define sit_in_journal(sum, i) (sum->sit_j.entries[i].se)
  76. #define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno)
  77. static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
  78. {
  79. int before = nats_in_cursum(rs);
  80. rs->n_nats = cpu_to_le16(before + i);
  81. return before;
  82. }
  83. static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
  84. {
  85. int before = sits_in_cursum(rs);
  86. rs->n_sits = cpu_to_le16(before + i);
  87. return before;
  88. }
  89. /*
  90. * For INODE and NODE manager
  91. */
  92. #define XATTR_NODE_OFFSET (-1) /*
  93. * store xattrs to one node block per
  94. * file keeping -1 as its node offset to
  95. * distinguish from index node blocks.
  96. */
  97. #define RDONLY_NODE 1 /*
  98. * specify a read-only mode when getting
  99. * a node block. 0 is read-write mode.
  100. * used by get_dnode_of_data().
  101. */
  102. #define F2FS_LINK_MAX 32000 /* maximum link count per file */
  103. /* for in-memory extent cache entry */
  104. struct extent_info {
  105. rwlock_t ext_lock; /* rwlock for consistency */
  106. unsigned int fofs; /* start offset in a file */
  107. u32 blk_addr; /* start block address of the extent */
  108. unsigned int len; /* lenth of the extent */
  109. };
  110. /*
  111. * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
  112. */
  113. #define FADVISE_COLD_BIT 0x01
  114. struct f2fs_inode_info {
  115. struct inode vfs_inode; /* serve a vfs inode */
  116. unsigned long i_flags; /* keep an inode flags for ioctl */
  117. unsigned char i_advise; /* use to give file attribute hints */
  118. unsigned int i_current_depth; /* use only in directory structure */
  119. unsigned int i_pino; /* parent inode number */
  120. umode_t i_acl_mode; /* keep file acl mode temporarily */
  121. /* Use below internally in f2fs*/
  122. unsigned long flags; /* use to pass per-file flags */
  123. unsigned long long data_version;/* lastes version of data for fsync */
  124. atomic_t dirty_dents; /* # of dirty dentry pages */
  125. f2fs_hash_t chash; /* hash value of given file name */
  126. unsigned int clevel; /* maximum level of given file name */
  127. nid_t i_xattr_nid; /* node id that contains xattrs */
  128. struct extent_info ext; /* in-memory extent cache entry */
  129. };
  130. static inline void get_extent_info(struct extent_info *ext,
  131. struct f2fs_extent i_ext)
  132. {
  133. write_lock(&ext->ext_lock);
  134. ext->fofs = le32_to_cpu(i_ext.fofs);
  135. ext->blk_addr = le32_to_cpu(i_ext.blk_addr);
  136. ext->len = le32_to_cpu(i_ext.len);
  137. write_unlock(&ext->ext_lock);
  138. }
  139. static inline void set_raw_extent(struct extent_info *ext,
  140. struct f2fs_extent *i_ext)
  141. {
  142. read_lock(&ext->ext_lock);
  143. i_ext->fofs = cpu_to_le32(ext->fofs);
  144. i_ext->blk_addr = cpu_to_le32(ext->blk_addr);
  145. i_ext->len = cpu_to_le32(ext->len);
  146. read_unlock(&ext->ext_lock);
  147. }
  148. struct f2fs_nm_info {
  149. block_t nat_blkaddr; /* base disk address of NAT */
  150. nid_t max_nid; /* maximum possible node ids */
  151. nid_t init_scan_nid; /* the first nid to be scanned */
  152. nid_t next_scan_nid; /* the next nid to be scanned */
  153. /* NAT cache management */
  154. struct radix_tree_root nat_root;/* root of the nat entry cache */
  155. rwlock_t nat_tree_lock; /* protect nat_tree_lock */
  156. unsigned int nat_cnt; /* the # of cached nat entries */
  157. struct list_head nat_entries; /* cached nat entry list (clean) */
  158. struct list_head dirty_nat_entries; /* cached nat entry list (dirty) */
  159. /* free node ids management */
  160. struct list_head free_nid_list; /* a list for free nids */
  161. spinlock_t free_nid_list_lock; /* protect free nid list */
  162. unsigned int fcnt; /* the number of free node id */
  163. struct mutex build_lock; /* lock for build free nids */
  164. /* for checkpoint */
  165. char *nat_bitmap; /* NAT bitmap pointer */
  166. int bitmap_size; /* bitmap size */
  167. };
  168. /*
  169. * this structure is used as one of function parameters.
  170. * all the information are dedicated to a given direct node block determined
  171. * by the data offset in a file.
  172. */
  173. struct dnode_of_data {
  174. struct inode *inode; /* vfs inode pointer */
  175. struct page *inode_page; /* its inode page, NULL is possible */
  176. struct page *node_page; /* cached direct node page */
  177. nid_t nid; /* node id of the direct node block */
  178. unsigned int ofs_in_node; /* data offset in the node page */
  179. bool inode_page_locked; /* inode page is locked or not */
  180. block_t data_blkaddr; /* block address of the node block */
  181. };
  182. static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
  183. struct page *ipage, struct page *npage, nid_t nid)
  184. {
  185. memset(dn, 0, sizeof(*dn));
  186. dn->inode = inode;
  187. dn->inode_page = ipage;
  188. dn->node_page = npage;
  189. dn->nid = nid;
  190. }
  191. /*
  192. * For SIT manager
  193. *
  194. * By default, there are 6 active log areas across the whole main area.
  195. * When considering hot and cold data separation to reduce cleaning overhead,
  196. * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
  197. * respectively.
  198. * In the current design, you should not change the numbers intentionally.
  199. * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
  200. * logs individually according to the underlying devices. (default: 6)
  201. * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
  202. * data and 8 for node logs.
  203. */
  204. #define NR_CURSEG_DATA_TYPE (3)
  205. #define NR_CURSEG_NODE_TYPE (3)
  206. #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
  207. enum {
  208. CURSEG_HOT_DATA = 0, /* directory entry blocks */
  209. CURSEG_WARM_DATA, /* data blocks */
  210. CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
  211. CURSEG_HOT_NODE, /* direct node blocks of directory files */
  212. CURSEG_WARM_NODE, /* direct node blocks of normal files */
  213. CURSEG_COLD_NODE, /* indirect node blocks */
  214. NO_CHECK_TYPE
  215. };
  216. struct f2fs_sm_info {
  217. struct sit_info *sit_info; /* whole segment information */
  218. struct free_segmap_info *free_info; /* free segment information */
  219. struct dirty_seglist_info *dirty_info; /* dirty segment information */
  220. struct curseg_info *curseg_array; /* active segment information */
  221. struct list_head wblist_head; /* list of under-writeback pages */
  222. spinlock_t wblist_lock; /* lock for checkpoint */
  223. block_t seg0_blkaddr; /* block address of 0'th segment */
  224. block_t main_blkaddr; /* start block address of main area */
  225. block_t ssa_blkaddr; /* start block address of SSA area */
  226. unsigned int segment_count; /* total # of segments */
  227. unsigned int main_segments; /* # of segments in main area */
  228. unsigned int reserved_segments; /* # of reserved segments */
  229. unsigned int ovp_segments; /* # of overprovision segments */
  230. };
  231. /*
  232. * For directory operation
  233. */
  234. #define NODE_DIR1_BLOCK (ADDRS_PER_INODE + 1)
  235. #define NODE_DIR2_BLOCK (ADDRS_PER_INODE + 2)
  236. #define NODE_IND1_BLOCK (ADDRS_PER_INODE + 3)
  237. #define NODE_IND2_BLOCK (ADDRS_PER_INODE + 4)
  238. #define NODE_DIND_BLOCK (ADDRS_PER_INODE + 5)
  239. /*
  240. * For superblock
  241. */
  242. /*
  243. * COUNT_TYPE for monitoring
  244. *
  245. * f2fs monitors the number of several block types such as on-writeback,
  246. * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
  247. */
  248. enum count_type {
  249. F2FS_WRITEBACK,
  250. F2FS_DIRTY_DENTS,
  251. F2FS_DIRTY_NODES,
  252. F2FS_DIRTY_META,
  253. NR_COUNT_TYPE,
  254. };
  255. /*
  256. * FS_LOCK nesting subclasses for the lock validator:
  257. *
  258. * The locking order between these classes is
  259. * RENAME -> DENTRY_OPS -> DATA_WRITE -> DATA_NEW
  260. * -> DATA_TRUNC -> NODE_WRITE -> NODE_NEW -> NODE_TRUNC
  261. */
  262. enum lock_type {
  263. RENAME, /* for renaming operations */
  264. DENTRY_OPS, /* for directory operations */
  265. DATA_WRITE, /* for data write */
  266. DATA_NEW, /* for data allocation */
  267. DATA_TRUNC, /* for data truncate */
  268. NODE_NEW, /* for node allocation */
  269. NODE_TRUNC, /* for node truncate */
  270. NODE_WRITE, /* for node write */
  271. NR_LOCK_TYPE,
  272. };
  273. /*
  274. * The below are the page types of bios used in submti_bio().
  275. * The available types are:
  276. * DATA User data pages. It operates as async mode.
  277. * NODE Node pages. It operates as async mode.
  278. * META FS metadata pages such as SIT, NAT, CP.
  279. * NR_PAGE_TYPE The number of page types.
  280. * META_FLUSH Make sure the previous pages are written
  281. * with waiting the bio's completion
  282. * ... Only can be used with META.
  283. */
  284. enum page_type {
  285. DATA,
  286. NODE,
  287. META,
  288. NR_PAGE_TYPE,
  289. META_FLUSH,
  290. };
  291. struct f2fs_sb_info {
  292. struct super_block *sb; /* pointer to VFS super block */
  293. struct buffer_head *raw_super_buf; /* buffer head of raw sb */
  294. struct f2fs_super_block *raw_super; /* raw super block pointer */
  295. int s_dirty; /* dirty flag for checkpoint */
  296. /* for node-related operations */
  297. struct f2fs_nm_info *nm_info; /* node manager */
  298. struct inode *node_inode; /* cache node blocks */
  299. /* for segment-related operations */
  300. struct f2fs_sm_info *sm_info; /* segment manager */
  301. struct bio *bio[NR_PAGE_TYPE]; /* bios to merge */
  302. sector_t last_block_in_bio[NR_PAGE_TYPE]; /* last block number */
  303. struct rw_semaphore bio_sem; /* IO semaphore */
  304. /* for checkpoint */
  305. struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
  306. struct inode *meta_inode; /* cache meta blocks */
  307. struct mutex cp_mutex; /* for checkpoint procedure */
  308. struct mutex fs_lock[NR_LOCK_TYPE]; /* for blocking FS operations */
  309. struct mutex write_inode; /* mutex for write inode */
  310. struct mutex writepages; /* mutex for writepages() */
  311. int por_doing; /* recovery is doing or not */
  312. /* for orphan inode management */
  313. struct list_head orphan_inode_list; /* orphan inode list */
  314. struct mutex orphan_inode_mutex; /* for orphan inode list */
  315. unsigned int n_orphans; /* # of orphan inodes */
  316. /* for directory inode management */
  317. struct list_head dir_inode_list; /* dir inode list */
  318. spinlock_t dir_inode_lock; /* for dir inode list lock */
  319. unsigned int n_dirty_dirs; /* # of dir inodes */
  320. /* basic file system units */
  321. unsigned int log_sectors_per_block; /* log2 sectors per block */
  322. unsigned int log_blocksize; /* log2 block size */
  323. unsigned int blocksize; /* block size */
  324. unsigned int root_ino_num; /* root inode number*/
  325. unsigned int node_ino_num; /* node inode number*/
  326. unsigned int meta_ino_num; /* meta inode number*/
  327. unsigned int log_blocks_per_seg; /* log2 blocks per segment */
  328. unsigned int blocks_per_seg; /* blocks per segment */
  329. unsigned int segs_per_sec; /* segments per section */
  330. unsigned int secs_per_zone; /* sections per zone */
  331. unsigned int total_sections; /* total section count */
  332. unsigned int total_node_count; /* total node block count */
  333. unsigned int total_valid_node_count; /* valid node block count */
  334. unsigned int total_valid_inode_count; /* valid inode count */
  335. int active_logs; /* # of active logs */
  336. block_t user_block_count; /* # of user blocks */
  337. block_t total_valid_block_count; /* # of valid blocks */
  338. block_t alloc_valid_block_count; /* # of allocated blocks */
  339. block_t last_valid_block_count; /* for recovery */
  340. u32 s_next_generation; /* for NFS support */
  341. atomic_t nr_pages[NR_COUNT_TYPE]; /* # of pages, see count_type */
  342. struct f2fs_mount_info mount_opt; /* mount options */
  343. /* for cleaning operations */
  344. struct mutex gc_mutex; /* mutex for GC */
  345. struct f2fs_gc_kthread *gc_thread; /* GC thread */
  346. /*
  347. * for stat information.
  348. * one is for the LFS mode, and the other is for the SSR mode.
  349. */
  350. struct f2fs_stat_info *stat_info; /* FS status information */
  351. unsigned int segment_count[2]; /* # of allocated segments */
  352. unsigned int block_count[2]; /* # of allocated blocks */
  353. unsigned int last_victim[2]; /* last victim segment # */
  354. int total_hit_ext, read_hit_ext; /* extent cache hit ratio */
  355. int bg_gc; /* background gc calls */
  356. spinlock_t stat_lock; /* lock for stat operations */
  357. };
  358. /*
  359. * Inline functions
  360. */
  361. static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
  362. {
  363. return container_of(inode, struct f2fs_inode_info, vfs_inode);
  364. }
  365. static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
  366. {
  367. return sb->s_fs_info;
  368. }
  369. static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
  370. {
  371. return (struct f2fs_super_block *)(sbi->raw_super);
  372. }
  373. static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
  374. {
  375. return (struct f2fs_checkpoint *)(sbi->ckpt);
  376. }
  377. static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
  378. {
  379. return (struct f2fs_nm_info *)(sbi->nm_info);
  380. }
  381. static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
  382. {
  383. return (struct f2fs_sm_info *)(sbi->sm_info);
  384. }
  385. static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
  386. {
  387. return (struct sit_info *)(SM_I(sbi)->sit_info);
  388. }
  389. static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
  390. {
  391. return (struct free_segmap_info *)(SM_I(sbi)->free_info);
  392. }
  393. static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
  394. {
  395. return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
  396. }
  397. static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info *sbi)
  398. {
  399. sbi->s_dirty = 1;
  400. }
  401. static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info *sbi)
  402. {
  403. sbi->s_dirty = 0;
  404. }
  405. static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  406. {
  407. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  408. return ckpt_flags & f;
  409. }
  410. static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  411. {
  412. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  413. ckpt_flags |= f;
  414. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  415. }
  416. static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  417. {
  418. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  419. ckpt_flags &= (~f);
  420. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  421. }
  422. static inline void mutex_lock_op(struct f2fs_sb_info *sbi, enum lock_type t)
  423. {
  424. mutex_lock_nested(&sbi->fs_lock[t], t);
  425. }
  426. static inline void mutex_unlock_op(struct f2fs_sb_info *sbi, enum lock_type t)
  427. {
  428. mutex_unlock(&sbi->fs_lock[t]);
  429. }
  430. /*
  431. * Check whether the given nid is within node id range.
  432. */
  433. static inline void check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
  434. {
  435. BUG_ON((nid >= NM_I(sbi)->max_nid));
  436. }
  437. #define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
  438. /*
  439. * Check whether the inode has blocks or not
  440. */
  441. static inline int F2FS_HAS_BLOCKS(struct inode *inode)
  442. {
  443. if (F2FS_I(inode)->i_xattr_nid)
  444. return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1);
  445. else
  446. return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS);
  447. }
  448. static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
  449. struct inode *inode, blkcnt_t count)
  450. {
  451. block_t valid_block_count;
  452. spin_lock(&sbi->stat_lock);
  453. valid_block_count =
  454. sbi->total_valid_block_count + (block_t)count;
  455. if (valid_block_count > sbi->user_block_count) {
  456. spin_unlock(&sbi->stat_lock);
  457. return false;
  458. }
  459. inode->i_blocks += count;
  460. sbi->total_valid_block_count = valid_block_count;
  461. sbi->alloc_valid_block_count += (block_t)count;
  462. spin_unlock(&sbi->stat_lock);
  463. return true;
  464. }
  465. static inline int dec_valid_block_count(struct f2fs_sb_info *sbi,
  466. struct inode *inode,
  467. blkcnt_t count)
  468. {
  469. spin_lock(&sbi->stat_lock);
  470. BUG_ON(sbi->total_valid_block_count < (block_t) count);
  471. BUG_ON(inode->i_blocks < count);
  472. inode->i_blocks -= count;
  473. sbi->total_valid_block_count -= (block_t)count;
  474. spin_unlock(&sbi->stat_lock);
  475. return 0;
  476. }
  477. static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
  478. {
  479. atomic_inc(&sbi->nr_pages[count_type]);
  480. F2FS_SET_SB_DIRT(sbi);
  481. }
  482. static inline void inode_inc_dirty_dents(struct inode *inode)
  483. {
  484. atomic_inc(&F2FS_I(inode)->dirty_dents);
  485. }
  486. static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
  487. {
  488. atomic_dec(&sbi->nr_pages[count_type]);
  489. }
  490. static inline void inode_dec_dirty_dents(struct inode *inode)
  491. {
  492. atomic_dec(&F2FS_I(inode)->dirty_dents);
  493. }
  494. static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
  495. {
  496. return atomic_read(&sbi->nr_pages[count_type]);
  497. }
  498. static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
  499. {
  500. block_t ret;
  501. spin_lock(&sbi->stat_lock);
  502. ret = sbi->total_valid_block_count;
  503. spin_unlock(&sbi->stat_lock);
  504. return ret;
  505. }
  506. static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
  507. {
  508. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  509. /* return NAT or SIT bitmap */
  510. if (flag == NAT_BITMAP)
  511. return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  512. else if (flag == SIT_BITMAP)
  513. return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  514. return 0;
  515. }
  516. static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
  517. {
  518. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  519. int offset = (flag == NAT_BITMAP) ?
  520. le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
  521. return &ckpt->sit_nat_version_bitmap + offset;
  522. }
  523. static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
  524. {
  525. block_t start_addr;
  526. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  527. unsigned long long ckpt_version = le64_to_cpu(ckpt->checkpoint_ver);
  528. start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  529. /*
  530. * odd numbered checkpoint should at cp segment 0
  531. * and even segent must be at cp segment 1
  532. */
  533. if (!(ckpt_version & 1))
  534. start_addr += sbi->blocks_per_seg;
  535. return start_addr;
  536. }
  537. static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
  538. {
  539. return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  540. }
  541. static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
  542. struct inode *inode,
  543. unsigned int count)
  544. {
  545. block_t valid_block_count;
  546. unsigned int valid_node_count;
  547. spin_lock(&sbi->stat_lock);
  548. valid_block_count = sbi->total_valid_block_count + (block_t)count;
  549. sbi->alloc_valid_block_count += (block_t)count;
  550. valid_node_count = sbi->total_valid_node_count + count;
  551. if (valid_block_count > sbi->user_block_count) {
  552. spin_unlock(&sbi->stat_lock);
  553. return false;
  554. }
  555. if (valid_node_count > sbi->total_node_count) {
  556. spin_unlock(&sbi->stat_lock);
  557. return false;
  558. }
  559. if (inode)
  560. inode->i_blocks += count;
  561. sbi->total_valid_node_count = valid_node_count;
  562. sbi->total_valid_block_count = valid_block_count;
  563. spin_unlock(&sbi->stat_lock);
  564. return true;
  565. }
  566. static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
  567. struct inode *inode,
  568. unsigned int count)
  569. {
  570. spin_lock(&sbi->stat_lock);
  571. BUG_ON(sbi->total_valid_block_count < count);
  572. BUG_ON(sbi->total_valid_node_count < count);
  573. BUG_ON(inode->i_blocks < count);
  574. inode->i_blocks -= count;
  575. sbi->total_valid_node_count -= count;
  576. sbi->total_valid_block_count -= (block_t)count;
  577. spin_unlock(&sbi->stat_lock);
  578. }
  579. static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
  580. {
  581. unsigned int ret;
  582. spin_lock(&sbi->stat_lock);
  583. ret = sbi->total_valid_node_count;
  584. spin_unlock(&sbi->stat_lock);
  585. return ret;
  586. }
  587. static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
  588. {
  589. spin_lock(&sbi->stat_lock);
  590. BUG_ON(sbi->total_valid_inode_count == sbi->total_node_count);
  591. sbi->total_valid_inode_count++;
  592. spin_unlock(&sbi->stat_lock);
  593. }
  594. static inline int dec_valid_inode_count(struct f2fs_sb_info *sbi)
  595. {
  596. spin_lock(&sbi->stat_lock);
  597. BUG_ON(!sbi->total_valid_inode_count);
  598. sbi->total_valid_inode_count--;
  599. spin_unlock(&sbi->stat_lock);
  600. return 0;
  601. }
  602. static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
  603. {
  604. unsigned int ret;
  605. spin_lock(&sbi->stat_lock);
  606. ret = sbi->total_valid_inode_count;
  607. spin_unlock(&sbi->stat_lock);
  608. return ret;
  609. }
  610. static inline void f2fs_put_page(struct page *page, int unlock)
  611. {
  612. if (!page || IS_ERR(page))
  613. return;
  614. if (unlock) {
  615. BUG_ON(!PageLocked(page));
  616. unlock_page(page);
  617. }
  618. page_cache_release(page);
  619. }
  620. static inline void f2fs_put_dnode(struct dnode_of_data *dn)
  621. {
  622. if (dn->node_page)
  623. f2fs_put_page(dn->node_page, 1);
  624. if (dn->inode_page && dn->node_page != dn->inode_page)
  625. f2fs_put_page(dn->inode_page, 0);
  626. dn->node_page = NULL;
  627. dn->inode_page = NULL;
  628. }
  629. static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
  630. size_t size, void (*ctor)(void *))
  631. {
  632. return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, ctor);
  633. }
  634. #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
  635. static inline bool IS_INODE(struct page *page)
  636. {
  637. struct f2fs_node *p = (struct f2fs_node *)page_address(page);
  638. return RAW_IS_INODE(p);
  639. }
  640. static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
  641. {
  642. return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
  643. }
  644. static inline block_t datablock_addr(struct page *node_page,
  645. unsigned int offset)
  646. {
  647. struct f2fs_node *raw_node;
  648. __le32 *addr_array;
  649. raw_node = (struct f2fs_node *)page_address(node_page);
  650. addr_array = blkaddr_in_node(raw_node);
  651. return le32_to_cpu(addr_array[offset]);
  652. }
  653. static inline int f2fs_test_bit(unsigned int nr, char *addr)
  654. {
  655. int mask;
  656. addr += (nr >> 3);
  657. mask = 1 << (7 - (nr & 0x07));
  658. return mask & *addr;
  659. }
  660. static inline int f2fs_set_bit(unsigned int nr, char *addr)
  661. {
  662. int mask;
  663. int ret;
  664. addr += (nr >> 3);
  665. mask = 1 << (7 - (nr & 0x07));
  666. ret = mask & *addr;
  667. *addr |= mask;
  668. return ret;
  669. }
  670. static inline int f2fs_clear_bit(unsigned int nr, char *addr)
  671. {
  672. int mask;
  673. int ret;
  674. addr += (nr >> 3);
  675. mask = 1 << (7 - (nr & 0x07));
  676. ret = mask & *addr;
  677. *addr &= ~mask;
  678. return ret;
  679. }
  680. /* used for f2fs_inode_info->flags */
  681. enum {
  682. FI_NEW_INODE, /* indicate newly allocated inode */
  683. FI_NEED_CP, /* need to do checkpoint during fsync */
  684. FI_INC_LINK, /* need to increment i_nlink */
  685. FI_ACL_MODE, /* indicate acl mode */
  686. FI_NO_ALLOC, /* should not allocate any blocks */
  687. };
  688. static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
  689. {
  690. set_bit(flag, &fi->flags);
  691. }
  692. static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
  693. {
  694. return test_bit(flag, &fi->flags);
  695. }
  696. static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
  697. {
  698. clear_bit(flag, &fi->flags);
  699. }
  700. static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
  701. {
  702. fi->i_acl_mode = mode;
  703. set_inode_flag(fi, FI_ACL_MODE);
  704. }
  705. static inline int cond_clear_inode_flag(struct f2fs_inode_info *fi, int flag)
  706. {
  707. if (is_inode_flag_set(fi, FI_ACL_MODE)) {
  708. clear_inode_flag(fi, FI_ACL_MODE);
  709. return 1;
  710. }
  711. return 0;
  712. }
  713. /*
  714. * file.c
  715. */
  716. int f2fs_sync_file(struct file *, loff_t, loff_t, int);
  717. void truncate_data_blocks(struct dnode_of_data *);
  718. void f2fs_truncate(struct inode *);
  719. int f2fs_setattr(struct dentry *, struct iattr *);
  720. int truncate_hole(struct inode *, pgoff_t, pgoff_t);
  721. long f2fs_ioctl(struct file *, unsigned int, unsigned long);
  722. /*
  723. * inode.c
  724. */
  725. void f2fs_set_inode_flags(struct inode *);
  726. struct inode *f2fs_iget_nowait(struct super_block *, unsigned long);
  727. struct inode *f2fs_iget(struct super_block *, unsigned long);
  728. void update_inode(struct inode *, struct page *);
  729. int f2fs_write_inode(struct inode *, struct writeback_control *);
  730. void f2fs_evict_inode(struct inode *);
  731. /*
  732. * namei.c
  733. */
  734. struct dentry *f2fs_get_parent(struct dentry *child);
  735. /*
  736. * dir.c
  737. */
  738. struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
  739. struct page **);
  740. struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
  741. ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
  742. void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
  743. struct page *, struct inode *);
  744. void init_dent_inode(struct dentry *, struct page *);
  745. int f2fs_add_link(struct dentry *, struct inode *);
  746. void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *);
  747. int f2fs_make_empty(struct inode *, struct inode *);
  748. bool f2fs_empty_dir(struct inode *);
  749. /*
  750. * super.c
  751. */
  752. int f2fs_sync_fs(struct super_block *, int);
  753. extern __printf(3, 4)
  754. void f2fs_msg(struct super_block *, const char *, const char *, ...);
  755. /*
  756. * hash.c
  757. */
  758. f2fs_hash_t f2fs_dentry_hash(const char *, size_t);
  759. /*
  760. * node.c
  761. */
  762. struct dnode_of_data;
  763. struct node_info;
  764. int is_checkpointed_node(struct f2fs_sb_info *, nid_t);
  765. void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
  766. int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
  767. int truncate_inode_blocks(struct inode *, pgoff_t);
  768. int remove_inode_page(struct inode *);
  769. int new_inode_page(struct inode *, struct dentry *);
  770. struct page *new_node_page(struct dnode_of_data *, unsigned int);
  771. void ra_node_page(struct f2fs_sb_info *, nid_t);
  772. struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
  773. struct page *get_node_page_ra(struct page *, int);
  774. void sync_inode_page(struct dnode_of_data *);
  775. int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
  776. bool alloc_nid(struct f2fs_sb_info *, nid_t *);
  777. void alloc_nid_done(struct f2fs_sb_info *, nid_t);
  778. void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
  779. void recover_node_page(struct f2fs_sb_info *, struct page *,
  780. struct f2fs_summary *, struct node_info *, block_t);
  781. int recover_inode_page(struct f2fs_sb_info *, struct page *);
  782. int restore_node_summary(struct f2fs_sb_info *, unsigned int,
  783. struct f2fs_summary_block *);
  784. void flush_nat_entries(struct f2fs_sb_info *);
  785. int build_node_manager(struct f2fs_sb_info *);
  786. void destroy_node_manager(struct f2fs_sb_info *);
  787. int __init create_node_manager_caches(void);
  788. void destroy_node_manager_caches(void);
  789. /*
  790. * segment.c
  791. */
  792. void f2fs_balance_fs(struct f2fs_sb_info *);
  793. void invalidate_blocks(struct f2fs_sb_info *, block_t);
  794. void locate_dirty_segment(struct f2fs_sb_info *, unsigned int);
  795. void clear_prefree_segments(struct f2fs_sb_info *);
  796. int npages_for_summary_flush(struct f2fs_sb_info *);
  797. void allocate_new_segments(struct f2fs_sb_info *);
  798. struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
  799. struct bio *f2fs_bio_alloc(struct block_device *, int);
  800. void f2fs_submit_bio(struct f2fs_sb_info *, enum page_type, bool sync);
  801. int write_meta_page(struct f2fs_sb_info *, struct page *,
  802. struct writeback_control *);
  803. void write_node_page(struct f2fs_sb_info *, struct page *, unsigned int,
  804. block_t, block_t *);
  805. void write_data_page(struct inode *, struct page *, struct dnode_of_data*,
  806. block_t, block_t *);
  807. void rewrite_data_page(struct f2fs_sb_info *, struct page *, block_t);
  808. void recover_data_page(struct f2fs_sb_info *, struct page *,
  809. struct f2fs_summary *, block_t, block_t);
  810. void rewrite_node_page(struct f2fs_sb_info *, struct page *,
  811. struct f2fs_summary *, block_t, block_t);
  812. void write_data_summaries(struct f2fs_sb_info *, block_t);
  813. void write_node_summaries(struct f2fs_sb_info *, block_t);
  814. int lookup_journal_in_cursum(struct f2fs_summary_block *,
  815. int, unsigned int, int);
  816. void flush_sit_entries(struct f2fs_sb_info *);
  817. int build_segment_manager(struct f2fs_sb_info *);
  818. void reset_victim_segmap(struct f2fs_sb_info *);
  819. void destroy_segment_manager(struct f2fs_sb_info *);
  820. /*
  821. * checkpoint.c
  822. */
  823. struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
  824. struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
  825. long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
  826. int check_orphan_space(struct f2fs_sb_info *);
  827. void add_orphan_inode(struct f2fs_sb_info *, nid_t);
  828. void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
  829. int recover_orphan_inodes(struct f2fs_sb_info *);
  830. int get_valid_checkpoint(struct f2fs_sb_info *);
  831. void set_dirty_dir_page(struct inode *, struct page *);
  832. void remove_dirty_dir_inode(struct inode *);
  833. void sync_dirty_dir_inodes(struct f2fs_sb_info *);
  834. void block_operations(struct f2fs_sb_info *);
  835. void write_checkpoint(struct f2fs_sb_info *, bool, bool);
  836. void init_orphan_info(struct f2fs_sb_info *);
  837. int __init create_checkpoint_caches(void);
  838. void destroy_checkpoint_caches(void);
  839. /*
  840. * data.c
  841. */
  842. int reserve_new_block(struct dnode_of_data *);
  843. void update_extent_cache(block_t, struct dnode_of_data *);
  844. struct page *find_data_page(struct inode *, pgoff_t);
  845. struct page *get_lock_data_page(struct inode *, pgoff_t);
  846. struct page *get_new_data_page(struct inode *, pgoff_t, bool);
  847. int f2fs_readpage(struct f2fs_sb_info *, struct page *, block_t, int);
  848. int do_write_data_page(struct page *);
  849. /*
  850. * gc.c
  851. */
  852. int start_gc_thread(struct f2fs_sb_info *);
  853. void stop_gc_thread(struct f2fs_sb_info *);
  854. block_t start_bidx_of_node(unsigned int);
  855. int f2fs_gc(struct f2fs_sb_info *);
  856. void build_gc_manager(struct f2fs_sb_info *);
  857. int __init create_gc_caches(void);
  858. void destroy_gc_caches(void);
  859. /*
  860. * recovery.c
  861. */
  862. void recover_fsync_data(struct f2fs_sb_info *);
  863. bool space_for_roll_forward(struct f2fs_sb_info *);
  864. /*
  865. * debug.c
  866. */
  867. #ifdef CONFIG_F2FS_STAT_FS
  868. struct f2fs_stat_info {
  869. struct list_head stat_list;
  870. struct f2fs_sb_info *sbi;
  871. struct mutex stat_lock;
  872. int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
  873. int main_area_segs, main_area_sections, main_area_zones;
  874. int hit_ext, total_ext;
  875. int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
  876. int nats, sits, fnids;
  877. int total_count, utilization;
  878. int bg_gc;
  879. unsigned int valid_count, valid_node_count, valid_inode_count;
  880. unsigned int bimodal, avg_vblocks;
  881. int util_free, util_valid, util_invalid;
  882. int rsvd_segs, overp_segs;
  883. int dirty_count, node_pages, meta_pages;
  884. int prefree_count, call_count;
  885. int tot_segs, node_segs, data_segs, free_segs, free_secs;
  886. int tot_blks, data_blks, node_blks;
  887. int curseg[NR_CURSEG_TYPE];
  888. int cursec[NR_CURSEG_TYPE];
  889. int curzone[NR_CURSEG_TYPE];
  890. unsigned int segment_count[2];
  891. unsigned int block_count[2];
  892. unsigned base_mem, cache_mem;
  893. };
  894. #define stat_inc_call_count(si) ((si)->call_count++)
  895. #define stat_inc_seg_count(sbi, type) \
  896. do { \
  897. struct f2fs_stat_info *si = sbi->stat_info; \
  898. (si)->tot_segs++; \
  899. if (type == SUM_TYPE_DATA) \
  900. si->data_segs++; \
  901. else \
  902. si->node_segs++; \
  903. } while (0)
  904. #define stat_inc_tot_blk_count(si, blks) \
  905. (si->tot_blks += (blks))
  906. #define stat_inc_data_blk_count(sbi, blks) \
  907. do { \
  908. struct f2fs_stat_info *si = sbi->stat_info; \
  909. stat_inc_tot_blk_count(si, blks); \
  910. si->data_blks += (blks); \
  911. } while (0)
  912. #define stat_inc_node_blk_count(sbi, blks) \
  913. do { \
  914. struct f2fs_stat_info *si = sbi->stat_info; \
  915. stat_inc_tot_blk_count(si, blks); \
  916. si->node_blks += (blks); \
  917. } while (0)
  918. int f2fs_build_stats(struct f2fs_sb_info *);
  919. void f2fs_destroy_stats(struct f2fs_sb_info *);
  920. void __init f2fs_create_root_stats(void);
  921. void f2fs_destroy_root_stats(void);
  922. #else
  923. #define stat_inc_call_count(si)
  924. #define stat_inc_seg_count(si, type)
  925. #define stat_inc_tot_blk_count(si, blks)
  926. #define stat_inc_data_blk_count(si, blks)
  927. #define stat_inc_node_blk_count(sbi, blks)
  928. static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
  929. static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
  930. static inline void __init f2fs_create_root_stats(void) { }
  931. static inline void f2fs_destroy_root_stats(void) { }
  932. #endif
  933. extern const struct file_operations f2fs_dir_operations;
  934. extern const struct file_operations f2fs_file_operations;
  935. extern const struct inode_operations f2fs_file_inode_operations;
  936. extern const struct address_space_operations f2fs_dblock_aops;
  937. extern const struct address_space_operations f2fs_node_aops;
  938. extern const struct address_space_operations f2fs_meta_aops;
  939. extern const struct inode_operations f2fs_dir_inode_operations;
  940. extern const struct inode_operations f2fs_symlink_inode_operations;
  941. extern const struct inode_operations f2fs_special_inode_operations;
  942. #endif