f2fs.h 33 KB

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