xfs_alloc_btree.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. * Prototypes for internal functions.
  44. */
  45. STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
  46. STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  47. STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  48. STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  49. /*
  50. * Internal functions.
  51. */
  52. /*
  53. * Single level of the xfs_alloc_delete record deletion routine.
  54. * Delete record pointed to by cur/level.
  55. * Remove the record from its block then rebalance the tree.
  56. * Return 0 for error, 1 for done, 2 to go on to the next level.
  57. */
  58. STATIC int /* error */
  59. xfs_alloc_delrec(
  60. xfs_btree_cur_t *cur, /* btree cursor */
  61. int level, /* level removing record from */
  62. int *stat) /* fail/done/go-on */
  63. {
  64. xfs_agf_t *agf; /* allocation group freelist header */
  65. xfs_alloc_block_t *block; /* btree block record/key lives in */
  66. xfs_agblock_t bno; /* btree block number */
  67. xfs_buf_t *bp; /* buffer for block */
  68. int error; /* error return value */
  69. int i; /* loop index */
  70. xfs_alloc_key_t key; /* kp points here if block is level 0 */
  71. xfs_agblock_t lbno; /* left block's block number */
  72. xfs_buf_t *lbp; /* left block's buffer pointer */
  73. xfs_alloc_block_t *left; /* left btree block */
  74. xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
  75. xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
  76. int lrecs=0; /* number of records in left block */
  77. xfs_alloc_rec_t *lrp; /* left block record pointer */
  78. xfs_mount_t *mp; /* mount structure */
  79. int ptr; /* index in btree block for this rec */
  80. xfs_agblock_t rbno; /* right block's block number */
  81. xfs_buf_t *rbp; /* right block's buffer pointer */
  82. xfs_alloc_block_t *right; /* right btree block */
  83. xfs_alloc_key_t *rkp; /* right block key pointer */
  84. xfs_alloc_ptr_t *rpp; /* right block address pointer */
  85. int rrecs=0; /* number of records in right block */
  86. int numrecs;
  87. xfs_alloc_rec_t *rrp; /* right block record pointer */
  88. xfs_btree_cur_t *tcur; /* temporary btree cursor */
  89. /*
  90. * Get the index of the entry being deleted, check for nothing there.
  91. */
  92. ptr = cur->bc_ptrs[level];
  93. if (ptr == 0) {
  94. *stat = 0;
  95. return 0;
  96. }
  97. /*
  98. * Get the buffer & block containing the record or key/ptr.
  99. */
  100. bp = cur->bc_bufs[level];
  101. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  102. #ifdef DEBUG
  103. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  104. return error;
  105. #endif
  106. /*
  107. * Fail if we're off the end of the block.
  108. */
  109. numrecs = be16_to_cpu(block->bb_numrecs);
  110. if (ptr > numrecs) {
  111. *stat = 0;
  112. return 0;
  113. }
  114. XFS_STATS_INC(xs_abt_delrec);
  115. /*
  116. * It's a nonleaf. Excise the key and ptr being deleted, by
  117. * sliding the entries past them down one.
  118. * Log the changed areas of the block.
  119. */
  120. if (level > 0) {
  121. lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
  122. lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
  123. #ifdef DEBUG
  124. for (i = ptr; i < numrecs; i++) {
  125. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
  126. return error;
  127. }
  128. #endif
  129. if (ptr < numrecs) {
  130. memmove(&lkp[ptr - 1], &lkp[ptr],
  131. (numrecs - ptr) * sizeof(*lkp));
  132. memmove(&lpp[ptr - 1], &lpp[ptr],
  133. (numrecs - ptr) * sizeof(*lpp));
  134. xfs_alloc_log_ptrs(cur, bp, ptr, numrecs - 1);
  135. xfs_alloc_log_keys(cur, bp, ptr, numrecs - 1);
  136. }
  137. }
  138. /*
  139. * It's a leaf. Excise the record being deleted, by sliding the
  140. * entries past it down one. Log the changed areas of the block.
  141. */
  142. else {
  143. lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
  144. if (ptr < numrecs) {
  145. memmove(&lrp[ptr - 1], &lrp[ptr],
  146. (numrecs - ptr) * sizeof(*lrp));
  147. xfs_alloc_log_recs(cur, bp, ptr, numrecs - 1);
  148. }
  149. /*
  150. * If it's the first record in the block, we'll need a key
  151. * structure to pass up to the next level (updkey).
  152. */
  153. if (ptr == 1) {
  154. key.ar_startblock = lrp->ar_startblock;
  155. key.ar_blockcount = lrp->ar_blockcount;
  156. lkp = &key;
  157. }
  158. }
  159. /*
  160. * Decrement and log the number of entries in the block.
  161. */
  162. numrecs--;
  163. block->bb_numrecs = cpu_to_be16(numrecs);
  164. xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
  165. /*
  166. * See if the longest free extent in the allocation group was
  167. * changed by this operation. True if it's the by-size btree, and
  168. * this is the leaf level, and there is no right sibling block,
  169. * and this was the last record.
  170. */
  171. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  172. mp = cur->bc_mp;
  173. if (level == 0 &&
  174. cur->bc_btnum == XFS_BTNUM_CNT &&
  175. be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
  176. ptr > numrecs) {
  177. ASSERT(ptr == numrecs + 1);
  178. /*
  179. * There are still records in the block. Grab the size
  180. * from the last one.
  181. */
  182. if (numrecs) {
  183. rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
  184. agf->agf_longest = rrp->ar_blockcount;
  185. }
  186. /*
  187. * No free extents left.
  188. */
  189. else
  190. agf->agf_longest = 0;
  191. mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest =
  192. be32_to_cpu(agf->agf_longest);
  193. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  194. XFS_AGF_LONGEST);
  195. }
  196. /*
  197. * Is this the root level? If so, we're almost done.
  198. */
  199. if (level == cur->bc_nlevels - 1) {
  200. /*
  201. * If this is the root level,
  202. * and there's only one entry left,
  203. * and it's NOT the leaf level,
  204. * then we can get rid of this level.
  205. */
  206. if (numrecs == 1 && level > 0) {
  207. /*
  208. * lpp is still set to the first pointer in the block.
  209. * Make it the new root of the btree.
  210. */
  211. bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
  212. agf->agf_roots[cur->bc_btnum] = *lpp;
  213. be32_add_cpu(&agf->agf_levels[cur->bc_btnum], -1);
  214. mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--;
  215. /*
  216. * Put this buffer/block on the ag's freelist.
  217. */
  218. error = xfs_alloc_put_freelist(cur->bc_tp,
  219. cur->bc_private.a.agbp, NULL, bno, 1);
  220. if (error)
  221. return error;
  222. /*
  223. * Since blocks move to the free list without the
  224. * coordination used in xfs_bmap_finish, we can't allow
  225. * block to be available for reallocation and
  226. * non-transaction writing (user data) until we know
  227. * that the transaction that moved it to the free list
  228. * is permanently on disk. We track the blocks by
  229. * declaring these blocks as "busy"; the busy list is
  230. * maintained on a per-ag basis and each transaction
  231. * records which entries should be removed when the
  232. * iclog commits to disk. If a busy block is
  233. * allocated, the iclog is pushed up to the LSN
  234. * that freed the block.
  235. */
  236. xfs_alloc_mark_busy(cur->bc_tp,
  237. be32_to_cpu(agf->agf_seqno), bno, 1);
  238. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  239. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  240. XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  241. /*
  242. * Update the cursor so there's one fewer level.
  243. */
  244. xfs_btree_setbuf(cur, level, NULL);
  245. cur->bc_nlevels--;
  246. } else if (level > 0 &&
  247. (error = xfs_btree_decrement(cur, level, &i)))
  248. return error;
  249. *stat = 1;
  250. return 0;
  251. }
  252. /*
  253. * If we deleted the leftmost entry in the block, update the
  254. * key values above us in the tree.
  255. */
  256. if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)lkp, level + 1)))
  257. return error;
  258. /*
  259. * If the number of records remaining in the block is at least
  260. * the minimum, we're done.
  261. */
  262. if (numrecs >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
  263. if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
  264. return error;
  265. *stat = 1;
  266. return 0;
  267. }
  268. /*
  269. * Otherwise, we have to move some records around to keep the
  270. * tree balanced. Look at the left and right sibling blocks to
  271. * see if we can re-balance by moving only one record.
  272. */
  273. rbno = be32_to_cpu(block->bb_rightsib);
  274. lbno = be32_to_cpu(block->bb_leftsib);
  275. bno = NULLAGBLOCK;
  276. ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
  277. /*
  278. * Duplicate the cursor so our btree manipulations here won't
  279. * disrupt the next level up.
  280. */
  281. if ((error = xfs_btree_dup_cursor(cur, &tcur)))
  282. return error;
  283. /*
  284. * If there's a right sibling, see if it's ok to shift an entry
  285. * out of it.
  286. */
  287. if (rbno != NULLAGBLOCK) {
  288. /*
  289. * Move the temp cursor to the last entry in the next block.
  290. * Actually any entry but the first would suffice.
  291. */
  292. i = xfs_btree_lastrec(tcur, level);
  293. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  294. if ((error = xfs_btree_increment(tcur, level, &i)))
  295. goto error0;
  296. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  297. i = xfs_btree_lastrec(tcur, level);
  298. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  299. /*
  300. * Grab a pointer to the block.
  301. */
  302. rbp = tcur->bc_bufs[level];
  303. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  304. #ifdef DEBUG
  305. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  306. goto error0;
  307. #endif
  308. /*
  309. * Grab the current block number, for future use.
  310. */
  311. bno = be32_to_cpu(right->bb_leftsib);
  312. /*
  313. * If right block is full enough so that removing one entry
  314. * won't make it too empty, and left-shifting an entry out
  315. * of right to us works, we're done.
  316. */
  317. if (be16_to_cpu(right->bb_numrecs) - 1 >=
  318. XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
  319. if ((error = xfs_btree_lshift(tcur, level, &i)))
  320. goto error0;
  321. if (i) {
  322. ASSERT(be16_to_cpu(block->bb_numrecs) >=
  323. XFS_ALLOC_BLOCK_MINRECS(level, cur));
  324. xfs_btree_del_cursor(tcur,
  325. XFS_BTREE_NOERROR);
  326. if (level > 0 &&
  327. (error = xfs_btree_decrement(cur, level,
  328. &i)))
  329. return error;
  330. *stat = 1;
  331. return 0;
  332. }
  333. }
  334. /*
  335. * Otherwise, grab the number of records in right for
  336. * future reference, and fix up the temp cursor to point
  337. * to our block again (last record).
  338. */
  339. rrecs = be16_to_cpu(right->bb_numrecs);
  340. if (lbno != NULLAGBLOCK) {
  341. i = xfs_btree_firstrec(tcur, level);
  342. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  343. if ((error = xfs_btree_decrement(tcur, level, &i)))
  344. goto error0;
  345. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  346. }
  347. }
  348. /*
  349. * If there's a left sibling, see if it's ok to shift an entry
  350. * out of it.
  351. */
  352. if (lbno != NULLAGBLOCK) {
  353. /*
  354. * Move the temp cursor to the first entry in the
  355. * previous block.
  356. */
  357. i = xfs_btree_firstrec(tcur, level);
  358. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  359. if ((error = xfs_btree_decrement(tcur, level, &i)))
  360. goto error0;
  361. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  362. xfs_btree_firstrec(tcur, level);
  363. /*
  364. * Grab a pointer to the block.
  365. */
  366. lbp = tcur->bc_bufs[level];
  367. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  368. #ifdef DEBUG
  369. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  370. goto error0;
  371. #endif
  372. /*
  373. * Grab the current block number, for future use.
  374. */
  375. bno = be32_to_cpu(left->bb_rightsib);
  376. /*
  377. * If left block is full enough so that removing one entry
  378. * won't make it too empty, and right-shifting an entry out
  379. * of left to us works, we're done.
  380. */
  381. if (be16_to_cpu(left->bb_numrecs) - 1 >=
  382. XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
  383. if ((error = xfs_btree_rshift(tcur, level, &i)))
  384. goto error0;
  385. if (i) {
  386. ASSERT(be16_to_cpu(block->bb_numrecs) >=
  387. XFS_ALLOC_BLOCK_MINRECS(level, cur));
  388. xfs_btree_del_cursor(tcur,
  389. XFS_BTREE_NOERROR);
  390. if (level == 0)
  391. cur->bc_ptrs[0]++;
  392. *stat = 1;
  393. return 0;
  394. }
  395. }
  396. /*
  397. * Otherwise, grab the number of records in right for
  398. * future reference.
  399. */
  400. lrecs = be16_to_cpu(left->bb_numrecs);
  401. }
  402. /*
  403. * Delete the temp cursor, we're done with it.
  404. */
  405. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  406. /*
  407. * If here, we need to do a join to keep the tree balanced.
  408. */
  409. ASSERT(bno != NULLAGBLOCK);
  410. /*
  411. * See if we can join with the left neighbor block.
  412. */
  413. if (lbno != NULLAGBLOCK &&
  414. lrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  415. /*
  416. * Set "right" to be the starting block,
  417. * "left" to be the left neighbor.
  418. */
  419. rbno = bno;
  420. right = block;
  421. rrecs = be16_to_cpu(right->bb_numrecs);
  422. rbp = bp;
  423. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  424. cur->bc_private.a.agno, lbno, 0, &lbp,
  425. XFS_ALLOC_BTREE_REF)))
  426. return error;
  427. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  428. lrecs = be16_to_cpu(left->bb_numrecs);
  429. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  430. return error;
  431. }
  432. /*
  433. * If that won't work, see if we can join with the right neighbor block.
  434. */
  435. else if (rbno != NULLAGBLOCK &&
  436. rrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  437. /*
  438. * Set "left" to be the starting block,
  439. * "right" to be the right neighbor.
  440. */
  441. lbno = bno;
  442. left = block;
  443. lrecs = be16_to_cpu(left->bb_numrecs);
  444. lbp = bp;
  445. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  446. cur->bc_private.a.agno, rbno, 0, &rbp,
  447. XFS_ALLOC_BTREE_REF)))
  448. return error;
  449. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  450. rrecs = be16_to_cpu(right->bb_numrecs);
  451. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  452. return error;
  453. }
  454. /*
  455. * Otherwise, we can't fix the imbalance.
  456. * Just return. This is probably a logic error, but it's not fatal.
  457. */
  458. else {
  459. if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
  460. return error;
  461. *stat = 1;
  462. return 0;
  463. }
  464. /*
  465. * We're now going to join "left" and "right" by moving all the stuff
  466. * in "right" to "left" and deleting "right".
  467. */
  468. if (level > 0) {
  469. /*
  470. * It's a non-leaf. Move keys and pointers.
  471. */
  472. lkp = XFS_ALLOC_KEY_ADDR(left, lrecs + 1, cur);
  473. lpp = XFS_ALLOC_PTR_ADDR(left, lrecs + 1, cur);
  474. rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
  475. rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
  476. #ifdef DEBUG
  477. for (i = 0; i < rrecs; i++) {
  478. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
  479. return error;
  480. }
  481. #endif
  482. memcpy(lkp, rkp, rrecs * sizeof(*lkp));
  483. memcpy(lpp, rpp, rrecs * sizeof(*lpp));
  484. xfs_alloc_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
  485. xfs_alloc_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
  486. } else {
  487. /*
  488. * It's a leaf. Move records.
  489. */
  490. lrp = XFS_ALLOC_REC_ADDR(left, lrecs + 1, cur);
  491. rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  492. memcpy(lrp, rrp, rrecs * sizeof(*lrp));
  493. xfs_alloc_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
  494. }
  495. /*
  496. * If we joined with the left neighbor, set the buffer in the
  497. * cursor to the left block, and fix up the index.
  498. */
  499. if (bp != lbp) {
  500. xfs_btree_setbuf(cur, level, lbp);
  501. cur->bc_ptrs[level] += lrecs;
  502. }
  503. /*
  504. * If we joined with the right neighbor and there's a level above
  505. * us, increment the cursor at that level.
  506. */
  507. else if (level + 1 < cur->bc_nlevels &&
  508. (error = xfs_btree_increment(cur, level + 1, &i)))
  509. return error;
  510. /*
  511. * Fix up the number of records in the surviving block.
  512. */
  513. lrecs += rrecs;
  514. left->bb_numrecs = cpu_to_be16(lrecs);
  515. /*
  516. * Fix up the right block pointer in the surviving block, and log it.
  517. */
  518. left->bb_rightsib = right->bb_rightsib;
  519. xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  520. /*
  521. * If there is a right sibling now, make it point to the
  522. * remaining block.
  523. */
  524. if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
  525. xfs_alloc_block_t *rrblock;
  526. xfs_buf_t *rrbp;
  527. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  528. cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
  529. &rrbp, XFS_ALLOC_BTREE_REF)))
  530. return error;
  531. rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
  532. if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
  533. return error;
  534. rrblock->bb_leftsib = cpu_to_be32(lbno);
  535. xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
  536. }
  537. /*
  538. * Free the deleting block by putting it on the freelist.
  539. */
  540. error = xfs_alloc_put_freelist(cur->bc_tp,
  541. cur->bc_private.a.agbp, NULL, rbno, 1);
  542. if (error)
  543. return error;
  544. /*
  545. * Since blocks move to the free list without the coordination
  546. * used in xfs_bmap_finish, we can't allow block to be available
  547. * for reallocation and non-transaction writing (user data)
  548. * until we know that the transaction that moved it to the free
  549. * list is permanently on disk. We track the blocks by declaring
  550. * these blocks as "busy"; the busy list is maintained on a
  551. * per-ag basis and each transaction records which entries
  552. * should be removed when the iclog commits to disk. If a
  553. * busy block is allocated, the iclog is pushed up to the
  554. * LSN that freed the block.
  555. */
  556. xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
  557. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  558. /*
  559. * Adjust the current level's cursor so that we're left referring
  560. * to the right node, after we're done.
  561. * If this leaves the ptr value 0 our caller will fix it up.
  562. */
  563. if (level > 0)
  564. cur->bc_ptrs[level]--;
  565. /*
  566. * Return value means the next level up has something to do.
  567. */
  568. *stat = 2;
  569. return 0;
  570. error0:
  571. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  572. return error;
  573. }
  574. /*
  575. * Log header fields from a btree block.
  576. */
  577. STATIC void
  578. xfs_alloc_log_block(
  579. xfs_trans_t *tp, /* transaction pointer */
  580. xfs_buf_t *bp, /* buffer containing btree block */
  581. int fields) /* mask of fields: XFS_BB_... */
  582. {
  583. int first; /* first byte offset logged */
  584. int last; /* last byte offset logged */
  585. static const short offsets[] = { /* table of offsets */
  586. offsetof(xfs_alloc_block_t, bb_magic),
  587. offsetof(xfs_alloc_block_t, bb_level),
  588. offsetof(xfs_alloc_block_t, bb_numrecs),
  589. offsetof(xfs_alloc_block_t, bb_leftsib),
  590. offsetof(xfs_alloc_block_t, bb_rightsib),
  591. sizeof(xfs_alloc_block_t)
  592. };
  593. xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
  594. xfs_trans_log_buf(tp, bp, first, last);
  595. }
  596. /*
  597. * Log keys from a btree block (nonleaf).
  598. */
  599. STATIC void
  600. xfs_alloc_log_keys(
  601. xfs_btree_cur_t *cur, /* btree cursor */
  602. xfs_buf_t *bp, /* buffer containing btree block */
  603. int kfirst, /* index of first key to log */
  604. int klast) /* index of last key to log */
  605. {
  606. xfs_alloc_block_t *block; /* btree block to log from */
  607. int first; /* first byte offset logged */
  608. xfs_alloc_key_t *kp; /* key pointer in btree block */
  609. int last; /* last byte offset logged */
  610. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  611. kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
  612. first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
  613. last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
  614. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  615. }
  616. /*
  617. * Log block pointer fields from a btree block (nonleaf).
  618. */
  619. STATIC void
  620. xfs_alloc_log_ptrs(
  621. xfs_btree_cur_t *cur, /* btree cursor */
  622. xfs_buf_t *bp, /* buffer containing btree block */
  623. int pfirst, /* index of first pointer to log */
  624. int plast) /* index of last pointer to log */
  625. {
  626. xfs_alloc_block_t *block; /* btree block to log from */
  627. int first; /* first byte offset logged */
  628. int last; /* last byte offset logged */
  629. xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
  630. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  631. pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
  632. first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
  633. last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
  634. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  635. }
  636. /*
  637. * Log records from a btree block (leaf).
  638. */
  639. STATIC void
  640. xfs_alloc_log_recs(
  641. xfs_btree_cur_t *cur, /* btree cursor */
  642. xfs_buf_t *bp, /* buffer containing btree block */
  643. int rfirst, /* index of first record to log */
  644. int rlast) /* index of last record to log */
  645. {
  646. xfs_alloc_block_t *block; /* btree block to log from */
  647. int first; /* first byte offset logged */
  648. int last; /* last byte offset logged */
  649. xfs_alloc_rec_t *rp; /* record pointer for btree block */
  650. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  651. rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
  652. #ifdef DEBUG
  653. {
  654. xfs_agf_t *agf;
  655. xfs_alloc_rec_t *p;
  656. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  657. for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
  658. ASSERT(be32_to_cpu(p->ar_startblock) +
  659. be32_to_cpu(p->ar_blockcount) <=
  660. be32_to_cpu(agf->agf_length));
  661. }
  662. #endif
  663. first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
  664. last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
  665. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  666. }
  667. /*
  668. * Externally visible routines.
  669. */
  670. /*
  671. * Delete the record pointed to by cur.
  672. * The cursor refers to the place where the record was (could be inserted)
  673. * when the operation returns.
  674. */
  675. int /* error */
  676. xfs_alloc_delete(
  677. xfs_btree_cur_t *cur, /* btree cursor */
  678. int *stat) /* success/failure */
  679. {
  680. int error; /* error return value */
  681. int i; /* result code */
  682. int level; /* btree level */
  683. /*
  684. * Go up the tree, starting at leaf level.
  685. * If 2 is returned then a join was done; go to the next level.
  686. * Otherwise we are done.
  687. */
  688. for (level = 0, i = 2; i == 2; level++) {
  689. if ((error = xfs_alloc_delrec(cur, level, &i)))
  690. return error;
  691. }
  692. if (i == 0) {
  693. for (level = 1; level < cur->bc_nlevels; level++) {
  694. if (cur->bc_ptrs[level] == 0) {
  695. if ((error = xfs_btree_decrement(cur, level, &i)))
  696. return error;
  697. break;
  698. }
  699. }
  700. }
  701. *stat = i;
  702. return 0;
  703. }
  704. /*
  705. * Get the data from the pointed-to record.
  706. */
  707. int /* error */
  708. xfs_alloc_get_rec(
  709. xfs_btree_cur_t *cur, /* btree cursor */
  710. xfs_agblock_t *bno, /* output: starting block of extent */
  711. xfs_extlen_t *len, /* output: length of extent */
  712. int *stat) /* output: success/failure */
  713. {
  714. xfs_alloc_block_t *block; /* btree block */
  715. #ifdef DEBUG
  716. int error; /* error return value */
  717. #endif
  718. int ptr; /* record number */
  719. ptr = cur->bc_ptrs[0];
  720. block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
  721. #ifdef DEBUG
  722. if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
  723. return error;
  724. #endif
  725. /*
  726. * Off the right end or left end, return failure.
  727. */
  728. if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
  729. *stat = 0;
  730. return 0;
  731. }
  732. /*
  733. * Point to the record and extract its data.
  734. */
  735. {
  736. xfs_alloc_rec_t *rec; /* record data */
  737. rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
  738. *bno = be32_to_cpu(rec->ar_startblock);
  739. *len = be32_to_cpu(rec->ar_blockcount);
  740. }
  741. *stat = 1;
  742. return 0;
  743. }
  744. STATIC struct xfs_btree_cur *
  745. xfs_allocbt_dup_cursor(
  746. struct xfs_btree_cur *cur)
  747. {
  748. return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
  749. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  750. cur->bc_btnum);
  751. }
  752. STATIC void
  753. xfs_allocbt_set_root(
  754. struct xfs_btree_cur *cur,
  755. union xfs_btree_ptr *ptr,
  756. int inc)
  757. {
  758. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  759. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  760. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  761. int btnum = cur->bc_btnum;
  762. ASSERT(ptr->s != 0);
  763. agf->agf_roots[btnum] = ptr->s;
  764. be32_add_cpu(&agf->agf_levels[btnum], inc);
  765. cur->bc_mp->m_perag[seqno].pagf_levels[btnum] += inc;
  766. xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  767. }
  768. STATIC int
  769. xfs_allocbt_alloc_block(
  770. struct xfs_btree_cur *cur,
  771. union xfs_btree_ptr *start,
  772. union xfs_btree_ptr *new,
  773. int length,
  774. int *stat)
  775. {
  776. int error;
  777. xfs_agblock_t bno;
  778. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  779. /* Allocate the new block from the freelist. If we can't, give up. */
  780. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  781. &bno, 1);
  782. if (error) {
  783. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  784. return error;
  785. }
  786. if (bno == NULLAGBLOCK) {
  787. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  788. *stat = 0;
  789. return 0;
  790. }
  791. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  792. new->s = cpu_to_be32(bno);
  793. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  794. *stat = 1;
  795. return 0;
  796. }
  797. STATIC int
  798. xfs_allocbt_free_block(
  799. struct xfs_btree_cur *cur,
  800. struct xfs_buf *bp)
  801. {
  802. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  803. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  804. xfs_agblock_t bno;
  805. int error;
  806. bno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(bp));
  807. error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
  808. if (error)
  809. return error;
  810. /*
  811. * Since blocks move to the free list without the coordination used in
  812. * xfs_bmap_finish, we can't allow block to be available for
  813. * reallocation and non-transaction writing (user data) until we know
  814. * that the transaction that moved it to the free list is permanently
  815. * on disk. We track the blocks by declaring these blocks as "busy";
  816. * the busy list is maintained on a per-ag basis and each transaction
  817. * records which entries should be removed when the iclog commits to
  818. * disk. If a busy block is allocated, the iclog is pushed up to the
  819. * LSN that freed the block.
  820. */
  821. xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
  822. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  823. return 0;
  824. }
  825. /*
  826. * Update the longest extent in the AGF
  827. */
  828. STATIC void
  829. xfs_allocbt_update_lastrec(
  830. struct xfs_btree_cur *cur,
  831. struct xfs_btree_block *block,
  832. union xfs_btree_rec *rec,
  833. int ptr,
  834. int reason)
  835. {
  836. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  837. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  838. __be32 len;
  839. ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
  840. switch (reason) {
  841. case LASTREC_UPDATE:
  842. /*
  843. * If this is the last leaf block and it's the last record,
  844. * then update the size of the longest extent in the AG.
  845. */
  846. if (ptr != xfs_btree_get_numrecs(block))
  847. return;
  848. len = rec->alloc.ar_blockcount;
  849. break;
  850. case LASTREC_INSREC:
  851. if (be32_to_cpu(rec->alloc.ar_blockcount) <=
  852. be32_to_cpu(agf->agf_longest))
  853. return;
  854. len = rec->alloc.ar_blockcount;
  855. break;
  856. default:
  857. ASSERT(0);
  858. return;
  859. }
  860. agf->agf_longest = len;
  861. cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
  862. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
  863. }
  864. STATIC int
  865. xfs_allocbt_get_maxrecs(
  866. struct xfs_btree_cur *cur,
  867. int level)
  868. {
  869. return cur->bc_mp->m_alloc_mxr[level != 0];
  870. }
  871. STATIC void
  872. xfs_allocbt_init_key_from_rec(
  873. union xfs_btree_key *key,
  874. union xfs_btree_rec *rec)
  875. {
  876. ASSERT(rec->alloc.ar_startblock != 0);
  877. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  878. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  879. }
  880. STATIC void
  881. xfs_allocbt_init_rec_from_key(
  882. union xfs_btree_key *key,
  883. union xfs_btree_rec *rec)
  884. {
  885. ASSERT(key->alloc.ar_startblock != 0);
  886. rec->alloc.ar_startblock = key->alloc.ar_startblock;
  887. rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
  888. }
  889. STATIC void
  890. xfs_allocbt_init_rec_from_cur(
  891. struct xfs_btree_cur *cur,
  892. union xfs_btree_rec *rec)
  893. {
  894. ASSERT(cur->bc_rec.a.ar_startblock != 0);
  895. rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  896. rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  897. }
  898. STATIC void
  899. xfs_allocbt_init_ptr_from_cur(
  900. struct xfs_btree_cur *cur,
  901. union xfs_btree_ptr *ptr)
  902. {
  903. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  904. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  905. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  906. ptr->s = agf->agf_roots[cur->bc_btnum];
  907. }
  908. STATIC __int64_t
  909. xfs_allocbt_key_diff(
  910. struct xfs_btree_cur *cur,
  911. union xfs_btree_key *key)
  912. {
  913. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  914. xfs_alloc_key_t *kp = &key->alloc;
  915. __int64_t diff;
  916. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  917. return (__int64_t)be32_to_cpu(kp->ar_startblock) -
  918. rec->ar_startblock;
  919. }
  920. diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  921. if (diff)
  922. return diff;
  923. return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  924. }
  925. #ifdef XFS_BTREE_TRACE
  926. ktrace_t *xfs_allocbt_trace_buf;
  927. STATIC void
  928. xfs_allocbt_trace_enter(
  929. struct xfs_btree_cur *cur,
  930. const char *func,
  931. char *s,
  932. int type,
  933. int line,
  934. __psunsigned_t a0,
  935. __psunsigned_t a1,
  936. __psunsigned_t a2,
  937. __psunsigned_t a3,
  938. __psunsigned_t a4,
  939. __psunsigned_t a5,
  940. __psunsigned_t a6,
  941. __psunsigned_t a7,
  942. __psunsigned_t a8,
  943. __psunsigned_t a9,
  944. __psunsigned_t a10)
  945. {
  946. ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
  947. (void *)func, (void *)s, NULL, (void *)cur,
  948. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  949. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  950. (void *)a8, (void *)a9, (void *)a10);
  951. }
  952. STATIC void
  953. xfs_allocbt_trace_cursor(
  954. struct xfs_btree_cur *cur,
  955. __uint32_t *s0,
  956. __uint64_t *l0,
  957. __uint64_t *l1)
  958. {
  959. *s0 = cur->bc_private.a.agno;
  960. *l0 = cur->bc_rec.a.ar_startblock;
  961. *l1 = cur->bc_rec.a.ar_blockcount;
  962. }
  963. STATIC void
  964. xfs_allocbt_trace_key(
  965. struct xfs_btree_cur *cur,
  966. union xfs_btree_key *key,
  967. __uint64_t *l0,
  968. __uint64_t *l1)
  969. {
  970. *l0 = be32_to_cpu(key->alloc.ar_startblock);
  971. *l1 = be32_to_cpu(key->alloc.ar_blockcount);
  972. }
  973. STATIC void
  974. xfs_allocbt_trace_record(
  975. struct xfs_btree_cur *cur,
  976. union xfs_btree_rec *rec,
  977. __uint64_t *l0,
  978. __uint64_t *l1,
  979. __uint64_t *l2)
  980. {
  981. *l0 = be32_to_cpu(rec->alloc.ar_startblock);
  982. *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
  983. *l2 = 0;
  984. }
  985. #endif /* XFS_BTREE_TRACE */
  986. static const struct xfs_btree_ops xfs_allocbt_ops = {
  987. .rec_len = sizeof(xfs_alloc_rec_t),
  988. .key_len = sizeof(xfs_alloc_key_t),
  989. .dup_cursor = xfs_allocbt_dup_cursor,
  990. .set_root = xfs_allocbt_set_root,
  991. .alloc_block = xfs_allocbt_alloc_block,
  992. .free_block = xfs_allocbt_free_block,
  993. .update_lastrec = xfs_allocbt_update_lastrec,
  994. .get_maxrecs = xfs_allocbt_get_maxrecs,
  995. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  996. .init_rec_from_key = xfs_allocbt_init_rec_from_key,
  997. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  998. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  999. .key_diff = xfs_allocbt_key_diff,
  1000. #ifdef XFS_BTREE_TRACE
  1001. .trace_enter = xfs_allocbt_trace_enter,
  1002. .trace_cursor = xfs_allocbt_trace_cursor,
  1003. .trace_key = xfs_allocbt_trace_key,
  1004. .trace_record = xfs_allocbt_trace_record,
  1005. #endif
  1006. };
  1007. /*
  1008. * Allocate a new allocation btree cursor.
  1009. */
  1010. struct xfs_btree_cur * /* new alloc btree cursor */
  1011. xfs_allocbt_init_cursor(
  1012. struct xfs_mount *mp, /* file system mount point */
  1013. struct xfs_trans *tp, /* transaction pointer */
  1014. struct xfs_buf *agbp, /* buffer for agf structure */
  1015. xfs_agnumber_t agno, /* allocation group number */
  1016. xfs_btnum_t btnum) /* btree identifier */
  1017. {
  1018. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  1019. struct xfs_btree_cur *cur;
  1020. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  1021. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  1022. cur->bc_tp = tp;
  1023. cur->bc_mp = mp;
  1024. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
  1025. cur->bc_btnum = btnum;
  1026. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  1027. cur->bc_ops = &xfs_allocbt_ops;
  1028. if (btnum == XFS_BTNUM_CNT)
  1029. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  1030. cur->bc_private.a.agbp = agbp;
  1031. cur->bc_private.a.agno = agno;
  1032. return cur;
  1033. }