xfs_alloc_btree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_btree.h"
  34. #include "xfs_btree_trace.h"
  35. #include "xfs_alloc.h"
  36. #include "xfs_error.h"
  37. #include "xfs_trace.h"
  38. STATIC struct xfs_btree_cur *
  39. xfs_allocbt_dup_cursor(
  40. struct xfs_btree_cur *cur)
  41. {
  42. return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
  43. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  44. cur->bc_btnum);
  45. }
  46. STATIC void
  47. xfs_allocbt_set_root(
  48. struct xfs_btree_cur *cur,
  49. union xfs_btree_ptr *ptr,
  50. int inc)
  51. {
  52. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  53. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  54. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  55. int btnum = cur->bc_btnum;
  56. struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
  57. ASSERT(ptr->s != 0);
  58. agf->agf_roots[btnum] = ptr->s;
  59. be32_add_cpu(&agf->agf_levels[btnum], inc);
  60. pag->pagf_levels[btnum] += inc;
  61. xfs_perag_put(pag);
  62. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  63. }
  64. STATIC int
  65. xfs_allocbt_alloc_block(
  66. struct xfs_btree_cur *cur,
  67. union xfs_btree_ptr *start,
  68. union xfs_btree_ptr *new,
  69. int length,
  70. int *stat)
  71. {
  72. int error;
  73. xfs_agblock_t bno;
  74. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  75. /* Allocate the new block from the freelist. If we can't, give up. */
  76. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  77. &bno, 1);
  78. if (error) {
  79. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  80. return error;
  81. }
  82. if (bno == NULLAGBLOCK) {
  83. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  84. *stat = 0;
  85. return 0;
  86. }
  87. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  88. new->s = cpu_to_be32(bno);
  89. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  90. *stat = 1;
  91. return 0;
  92. }
  93. STATIC int
  94. xfs_allocbt_free_block(
  95. struct xfs_btree_cur *cur,
  96. struct xfs_buf *bp)
  97. {
  98. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  99. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  100. xfs_agblock_t bno;
  101. int error;
  102. bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
  103. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  104. if (error)
  105. return error;
  106. /*
  107. * Since blocks move to the free list without the coordination used in
  108. * xfs_bmap_finish, we can't allow block to be available for
  109. * reallocation and non-transaction writing (user data) until we know
  110. * that the transaction that moved it to the free list is permanently
  111. * on disk. We track the blocks by declaring these blocks as "busy";
  112. * the busy list is maintained on a per-ag basis and each transaction
  113. * records which entries should be removed when the iclog commits to
  114. * disk. If a busy block is allocated, the iclog is pushed up to the
  115. * LSN that freed the block.
  116. */
  117. xfs_alloc_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
  118. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  119. return 0;
  120. }
  121. /*
  122. * Update the longest extent in the AGF
  123. */
  124. STATIC void
  125. xfs_allocbt_update_lastrec(
  126. struct xfs_btree_cur *cur,
  127. struct xfs_btree_block *block,
  128. union xfs_btree_rec *rec,
  129. int ptr,
  130. int reason)
  131. {
  132. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  133. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  134. struct xfs_perag *pag;
  135. __be32 len;
  136. int numrecs;
  137. ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
  138. switch (reason) {
  139. case LASTREC_UPDATE:
  140. /*
  141. * If this is the last leaf block and it's the last record,
  142. * then update the size of the longest extent in the AG.
  143. */
  144. if (ptr != xfs_btree_get_numrecs(block))
  145. return;
  146. len = rec->alloc.ar_blockcount;
  147. break;
  148. case LASTREC_INSREC:
  149. if (be32_to_cpu(rec->alloc.ar_blockcount) <=
  150. be32_to_cpu(agf->agf_longest))
  151. return;
  152. len = rec->alloc.ar_blockcount;
  153. break;
  154. case LASTREC_DELREC:
  155. numrecs = xfs_btree_get_numrecs(block);
  156. if (ptr <= numrecs)
  157. return;
  158. ASSERT(ptr == numrecs + 1);
  159. if (numrecs) {
  160. xfs_alloc_rec_t *rrp;
  161. rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
  162. len = rrp->ar_blockcount;
  163. } else {
  164. len = 0;
  165. }
  166. break;
  167. default:
  168. ASSERT(0);
  169. return;
  170. }
  171. agf->agf_longest = len;
  172. pag = xfs_perag_get(cur->bc_mp, seqno);
  173. pag->pagf_longest = be32_to_cpu(len);
  174. xfs_perag_put(pag);
  175. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
  176. }
  177. STATIC int
  178. xfs_allocbt_get_minrecs(
  179. struct xfs_btree_cur *cur,
  180. int level)
  181. {
  182. return cur->bc_mp->m_alloc_mnr[level != 0];
  183. }
  184. STATIC int
  185. xfs_allocbt_get_maxrecs(
  186. struct xfs_btree_cur *cur,
  187. int level)
  188. {
  189. return cur->bc_mp->m_alloc_mxr[level != 0];
  190. }
  191. STATIC void
  192. xfs_allocbt_init_key_from_rec(
  193. union xfs_btree_key *key,
  194. union xfs_btree_rec *rec)
  195. {
  196. ASSERT(rec->alloc.ar_startblock != 0);
  197. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  198. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  199. }
  200. STATIC void
  201. xfs_allocbt_init_rec_from_key(
  202. union xfs_btree_key *key,
  203. union xfs_btree_rec *rec)
  204. {
  205. ASSERT(key->alloc.ar_startblock != 0);
  206. rec->alloc.ar_startblock = key->alloc.ar_startblock;
  207. rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
  208. }
  209. STATIC void
  210. xfs_allocbt_init_rec_from_cur(
  211. struct xfs_btree_cur *cur,
  212. union xfs_btree_rec *rec)
  213. {
  214. ASSERT(cur->bc_rec.a.ar_startblock != 0);
  215. rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  216. rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  217. }
  218. STATIC void
  219. xfs_allocbt_init_ptr_from_cur(
  220. struct xfs_btree_cur *cur,
  221. union xfs_btree_ptr *ptr)
  222. {
  223. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  224. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  225. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  226. ptr->s = agf->agf_roots[cur->bc_btnum];
  227. }
  228. STATIC __int64_t
  229. xfs_allocbt_key_diff(
  230. struct xfs_btree_cur *cur,
  231. union xfs_btree_key *key)
  232. {
  233. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  234. xfs_alloc_key_t *kp = &key->alloc;
  235. __int64_t diff;
  236. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  237. return (__int64_t)be32_to_cpu(kp->ar_startblock) -
  238. rec->ar_startblock;
  239. }
  240. diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  241. if (diff)
  242. return diff;
  243. return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  244. }
  245. STATIC int
  246. xfs_allocbt_kill_root(
  247. struct xfs_btree_cur *cur,
  248. struct xfs_buf *bp,
  249. int level,
  250. union xfs_btree_ptr *newroot)
  251. {
  252. int error;
  253. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  254. XFS_BTREE_STATS_INC(cur, killroot);
  255. /*
  256. * Update the root pointer, decreasing the level by 1 and then
  257. * free the old root.
  258. */
  259. xfs_allocbt_set_root(cur, newroot, -1);
  260. error = xfs_allocbt_free_block(cur, bp);
  261. if (error) {
  262. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  263. return error;
  264. }
  265. XFS_BTREE_STATS_INC(cur, free);
  266. xfs_btree_setbuf(cur, level, NULL);
  267. cur->bc_nlevels--;
  268. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  269. return 0;
  270. }
  271. #ifdef DEBUG
  272. STATIC int
  273. xfs_allocbt_keys_inorder(
  274. struct xfs_btree_cur *cur,
  275. union xfs_btree_key *k1,
  276. union xfs_btree_key *k2)
  277. {
  278. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  279. return be32_to_cpu(k1->alloc.ar_startblock) <
  280. be32_to_cpu(k2->alloc.ar_startblock);
  281. } else {
  282. return be32_to_cpu(k1->alloc.ar_blockcount) <
  283. be32_to_cpu(k2->alloc.ar_blockcount) ||
  284. (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
  285. be32_to_cpu(k1->alloc.ar_startblock) <
  286. be32_to_cpu(k2->alloc.ar_startblock));
  287. }
  288. }
  289. STATIC int
  290. xfs_allocbt_recs_inorder(
  291. struct xfs_btree_cur *cur,
  292. union xfs_btree_rec *r1,
  293. union xfs_btree_rec *r2)
  294. {
  295. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  296. return be32_to_cpu(r1->alloc.ar_startblock) +
  297. be32_to_cpu(r1->alloc.ar_blockcount) <=
  298. be32_to_cpu(r2->alloc.ar_startblock);
  299. } else {
  300. return be32_to_cpu(r1->alloc.ar_blockcount) <
  301. be32_to_cpu(r2->alloc.ar_blockcount) ||
  302. (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
  303. be32_to_cpu(r1->alloc.ar_startblock) <
  304. be32_to_cpu(r2->alloc.ar_startblock));
  305. }
  306. }
  307. #endif /* DEBUG */
  308. #ifdef XFS_BTREE_TRACE
  309. ktrace_t *xfs_allocbt_trace_buf;
  310. STATIC void
  311. xfs_allocbt_trace_enter(
  312. struct xfs_btree_cur *cur,
  313. const char *func,
  314. char *s,
  315. int type,
  316. int line,
  317. __psunsigned_t a0,
  318. __psunsigned_t a1,
  319. __psunsigned_t a2,
  320. __psunsigned_t a3,
  321. __psunsigned_t a4,
  322. __psunsigned_t a5,
  323. __psunsigned_t a6,
  324. __psunsigned_t a7,
  325. __psunsigned_t a8,
  326. __psunsigned_t a9,
  327. __psunsigned_t a10)
  328. {
  329. ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
  330. (void *)func, (void *)s, NULL, (void *)cur,
  331. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  332. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  333. (void *)a8, (void *)a9, (void *)a10);
  334. }
  335. STATIC void
  336. xfs_allocbt_trace_cursor(
  337. struct xfs_btree_cur *cur,
  338. __uint32_t *s0,
  339. __uint64_t *l0,
  340. __uint64_t *l1)
  341. {
  342. *s0 = cur->bc_private.a.agno;
  343. *l0 = cur->bc_rec.a.ar_startblock;
  344. *l1 = cur->bc_rec.a.ar_blockcount;
  345. }
  346. STATIC void
  347. xfs_allocbt_trace_key(
  348. struct xfs_btree_cur *cur,
  349. union xfs_btree_key *key,
  350. __uint64_t *l0,
  351. __uint64_t *l1)
  352. {
  353. *l0 = be32_to_cpu(key->alloc.ar_startblock);
  354. *l1 = be32_to_cpu(key->alloc.ar_blockcount);
  355. }
  356. STATIC void
  357. xfs_allocbt_trace_record(
  358. struct xfs_btree_cur *cur,
  359. union xfs_btree_rec *rec,
  360. __uint64_t *l0,
  361. __uint64_t *l1,
  362. __uint64_t *l2)
  363. {
  364. *l0 = be32_to_cpu(rec->alloc.ar_startblock);
  365. *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
  366. *l2 = 0;
  367. }
  368. #endif /* XFS_BTREE_TRACE */
  369. static const struct xfs_btree_ops xfs_allocbt_ops = {
  370. .rec_len = sizeof(xfs_alloc_rec_t),
  371. .key_len = sizeof(xfs_alloc_key_t),
  372. .dup_cursor = xfs_allocbt_dup_cursor,
  373. .set_root = xfs_allocbt_set_root,
  374. .kill_root = xfs_allocbt_kill_root,
  375. .alloc_block = xfs_allocbt_alloc_block,
  376. .free_block = xfs_allocbt_free_block,
  377. .update_lastrec = xfs_allocbt_update_lastrec,
  378. .get_minrecs = xfs_allocbt_get_minrecs,
  379. .get_maxrecs = xfs_allocbt_get_maxrecs,
  380. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  381. .init_rec_from_key = xfs_allocbt_init_rec_from_key,
  382. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  383. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  384. .key_diff = xfs_allocbt_key_diff,
  385. #ifdef DEBUG
  386. .keys_inorder = xfs_allocbt_keys_inorder,
  387. .recs_inorder = xfs_allocbt_recs_inorder,
  388. #endif
  389. #ifdef XFS_BTREE_TRACE
  390. .trace_enter = xfs_allocbt_trace_enter,
  391. .trace_cursor = xfs_allocbt_trace_cursor,
  392. .trace_key = xfs_allocbt_trace_key,
  393. .trace_record = xfs_allocbt_trace_record,
  394. #endif
  395. };
  396. /*
  397. * Allocate a new allocation btree cursor.
  398. */
  399. struct xfs_btree_cur * /* new alloc btree cursor */
  400. xfs_allocbt_init_cursor(
  401. struct xfs_mount *mp, /* file system mount point */
  402. struct xfs_trans *tp, /* transaction pointer */
  403. struct xfs_buf *agbp, /* buffer for agf structure */
  404. xfs_agnumber_t agno, /* allocation group number */
  405. xfs_btnum_t btnum) /* btree identifier */
  406. {
  407. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  408. struct xfs_btree_cur *cur;
  409. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  410. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  411. cur->bc_tp = tp;
  412. cur->bc_mp = mp;
  413. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
  414. cur->bc_btnum = btnum;
  415. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  416. cur->bc_ops = &xfs_allocbt_ops;
  417. if (btnum == XFS_BTNUM_CNT)
  418. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  419. cur->bc_private.a.agbp = agbp;
  420. cur->bc_private.a.agno = agno;
  421. return cur;
  422. }
  423. /*
  424. * Calculate number of records in an alloc btree block.
  425. */
  426. int
  427. xfs_allocbt_maxrecs(
  428. struct xfs_mount *mp,
  429. int blocklen,
  430. int leaf)
  431. {
  432. blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
  433. if (leaf)
  434. return blocklen / sizeof(xfs_alloc_rec_t);
  435. return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
  436. }