xfs_alloc_btree.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. /*
  798. * Update the longest extent in the AGF
  799. */
  800. STATIC void
  801. xfs_allocbt_update_lastrec(
  802. struct xfs_btree_cur *cur,
  803. struct xfs_btree_block *block,
  804. union xfs_btree_rec *rec,
  805. int ptr,
  806. int reason)
  807. {
  808. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  809. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  810. __be32 len;
  811. ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
  812. switch (reason) {
  813. case LASTREC_UPDATE:
  814. /*
  815. * If this is the last leaf block and it's the last record,
  816. * then update the size of the longest extent in the AG.
  817. */
  818. if (ptr != xfs_btree_get_numrecs(block))
  819. return;
  820. len = rec->alloc.ar_blockcount;
  821. break;
  822. case LASTREC_INSREC:
  823. if (be32_to_cpu(rec->alloc.ar_blockcount) <=
  824. be32_to_cpu(agf->agf_longest))
  825. return;
  826. len = rec->alloc.ar_blockcount;
  827. break;
  828. default:
  829. ASSERT(0);
  830. return;
  831. }
  832. agf->agf_longest = len;
  833. cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
  834. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
  835. }
  836. STATIC int
  837. xfs_allocbt_get_maxrecs(
  838. struct xfs_btree_cur *cur,
  839. int level)
  840. {
  841. return cur->bc_mp->m_alloc_mxr[level != 0];
  842. }
  843. STATIC void
  844. xfs_allocbt_init_key_from_rec(
  845. union xfs_btree_key *key,
  846. union xfs_btree_rec *rec)
  847. {
  848. ASSERT(rec->alloc.ar_startblock != 0);
  849. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  850. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  851. }
  852. STATIC void
  853. xfs_allocbt_init_rec_from_key(
  854. union xfs_btree_key *key,
  855. union xfs_btree_rec *rec)
  856. {
  857. ASSERT(key->alloc.ar_startblock != 0);
  858. rec->alloc.ar_startblock = key->alloc.ar_startblock;
  859. rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
  860. }
  861. STATIC void
  862. xfs_allocbt_init_rec_from_cur(
  863. struct xfs_btree_cur *cur,
  864. union xfs_btree_rec *rec)
  865. {
  866. ASSERT(cur->bc_rec.a.ar_startblock != 0);
  867. rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  868. rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  869. }
  870. STATIC void
  871. xfs_allocbt_init_ptr_from_cur(
  872. struct xfs_btree_cur *cur,
  873. union xfs_btree_ptr *ptr)
  874. {
  875. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  876. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  877. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  878. ptr->s = agf->agf_roots[cur->bc_btnum];
  879. }
  880. STATIC __int64_t
  881. xfs_allocbt_key_diff(
  882. struct xfs_btree_cur *cur,
  883. union xfs_btree_key *key)
  884. {
  885. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  886. xfs_alloc_key_t *kp = &key->alloc;
  887. __int64_t diff;
  888. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  889. return (__int64_t)be32_to_cpu(kp->ar_startblock) -
  890. rec->ar_startblock;
  891. }
  892. diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  893. if (diff)
  894. return diff;
  895. return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  896. }
  897. #ifdef XFS_BTREE_TRACE
  898. ktrace_t *xfs_allocbt_trace_buf;
  899. STATIC void
  900. xfs_allocbt_trace_enter(
  901. struct xfs_btree_cur *cur,
  902. const char *func,
  903. char *s,
  904. int type,
  905. int line,
  906. __psunsigned_t a0,
  907. __psunsigned_t a1,
  908. __psunsigned_t a2,
  909. __psunsigned_t a3,
  910. __psunsigned_t a4,
  911. __psunsigned_t a5,
  912. __psunsigned_t a6,
  913. __psunsigned_t a7,
  914. __psunsigned_t a8,
  915. __psunsigned_t a9,
  916. __psunsigned_t a10)
  917. {
  918. ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
  919. (void *)func, (void *)s, NULL, (void *)cur,
  920. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  921. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  922. (void *)a8, (void *)a9, (void *)a10);
  923. }
  924. STATIC void
  925. xfs_allocbt_trace_cursor(
  926. struct xfs_btree_cur *cur,
  927. __uint32_t *s0,
  928. __uint64_t *l0,
  929. __uint64_t *l1)
  930. {
  931. *s0 = cur->bc_private.a.agno;
  932. *l0 = cur->bc_rec.a.ar_startblock;
  933. *l1 = cur->bc_rec.a.ar_blockcount;
  934. }
  935. STATIC void
  936. xfs_allocbt_trace_key(
  937. struct xfs_btree_cur *cur,
  938. union xfs_btree_key *key,
  939. __uint64_t *l0,
  940. __uint64_t *l1)
  941. {
  942. *l0 = be32_to_cpu(key->alloc.ar_startblock);
  943. *l1 = be32_to_cpu(key->alloc.ar_blockcount);
  944. }
  945. STATIC void
  946. xfs_allocbt_trace_record(
  947. struct xfs_btree_cur *cur,
  948. union xfs_btree_rec *rec,
  949. __uint64_t *l0,
  950. __uint64_t *l1,
  951. __uint64_t *l2)
  952. {
  953. *l0 = be32_to_cpu(rec->alloc.ar_startblock);
  954. *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
  955. *l2 = 0;
  956. }
  957. #endif /* XFS_BTREE_TRACE */
  958. static const struct xfs_btree_ops xfs_allocbt_ops = {
  959. .rec_len = sizeof(xfs_alloc_rec_t),
  960. .key_len = sizeof(xfs_alloc_key_t),
  961. .dup_cursor = xfs_allocbt_dup_cursor,
  962. .set_root = xfs_allocbt_set_root,
  963. .alloc_block = xfs_allocbt_alloc_block,
  964. .update_lastrec = xfs_allocbt_update_lastrec,
  965. .get_maxrecs = xfs_allocbt_get_maxrecs,
  966. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  967. .init_rec_from_key = xfs_allocbt_init_rec_from_key,
  968. .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
  969. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  970. .key_diff = xfs_allocbt_key_diff,
  971. #ifdef XFS_BTREE_TRACE
  972. .trace_enter = xfs_allocbt_trace_enter,
  973. .trace_cursor = xfs_allocbt_trace_cursor,
  974. .trace_key = xfs_allocbt_trace_key,
  975. .trace_record = xfs_allocbt_trace_record,
  976. #endif
  977. };
  978. /*
  979. * Allocate a new allocation btree cursor.
  980. */
  981. struct xfs_btree_cur * /* new alloc btree cursor */
  982. xfs_allocbt_init_cursor(
  983. struct xfs_mount *mp, /* file system mount point */
  984. struct xfs_trans *tp, /* transaction pointer */
  985. struct xfs_buf *agbp, /* buffer for agf structure */
  986. xfs_agnumber_t agno, /* allocation group number */
  987. xfs_btnum_t btnum) /* btree identifier */
  988. {
  989. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  990. struct xfs_btree_cur *cur;
  991. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  992. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  993. cur->bc_tp = tp;
  994. cur->bc_mp = mp;
  995. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
  996. cur->bc_btnum = btnum;
  997. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  998. cur->bc_ops = &xfs_allocbt_ops;
  999. if (btnum == XFS_BTNUM_CNT)
  1000. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  1001. cur->bc_private.a.agbp = agbp;
  1002. cur->bc_private.a.agno = agno;
  1003. return cur;
  1004. }