node.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * fs/f2fs/node.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. /* start node id of a node block dedicated to the given node id */
  12. #define START_NID(nid) ((nid / NAT_ENTRY_PER_BLOCK) * NAT_ENTRY_PER_BLOCK)
  13. /* node block offset on the NAT area dedicated to the given start node id */
  14. #define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)
  15. /* # of pages to perform synchronous readahead before building free nids */
  16. #define FREE_NID_PAGES 8
  17. #define MAX_FREE_NIDS (NAT_ENTRY_PER_BLOCK * FREE_NID_PAGES)
  18. #define DEF_RA_NID_PAGES 0 /* # of nid pages to be readaheaded */
  19. /* maximum readahead size for node during getting data blocks */
  20. #define MAX_RA_NODE 128
  21. /* control the memory footprint threshold (10MB per 1GB ram) */
  22. #define DEF_RAM_THRESHOLD 1
  23. /* control dirty nats ratio threshold (default: 10% over max nid count) */
  24. #define DEF_DIRTY_NAT_RATIO_THRESHOLD 10
  25. /* control total # of nats */
  26. #define DEF_NAT_CACHE_THRESHOLD 100000
  27. /* vector size for gang look-up from nat cache that consists of radix tree */
  28. #define NATVEC_SIZE 64
  29. #define SETVEC_SIZE 32
  30. /* return value for read_node_page */
  31. #define LOCKED_PAGE 1
  32. /* For flag in struct node_info */
  33. enum {
  34. IS_CHECKPOINTED, /* is it checkpointed before? */
  35. HAS_FSYNCED_INODE, /* is the inode fsynced before? */
  36. HAS_LAST_FSYNC, /* has the latest node fsync mark? */
  37. IS_DIRTY, /* this nat entry is dirty? */
  38. };
  39. /*
  40. * For node information
  41. */
  42. struct node_info {
  43. nid_t nid; /* node id */
  44. nid_t ino; /* inode number of the node's owner */
  45. block_t blk_addr; /* block address of the node */
  46. unsigned char version; /* version of the node */
  47. unsigned char flag; /* for node information bits */
  48. };
  49. struct nat_entry {
  50. struct list_head list; /* for clean or dirty nat list */
  51. struct node_info ni; /* in-memory node information */
  52. };
  53. #define nat_get_nid(nat) (nat->ni.nid)
  54. #define nat_set_nid(nat, n) (nat->ni.nid = n)
  55. #define nat_get_blkaddr(nat) (nat->ni.blk_addr)
  56. #define nat_set_blkaddr(nat, b) (nat->ni.blk_addr = b)
  57. #define nat_get_ino(nat) (nat->ni.ino)
  58. #define nat_set_ino(nat, i) (nat->ni.ino = i)
  59. #define nat_get_version(nat) (nat->ni.version)
  60. #define nat_set_version(nat, v) (nat->ni.version = v)
  61. #define inc_node_version(version) (++version)
  62. static inline void copy_node_info(struct node_info *dst,
  63. struct node_info *src)
  64. {
  65. dst->nid = src->nid;
  66. dst->ino = src->ino;
  67. dst->blk_addr = src->blk_addr;
  68. dst->version = src->version;
  69. /* should not copy flag here */
  70. }
  71. static inline void set_nat_flag(struct nat_entry *ne,
  72. unsigned int type, bool set)
  73. {
  74. unsigned char mask = 0x01 << type;
  75. if (set)
  76. ne->ni.flag |= mask;
  77. else
  78. ne->ni.flag &= ~mask;
  79. }
  80. static inline bool get_nat_flag(struct nat_entry *ne, unsigned int type)
  81. {
  82. unsigned char mask = 0x01 << type;
  83. return ne->ni.flag & mask;
  84. }
  85. static inline void nat_reset_flag(struct nat_entry *ne)
  86. {
  87. /* these states can be set only after checkpoint was done */
  88. set_nat_flag(ne, IS_CHECKPOINTED, true);
  89. set_nat_flag(ne, HAS_FSYNCED_INODE, false);
  90. set_nat_flag(ne, HAS_LAST_FSYNC, true);
  91. }
  92. static inline void node_info_from_raw_nat(struct node_info *ni,
  93. struct f2fs_nat_entry *raw_ne)
  94. {
  95. ni->ino = le32_to_cpu(raw_ne->ino);
  96. ni->blk_addr = le32_to_cpu(raw_ne->block_addr);
  97. ni->version = raw_ne->version;
  98. }
  99. static inline void raw_nat_from_node_info(struct f2fs_nat_entry *raw_ne,
  100. struct node_info *ni)
  101. {
  102. raw_ne->ino = cpu_to_le32(ni->ino);
  103. raw_ne->block_addr = cpu_to_le32(ni->blk_addr);
  104. raw_ne->version = ni->version;
  105. }
  106. static inline bool excess_dirty_nats(struct f2fs_sb_info *sbi)
  107. {
  108. return NM_I(sbi)->dirty_nat_cnt >= NM_I(sbi)->max_nid *
  109. NM_I(sbi)->dirty_nats_ratio / 100;
  110. }
  111. static inline bool excess_cached_nats(struct f2fs_sb_info *sbi)
  112. {
  113. return NM_I(sbi)->nat_cnt >= DEF_NAT_CACHE_THRESHOLD;
  114. }
  115. enum mem_type {
  116. FREE_NIDS, /* indicates the free nid list */
  117. NAT_ENTRIES, /* indicates the cached nat entry */
  118. DIRTY_DENTS, /* indicates dirty dentry pages */
  119. INO_ENTRIES, /* indicates inode entries */
  120. EXTENT_CACHE, /* indicates extent cache */
  121. BASE_CHECK, /* check kernel status */
  122. };
  123. struct nat_entry_set {
  124. struct list_head set_list; /* link with other nat sets */
  125. struct list_head entry_list; /* link with dirty nat entries */
  126. nid_t set; /* set number*/
  127. unsigned int entry_cnt; /* the # of nat entries in set */
  128. };
  129. /*
  130. * For free nid mangement
  131. */
  132. enum nid_state {
  133. NID_NEW, /* newly added to free nid list */
  134. NID_ALLOC /* it is allocated */
  135. };
  136. struct free_nid {
  137. struct list_head list; /* for free node id list */
  138. nid_t nid; /* node id */
  139. int state; /* in use or not: NID_NEW or NID_ALLOC */
  140. };
  141. static inline void next_free_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  142. {
  143. struct f2fs_nm_info *nm_i = NM_I(sbi);
  144. struct free_nid *fnid;
  145. spin_lock(&nm_i->free_nid_list_lock);
  146. if (nm_i->fcnt <= 0) {
  147. spin_unlock(&nm_i->free_nid_list_lock);
  148. return;
  149. }
  150. fnid = list_entry(nm_i->free_nid_list.next, struct free_nid, list);
  151. *nid = fnid->nid;
  152. spin_unlock(&nm_i->free_nid_list_lock);
  153. }
  154. /*
  155. * inline functions
  156. */
  157. static inline void get_nat_bitmap(struct f2fs_sb_info *sbi, void *addr)
  158. {
  159. struct f2fs_nm_info *nm_i = NM_I(sbi);
  160. memcpy(addr, nm_i->nat_bitmap, nm_i->bitmap_size);
  161. }
  162. static inline pgoff_t current_nat_addr(struct f2fs_sb_info *sbi, nid_t start)
  163. {
  164. struct f2fs_nm_info *nm_i = NM_I(sbi);
  165. pgoff_t block_off;
  166. pgoff_t block_addr;
  167. int seg_off;
  168. block_off = NAT_BLOCK_OFFSET(start);
  169. seg_off = block_off >> sbi->log_blocks_per_seg;
  170. block_addr = (pgoff_t)(nm_i->nat_blkaddr +
  171. (seg_off << sbi->log_blocks_per_seg << 1) +
  172. (block_off & (sbi->blocks_per_seg - 1)));
  173. if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
  174. block_addr += sbi->blocks_per_seg;
  175. return block_addr;
  176. }
  177. static inline pgoff_t next_nat_addr(struct f2fs_sb_info *sbi,
  178. pgoff_t block_addr)
  179. {
  180. struct f2fs_nm_info *nm_i = NM_I(sbi);
  181. block_addr -= nm_i->nat_blkaddr;
  182. if ((block_addr >> sbi->log_blocks_per_seg) % 2)
  183. block_addr -= sbi->blocks_per_seg;
  184. else
  185. block_addr += sbi->blocks_per_seg;
  186. return block_addr + nm_i->nat_blkaddr;
  187. }
  188. static inline void set_to_next_nat(struct f2fs_nm_info *nm_i, nid_t start_nid)
  189. {
  190. unsigned int block_off = NAT_BLOCK_OFFSET(start_nid);
  191. f2fs_change_bit(block_off, nm_i->nat_bitmap);
  192. }
  193. static inline nid_t ino_of_node(struct page *node_page)
  194. {
  195. struct f2fs_node *rn = F2FS_NODE(node_page);
  196. return le32_to_cpu(rn->footer.ino);
  197. }
  198. static inline nid_t nid_of_node(struct page *node_page)
  199. {
  200. struct f2fs_node *rn = F2FS_NODE(node_page);
  201. return le32_to_cpu(rn->footer.nid);
  202. }
  203. static inline unsigned int ofs_of_node(struct page *node_page)
  204. {
  205. struct f2fs_node *rn = F2FS_NODE(node_page);
  206. unsigned flag = le32_to_cpu(rn->footer.flag);
  207. return flag >> OFFSET_BIT_SHIFT;
  208. }
  209. static inline __u64 cpver_of_node(struct page *node_page)
  210. {
  211. struct f2fs_node *rn = F2FS_NODE(node_page);
  212. return le64_to_cpu(rn->footer.cp_ver);
  213. }
  214. static inline block_t next_blkaddr_of_node(struct page *node_page)
  215. {
  216. struct f2fs_node *rn = F2FS_NODE(node_page);
  217. return le32_to_cpu(rn->footer.next_blkaddr);
  218. }
  219. static inline void fill_node_footer(struct page *page, nid_t nid,
  220. nid_t ino, unsigned int ofs, bool reset)
  221. {
  222. struct f2fs_node *rn = F2FS_NODE(page);
  223. unsigned int old_flag = 0;
  224. if (reset)
  225. memset(rn, 0, sizeof(*rn));
  226. else
  227. old_flag = le32_to_cpu(rn->footer.flag);
  228. rn->footer.nid = cpu_to_le32(nid);
  229. rn->footer.ino = cpu_to_le32(ino);
  230. /* should remain old flag bits such as COLD_BIT_SHIFT */
  231. rn->footer.flag = cpu_to_le32((ofs << OFFSET_BIT_SHIFT) |
  232. (old_flag & OFFSET_BIT_MASK));
  233. }
  234. static inline void copy_node_footer(struct page *dst, struct page *src)
  235. {
  236. struct f2fs_node *src_rn = F2FS_NODE(src);
  237. struct f2fs_node *dst_rn = F2FS_NODE(dst);
  238. memcpy(&dst_rn->footer, &src_rn->footer, sizeof(struct node_footer));
  239. }
  240. static inline void fill_node_footer_blkaddr(struct page *page, block_t blkaddr)
  241. {
  242. struct f2fs_checkpoint *ckpt = F2FS_CKPT(F2FS_P_SB(page));
  243. struct f2fs_node *rn = F2FS_NODE(page);
  244. size_t crc_offset = le32_to_cpu(ckpt->checksum_offset);
  245. __u64 cp_ver = le64_to_cpu(ckpt->checkpoint_ver);
  246. if (__is_set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG)) {
  247. __u64 crc = le32_to_cpu(*((__le32 *)
  248. ((unsigned char *)ckpt + crc_offset)));
  249. cp_ver |= (crc << 32);
  250. }
  251. rn->footer.cp_ver = cpu_to_le64(cp_ver);
  252. rn->footer.next_blkaddr = cpu_to_le32(blkaddr);
  253. }
  254. static inline bool is_recoverable_dnode(struct page *page)
  255. {
  256. struct f2fs_checkpoint *ckpt = F2FS_CKPT(F2FS_P_SB(page));
  257. size_t crc_offset = le32_to_cpu(ckpt->checksum_offset);
  258. __u64 cp_ver = cur_cp_version(ckpt);
  259. if (__is_set_ckpt_flags(ckpt, CP_CRC_RECOVERY_FLAG)) {
  260. __u64 crc = le32_to_cpu(*((__le32 *)
  261. ((unsigned char *)ckpt + crc_offset)));
  262. cp_ver |= (crc << 32);
  263. }
  264. return cpu_to_le64(cp_ver) == cpver_of_node(page);
  265. }
  266. /*
  267. * f2fs assigns the following node offsets described as (num).
  268. * N = NIDS_PER_BLOCK
  269. *
  270. * Inode block (0)
  271. * |- direct node (1)
  272. * |- direct node (2)
  273. * |- indirect node (3)
  274. * | `- direct node (4 => 4 + N - 1)
  275. * |- indirect node (4 + N)
  276. * | `- direct node (5 + N => 5 + 2N - 1)
  277. * `- double indirect node (5 + 2N)
  278. * `- indirect node (6 + 2N)
  279. * `- direct node
  280. * ......
  281. * `- indirect node ((6 + 2N) + x(N + 1))
  282. * `- direct node
  283. * ......
  284. * `- indirect node ((6 + 2N) + (N - 1)(N + 1))
  285. * `- direct node
  286. */
  287. static inline bool IS_DNODE(struct page *node_page)
  288. {
  289. unsigned int ofs = ofs_of_node(node_page);
  290. if (f2fs_has_xattr_block(ofs))
  291. return false;
  292. if (ofs == 3 || ofs == 4 + NIDS_PER_BLOCK ||
  293. ofs == 5 + 2 * NIDS_PER_BLOCK)
  294. return false;
  295. if (ofs >= 6 + 2 * NIDS_PER_BLOCK) {
  296. ofs -= 6 + 2 * NIDS_PER_BLOCK;
  297. if (!((long int)ofs % (NIDS_PER_BLOCK + 1)))
  298. return false;
  299. }
  300. return true;
  301. }
  302. static inline int set_nid(struct page *p, int off, nid_t nid, bool i)
  303. {
  304. struct f2fs_node *rn = F2FS_NODE(p);
  305. f2fs_wait_on_page_writeback(p, NODE, true);
  306. if (i)
  307. rn->i.i_nid[off - NODE_DIR1_BLOCK] = cpu_to_le32(nid);
  308. else
  309. rn->in.nid[off] = cpu_to_le32(nid);
  310. return set_page_dirty(p);
  311. }
  312. static inline nid_t get_nid(struct page *p, int off, bool i)
  313. {
  314. struct f2fs_node *rn = F2FS_NODE(p);
  315. if (i)
  316. return le32_to_cpu(rn->i.i_nid[off - NODE_DIR1_BLOCK]);
  317. return le32_to_cpu(rn->in.nid[off]);
  318. }
  319. /*
  320. * Coldness identification:
  321. * - Mark cold files in f2fs_inode_info
  322. * - Mark cold node blocks in their node footer
  323. * - Mark cold data pages in page cache
  324. */
  325. static inline int is_cold_data(struct page *page)
  326. {
  327. return PageChecked(page);
  328. }
  329. static inline void set_cold_data(struct page *page)
  330. {
  331. SetPageChecked(page);
  332. }
  333. static inline void clear_cold_data(struct page *page)
  334. {
  335. ClearPageChecked(page);
  336. }
  337. static inline int is_node(struct page *page, int type)
  338. {
  339. struct f2fs_node *rn = F2FS_NODE(page);
  340. return le32_to_cpu(rn->footer.flag) & (1 << type);
  341. }
  342. #define is_cold_node(page) is_node(page, COLD_BIT_SHIFT)
  343. #define is_fsync_dnode(page) is_node(page, FSYNC_BIT_SHIFT)
  344. #define is_dent_dnode(page) is_node(page, DENT_BIT_SHIFT)
  345. static inline int is_inline_node(struct page *page)
  346. {
  347. return PageChecked(page);
  348. }
  349. static inline void set_inline_node(struct page *page)
  350. {
  351. SetPageChecked(page);
  352. }
  353. static inline void clear_inline_node(struct page *page)
  354. {
  355. ClearPageChecked(page);
  356. }
  357. static inline void set_cold_node(struct inode *inode, struct page *page)
  358. {
  359. struct f2fs_node *rn = F2FS_NODE(page);
  360. unsigned int flag = le32_to_cpu(rn->footer.flag);
  361. if (S_ISDIR(inode->i_mode))
  362. flag &= ~(0x1 << COLD_BIT_SHIFT);
  363. else
  364. flag |= (0x1 << COLD_BIT_SHIFT);
  365. rn->footer.flag = cpu_to_le32(flag);
  366. }
  367. static inline void set_mark(struct page *page, int mark, int type)
  368. {
  369. struct f2fs_node *rn = F2FS_NODE(page);
  370. unsigned int flag = le32_to_cpu(rn->footer.flag);
  371. if (mark)
  372. flag |= (0x1 << type);
  373. else
  374. flag &= ~(0x1 << type);
  375. rn->footer.flag = cpu_to_le32(flag);
  376. }
  377. #define set_dentry_mark(page, mark) set_mark(page, mark, DENT_BIT_SHIFT)
  378. #define set_fsync_mark(page, mark) set_mark(page, mark, FSYNC_BIT_SHIFT)