f2fs.h 32 KB

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