node.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 readahead before building free nids */
  16. #define FREE_NID_PAGES 4
  17. /* maximum readahead size for node during getting data blocks */
  18. #define MAX_RA_NODE 128
  19. /* control the memory footprint threshold (10MB per 1GB ram) */
  20. #define DEF_RAM_THRESHOLD 10
  21. /* vector size for gang look-up from nat cache that consists of radix tree */
  22. #define NATVEC_SIZE 64
  23. /* return value for read_node_page */
  24. #define LOCKED_PAGE 1
  25. /*
  26. * For node information
  27. */
  28. struct node_info {
  29. nid_t nid; /* node id */
  30. nid_t ino; /* inode number of the node's owner */
  31. block_t blk_addr; /* block address of the node */
  32. unsigned char version; /* version of the node */
  33. };
  34. struct nat_entry {
  35. struct list_head list; /* for clean or dirty nat list */
  36. bool checkpointed; /* whether it is checkpointed or not */
  37. bool fsync_done; /* whether the latest node has fsync mark */
  38. struct node_info ni; /* in-memory node information */
  39. };
  40. #define nat_get_nid(nat) (nat->ni.nid)
  41. #define nat_set_nid(nat, n) (nat->ni.nid = n)
  42. #define nat_get_blkaddr(nat) (nat->ni.blk_addr)
  43. #define nat_set_blkaddr(nat, b) (nat->ni.blk_addr = b)
  44. #define nat_get_ino(nat) (nat->ni.ino)
  45. #define nat_set_ino(nat, i) (nat->ni.ino = i)
  46. #define nat_get_version(nat) (nat->ni.version)
  47. #define nat_set_version(nat, v) (nat->ni.version = v)
  48. #define __set_nat_cache_dirty(nm_i, ne) \
  49. do { \
  50. ne->checkpointed = false; \
  51. list_move_tail(&ne->list, &nm_i->dirty_nat_entries); \
  52. } while (0)
  53. #define __clear_nat_cache_dirty(nm_i, ne) \
  54. do { \
  55. ne->checkpointed = true; \
  56. list_move_tail(&ne->list, &nm_i->nat_entries); \
  57. } while (0)
  58. #define inc_node_version(version) (++version)
  59. static inline void node_info_from_raw_nat(struct node_info *ni,
  60. struct f2fs_nat_entry *raw_ne)
  61. {
  62. ni->ino = le32_to_cpu(raw_ne->ino);
  63. ni->blk_addr = le32_to_cpu(raw_ne->block_addr);
  64. ni->version = raw_ne->version;
  65. }
  66. static inline void raw_nat_from_node_info(struct f2fs_nat_entry *raw_ne,
  67. struct node_info *ni)
  68. {
  69. raw_ne->ino = cpu_to_le32(ni->ino);
  70. raw_ne->block_addr = cpu_to_le32(ni->blk_addr);
  71. raw_ne->version = ni->version;
  72. }
  73. enum mem_type {
  74. FREE_NIDS, /* indicates the free nid list */
  75. NAT_ENTRIES, /* indicates the cached nat entry */
  76. DIRTY_DENTS /* indicates dirty dentry pages */
  77. };
  78. struct nat_entry_set {
  79. struct list_head set_list; /* link with all nat sets */
  80. struct list_head entry_list; /* link with dirty nat entries */
  81. nid_t start_nid; /* start nid of nats in set */
  82. unsigned int entry_cnt; /* the # of nat entries in set */
  83. };
  84. /*
  85. * For free nid mangement
  86. */
  87. enum nid_state {
  88. NID_NEW, /* newly added to free nid list */
  89. NID_ALLOC /* it is allocated */
  90. };
  91. struct free_nid {
  92. struct list_head list; /* for free node id list */
  93. nid_t nid; /* node id */
  94. int state; /* in use or not: NID_NEW or NID_ALLOC */
  95. };
  96. static inline int next_free_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  97. {
  98. struct f2fs_nm_info *nm_i = NM_I(sbi);
  99. struct free_nid *fnid;
  100. if (nm_i->fcnt <= 0)
  101. return -1;
  102. spin_lock(&nm_i->free_nid_list_lock);
  103. fnid = list_entry(nm_i->free_nid_list.next, struct free_nid, list);
  104. *nid = fnid->nid;
  105. spin_unlock(&nm_i->free_nid_list_lock);
  106. return 0;
  107. }
  108. /*
  109. * inline functions
  110. */
  111. static inline void get_nat_bitmap(struct f2fs_sb_info *sbi, void *addr)
  112. {
  113. struct f2fs_nm_info *nm_i = NM_I(sbi);
  114. memcpy(addr, nm_i->nat_bitmap, nm_i->bitmap_size);
  115. }
  116. static inline pgoff_t current_nat_addr(struct f2fs_sb_info *sbi, nid_t start)
  117. {
  118. struct f2fs_nm_info *nm_i = NM_I(sbi);
  119. pgoff_t block_off;
  120. pgoff_t block_addr;
  121. int seg_off;
  122. block_off = NAT_BLOCK_OFFSET(start);
  123. seg_off = block_off >> sbi->log_blocks_per_seg;
  124. block_addr = (pgoff_t)(nm_i->nat_blkaddr +
  125. (seg_off << sbi->log_blocks_per_seg << 1) +
  126. (block_off & ((1 << sbi->log_blocks_per_seg) - 1)));
  127. if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
  128. block_addr += sbi->blocks_per_seg;
  129. return block_addr;
  130. }
  131. static inline pgoff_t next_nat_addr(struct f2fs_sb_info *sbi,
  132. pgoff_t block_addr)
  133. {
  134. struct f2fs_nm_info *nm_i = NM_I(sbi);
  135. block_addr -= nm_i->nat_blkaddr;
  136. if ((block_addr >> sbi->log_blocks_per_seg) % 2)
  137. block_addr -= sbi->blocks_per_seg;
  138. else
  139. block_addr += sbi->blocks_per_seg;
  140. return block_addr + nm_i->nat_blkaddr;
  141. }
  142. static inline void set_to_next_nat(struct f2fs_nm_info *nm_i, nid_t start_nid)
  143. {
  144. unsigned int block_off = NAT_BLOCK_OFFSET(start_nid);
  145. if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
  146. f2fs_clear_bit(block_off, nm_i->nat_bitmap);
  147. else
  148. f2fs_set_bit(block_off, nm_i->nat_bitmap);
  149. }
  150. static inline void fill_node_footer(struct page *page, nid_t nid,
  151. nid_t ino, unsigned int ofs, bool reset)
  152. {
  153. struct f2fs_node *rn = F2FS_NODE(page);
  154. if (reset)
  155. memset(rn, 0, sizeof(*rn));
  156. rn->footer.nid = cpu_to_le32(nid);
  157. rn->footer.ino = cpu_to_le32(ino);
  158. rn->footer.flag = cpu_to_le32(ofs << OFFSET_BIT_SHIFT);
  159. }
  160. static inline void copy_node_footer(struct page *dst, struct page *src)
  161. {
  162. struct f2fs_node *src_rn = F2FS_NODE(src);
  163. struct f2fs_node *dst_rn = F2FS_NODE(dst);
  164. memcpy(&dst_rn->footer, &src_rn->footer, sizeof(struct node_footer));
  165. }
  166. static inline void fill_node_footer_blkaddr(struct page *page, block_t blkaddr)
  167. {
  168. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  169. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  170. struct f2fs_node *rn = F2FS_NODE(page);
  171. rn->footer.cp_ver = ckpt->checkpoint_ver;
  172. rn->footer.next_blkaddr = cpu_to_le32(blkaddr);
  173. }
  174. static inline nid_t ino_of_node(struct page *node_page)
  175. {
  176. struct f2fs_node *rn = F2FS_NODE(node_page);
  177. return le32_to_cpu(rn->footer.ino);
  178. }
  179. static inline nid_t nid_of_node(struct page *node_page)
  180. {
  181. struct f2fs_node *rn = F2FS_NODE(node_page);
  182. return le32_to_cpu(rn->footer.nid);
  183. }
  184. static inline unsigned int ofs_of_node(struct page *node_page)
  185. {
  186. struct f2fs_node *rn = F2FS_NODE(node_page);
  187. unsigned flag = le32_to_cpu(rn->footer.flag);
  188. return flag >> OFFSET_BIT_SHIFT;
  189. }
  190. static inline unsigned long long cpver_of_node(struct page *node_page)
  191. {
  192. struct f2fs_node *rn = F2FS_NODE(node_page);
  193. return le64_to_cpu(rn->footer.cp_ver);
  194. }
  195. static inline block_t next_blkaddr_of_node(struct page *node_page)
  196. {
  197. struct f2fs_node *rn = F2FS_NODE(node_page);
  198. return le32_to_cpu(rn->footer.next_blkaddr);
  199. }
  200. /*
  201. * f2fs assigns the following node offsets described as (num).
  202. * N = NIDS_PER_BLOCK
  203. *
  204. * Inode block (0)
  205. * |- direct node (1)
  206. * |- direct node (2)
  207. * |- indirect node (3)
  208. * | `- direct node (4 => 4 + N - 1)
  209. * |- indirect node (4 + N)
  210. * | `- direct node (5 + N => 5 + 2N - 1)
  211. * `- double indirect node (5 + 2N)
  212. * `- indirect node (6 + 2N)
  213. * `- direct node
  214. * ......
  215. * `- indirect node ((6 + 2N) + x(N + 1))
  216. * `- direct node
  217. * ......
  218. * `- indirect node ((6 + 2N) + (N - 1)(N + 1))
  219. * `- direct node
  220. */
  221. static inline bool IS_DNODE(struct page *node_page)
  222. {
  223. unsigned int ofs = ofs_of_node(node_page);
  224. if (f2fs_has_xattr_block(ofs))
  225. return false;
  226. if (ofs == 3 || ofs == 4 + NIDS_PER_BLOCK ||
  227. ofs == 5 + 2 * NIDS_PER_BLOCK)
  228. return false;
  229. if (ofs >= 6 + 2 * NIDS_PER_BLOCK) {
  230. ofs -= 6 + 2 * NIDS_PER_BLOCK;
  231. if (!((long int)ofs % (NIDS_PER_BLOCK + 1)))
  232. return false;
  233. }
  234. return true;
  235. }
  236. static inline void set_nid(struct page *p, int off, nid_t nid, bool i)
  237. {
  238. struct f2fs_node *rn = F2FS_NODE(p);
  239. f2fs_wait_on_page_writeback(p, NODE);
  240. if (i)
  241. rn->i.i_nid[off - NODE_DIR1_BLOCK] = cpu_to_le32(nid);
  242. else
  243. rn->in.nid[off] = cpu_to_le32(nid);
  244. set_page_dirty(p);
  245. }
  246. static inline nid_t get_nid(struct page *p, int off, bool i)
  247. {
  248. struct f2fs_node *rn = F2FS_NODE(p);
  249. if (i)
  250. return le32_to_cpu(rn->i.i_nid[off - NODE_DIR1_BLOCK]);
  251. return le32_to_cpu(rn->in.nid[off]);
  252. }
  253. /*
  254. * Coldness identification:
  255. * - Mark cold files in f2fs_inode_info
  256. * - Mark cold node blocks in their node footer
  257. * - Mark cold data pages in page cache
  258. */
  259. static inline int is_file(struct inode *inode, int type)
  260. {
  261. return F2FS_I(inode)->i_advise & type;
  262. }
  263. static inline void set_file(struct inode *inode, int type)
  264. {
  265. F2FS_I(inode)->i_advise |= type;
  266. }
  267. static inline void clear_file(struct inode *inode, int type)
  268. {
  269. F2FS_I(inode)->i_advise &= ~type;
  270. }
  271. #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
  272. #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
  273. #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
  274. #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
  275. #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
  276. #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
  277. static inline int is_cold_data(struct page *page)
  278. {
  279. return PageChecked(page);
  280. }
  281. static inline void set_cold_data(struct page *page)
  282. {
  283. SetPageChecked(page);
  284. }
  285. static inline void clear_cold_data(struct page *page)
  286. {
  287. ClearPageChecked(page);
  288. }
  289. static inline int is_node(struct page *page, int type)
  290. {
  291. struct f2fs_node *rn = F2FS_NODE(page);
  292. return le32_to_cpu(rn->footer.flag) & (1 << type);
  293. }
  294. #define is_cold_node(page) is_node(page, COLD_BIT_SHIFT)
  295. #define is_fsync_dnode(page) is_node(page, FSYNC_BIT_SHIFT)
  296. #define is_dent_dnode(page) is_node(page, DENT_BIT_SHIFT)
  297. static inline void set_cold_node(struct inode *inode, struct page *page)
  298. {
  299. struct f2fs_node *rn = F2FS_NODE(page);
  300. unsigned int flag = le32_to_cpu(rn->footer.flag);
  301. if (S_ISDIR(inode->i_mode))
  302. flag &= ~(0x1 << COLD_BIT_SHIFT);
  303. else
  304. flag |= (0x1 << COLD_BIT_SHIFT);
  305. rn->footer.flag = cpu_to_le32(flag);
  306. }
  307. static inline void set_mark(struct page *page, int mark, int type)
  308. {
  309. struct f2fs_node *rn = F2FS_NODE(page);
  310. unsigned int flag = le32_to_cpu(rn->footer.flag);
  311. if (mark)
  312. flag |= (0x1 << type);
  313. else
  314. flag &= ~(0x1 << type);
  315. rn->footer.flag = cpu_to_le32(flag);
  316. }
  317. #define set_dentry_mark(page, mark) set_mark(page, mark, DENT_BIT_SHIFT)
  318. #define set_fsync_mark(page, mark) set_mark(page, mark, FSYNC_BIT_SHIFT)