xfs_alloc_btree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_btree_trace.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_alloc.h"
  41. #include "xfs_error.h"
  42. STATIC struct xfs_btree_cur *
  43. xfs_allocbt_dup_cursor(
  44. struct xfs_btree_cur *cur)
  45. {
  46. return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
  47. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  48. cur->bc_btnum);
  49. }
  50. STATIC void
  51. xfs_allocbt_set_root(
  52. struct xfs_btree_cur *cur,
  53. union xfs_btree_ptr *ptr,
  54. int inc)
  55. {
  56. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  57. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  58. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  59. int btnum = cur->bc_btnum;
  60. ASSERT(ptr->s != 0);
  61. agf->agf_roots[btnum] = ptr->s;
  62. be32_add_cpu(&agf->agf_levels[btnum], inc);
  63. cur->bc_mp->m_perag[seqno].pagf_levels[btnum] += inc;
  64. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  65. }
  66. STATIC int
  67. xfs_allocbt_alloc_block(
  68. struct xfs_btree_cur *cur,
  69. union xfs_btree_ptr *start,
  70. union xfs_btree_ptr *new,
  71. int length,
  72. int *stat)
  73. {
  74. int error;
  75. xfs_agblock_t bno;
  76. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  77. /* Allocate the new block from the freelist. If we can't, give up. */
  78. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  79. &bno, 1);
  80. if (error) {
  81. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  82. return error;
  83. }
  84. if (bno == NULLAGBLOCK) {
  85. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  86. *stat = 0;
  87. return 0;
  88. }
  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_mark_busy(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. __be32 len;
  137. int numrecs;
  138. ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
  139. switch (reason) {
  140. case LASTREC_UPDATE:
  141. /*
  142. * If this is the last leaf block and it's the last record,
  143. * then update the size of the longest extent in the AG.
  144. */
  145. if (ptr != xfs_btree_get_numrecs(block))
  146. return;
  147. len = rec->alloc.ar_blockcount;
  148. break;
  149. case LASTREC_INSREC:
  150. if (be32_to_cpu(rec->alloc.ar_blockcount) <=
  151. be32_to_cpu(agf->agf_longest))
  152. return;
  153. len = rec->alloc.ar_blockcount;
  154. break;
  155. case LASTREC_DELREC:
  156. numrecs = xfs_btree_get_numrecs(block);
  157. if (ptr <= numrecs)
  158. return;
  159. ASSERT(ptr == numrecs + 1);
  160. if (numrecs) {
  161. xfs_alloc_rec_t *rrp;
  162. rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
  163. len = rrp->ar_blockcount;
  164. } else {
  165. len = 0;
  166. }
  167. break;
  168. default:
  169. ASSERT(0);
  170. return;
  171. }
  172. agf->agf_longest = len;
  173. cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
  174. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
  175. }
  176. STATIC int
  177. xfs_allocbt_get_minrecs(
  178. struct xfs_btree_cur *cur,
  179. int level)
  180. {
  181. return cur->bc_mp->m_alloc_mnr[level != 0];
  182. }
  183. STATIC int
  184. xfs_allocbt_get_maxrecs(
  185. struct xfs_btree_cur *cur,
  186. int level)
  187. {
  188. return cur->bc_mp->m_alloc_mxr[level != 0];
  189. }
  190. STATIC void
  191. xfs_allocbt_init_key_from_rec(
  192. union xfs_btree_key *key,
  193. union xfs_btree_rec *rec)
  194. {
  195. ASSERT(rec->alloc.ar_startblock != 0);
  196. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  197. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  198. }
  199. STATIC void
  200. xfs_allocbt_init_rec_from_key(
  201. union xfs_btree_key *key,
  202. union xfs_btree_rec *rec)
  203. {
  204. ASSERT(key->alloc.ar_startblock != 0);
  205. rec->alloc.ar_startblock = key->alloc.ar_startblock;
  206. rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
  207. }
  208. STATIC void
  209. xfs_allocbt_init_rec_from_cur(
  210. struct xfs_btree_cur *cur,
  211. union xfs_btree_rec *rec)
  212. {
  213. ASSERT(cur->bc_rec.a.ar_startblock != 0);
  214. rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  215. rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  216. }
  217. STATIC void
  218. xfs_allocbt_init_ptr_from_cur(
  219. struct xfs_btree_cur *cur,
  220. union xfs_btree_ptr *ptr)
  221. {
  222. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  223. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  224. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  225. ptr->s = agf->agf_roots[cur->bc_btnum];
  226. }
  227. STATIC __int64_t
  228. xfs_allocbt_key_diff(
  229. struct xfs_btree_cur *cur,
  230. union xfs_btree_key *key)
  231. {
  232. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  233. xfs_alloc_key_t *kp = &key->alloc;
  234. __int64_t diff;
  235. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  236. return (__int64_t)be32_to_cpu(kp->ar_startblock) -
  237. rec->ar_startblock;
  238. }
  239. diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  240. if (diff)
  241. return diff;
  242. return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  243. }
  244. STATIC int
  245. xfs_allocbt_kill_root(
  246. struct xfs_btree_cur *cur,
  247. struct xfs_buf *bp,
  248. int level,
  249. union xfs_btree_ptr *newroot)
  250. {
  251. int error;
  252. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  253. XFS_BTREE_STATS_INC(cur, killroot);
  254. /*
  255. * Update the root pointer, decreasing the level by 1 and then
  256. * free the old root.
  257. */
  258. xfs_allocbt_set_root(cur, newroot, -1);
  259. error = xfs_allocbt_free_block(cur, bp);
  260. if (error) {
  261. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  262. return error;
  263. }
  264. XFS_BTREE_STATS_INC(cur, free);
  265. xfs_btree_setbuf(cur, level, NULL);
  266. cur->bc_nlevels--;
  267. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  268. return 0;
  269. }
  270. #ifdef DEBUG
  271. STATIC int
  272. xfs_allocbt_keys_inorder(
  273. struct xfs_btree_cur *cur,
  274. union xfs_btree_key *k1,
  275. union xfs_btree_key *k2)
  276. {
  277. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  278. return be32_to_cpu(k1->alloc.ar_startblock) <
  279. be32_to_cpu(k2->alloc.ar_startblock);
  280. } else {
  281. return be32_to_cpu(k1->alloc.ar_blockcount) <
  282. be32_to_cpu(k2->alloc.ar_blockcount) ||
  283. (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
  284. be32_to_cpu(k1->alloc.ar_startblock) <
  285. be32_to_cpu(k2->alloc.ar_startblock));
  286. }
  287. }
  288. STATIC int
  289. xfs_allocbt_recs_inorder(
  290. struct xfs_btree_cur *cur,
  291. union xfs_btree_rec *r1,
  292. union xfs_btree_rec *r2)
  293. {
  294. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  295. return be32_to_cpu(r1->alloc.ar_startblock) +
  296. be32_to_cpu(r1->alloc.ar_blockcount) <=
  297. be32_to_cpu(r2->alloc.ar_startblock);
  298. } else {
  299. return be32_to_cpu(r1->alloc.ar_blockcount) <
  300. be32_to_cpu(r2->alloc.ar_blockcount) ||
  301. (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
  302. be32_to_cpu(r1->alloc.ar_startblock) <
  303. be32_to_cpu(r2->alloc.ar_startblock));
  304. }
  305. }
  306. #endif /* DEBUG */
  307. #ifdef XFS_BTREE_TRACE
  308. ktrace_t *xfs_allocbt_trace_buf;
  309. STATIC void
  310. xfs_allocbt_trace_enter(
  311. struct xfs_btree_cur *cur,
  312. const char *func,
  313. char *s,
  314. int type,
  315. int line,
  316. __psunsigned_t a0,
  317. __psunsigned_t a1,
  318. __psunsigned_t a2,
  319. __psunsigned_t a3,
  320. __psunsigned_t a4,
  321. __psunsigned_t a5,
  322. __psunsigned_t a6,
  323. __psunsigned_t a7,
  324. __psunsigned_t a8,
  325. __psunsigned_t a9,
  326. __psunsigned_t a10)
  327. {
  328. ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
  329. (void *)func, (void *)s, NULL, (void *)cur,
  330. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  331. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  332. (void *)a8, (void *)a9, (void *)a10);
  333. }
  334. STATIC void
  335. xfs_allocbt_trace_cursor(
  336. struct xfs_btree_cur *cur,
  337. __uint32_t *s0,
  338. __uint64_t *l0,
  339. __uint64_t *l1)
  340. {
  341. *s0 = cur->bc_private.a.agno;
  342. *l0 = cur->bc_rec.a.ar_startblock;
  343. *l1 = cur->bc_rec.a.ar_blockcount;
  344. }
  345. STATIC void
  346. xfs_allocbt_trace_key(
  347. struct xfs_btree_cur *cur,
  348. union xfs_btree_key *key,
  349. __uint64_t *l0,
  350. __uint64_t *l1)
  351. {
  352. *l0 = be32_to_cpu(key->alloc.ar_startblock);
  353. *l1 = be32_to_cpu(key->alloc.ar_blockcount);
  354. }
  355. STATIC void
  356. xfs_allocbt_trace_record(
  357. struct xfs_btree_cur *cur,
  358. union xfs_btree_rec *rec,
  359. __uint64_t *l0,
  360. __uint64_t *l1,
  361. __uint64_t *l2)
  362. {
  363. *l0 = be32_to_cpu(rec->alloc.ar_startblock);
  364. *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
  365. *l2 = 0;
  366. }
  367. #endif /* XFS_BTREE_TRACE */
  368. static const struct xfs_btree_ops xfs_allocbt_ops = {
  369. .rec_len = sizeof(xfs_alloc_rec_t),
  370. .key_len = sizeof(xfs_alloc_key_t),
  371. .dup_cursor = xfs_allocbt_dup_cursor,
  372. .set_root = xfs_allocbt_set_root,
  373. .kill_root = xfs_allocbt_kill_root,
  374. .alloc_block = xfs_allocbt_alloc_block,
  375. .free_block = xfs_allocbt_free_block,
  376. .update_lastrec = xfs_allocbt_update_lastrec,
  377. .get_minrecs = xfs_allocbt_get_minrecs,
  378. .get_maxrecs = xfs_allocbt_get_maxrecs,
  379. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  380. .init_rec_from_key = xfs_allocbt_init_rec_from_key,
  381. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  382. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  383. .key_diff = xfs_allocbt_key_diff,
  384. #ifdef DEBUG
  385. .keys_inorder = xfs_allocbt_keys_inorder,
  386. .recs_inorder = xfs_allocbt_recs_inorder,
  387. #endif
  388. #ifdef XFS_BTREE_TRACE
  389. .trace_enter = xfs_allocbt_trace_enter,
  390. .trace_cursor = xfs_allocbt_trace_cursor,
  391. .trace_key = xfs_allocbt_trace_key,
  392. .trace_record = xfs_allocbt_trace_record,
  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_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
  413. cur->bc_btnum = btnum;
  414. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  415. cur->bc_ops = &xfs_allocbt_ops;
  416. if (btnum == XFS_BTNUM_CNT)
  417. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  418. cur->bc_private.a.agbp = agbp;
  419. cur->bc_private.a.agno = agno;
  420. return cur;
  421. }
  422. /*
  423. * Calculate number of records in an alloc btree block.
  424. */
  425. int
  426. xfs_allocbt_maxrecs(
  427. struct xfs_mount *mp,
  428. int blocklen,
  429. int leaf)
  430. {
  431. blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
  432. if (leaf)
  433. return blocklen / sizeof(xfs_alloc_rec_t);
  434. return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
  435. }