xfs_alloc_btree.c 12 KB

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