xfs_alloc_btree.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_btree.h"
  28. #include "xfs_alloc_btree.h"
  29. #include "xfs_alloc.h"
  30. #include "xfs_extent_busy.h"
  31. #include "xfs_error.h"
  32. #include "xfs_trace.h"
  33. #include "xfs_cksum.h"
  34. #include "xfs_trans.h"
  35. STATIC struct xfs_btree_cur *
  36. xfs_allocbt_dup_cursor(
  37. struct xfs_btree_cur *cur)
  38. {
  39. return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
  40. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  41. cur->bc_btnum);
  42. }
  43. STATIC void
  44. xfs_allocbt_set_root(
  45. struct xfs_btree_cur *cur,
  46. union xfs_btree_ptr *ptr,
  47. int inc)
  48. {
  49. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  50. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  51. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  52. int btnum = cur->bc_btnum;
  53. struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
  54. ASSERT(ptr->s != 0);
  55. agf->agf_roots[btnum] = ptr->s;
  56. be32_add_cpu(&agf->agf_levels[btnum], inc);
  57. pag->pagf_levels[btnum] += inc;
  58. xfs_perag_put(pag);
  59. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  60. }
  61. STATIC int
  62. xfs_allocbt_alloc_block(
  63. struct xfs_btree_cur *cur,
  64. union xfs_btree_ptr *start,
  65. union xfs_btree_ptr *new,
  66. int length,
  67. int *stat)
  68. {
  69. int error;
  70. xfs_agblock_t bno;
  71. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  72. /* Allocate the new block from the freelist. If we can't, give up. */
  73. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  74. &bno, 1);
  75. if (error) {
  76. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  77. return error;
  78. }
  79. if (bno == NULLAGBLOCK) {
  80. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  81. *stat = 0;
  82. return 0;
  83. }
  84. xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1, false);
  85. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  86. new->s = cpu_to_be32(bno);
  87. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  88. *stat = 1;
  89. return 0;
  90. }
  91. STATIC int
  92. xfs_allocbt_free_block(
  93. struct xfs_btree_cur *cur,
  94. struct xfs_buf *bp)
  95. {
  96. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  97. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  98. xfs_agblock_t bno;
  99. int error;
  100. bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
  101. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  102. if (error)
  103. return error;
  104. xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
  105. XFS_EXTENT_BUSY_SKIP_DISCARD);
  106. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  107. xfs_trans_binval(cur->bc_tp, bp);
  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. static bool
  235. xfs_allocbt_verify(
  236. struct xfs_buf *bp)
  237. {
  238. struct xfs_mount *mp = bp->b_target->bt_mount;
  239. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  240. struct xfs_perag *pag = bp->b_pag;
  241. unsigned int level;
  242. /*
  243. * magic number and level verification
  244. *
  245. * During growfs operations, we can't verify the exact level or owner as
  246. * the perag is not fully initialised and hence not attached to the
  247. * buffer. In this case, check against the maximum tree depth.
  248. *
  249. * Similarly, during log recovery we will have a perag structure
  250. * attached, but the agf information will not yet have been initialised
  251. * from the on disk AGF. Again, we can only check against maximum limits
  252. * in this case.
  253. */
  254. level = be16_to_cpu(block->bb_level);
  255. switch (block->bb_magic) {
  256. case cpu_to_be32(XFS_ABTB_CRC_MAGIC):
  257. if (!xfs_sb_version_hascrc(&mp->m_sb))
  258. return false;
  259. if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid))
  260. return false;
  261. if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
  262. return false;
  263. if (pag &&
  264. be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
  265. return false;
  266. /* fall through */
  267. case cpu_to_be32(XFS_ABTB_MAGIC):
  268. if (pag && pag->pagf_init) {
  269. if (level >= pag->pagf_levels[XFS_BTNUM_BNOi])
  270. return false;
  271. } else if (level >= mp->m_ag_maxlevels)
  272. return false;
  273. break;
  274. case cpu_to_be32(XFS_ABTC_CRC_MAGIC):
  275. if (!xfs_sb_version_hascrc(&mp->m_sb))
  276. return false;
  277. if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid))
  278. return false;
  279. if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
  280. return false;
  281. if (pag &&
  282. be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
  283. return false;
  284. /* fall through */
  285. case cpu_to_be32(XFS_ABTC_MAGIC):
  286. if (pag && pag->pagf_init) {
  287. if (level >= pag->pagf_levels[XFS_BTNUM_CNTi])
  288. return false;
  289. } else if (level >= mp->m_ag_maxlevels)
  290. return false;
  291. break;
  292. default:
  293. return false;
  294. }
  295. /* numrecs verification */
  296. if (be16_to_cpu(block->bb_numrecs) > mp->m_alloc_mxr[level != 0])
  297. return false;
  298. /* sibling pointer verification */
  299. if (!block->bb_u.s.bb_leftsib ||
  300. (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
  301. block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
  302. return false;
  303. if (!block->bb_u.s.bb_rightsib ||
  304. (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
  305. block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
  306. return false;
  307. return true;
  308. }
  309. static void
  310. xfs_allocbt_read_verify(
  311. struct xfs_buf *bp)
  312. {
  313. if (!(xfs_btree_sblock_verify_crc(bp) &&
  314. xfs_allocbt_verify(bp))) {
  315. trace_xfs_btree_corrupt(bp, _RET_IP_);
  316. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
  317. bp->b_target->bt_mount, bp->b_addr);
  318. xfs_buf_ioerror(bp, EFSCORRUPTED);
  319. }
  320. }
  321. static void
  322. xfs_allocbt_write_verify(
  323. struct xfs_buf *bp)
  324. {
  325. if (!xfs_allocbt_verify(bp)) {
  326. trace_xfs_btree_corrupt(bp, _RET_IP_);
  327. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
  328. bp->b_target->bt_mount, bp->b_addr);
  329. xfs_buf_ioerror(bp, EFSCORRUPTED);
  330. }
  331. xfs_btree_sblock_calc_crc(bp);
  332. }
  333. const struct xfs_buf_ops xfs_allocbt_buf_ops = {
  334. .verify_read = xfs_allocbt_read_verify,
  335. .verify_write = xfs_allocbt_write_verify,
  336. };
  337. #if defined(DEBUG) || defined(XFS_WARN)
  338. STATIC int
  339. xfs_allocbt_keys_inorder(
  340. struct xfs_btree_cur *cur,
  341. union xfs_btree_key *k1,
  342. union xfs_btree_key *k2)
  343. {
  344. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  345. return be32_to_cpu(k1->alloc.ar_startblock) <
  346. be32_to_cpu(k2->alloc.ar_startblock);
  347. } else {
  348. return be32_to_cpu(k1->alloc.ar_blockcount) <
  349. be32_to_cpu(k2->alloc.ar_blockcount) ||
  350. (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
  351. be32_to_cpu(k1->alloc.ar_startblock) <
  352. be32_to_cpu(k2->alloc.ar_startblock));
  353. }
  354. }
  355. STATIC int
  356. xfs_allocbt_recs_inorder(
  357. struct xfs_btree_cur *cur,
  358. union xfs_btree_rec *r1,
  359. union xfs_btree_rec *r2)
  360. {
  361. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  362. return be32_to_cpu(r1->alloc.ar_startblock) +
  363. be32_to_cpu(r1->alloc.ar_blockcount) <=
  364. be32_to_cpu(r2->alloc.ar_startblock);
  365. } else {
  366. return be32_to_cpu(r1->alloc.ar_blockcount) <
  367. be32_to_cpu(r2->alloc.ar_blockcount) ||
  368. (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
  369. be32_to_cpu(r1->alloc.ar_startblock) <
  370. be32_to_cpu(r2->alloc.ar_startblock));
  371. }
  372. }
  373. #endif /* DEBUG */
  374. static const struct xfs_btree_ops xfs_allocbt_ops = {
  375. .rec_len = sizeof(xfs_alloc_rec_t),
  376. .key_len = sizeof(xfs_alloc_key_t),
  377. .dup_cursor = xfs_allocbt_dup_cursor,
  378. .set_root = xfs_allocbt_set_root,
  379. .alloc_block = xfs_allocbt_alloc_block,
  380. .free_block = xfs_allocbt_free_block,
  381. .update_lastrec = xfs_allocbt_update_lastrec,
  382. .get_minrecs = xfs_allocbt_get_minrecs,
  383. .get_maxrecs = xfs_allocbt_get_maxrecs,
  384. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  385. .init_rec_from_key = xfs_allocbt_init_rec_from_key,
  386. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  387. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  388. .key_diff = xfs_allocbt_key_diff,
  389. .buf_ops = &xfs_allocbt_buf_ops,
  390. #if defined(DEBUG) || defined(XFS_WARN)
  391. .keys_inorder = xfs_allocbt_keys_inorder,
  392. .recs_inorder = xfs_allocbt_recs_inorder,
  393. #endif
  394. };
  395. /*
  396. * Allocate a new allocation btree cursor.
  397. */
  398. struct xfs_btree_cur * /* new alloc btree cursor */
  399. xfs_allocbt_init_cursor(
  400. struct xfs_mount *mp, /* file system mount point */
  401. struct xfs_trans *tp, /* transaction pointer */
  402. struct xfs_buf *agbp, /* buffer for agf structure */
  403. xfs_agnumber_t agno, /* allocation group number */
  404. xfs_btnum_t btnum) /* btree identifier */
  405. {
  406. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  407. struct xfs_btree_cur *cur;
  408. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  409. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  410. cur->bc_tp = tp;
  411. cur->bc_mp = mp;
  412. cur->bc_btnum = btnum;
  413. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  414. cur->bc_ops = &xfs_allocbt_ops;
  415. if (btnum == XFS_BTNUM_CNT) {
  416. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
  417. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  418. } else {
  419. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
  420. }
  421. cur->bc_private.a.agbp = agbp;
  422. cur->bc_private.a.agno = agno;
  423. if (xfs_sb_version_hascrc(&mp->m_sb))
  424. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  425. return cur;
  426. }
  427. /*
  428. * Calculate number of records in an alloc btree block.
  429. */
  430. int
  431. xfs_allocbt_maxrecs(
  432. struct xfs_mount *mp,
  433. int blocklen,
  434. int leaf)
  435. {
  436. blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
  437. if (leaf)
  438. return blocklen / sizeof(xfs_alloc_rec_t);
  439. return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
  440. }