xfs_alloc_btree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /* Allocate the new block from the freelist. If we can't, give up. */
  70. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  71. &bno, 1);
  72. if (error)
  73. return error;
  74. if (bno == NULLAGBLOCK) {
  75. *stat = 0;
  76. return 0;
  77. }
  78. xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1, false);
  79. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  80. new->s = cpu_to_be32(bno);
  81. *stat = 1;
  82. return 0;
  83. }
  84. STATIC int
  85. xfs_allocbt_free_block(
  86. struct xfs_btree_cur *cur,
  87. struct xfs_buf *bp)
  88. {
  89. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  90. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  91. xfs_agblock_t bno;
  92. int error;
  93. bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
  94. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  95. if (error)
  96. return error;
  97. xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
  98. XFS_EXTENT_BUSY_SKIP_DISCARD);
  99. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  100. return 0;
  101. }
  102. /*
  103. * Update the longest extent in the AGF
  104. */
  105. STATIC void
  106. xfs_allocbt_update_lastrec(
  107. struct xfs_btree_cur *cur,
  108. struct xfs_btree_block *block,
  109. union xfs_btree_rec *rec,
  110. int ptr,
  111. int reason)
  112. {
  113. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  114. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  115. struct xfs_perag *pag;
  116. __be32 len;
  117. int numrecs;
  118. ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
  119. switch (reason) {
  120. case LASTREC_UPDATE:
  121. /*
  122. * If this is the last leaf block and it's the last record,
  123. * then update the size of the longest extent in the AG.
  124. */
  125. if (ptr != xfs_btree_get_numrecs(block))
  126. return;
  127. len = rec->alloc.ar_blockcount;
  128. break;
  129. case LASTREC_INSREC:
  130. if (be32_to_cpu(rec->alloc.ar_blockcount) <=
  131. be32_to_cpu(agf->agf_longest))
  132. return;
  133. len = rec->alloc.ar_blockcount;
  134. break;
  135. case LASTREC_DELREC:
  136. numrecs = xfs_btree_get_numrecs(block);
  137. if (ptr <= numrecs)
  138. return;
  139. ASSERT(ptr == numrecs + 1);
  140. if (numrecs) {
  141. xfs_alloc_rec_t *rrp;
  142. rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
  143. len = rrp->ar_blockcount;
  144. } else {
  145. len = 0;
  146. }
  147. break;
  148. default:
  149. ASSERT(0);
  150. return;
  151. }
  152. agf->agf_longest = len;
  153. pag = xfs_perag_get(cur->bc_mp, seqno);
  154. pag->pagf_longest = be32_to_cpu(len);
  155. xfs_perag_put(pag);
  156. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
  157. }
  158. STATIC int
  159. xfs_allocbt_get_minrecs(
  160. struct xfs_btree_cur *cur,
  161. int level)
  162. {
  163. return cur->bc_mp->m_alloc_mnr[level != 0];
  164. }
  165. STATIC int
  166. xfs_allocbt_get_maxrecs(
  167. struct xfs_btree_cur *cur,
  168. int level)
  169. {
  170. return cur->bc_mp->m_alloc_mxr[level != 0];
  171. }
  172. STATIC void
  173. xfs_allocbt_init_key_from_rec(
  174. union xfs_btree_key *key,
  175. union xfs_btree_rec *rec)
  176. {
  177. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  178. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  179. }
  180. STATIC void
  181. xfs_bnobt_init_high_key_from_rec(
  182. union xfs_btree_key *key,
  183. union xfs_btree_rec *rec)
  184. {
  185. __u32 x;
  186. x = be32_to_cpu(rec->alloc.ar_startblock);
  187. x += be32_to_cpu(rec->alloc.ar_blockcount) - 1;
  188. key->alloc.ar_startblock = cpu_to_be32(x);
  189. key->alloc.ar_blockcount = 0;
  190. }
  191. STATIC void
  192. xfs_cntbt_init_high_key_from_rec(
  193. union xfs_btree_key *key,
  194. union xfs_btree_rec *rec)
  195. {
  196. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  197. key->alloc.ar_startblock = 0;
  198. }
  199. STATIC void
  200. xfs_allocbt_init_rec_from_cur(
  201. struct xfs_btree_cur *cur,
  202. union xfs_btree_rec *rec)
  203. {
  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_bnobt_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. return (int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  225. }
  226. STATIC int64_t
  227. xfs_cntbt_key_diff(
  228. struct xfs_btree_cur *cur,
  229. union xfs_btree_key *key)
  230. {
  231. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  232. xfs_alloc_key_t *kp = &key->alloc;
  233. int64_t diff;
  234. diff = (int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  235. if (diff)
  236. return diff;
  237. return (int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  238. }
  239. STATIC int64_t
  240. xfs_bnobt_diff_two_keys(
  241. struct xfs_btree_cur *cur,
  242. union xfs_btree_key *k1,
  243. union xfs_btree_key *k2)
  244. {
  245. return (int64_t)be32_to_cpu(k1->alloc.ar_startblock) -
  246. be32_to_cpu(k2->alloc.ar_startblock);
  247. }
  248. STATIC int64_t
  249. xfs_cntbt_diff_two_keys(
  250. struct xfs_btree_cur *cur,
  251. union xfs_btree_key *k1,
  252. union xfs_btree_key *k2)
  253. {
  254. int64_t diff;
  255. diff = be32_to_cpu(k1->alloc.ar_blockcount) -
  256. be32_to_cpu(k2->alloc.ar_blockcount);
  257. if (diff)
  258. return diff;
  259. return be32_to_cpu(k1->alloc.ar_startblock) -
  260. be32_to_cpu(k2->alloc.ar_startblock);
  261. }
  262. static xfs_failaddr_t
  263. xfs_allocbt_verify(
  264. struct xfs_buf *bp)
  265. {
  266. struct xfs_mount *mp = bp->b_target->bt_mount;
  267. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  268. struct xfs_perag *pag = bp->b_pag;
  269. xfs_failaddr_t fa;
  270. unsigned int level;
  271. /*
  272. * magic number and level verification
  273. *
  274. * During growfs operations, we can't verify the exact level or owner as
  275. * the perag is not fully initialised and hence not attached to the
  276. * buffer. In this case, check against the maximum tree depth.
  277. *
  278. * Similarly, during log recovery we will have a perag structure
  279. * attached, but the agf information will not yet have been initialised
  280. * from the on disk AGF. Again, we can only check against maximum limits
  281. * in this case.
  282. */
  283. level = be16_to_cpu(block->bb_level);
  284. switch (block->bb_magic) {
  285. case cpu_to_be32(XFS_ABTB_CRC_MAGIC):
  286. fa = xfs_btree_sblock_v5hdr_verify(bp);
  287. if (fa)
  288. return fa;
  289. /* fall through */
  290. case cpu_to_be32(XFS_ABTB_MAGIC):
  291. if (pag && pag->pagf_init) {
  292. if (level >= pag->pagf_levels[XFS_BTNUM_BNOi])
  293. return __this_address;
  294. } else if (level >= mp->m_ag_maxlevels)
  295. return __this_address;
  296. break;
  297. case cpu_to_be32(XFS_ABTC_CRC_MAGIC):
  298. fa = xfs_btree_sblock_v5hdr_verify(bp);
  299. if (fa)
  300. return fa;
  301. /* fall through */
  302. case cpu_to_be32(XFS_ABTC_MAGIC):
  303. if (pag && pag->pagf_init) {
  304. if (level >= pag->pagf_levels[XFS_BTNUM_CNTi])
  305. return __this_address;
  306. } else if (level >= mp->m_ag_maxlevels)
  307. return __this_address;
  308. break;
  309. default:
  310. return __this_address;
  311. }
  312. return xfs_btree_sblock_verify(bp, mp->m_alloc_mxr[level != 0]);
  313. }
  314. static void
  315. xfs_allocbt_read_verify(
  316. struct xfs_buf *bp)
  317. {
  318. xfs_failaddr_t fa;
  319. if (!xfs_btree_sblock_verify_crc(bp))
  320. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  321. else {
  322. fa = xfs_allocbt_verify(bp);
  323. if (fa)
  324. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  325. }
  326. if (bp->b_error)
  327. trace_xfs_btree_corrupt(bp, _RET_IP_);
  328. }
  329. static void
  330. xfs_allocbt_write_verify(
  331. struct xfs_buf *bp)
  332. {
  333. xfs_failaddr_t fa;
  334. fa = xfs_allocbt_verify(bp);
  335. if (fa) {
  336. trace_xfs_btree_corrupt(bp, _RET_IP_);
  337. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  338. return;
  339. }
  340. xfs_btree_sblock_calc_crc(bp);
  341. }
  342. const struct xfs_buf_ops xfs_allocbt_buf_ops = {
  343. .name = "xfs_allocbt",
  344. .verify_read = xfs_allocbt_read_verify,
  345. .verify_write = xfs_allocbt_write_verify,
  346. .verify_struct = xfs_allocbt_verify,
  347. };
  348. STATIC int
  349. xfs_bnobt_keys_inorder(
  350. struct xfs_btree_cur *cur,
  351. union xfs_btree_key *k1,
  352. union xfs_btree_key *k2)
  353. {
  354. return be32_to_cpu(k1->alloc.ar_startblock) <
  355. be32_to_cpu(k2->alloc.ar_startblock);
  356. }
  357. STATIC int
  358. xfs_bnobt_recs_inorder(
  359. struct xfs_btree_cur *cur,
  360. union xfs_btree_rec *r1,
  361. union xfs_btree_rec *r2)
  362. {
  363. return be32_to_cpu(r1->alloc.ar_startblock) +
  364. be32_to_cpu(r1->alloc.ar_blockcount) <=
  365. be32_to_cpu(r2->alloc.ar_startblock);
  366. }
  367. STATIC int
  368. xfs_cntbt_keys_inorder(
  369. struct xfs_btree_cur *cur,
  370. union xfs_btree_key *k1,
  371. union xfs_btree_key *k2)
  372. {
  373. return be32_to_cpu(k1->alloc.ar_blockcount) <
  374. be32_to_cpu(k2->alloc.ar_blockcount) ||
  375. (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
  376. be32_to_cpu(k1->alloc.ar_startblock) <
  377. be32_to_cpu(k2->alloc.ar_startblock));
  378. }
  379. STATIC int
  380. xfs_cntbt_recs_inorder(
  381. struct xfs_btree_cur *cur,
  382. union xfs_btree_rec *r1,
  383. union xfs_btree_rec *r2)
  384. {
  385. return be32_to_cpu(r1->alloc.ar_blockcount) <
  386. be32_to_cpu(r2->alloc.ar_blockcount) ||
  387. (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
  388. be32_to_cpu(r1->alloc.ar_startblock) <
  389. be32_to_cpu(r2->alloc.ar_startblock));
  390. }
  391. static const struct xfs_btree_ops xfs_bnobt_ops = {
  392. .rec_len = sizeof(xfs_alloc_rec_t),
  393. .key_len = sizeof(xfs_alloc_key_t),
  394. .dup_cursor = xfs_allocbt_dup_cursor,
  395. .set_root = xfs_allocbt_set_root,
  396. .alloc_block = xfs_allocbt_alloc_block,
  397. .free_block = xfs_allocbt_free_block,
  398. .update_lastrec = xfs_allocbt_update_lastrec,
  399. .get_minrecs = xfs_allocbt_get_minrecs,
  400. .get_maxrecs = xfs_allocbt_get_maxrecs,
  401. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  402. .init_high_key_from_rec = xfs_bnobt_init_high_key_from_rec,
  403. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  404. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  405. .key_diff = xfs_bnobt_key_diff,
  406. .buf_ops = &xfs_allocbt_buf_ops,
  407. .diff_two_keys = xfs_bnobt_diff_two_keys,
  408. .keys_inorder = xfs_bnobt_keys_inorder,
  409. .recs_inorder = xfs_bnobt_recs_inorder,
  410. };
  411. static const struct xfs_btree_ops xfs_cntbt_ops = {
  412. .rec_len = sizeof(xfs_alloc_rec_t),
  413. .key_len = sizeof(xfs_alloc_key_t),
  414. .dup_cursor = xfs_allocbt_dup_cursor,
  415. .set_root = xfs_allocbt_set_root,
  416. .alloc_block = xfs_allocbt_alloc_block,
  417. .free_block = xfs_allocbt_free_block,
  418. .update_lastrec = xfs_allocbt_update_lastrec,
  419. .get_minrecs = xfs_allocbt_get_minrecs,
  420. .get_maxrecs = xfs_allocbt_get_maxrecs,
  421. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  422. .init_high_key_from_rec = xfs_cntbt_init_high_key_from_rec,
  423. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  424. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  425. .key_diff = xfs_cntbt_key_diff,
  426. .buf_ops = &xfs_allocbt_buf_ops,
  427. .diff_two_keys = xfs_cntbt_diff_two_keys,
  428. .keys_inorder = xfs_cntbt_keys_inorder,
  429. .recs_inorder = xfs_cntbt_recs_inorder,
  430. };
  431. /*
  432. * Allocate a new allocation btree cursor.
  433. */
  434. struct xfs_btree_cur * /* new alloc btree cursor */
  435. xfs_allocbt_init_cursor(
  436. struct xfs_mount *mp, /* file system mount point */
  437. struct xfs_trans *tp, /* transaction pointer */
  438. struct xfs_buf *agbp, /* buffer for agf structure */
  439. xfs_agnumber_t agno, /* allocation group number */
  440. xfs_btnum_t btnum) /* btree identifier */
  441. {
  442. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  443. struct xfs_btree_cur *cur;
  444. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  445. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
  446. cur->bc_tp = tp;
  447. cur->bc_mp = mp;
  448. cur->bc_btnum = btnum;
  449. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  450. if (btnum == XFS_BTNUM_CNT) {
  451. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_abtc_2);
  452. cur->bc_ops = &xfs_cntbt_ops;
  453. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
  454. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  455. } else {
  456. cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_abtb_2);
  457. cur->bc_ops = &xfs_bnobt_ops;
  458. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
  459. }
  460. cur->bc_private.a.agbp = agbp;
  461. cur->bc_private.a.agno = agno;
  462. if (xfs_sb_version_hascrc(&mp->m_sb))
  463. cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
  464. return cur;
  465. }
  466. /*
  467. * Calculate number of records in an alloc btree block.
  468. */
  469. int
  470. xfs_allocbt_maxrecs(
  471. struct xfs_mount *mp,
  472. int blocklen,
  473. int leaf)
  474. {
  475. blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
  476. if (leaf)
  477. return blocklen / sizeof(xfs_alloc_rec_t);
  478. return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
  479. }