f2fs.h 33 KB

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