xfs_alloc_btree.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_log.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_bmap_btree.h"
  27. #include "xfs_alloc_btree.h"
  28. #include "xfs_ialloc_btree.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_btree.h"
  32. #include "xfs_alloc.h"
  33. #include "xfs_extent_busy.h"
  34. #include "xfs_error.h"
  35. #include "xfs_trace.h"
  36. STATIC struct xfs_btree_cur *
  37. xfs_allocbt_dup_cursor(
  38. struct xfs_btree_cur *cur)
  39. {
  40. return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
  41. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  42. cur->bc_btnum);
  43. }
  44. STATIC void
  45. xfs_allocbt_set_root(
  46. struct xfs_btree_cur *cur,
  47. union xfs_btree_ptr *ptr,
  48. int inc)
  49. {
  50. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  51. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  52. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  53. int btnum = cur->bc_btnum;
  54. struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
  55. ASSERT(ptr->s != 0);
  56. agf->agf_roots[btnum] = ptr->s;
  57. be32_add_cpu(&agf->agf_levels[btnum], inc);
  58. pag->pagf_levels[btnum] += inc;
  59. xfs_perag_put(pag);
  60. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  61. }
  62. STATIC int
  63. xfs_allocbt_alloc_block(
  64. struct xfs_btree_cur *cur,
  65. union xfs_btree_ptr *start,
  66. union xfs_btree_ptr *new,
  67. int length,
  68. int *stat)
  69. {
  70. int error;
  71. xfs_agblock_t bno;
  72. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  73. /* Allocate the new block from the freelist. If we can't, give up. */
  74. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  75. &bno, 1);
  76. if (error) {
  77. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  78. return error;
  79. }
  80. if (bno == NULLAGBLOCK) {
  81. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  82. *stat = 0;
  83. return 0;
  84. }
  85. xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1, false);
  86. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  87. new->s = cpu_to_be32(bno);
  88. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  89. *stat = 1;
  90. return 0;
  91. }
  92. STATIC int
  93. xfs_allocbt_free_block(
  94. struct xfs_btree_cur *cur,
  95. struct xfs_buf *bp)
  96. {
  97. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  98. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  99. xfs_agblock_t bno;
  100. int error;
  101. bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
  102. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  103. if (error)
  104. return error;
  105. xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
  106. XFS_EXTENT_BUSY_SKIP_DISCARD);
  107. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  108. return 0;
  109. }
  110. /*
  111. * Update the longest extent in the AGF
  112. */
  113. STATIC void
  114. xfs_allocbt_update_lastrec(
  115. struct xfs_btree_cur *cur,
  116. struct xfs_btree_block *block,
  117. union xfs_btree_rec *rec,
  118. int ptr,
  119. int reason)
  120. {
  121. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  122. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  123. struct xfs_perag *pag;
  124. __be32 len;
  125. int numrecs;
  126. ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
  127. switch (reason) {
  128. case LASTREC_UPDATE:
  129. /*
  130. * If this is the last leaf block and it's the last record,
  131. * then update the size of the longest extent in the AG.
  132. */
  133. if (ptr != xfs_btree_get_numrecs(block))
  134. return;
  135. len = rec->alloc.ar_blockcount;
  136. break;
  137. case LASTREC_INSREC:
  138. if (be32_to_cpu(rec->alloc.ar_blockcount) <=
  139. be32_to_cpu(agf->agf_longest))
  140. return;
  141. len = rec->alloc.ar_blockcount;
  142. break;
  143. case LASTREC_DELREC:
  144. numrecs = xfs_btree_get_numrecs(block);
  145. if (ptr <= numrecs)
  146. return;
  147. ASSERT(ptr == numrecs + 1);
  148. if (numrecs) {
  149. xfs_alloc_rec_t *rrp;
  150. rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
  151. len = rrp->ar_blockcount;
  152. } else {
  153. len = 0;
  154. }
  155. break;
  156. default:
  157. ASSERT(0);
  158. return;
  159. }
  160. agf->agf_longest = len;
  161. pag = xfs_perag_get(cur->bc_mp, seqno);
  162. pag->pagf_longest = be32_to_cpu(len);
  163. xfs_perag_put(pag);
  164. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
  165. }
  166. STATIC int
  167. xfs_allocbt_get_minrecs(
  168. struct xfs_btree_cur *cur,
  169. int level)
  170. {
  171. return cur->bc_mp->m_alloc_mnr[level != 0];
  172. }
  173. STATIC int
  174. xfs_allocbt_get_maxrecs(
  175. struct xfs_btree_cur *cur,
  176. int level)
  177. {
  178. return cur->bc_mp->m_alloc_mxr[level != 0];
  179. }
  180. STATIC void
  181. xfs_allocbt_init_key_from_rec(
  182. union xfs_btree_key *key,
  183. union xfs_btree_rec *rec)
  184. {
  185. ASSERT(rec->alloc.ar_startblock != 0);
  186. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  187. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  188. }
  189. STATIC void
  190. xfs_allocbt_init_rec_from_key(
  191. union xfs_btree_key *key,
  192. union xfs_btree_rec *rec)
  193. {
  194. ASSERT(key->alloc.ar_startblock != 0);
  195. rec->alloc.ar_startblock = key->alloc.ar_startblock;
  196. rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
  197. }
  198. STATIC void
  199. xfs_allocbt_init_rec_from_cur(
  200. struct xfs_btree_cur *cur,
  201. union xfs_btree_rec *rec)
  202. {
  203. ASSERT(cur->bc_rec.a.ar_startblock != 0);
  204. rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  205. rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  206. }
  207. STATIC void
  208. xfs_allocbt_init_ptr_from_cur(
  209. struct xfs_btree_cur *cur,
  210. union xfs_btree_ptr *ptr)
  211. {
  212. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  213. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  214. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  215. ptr->s = agf->agf_roots[cur->bc_btnum];
  216. }
  217. STATIC __int64_t
  218. xfs_allocbt_key_diff(
  219. struct xfs_btree_cur *cur,
  220. union xfs_btree_key *key)
  221. {
  222. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  223. xfs_alloc_key_t *kp = &key->alloc;
  224. __int64_t diff;
  225. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  226. return (__int64_t)be32_to_cpu(kp->ar_startblock) -
  227. rec->ar_startblock;
  228. }
  229. diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  230. if (diff)
  231. return diff;
  232. return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  233. }
  234. #ifdef DEBUG
  235. STATIC int
  236. xfs_allocbt_keys_inorder(
  237. struct xfs_btree_cur *cur,
  238. union xfs_btree_key *k1,
  239. union xfs_btree_key *k2)
  240. {
  241. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  242. return be32_to_cpu(k1->alloc.ar_startblock) <
  243. be32_to_cpu(k2->alloc.ar_startblock);
  244. } else {
  245. return be32_to_cpu(k1->alloc.ar_blockcount) <
  246. be32_to_cpu(k2->alloc.ar_blockcount) ||
  247. (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
  248. be32_to_cpu(k1->alloc.ar_startblock) <
  249. be32_to_cpu(k2->alloc.ar_startblock));
  250. }
  251. }
  252. STATIC int
  253. xfs_allocbt_recs_inorder(
  254. struct xfs_btree_cur *cur,
  255. union xfs_btree_rec *r1,
  256. union xfs_btree_rec *r2)
  257. {
  258. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  259. return be32_to_cpu(r1->alloc.ar_startblock) +
  260. be32_to_cpu(r1->alloc.ar_blockcount) <=
  261. be32_to_cpu(r2->alloc.ar_startblock);
  262. } else {
  263. return be32_to_cpu(r1->alloc.ar_blockcount) <
  264. be32_to_cpu(r2->alloc.ar_blockcount) ||
  265. (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
  266. be32_to_cpu(r1->alloc.ar_startblock) <
  267. be32_to_cpu(r2->alloc.ar_startblock));
  268. }
  269. }
  270. #endif /* DEBUG */
  271. static const struct xfs_btree_ops xfs_allocbt_ops = {
  272. .rec_len = sizeof(xfs_alloc_rec_t),
  273. .key_len = sizeof(xfs_alloc_key_t),
  274. .dup_cursor = xfs_allocbt_dup_cursor,
  275. .set_root = xfs_allocbt_set_root,
  276. .alloc_block = xfs_allocbt_alloc_block,
  277. .free_block = xfs_allocbt_free_block,
  278. .update_lastrec = xfs_allocbt_update_lastrec,
  279. .get_minrecs = xfs_allocbt_get_minrecs,
  280. .get_maxrecs = xfs_allocbt_get_maxrecs,
  281. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  282. .init_rec_from_key = xfs_allocbt_init_rec_from_key,
  283. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  284. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  285. .key_diff = xfs_allocbt_key_diff,
  286. #ifdef DEBUG
  287. .keys_inorder = xfs_allocbt_keys_inorder,
  288. .recs_inorder = xfs_allocbt_recs_inorder,
  289. #endif
  290. };
  291. /*
  292. * Allocate a new allocation btree cursor.
  293. */
  294. struct xfs_btree_cur * /* new alloc btree cursor */
  295. xfs_allocbt_init_cursor(
  296. struct xfs_mount *mp, /* file system mount point */
  297. struct xfs_trans *tp, /* transaction pointer */
  298. struct xfs_buf *agbp, /* buffer for agf structure */
  299. xfs_agnumber_t agno, /* allocation group number */
  300. xfs_btnum_t btnum) /* btree identifier */
  301. {
  302. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  303. struct xfs_btree_cur *cur;
  304. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  305. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  306. cur->bc_tp = tp;
  307. cur->bc_mp = mp;
  308. cur->bc_btnum = btnum;
  309. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  310. cur->bc_ops = &xfs_allocbt_ops;
  311. if (btnum == XFS_BTNUM_CNT) {
  312. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
  313. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  314. } else {
  315. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
  316. }
  317. cur->bc_private.a.agbp = agbp;
  318. cur->bc_private.a.agno = agno;
  319. return cur;
  320. }
  321. /*
  322. * Calculate number of records in an alloc btree block.
  323. */
  324. int
  325. xfs_allocbt_maxrecs(
  326. struct xfs_mount *mp,
  327. int blocklen,
  328. int leaf)
  329. {
  330. blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
  331. if (leaf)
  332. return blocklen / sizeof(xfs_alloc_rec_t);
  333. return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
  334. }