xfs_alloc_btree.c 12 KB

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