node.h 12 KB

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