jfs_dmap.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) International Business Machines Corp., 2000-2002
  4. */
  5. #ifndef _H_JFS_DMAP
  6. #define _H_JFS_DMAP
  7. #include "jfs_txnmgr.h"
  8. #define BMAPVERSION 1 /* version number */
  9. #define TREESIZE (256+64+16+4+1) /* size of a dmap tree */
  10. #define LEAFIND (64+16+4+1) /* index of 1st leaf of a dmap tree */
  11. #define LPERDMAP 256 /* num leaves per dmap tree */
  12. #define L2LPERDMAP 8 /* l2 number of leaves per dmap tree */
  13. #define DBWORD 32 /* # of blks covered by a map word */
  14. #define L2DBWORD 5 /* l2 # of blks covered by a mword */
  15. #define BUDMIN L2DBWORD /* max free string in a map word */
  16. #define BPERDMAP (LPERDMAP * DBWORD) /* num of blks per dmap */
  17. #define L2BPERDMAP 13 /* l2 num of blks per dmap */
  18. #define CTLTREESIZE (1024+256+64+16+4+1) /* size of a dmapctl tree */
  19. #define CTLLEAFIND (256+64+16+4+1) /* idx of 1st leaf of a dmapctl tree */
  20. #define LPERCTL 1024 /* num of leaves per dmapctl tree */
  21. #define L2LPERCTL 10 /* l2 num of leaves per dmapctl tree */
  22. #define ROOT 0 /* index of the root of a tree */
  23. #define NOFREE ((s8) -1) /* no blocks free */
  24. #define MAXAG 128 /* max number of allocation groups */
  25. #define L2MAXAG 7 /* l2 max num of AG */
  26. #define L2MINAGSZ 25 /* l2 of minimum AG size in bytes */
  27. #define BMAPBLKNO 0 /* lblkno of bmap within the map */
  28. /*
  29. * maximum l2 number of disk blocks at the various dmapctl levels.
  30. */
  31. #define L2MAXL0SIZE (L2BPERDMAP + 1 * L2LPERCTL)
  32. #define L2MAXL1SIZE (L2BPERDMAP + 2 * L2LPERCTL)
  33. #define L2MAXL2SIZE (L2BPERDMAP + 3 * L2LPERCTL)
  34. /*
  35. * maximum number of disk blocks at the various dmapctl levels.
  36. */
  37. #define MAXL0SIZE ((s64)1 << L2MAXL0SIZE)
  38. #define MAXL1SIZE ((s64)1 << L2MAXL1SIZE)
  39. #define MAXL2SIZE ((s64)1 << L2MAXL2SIZE)
  40. #define MAXMAPSIZE MAXL2SIZE /* maximum aggregate map size */
  41. /*
  42. * determine the maximum free string for four (lower level) nodes
  43. * of the tree.
  44. */
  45. static inline signed char TREEMAX(signed char *cp)
  46. {
  47. signed char tmp1, tmp2;
  48. tmp1 = max(*(cp+2), *(cp+3));
  49. tmp2 = max(*(cp), *(cp+1));
  50. return max(tmp1, tmp2);
  51. }
  52. /*
  53. * convert disk block number to the logical block number of the dmap
  54. * describing the disk block. s is the log2(number of logical blocks per page)
  55. *
  56. * The calculation figures out how many logical pages are in front of the dmap.
  57. * - the number of dmaps preceding it
  58. * - the number of L0 pages preceding its L0 page
  59. * - the number of L1 pages preceding its L1 page
  60. * - 3 is added to account for the L2, L1, and L0 page for this dmap
  61. * - 1 is added to account for the control page of the map.
  62. */
  63. #define BLKTODMAP(b,s) \
  64. ((((b) >> 13) + ((b) >> 23) + ((b) >> 33) + 3 + 1) << (s))
  65. /*
  66. * convert disk block number to the logical block number of the LEVEL 0
  67. * dmapctl describing the disk block. s is the log2(number of logical blocks
  68. * per page)
  69. *
  70. * The calculation figures out how many logical pages are in front of the L0.
  71. * - the number of dmap pages preceding it
  72. * - the number of L0 pages preceding it
  73. * - the number of L1 pages preceding its L1 page
  74. * - 2 is added to account for the L2, and L1 page for this L0
  75. * - 1 is added to account for the control page of the map.
  76. */
  77. #define BLKTOL0(b,s) \
  78. (((((b) >> 23) << 10) + ((b) >> 23) + ((b) >> 33) + 2 + 1) << (s))
  79. /*
  80. * convert disk block number to the logical block number of the LEVEL 1
  81. * dmapctl describing the disk block. s is the log2(number of logical blocks
  82. * per page)
  83. *
  84. * The calculation figures out how many logical pages are in front of the L1.
  85. * - the number of dmap pages preceding it
  86. * - the number of L0 pages preceding it
  87. * - the number of L1 pages preceding it
  88. * - 1 is added to account for the L2 page
  89. * - 1 is added to account for the control page of the map.
  90. */
  91. #define BLKTOL1(b,s) \
  92. (((((b) >> 33) << 20) + (((b) >> 33) << 10) + ((b) >> 33) + 1 + 1) << (s))
  93. /*
  94. * convert disk block number to the logical block number of the dmapctl
  95. * at the specified level which describes the disk block.
  96. */
  97. #define BLKTOCTL(b,s,l) \
  98. (((l) == 2) ? 1 : ((l) == 1) ? BLKTOL1((b),(s)) : BLKTOL0((b),(s)))
  99. /*
  100. * convert aggregate map size to the zero origin dmapctl level of the
  101. * top dmapctl.
  102. */
  103. #define BMAPSZTOLEV(size) \
  104. (((size) <= MAXL0SIZE) ? 0 : ((size) <= MAXL1SIZE) ? 1 : 2)
  105. /* convert disk block number to allocation group number.
  106. */
  107. #define BLKTOAG(b,sbi) ((b) >> ((sbi)->bmap->db_agl2size))
  108. /* convert allocation group number to starting disk block
  109. * number.
  110. */
  111. #define AGTOBLK(a,ip) \
  112. ((s64)(a) << (JFS_SBI((ip)->i_sb)->bmap->db_agl2size))
  113. /*
  114. * dmap summary tree
  115. *
  116. * dmaptree must be consistent with dmapctl.
  117. */
  118. struct dmaptree {
  119. __le32 nleafs; /* 4: number of tree leafs */
  120. __le32 l2nleafs; /* 4: l2 number of tree leafs */
  121. __le32 leafidx; /* 4: index of first tree leaf */
  122. __le32 height; /* 4: height of the tree */
  123. s8 budmin; /* 1: min l2 tree leaf value to combine */
  124. s8 stree[TREESIZE]; /* TREESIZE: tree */
  125. u8 pad[2]; /* 2: pad to word boundary */
  126. }; /* - 360 - */
  127. /*
  128. * dmap page per 8K blocks bitmap
  129. */
  130. struct dmap {
  131. __le32 nblocks; /* 4: num blks covered by this dmap */
  132. __le32 nfree; /* 4: num of free blks in this dmap */
  133. __le64 start; /* 8: starting blkno for this dmap */
  134. struct dmaptree tree; /* 360: dmap tree */
  135. u8 pad[1672]; /* 1672: pad to 2048 bytes */
  136. __le32 wmap[LPERDMAP]; /* 1024: bits of the working map */
  137. __le32 pmap[LPERDMAP]; /* 1024: bits of the persistent map */
  138. }; /* - 4096 - */
  139. /*
  140. * disk map control page per level.
  141. *
  142. * dmapctl must be consistent with dmaptree.
  143. */
  144. struct dmapctl {
  145. __le32 nleafs; /* 4: number of tree leafs */
  146. __le32 l2nleafs; /* 4: l2 number of tree leafs */
  147. __le32 leafidx; /* 4: index of the first tree leaf */
  148. __le32 height; /* 4: height of tree */
  149. s8 budmin; /* 1: minimum l2 tree leaf value */
  150. s8 stree[CTLTREESIZE]; /* CTLTREESIZE: dmapctl tree */
  151. u8 pad[2714]; /* 2714: pad to 4096 */
  152. }; /* - 4096 - */
  153. /*
  154. * common definition for dmaptree within dmap and dmapctl
  155. */
  156. typedef union dmtree {
  157. struct dmaptree t1;
  158. struct dmapctl t2;
  159. } dmtree_t;
  160. /* macros for accessing fields within dmtree */
  161. #define dmt_nleafs t1.nleafs
  162. #define dmt_l2nleafs t1.l2nleafs
  163. #define dmt_leafidx t1.leafidx
  164. #define dmt_height t1.height
  165. #define dmt_budmin t1.budmin
  166. #define dmt_stree t2.stree
  167. /*
  168. * on-disk aggregate disk allocation map descriptor.
  169. */
  170. struct dbmap_disk {
  171. __le64 dn_mapsize; /* 8: number of blocks in aggregate */
  172. __le64 dn_nfree; /* 8: num free blks in aggregate map */
  173. __le32 dn_l2nbperpage; /* 4: number of blks per page */
  174. __le32 dn_numag; /* 4: total number of ags */
  175. __le32 dn_maxlevel; /* 4: number of active ags */
  176. __le32 dn_maxag; /* 4: max active alloc group number */
  177. __le32 dn_agpref; /* 4: preferred alloc group (hint) */
  178. __le32 dn_aglevel; /* 4: dmapctl level holding the AG */
  179. __le32 dn_agheight; /* 4: height in dmapctl of the AG */
  180. __le32 dn_agwidth; /* 4: width in dmapctl of the AG */
  181. __le32 dn_agstart; /* 4: start tree index at AG height */
  182. __le32 dn_agl2size; /* 4: l2 num of blks per alloc group */
  183. __le64 dn_agfree[MAXAG];/* 8*MAXAG: per AG free count */
  184. __le64 dn_agsize; /* 8: num of blks per alloc group */
  185. s8 dn_maxfreebud; /* 1: max free buddy system */
  186. u8 pad[3007]; /* 3007: pad to 4096 */
  187. }; /* - 4096 - */
  188. struct dbmap {
  189. s64 dn_mapsize; /* number of blocks in aggregate */
  190. s64 dn_nfree; /* num free blks in aggregate map */
  191. int dn_l2nbperpage; /* number of blks per page */
  192. int dn_numag; /* total number of ags */
  193. int dn_maxlevel; /* number of active ags */
  194. int dn_maxag; /* max active alloc group number */
  195. int dn_agpref; /* preferred alloc group (hint) */
  196. int dn_aglevel; /* dmapctl level holding the AG */
  197. int dn_agheight; /* height in dmapctl of the AG */
  198. int dn_agwidth; /* width in dmapctl of the AG */
  199. int dn_agstart; /* start tree index at AG height */
  200. int dn_agl2size; /* l2 num of blks per alloc group */
  201. s64 dn_agfree[MAXAG]; /* per AG free count */
  202. s64 dn_agsize; /* num of blks per alloc group */
  203. signed char dn_maxfreebud; /* max free buddy system */
  204. }; /* - 4096 - */
  205. /*
  206. * in-memory aggregate disk allocation map descriptor.
  207. */
  208. struct bmap {
  209. struct dbmap db_bmap; /* on-disk aggregate map descriptor */
  210. struct inode *db_ipbmap; /* ptr to aggregate map incore inode */
  211. struct mutex db_bmaplock; /* aggregate map lock */
  212. atomic_t db_active[MAXAG]; /* count of active, open files in AG */
  213. u32 *db_DBmap;
  214. };
  215. /* macros for accessing fields within in-memory aggregate map descriptor */
  216. #define db_mapsize db_bmap.dn_mapsize
  217. #define db_nfree db_bmap.dn_nfree
  218. #define db_agfree db_bmap.dn_agfree
  219. #define db_agsize db_bmap.dn_agsize
  220. #define db_agl2size db_bmap.dn_agl2size
  221. #define db_agwidth db_bmap.dn_agwidth
  222. #define db_agheight db_bmap.dn_agheight
  223. #define db_agstart db_bmap.dn_agstart
  224. #define db_numag db_bmap.dn_numag
  225. #define db_maxlevel db_bmap.dn_maxlevel
  226. #define db_aglevel db_bmap.dn_aglevel
  227. #define db_agpref db_bmap.dn_agpref
  228. #define db_maxag db_bmap.dn_maxag
  229. #define db_maxfreebud db_bmap.dn_maxfreebud
  230. #define db_l2nbperpage db_bmap.dn_l2nbperpage
  231. /*
  232. * macros for various conversions needed by the allocators.
  233. * blkstol2(), cntlz(), and cnttz() are operating system dependent functions.
  234. */
  235. /* convert number of blocks to log2 number of blocks, rounding up to
  236. * the next log2 value if blocks is not a l2 multiple.
  237. */
  238. #define BLKSTOL2(d) (blkstol2(d))
  239. /* convert number of leafs to log2 leaf value */
  240. #define NLSTOL2BSZ(n) (31 - cntlz((n)) + BUDMIN)
  241. /* convert leaf index to log2 leaf value */
  242. #define LITOL2BSZ(n,m,b) ((((n) == 0) ? (m) : cnttz((n))) + (b))
  243. /* convert a block number to a dmap control leaf index */
  244. #define BLKTOCTLLEAF(b,m) \
  245. (((b) & (((s64)1 << ((m) + L2LPERCTL)) - 1)) >> (m))
  246. /* convert log2 leaf value to buddy size */
  247. #define BUDSIZE(s,m) (1 << ((s) - (m)))
  248. /*
  249. * external references.
  250. */
  251. extern int dbMount(struct inode *ipbmap);
  252. extern int dbUnmount(struct inode *ipbmap, int mounterror);
  253. extern int dbFree(struct inode *ipbmap, s64 blkno, s64 nblocks);
  254. extern int dbUpdatePMap(struct inode *ipbmap,
  255. int free, s64 blkno, s64 nblocks, struct tblock * tblk);
  256. extern int dbNextAG(struct inode *ipbmap);
  257. extern int dbAlloc(struct inode *ipbmap, s64 hint, s64 nblocks, s64 * results);
  258. extern int dbReAlloc(struct inode *ipbmap,
  259. s64 blkno, s64 nblocks, s64 addnblocks, s64 * results);
  260. extern int dbSync(struct inode *ipbmap);
  261. extern int dbAllocBottomUp(struct inode *ip, s64 blkno, s64 nblocks);
  262. extern int dbExtendFS(struct inode *ipbmap, s64 blkno, s64 nblocks);
  263. extern void dbFinalizeBmap(struct inode *ipbmap);
  264. extern s64 dbMapFileSizeToMapSize(struct inode *ipbmap);
  265. extern s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen);
  266. #endif /* _H_JFS_DMAP */