xfs_alloc_btree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_btree.h"
  27. #include "xfs_alloc_btree.h"
  28. #include "xfs_alloc.h"
  29. #include "xfs_extent_busy.h"
  30. #include "xfs_error.h"
  31. #include "xfs_trace.h"
  32. #include "xfs_cksum.h"
  33. #include "xfs_trans.h"
  34. STATIC struct xfs_btree_cur *
  35. xfs_allocbt_dup_cursor(
  36. struct xfs_btree_cur *cur)
  37. {
  38. return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
  39. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  40. cur->bc_btnum);
  41. }
  42. STATIC void
  43. xfs_allocbt_set_root(
  44. struct xfs_btree_cur *cur,
  45. union xfs_btree_ptr *ptr,
  46. int inc)
  47. {
  48. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  49. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  50. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  51. int btnum = cur->bc_btnum;
  52. struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
  53. ASSERT(ptr->s != 0);
  54. agf->agf_roots[btnum] = ptr->s;
  55. be32_add_cpu(&agf->agf_levels[btnum], inc);
  56. pag->pagf_levels[btnum] += inc;
  57. xfs_perag_put(pag);
  58. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  59. }
  60. STATIC int
  61. xfs_allocbt_alloc_block(
  62. struct xfs_btree_cur *cur,
  63. union xfs_btree_ptr *start,
  64. union xfs_btree_ptr *new,
  65. int *stat)
  66. {
  67. int error;
  68. xfs_agblock_t bno;
  69. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  70. /* Allocate the new block from the freelist. If we can't, give up. */
  71. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  72. &bno, 1);
  73. if (error) {
  74. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  75. return error;
  76. }
  77. if (bno == NULLAGBLOCK) {
  78. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  79. *stat = 0;
  80. return 0;
  81. }
  82. xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1, false);
  83. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  84. new->s = cpu_to_be32(bno);
  85. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  86. *stat = 1;
  87. return 0;
  88. }
  89. STATIC int
  90. xfs_allocbt_free_block(
  91. struct xfs_btree_cur *cur,
  92. struct xfs_buf *bp)
  93. {
  94. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  95. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  96. xfs_agblock_t bno;
  97. int error;
  98. bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
  99. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  100. if (error)
  101. return error;
  102. xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
  103. XFS_EXTENT_BUSY_SKIP_DISCARD);
  104. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  105. return 0;
  106. }
  107. /*
  108. * Update the longest extent in the AGF
  109. */
  110. STATIC void
  111. xfs_allocbt_update_lastrec(
  112. struct xfs_btree_cur *cur,
  113. struct xfs_btree_block *block,
  114. union xfs_btree_rec *rec,
  115. int ptr,
  116. int reason)
  117. {
  118. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  119. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  120. struct xfs_perag *pag;
  121. __be32 len;
  122. int numrecs;
  123. ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
  124. switch (reason) {
  125. case LASTREC_UPDATE:
  126. /*
  127. * If this is the last leaf block and it's the last record,
  128. * then update the size of the longest extent in the AG.
  129. */
  130. if (ptr != xfs_btree_get_numrecs(block))
  131. return;
  132. len = rec->alloc.ar_blockcount;
  133. break;
  134. case LASTREC_INSREC:
  135. if (be32_to_cpu(rec->alloc.ar_blockcount) <=
  136. be32_to_cpu(agf->agf_longest))
  137. return;
  138. len = rec->alloc.ar_blockcount;
  139. break;
  140. case LASTREC_DELREC:
  141. numrecs = xfs_btree_get_numrecs(block);
  142. if (ptr <= numrecs)
  143. return;
  144. ASSERT(ptr == numrecs + 1);
  145. if (numrecs) {
  146. xfs_alloc_rec_t *rrp;
  147. rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
  148. len = rrp->ar_blockcount;
  149. } else {
  150. len = 0;
  151. }
  152. break;
  153. default:
  154. ASSERT(0);
  155. return;
  156. }
  157. agf->agf_longest = len;
  158. pag = xfs_perag_get(cur->bc_mp, seqno);
  159. pag->pagf_longest = be32_to_cpu(len);
  160. xfs_perag_put(pag);
  161. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
  162. }
  163. STATIC int
  164. xfs_allocbt_get_minrecs(
  165. struct xfs_btree_cur *cur,
  166. int level)
  167. {
  168. return cur->bc_mp->m_alloc_mnr[level != 0];
  169. }
  170. STATIC int
  171. xfs_allocbt_get_maxrecs(
  172. struct xfs_btree_cur *cur,
  173. int level)
  174. {
  175. return cur->bc_mp->m_alloc_mxr[level != 0];
  176. }
  177. STATIC void
  178. xfs_allocbt_init_key_from_rec(
  179. union xfs_btree_key *key,
  180. union xfs_btree_rec *rec)
  181. {
  182. ASSERT(rec->alloc.ar_startblock != 0);
  183. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  184. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  185. }
  186. STATIC void
  187. xfs_allocbt_init_rec_from_key(
  188. union xfs_btree_key *key,
  189. union xfs_btree_rec *rec)
  190. {
  191. ASSERT(key->alloc.ar_startblock != 0);
  192. rec->alloc.ar_startblock = key->alloc.ar_startblock;
  193. rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
  194. }
  195. STATIC void
  196. xfs_allocbt_init_rec_from_cur(
  197. struct xfs_btree_cur *cur,
  198. union xfs_btree_rec *rec)
  199. {
  200. ASSERT(cur->bc_rec.a.ar_startblock != 0);
  201. rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  202. rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  203. }
  204. STATIC void
  205. xfs_allocbt_init_ptr_from_cur(
  206. struct xfs_btree_cur *cur,
  207. union xfs_btree_ptr *ptr)
  208. {
  209. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  210. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  211. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  212. ptr->s = agf->agf_roots[cur->bc_btnum];
  213. }
  214. STATIC __int64_t
  215. xfs_allocbt_key_diff(
  216. struct xfs_btree_cur *cur,
  217. union xfs_btree_key *key)
  218. {
  219. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  220. xfs_alloc_key_t *kp = &key->alloc;
  221. __int64_t diff;
  222. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  223. return (__int64_t)be32_to_cpu(kp->ar_startblock) -
  224. rec->ar_startblock;
  225. }
  226. diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  227. if (diff)
  228. return diff;
  229. return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  230. }
  231. static bool
  232. xfs_allocbt_verify(
  233. struct xfs_buf *bp)
  234. {
  235. struct xfs_mount *mp = bp->b_target->bt_mount;
  236. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  237. struct xfs_perag *pag = bp->b_pag;
  238. unsigned int level;
  239. /*
  240. * magic number and level verification
  241. *
  242. * During growfs operations, we can't verify the exact level or owner as
  243. * the perag is not fully initialised and hence not attached to the
  244. * buffer. In this case, check against the maximum tree depth.
  245. *
  246. * Similarly, during log recovery we will have a perag structure
  247. * attached, but the agf information will not yet have been initialised
  248. * from the on disk AGF. Again, we can only check against maximum limits
  249. * in this case.
  250. */
  251. level = be16_to_cpu(block->bb_level);
  252. switch (block->bb_magic) {
  253. case cpu_to_be32(XFS_ABTB_CRC_MAGIC):
  254. if (!xfs_btree_sblock_v5hdr_verify(bp))
  255. return false;
  256. /* fall through */
  257. case cpu_to_be32(XFS_ABTB_MAGIC):
  258. if (pag && pag->pagf_init) {
  259. if (level >= pag->pagf_levels[XFS_BTNUM_BNOi])
  260. return false;
  261. } else if (level >= mp->m_ag_maxlevels)
  262. return false;
  263. break;
  264. case cpu_to_be32(XFS_ABTC_CRC_MAGIC):
  265. if (!xfs_btree_sblock_v5hdr_verify(bp))
  266. return false;
  267. /* fall through */
  268. case cpu_to_be32(XFS_ABTC_MAGIC):
  269. if (pag && pag->pagf_init) {
  270. if (level >= pag->pagf_levels[XFS_BTNUM_CNTi])
  271. return false;
  272. } else if (level >= mp->m_ag_maxlevels)
  273. return false;
  274. break;
  275. default:
  276. return false;
  277. }
  278. return xfs_btree_sblock_verify(bp, mp->m_alloc_mxr[level != 0]);
  279. }
  280. static void
  281. xfs_allocbt_read_verify(
  282. struct xfs_buf *bp)
  283. {
  284. if (!xfs_btree_sblock_verify_crc(bp))
  285. xfs_buf_ioerror(bp, -EFSBADCRC);
  286. else if (!xfs_allocbt_verify(bp))
  287. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  288. if (bp->b_error) {
  289. trace_xfs_btree_corrupt(bp, _RET_IP_);
  290. xfs_verifier_error(bp);
  291. }
  292. }
  293. static void
  294. xfs_allocbt_write_verify(
  295. struct xfs_buf *bp)
  296. {
  297. if (!xfs_allocbt_verify(bp)) {
  298. trace_xfs_btree_corrupt(bp, _RET_IP_);
  299. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  300. xfs_verifier_error(bp);
  301. return;
  302. }
  303. xfs_btree_sblock_calc_crc(bp);
  304. }
  305. const struct xfs_buf_ops xfs_allocbt_buf_ops = {
  306. .name = "xfs_allocbt",
  307. .verify_read = xfs_allocbt_read_verify,
  308. .verify_write = xfs_allocbt_write_verify,
  309. };
  310. #if defined(DEBUG) || defined(XFS_WARN)
  311. STATIC int
  312. xfs_allocbt_keys_inorder(
  313. struct xfs_btree_cur *cur,
  314. union xfs_btree_key *k1,
  315. union xfs_btree_key *k2)
  316. {
  317. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  318. return be32_to_cpu(k1->alloc.ar_startblock) <
  319. be32_to_cpu(k2->alloc.ar_startblock);
  320. } else {
  321. return be32_to_cpu(k1->alloc.ar_blockcount) <
  322. be32_to_cpu(k2->alloc.ar_blockcount) ||
  323. (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
  324. be32_to_cpu(k1->alloc.ar_startblock) <
  325. be32_to_cpu(k2->alloc.ar_startblock));
  326. }
  327. }
  328. STATIC int
  329. xfs_allocbt_recs_inorder(
  330. struct xfs_btree_cur *cur,
  331. union xfs_btree_rec *r1,
  332. union xfs_btree_rec *r2)
  333. {
  334. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  335. return be32_to_cpu(r1->alloc.ar_startblock) +
  336. be32_to_cpu(r1->alloc.ar_blockcount) <=
  337. be32_to_cpu(r2->alloc.ar_startblock);
  338. } else {
  339. return be32_to_cpu(r1->alloc.ar_blockcount) <
  340. be32_to_cpu(r2->alloc.ar_blockcount) ||
  341. (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
  342. be32_to_cpu(r1->alloc.ar_startblock) <
  343. be32_to_cpu(r2->alloc.ar_startblock));
  344. }
  345. }
  346. #endif /* DEBUG */
  347. static const struct xfs_btree_ops xfs_allocbt_ops = {
  348. .rec_len = sizeof(xfs_alloc_rec_t),
  349. .key_len = sizeof(xfs_alloc_key_t),
  350. .dup_cursor = xfs_allocbt_dup_cursor,
  351. .set_root = xfs_allocbt_set_root,
  352. .alloc_block = xfs_allocbt_alloc_block,
  353. .free_block = xfs_allocbt_free_block,
  354. .update_lastrec = xfs_allocbt_update_lastrec,
  355. .get_minrecs = xfs_allocbt_get_minrecs,
  356. .get_maxrecs = xfs_allocbt_get_maxrecs,
  357. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  358. .init_rec_from_key = xfs_allocbt_init_rec_from_key,
  359. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  360. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  361. .key_diff = xfs_allocbt_key_diff,
  362. .buf_ops = &xfs_allocbt_buf_ops,
  363. #if defined(DEBUG) || defined(XFS_WARN)
  364. .keys_inorder = xfs_allocbt_keys_inorder,
  365. .recs_inorder = xfs_allocbt_recs_inorder,
  366. #endif
  367. };
  368. /*
  369. * Allocate a new allocation btree cursor.
  370. */
  371. struct xfs_btree_cur * /* new alloc btree cursor */
  372. xfs_allocbt_init_cursor(
  373. struct xfs_mount *mp, /* file system mount point */
  374. struct xfs_trans *tp, /* transaction pointer */
  375. struct xfs_buf *agbp, /* buffer for agf structure */
  376. xfs_agnumber_t agno, /* allocation group number */
  377. xfs_btnum_t btnum) /* btree identifier */
  378. {
  379. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  380. struct xfs_btree_cur *cur;
  381. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  382. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  383. cur->bc_tp = tp;
  384. cur->bc_mp = mp;
  385. cur->bc_btnum = btnum;
  386. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  387. cur->bc_ops = &xfs_allocbt_ops;
  388. if (btnum == XFS_BTNUM_CNT) {
  389. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
  390. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  391. } else {
  392. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
  393. }
  394. cur->bc_private.a.agbp = agbp;
  395. cur->bc_private.a.agno = agno;
  396. if (xfs_sb_version_hascrc(&mp->m_sb))
  397. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  398. return cur;
  399. }
  400. /*
  401. * Calculate number of records in an alloc btree block.
  402. */
  403. int
  404. xfs_allocbt_maxrecs(
  405. struct xfs_mount *mp,
  406. int blocklen,
  407. int leaf)
  408. {
  409. blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
  410. if (leaf)
  411. return blocklen / sizeof(xfs_alloc_rec_t);
  412. return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
  413. }