xfs_alloc_btree.c 12 KB

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