xfs_alloc_btree.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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. STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
  50. /*
  51. * Internal functions.
  52. */
  53. /*
  54. * Single level of the xfs_alloc_delete record deletion routine.
  55. * Delete record pointed to by cur/level.
  56. * Remove the record from its block then rebalance the tree.
  57. * Return 0 for error, 1 for done, 2 to go on to the next level.
  58. */
  59. STATIC int /* error */
  60. xfs_alloc_delrec(
  61. xfs_btree_cur_t *cur, /* btree cursor */
  62. int level, /* level removing record from */
  63. int *stat) /* fail/done/go-on */
  64. {
  65. xfs_agf_t *agf; /* allocation group freelist header */
  66. xfs_alloc_block_t *block; /* btree block record/key lives in */
  67. xfs_agblock_t bno; /* btree block number */
  68. xfs_buf_t *bp; /* buffer for block */
  69. int error; /* error return value */
  70. int i; /* loop index */
  71. xfs_alloc_key_t key; /* kp points here if block is level 0 */
  72. xfs_agblock_t lbno; /* left block's block number */
  73. xfs_buf_t *lbp; /* left block's buffer pointer */
  74. xfs_alloc_block_t *left; /* left btree block */
  75. xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
  76. xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
  77. int lrecs=0; /* number of records in left block */
  78. xfs_alloc_rec_t *lrp; /* left block record pointer */
  79. xfs_mount_t *mp; /* mount structure */
  80. int ptr; /* index in btree block for this rec */
  81. xfs_agblock_t rbno; /* right block's block number */
  82. xfs_buf_t *rbp; /* right block's buffer pointer */
  83. xfs_alloc_block_t *right; /* right btree block */
  84. xfs_alloc_key_t *rkp; /* right block key pointer */
  85. xfs_alloc_ptr_t *rpp; /* right block address pointer */
  86. int rrecs=0; /* number of records in right block */
  87. int numrecs;
  88. xfs_alloc_rec_t *rrp; /* right block record pointer */
  89. xfs_btree_cur_t *tcur; /* temporary btree cursor */
  90. /*
  91. * Get the index of the entry being deleted, check for nothing there.
  92. */
  93. ptr = cur->bc_ptrs[level];
  94. if (ptr == 0) {
  95. *stat = 0;
  96. return 0;
  97. }
  98. /*
  99. * Get the buffer & block containing the record or key/ptr.
  100. */
  101. bp = cur->bc_bufs[level];
  102. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  103. #ifdef DEBUG
  104. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  105. return error;
  106. #endif
  107. /*
  108. * Fail if we're off the end of the block.
  109. */
  110. numrecs = be16_to_cpu(block->bb_numrecs);
  111. if (ptr > numrecs) {
  112. *stat = 0;
  113. return 0;
  114. }
  115. XFS_STATS_INC(xs_abt_delrec);
  116. /*
  117. * It's a nonleaf. Excise the key and ptr being deleted, by
  118. * sliding the entries past them down one.
  119. * Log the changed areas of the block.
  120. */
  121. if (level > 0) {
  122. lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
  123. lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
  124. #ifdef DEBUG
  125. for (i = ptr; i < numrecs; i++) {
  126. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
  127. return error;
  128. }
  129. #endif
  130. if (ptr < numrecs) {
  131. memmove(&lkp[ptr - 1], &lkp[ptr],
  132. (numrecs - ptr) * sizeof(*lkp));
  133. memmove(&lpp[ptr - 1], &lpp[ptr],
  134. (numrecs - ptr) * sizeof(*lpp));
  135. xfs_alloc_log_ptrs(cur, bp, ptr, numrecs - 1);
  136. xfs_alloc_log_keys(cur, bp, ptr, numrecs - 1);
  137. }
  138. }
  139. /*
  140. * It's a leaf. Excise the record being deleted, by sliding the
  141. * entries past it down one. Log the changed areas of the block.
  142. */
  143. else {
  144. lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
  145. if (ptr < numrecs) {
  146. memmove(&lrp[ptr - 1], &lrp[ptr],
  147. (numrecs - ptr) * sizeof(*lrp));
  148. xfs_alloc_log_recs(cur, bp, ptr, numrecs - 1);
  149. }
  150. /*
  151. * If it's the first record in the block, we'll need a key
  152. * structure to pass up to the next level (updkey).
  153. */
  154. if (ptr == 1) {
  155. key.ar_startblock = lrp->ar_startblock;
  156. key.ar_blockcount = lrp->ar_blockcount;
  157. lkp = &key;
  158. }
  159. }
  160. /*
  161. * Decrement and log the number of entries in the block.
  162. */
  163. numrecs--;
  164. block->bb_numrecs = cpu_to_be16(numrecs);
  165. xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
  166. /*
  167. * See if the longest free extent in the allocation group was
  168. * changed by this operation. True if it's the by-size btree, and
  169. * this is the leaf level, and there is no right sibling block,
  170. * and this was the last record.
  171. */
  172. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  173. mp = cur->bc_mp;
  174. if (level == 0 &&
  175. cur->bc_btnum == XFS_BTNUM_CNT &&
  176. be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
  177. ptr > numrecs) {
  178. ASSERT(ptr == numrecs + 1);
  179. /*
  180. * There are still records in the block. Grab the size
  181. * from the last one.
  182. */
  183. if (numrecs) {
  184. rrp = XFS_ALLOC_REC_ADDR(block, numrecs, cur);
  185. agf->agf_longest = rrp->ar_blockcount;
  186. }
  187. /*
  188. * No free extents left.
  189. */
  190. else
  191. agf->agf_longest = 0;
  192. mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest =
  193. be32_to_cpu(agf->agf_longest);
  194. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  195. XFS_AGF_LONGEST);
  196. }
  197. /*
  198. * Is this the root level? If so, we're almost done.
  199. */
  200. if (level == cur->bc_nlevels - 1) {
  201. /*
  202. * If this is the root level,
  203. * and there's only one entry left,
  204. * and it's NOT the leaf level,
  205. * then we can get rid of this level.
  206. */
  207. if (numrecs == 1 && level > 0) {
  208. /*
  209. * lpp is still set to the first pointer in the block.
  210. * Make it the new root of the btree.
  211. */
  212. bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
  213. agf->agf_roots[cur->bc_btnum] = *lpp;
  214. be32_add_cpu(&agf->agf_levels[cur->bc_btnum], -1);
  215. mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--;
  216. /*
  217. * Put this buffer/block on the ag's freelist.
  218. */
  219. error = xfs_alloc_put_freelist(cur->bc_tp,
  220. cur->bc_private.a.agbp, NULL, bno, 1);
  221. if (error)
  222. return error;
  223. /*
  224. * Since blocks move to the free list without the
  225. * coordination used in xfs_bmap_finish, we can't allow
  226. * block to be available for reallocation and
  227. * non-transaction writing (user data) until we know
  228. * that the transaction that moved it to the free list
  229. * is permanently on disk. We track the blocks by
  230. * declaring these blocks as "busy"; the busy list is
  231. * maintained on a per-ag basis and each transaction
  232. * records which entries should be removed when the
  233. * iclog commits to disk. If a busy block is
  234. * allocated, the iclog is pushed up to the LSN
  235. * that freed the block.
  236. */
  237. xfs_alloc_mark_busy(cur->bc_tp,
  238. be32_to_cpu(agf->agf_seqno), bno, 1);
  239. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  240. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  241. XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  242. /*
  243. * Update the cursor so there's one fewer level.
  244. */
  245. xfs_btree_setbuf(cur, level, NULL);
  246. cur->bc_nlevels--;
  247. } else if (level > 0 &&
  248. (error = xfs_btree_decrement(cur, level, &i)))
  249. return error;
  250. *stat = 1;
  251. return 0;
  252. }
  253. /*
  254. * If we deleted the leftmost entry in the block, update the
  255. * key values above us in the tree.
  256. */
  257. if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)lkp, level + 1)))
  258. return error;
  259. /*
  260. * If the number of records remaining in the block is at least
  261. * the minimum, we're done.
  262. */
  263. if (numrecs >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
  264. if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
  265. return error;
  266. *stat = 1;
  267. return 0;
  268. }
  269. /*
  270. * Otherwise, we have to move some records around to keep the
  271. * tree balanced. Look at the left and right sibling blocks to
  272. * see if we can re-balance by moving only one record.
  273. */
  274. rbno = be32_to_cpu(block->bb_rightsib);
  275. lbno = be32_to_cpu(block->bb_leftsib);
  276. bno = NULLAGBLOCK;
  277. ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
  278. /*
  279. * Duplicate the cursor so our btree manipulations here won't
  280. * disrupt the next level up.
  281. */
  282. if ((error = xfs_btree_dup_cursor(cur, &tcur)))
  283. return error;
  284. /*
  285. * If there's a right sibling, see if it's ok to shift an entry
  286. * out of it.
  287. */
  288. if (rbno != NULLAGBLOCK) {
  289. /*
  290. * Move the temp cursor to the last entry in the next block.
  291. * Actually any entry but the first would suffice.
  292. */
  293. i = xfs_btree_lastrec(tcur, level);
  294. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  295. if ((error = xfs_btree_increment(tcur, level, &i)))
  296. goto error0;
  297. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  298. i = xfs_btree_lastrec(tcur, level);
  299. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  300. /*
  301. * Grab a pointer to the block.
  302. */
  303. rbp = tcur->bc_bufs[level];
  304. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  305. #ifdef DEBUG
  306. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  307. goto error0;
  308. #endif
  309. /*
  310. * Grab the current block number, for future use.
  311. */
  312. bno = be32_to_cpu(right->bb_leftsib);
  313. /*
  314. * If right block is full enough so that removing one entry
  315. * won't make it too empty, and left-shifting an entry out
  316. * of right to us works, we're done.
  317. */
  318. if (be16_to_cpu(right->bb_numrecs) - 1 >=
  319. XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
  320. if ((error = xfs_btree_lshift(tcur, level, &i)))
  321. goto error0;
  322. if (i) {
  323. ASSERT(be16_to_cpu(block->bb_numrecs) >=
  324. XFS_ALLOC_BLOCK_MINRECS(level, cur));
  325. xfs_btree_del_cursor(tcur,
  326. XFS_BTREE_NOERROR);
  327. if (level > 0 &&
  328. (error = xfs_btree_decrement(cur, level,
  329. &i)))
  330. return error;
  331. *stat = 1;
  332. return 0;
  333. }
  334. }
  335. /*
  336. * Otherwise, grab the number of records in right for
  337. * future reference, and fix up the temp cursor to point
  338. * to our block again (last record).
  339. */
  340. rrecs = be16_to_cpu(right->bb_numrecs);
  341. if (lbno != NULLAGBLOCK) {
  342. i = xfs_btree_firstrec(tcur, level);
  343. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  344. if ((error = xfs_btree_decrement(tcur, level, &i)))
  345. goto error0;
  346. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  347. }
  348. }
  349. /*
  350. * If there's a left sibling, see if it's ok to shift an entry
  351. * out of it.
  352. */
  353. if (lbno != NULLAGBLOCK) {
  354. /*
  355. * Move the temp cursor to the first entry in the
  356. * previous block.
  357. */
  358. i = xfs_btree_firstrec(tcur, level);
  359. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  360. if ((error = xfs_btree_decrement(tcur, level, &i)))
  361. goto error0;
  362. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  363. xfs_btree_firstrec(tcur, level);
  364. /*
  365. * Grab a pointer to the block.
  366. */
  367. lbp = tcur->bc_bufs[level];
  368. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  369. #ifdef DEBUG
  370. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  371. goto error0;
  372. #endif
  373. /*
  374. * Grab the current block number, for future use.
  375. */
  376. bno = be32_to_cpu(left->bb_rightsib);
  377. /*
  378. * If left block is full enough so that removing one entry
  379. * won't make it too empty, and right-shifting an entry out
  380. * of left to us works, we're done.
  381. */
  382. if (be16_to_cpu(left->bb_numrecs) - 1 >=
  383. XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
  384. if ((error = xfs_btree_rshift(tcur, level, &i)))
  385. goto error0;
  386. if (i) {
  387. ASSERT(be16_to_cpu(block->bb_numrecs) >=
  388. XFS_ALLOC_BLOCK_MINRECS(level, cur));
  389. xfs_btree_del_cursor(tcur,
  390. XFS_BTREE_NOERROR);
  391. if (level == 0)
  392. cur->bc_ptrs[0]++;
  393. *stat = 1;
  394. return 0;
  395. }
  396. }
  397. /*
  398. * Otherwise, grab the number of records in right for
  399. * future reference.
  400. */
  401. lrecs = be16_to_cpu(left->bb_numrecs);
  402. }
  403. /*
  404. * Delete the temp cursor, we're done with it.
  405. */
  406. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  407. /*
  408. * If here, we need to do a join to keep the tree balanced.
  409. */
  410. ASSERT(bno != NULLAGBLOCK);
  411. /*
  412. * See if we can join with the left neighbor block.
  413. */
  414. if (lbno != NULLAGBLOCK &&
  415. lrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  416. /*
  417. * Set "right" to be the starting block,
  418. * "left" to be the left neighbor.
  419. */
  420. rbno = bno;
  421. right = block;
  422. rrecs = be16_to_cpu(right->bb_numrecs);
  423. rbp = bp;
  424. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  425. cur->bc_private.a.agno, lbno, 0, &lbp,
  426. XFS_ALLOC_BTREE_REF)))
  427. return error;
  428. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  429. lrecs = be16_to_cpu(left->bb_numrecs);
  430. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  431. return error;
  432. }
  433. /*
  434. * If that won't work, see if we can join with the right neighbor block.
  435. */
  436. else if (rbno != NULLAGBLOCK &&
  437. rrecs + numrecs <= XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  438. /*
  439. * Set "left" to be the starting block,
  440. * "right" to be the right neighbor.
  441. */
  442. lbno = bno;
  443. left = block;
  444. lrecs = be16_to_cpu(left->bb_numrecs);
  445. lbp = bp;
  446. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  447. cur->bc_private.a.agno, rbno, 0, &rbp,
  448. XFS_ALLOC_BTREE_REF)))
  449. return error;
  450. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  451. rrecs = be16_to_cpu(right->bb_numrecs);
  452. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  453. return error;
  454. }
  455. /*
  456. * Otherwise, we can't fix the imbalance.
  457. * Just return. This is probably a logic error, but it's not fatal.
  458. */
  459. else {
  460. if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
  461. return error;
  462. *stat = 1;
  463. return 0;
  464. }
  465. /*
  466. * We're now going to join "left" and "right" by moving all the stuff
  467. * in "right" to "left" and deleting "right".
  468. */
  469. if (level > 0) {
  470. /*
  471. * It's a non-leaf. Move keys and pointers.
  472. */
  473. lkp = XFS_ALLOC_KEY_ADDR(left, lrecs + 1, cur);
  474. lpp = XFS_ALLOC_PTR_ADDR(left, lrecs + 1, cur);
  475. rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
  476. rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
  477. #ifdef DEBUG
  478. for (i = 0; i < rrecs; i++) {
  479. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
  480. return error;
  481. }
  482. #endif
  483. memcpy(lkp, rkp, rrecs * sizeof(*lkp));
  484. memcpy(lpp, rpp, rrecs * sizeof(*lpp));
  485. xfs_alloc_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
  486. xfs_alloc_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
  487. } else {
  488. /*
  489. * It's a leaf. Move records.
  490. */
  491. lrp = XFS_ALLOC_REC_ADDR(left, lrecs + 1, cur);
  492. rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  493. memcpy(lrp, rrp, rrecs * sizeof(*lrp));
  494. xfs_alloc_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
  495. }
  496. /*
  497. * If we joined with the left neighbor, set the buffer in the
  498. * cursor to the left block, and fix up the index.
  499. */
  500. if (bp != lbp) {
  501. xfs_btree_setbuf(cur, level, lbp);
  502. cur->bc_ptrs[level] += lrecs;
  503. }
  504. /*
  505. * If we joined with the right neighbor and there's a level above
  506. * us, increment the cursor at that level.
  507. */
  508. else if (level + 1 < cur->bc_nlevels &&
  509. (error = xfs_btree_increment(cur, level + 1, &i)))
  510. return error;
  511. /*
  512. * Fix up the number of records in the surviving block.
  513. */
  514. lrecs += rrecs;
  515. left->bb_numrecs = cpu_to_be16(lrecs);
  516. /*
  517. * Fix up the right block pointer in the surviving block, and log it.
  518. */
  519. left->bb_rightsib = right->bb_rightsib;
  520. xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  521. /*
  522. * If there is a right sibling now, make it point to the
  523. * remaining block.
  524. */
  525. if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
  526. xfs_alloc_block_t *rrblock;
  527. xfs_buf_t *rrbp;
  528. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  529. cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
  530. &rrbp, XFS_ALLOC_BTREE_REF)))
  531. return error;
  532. rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
  533. if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
  534. return error;
  535. rrblock->bb_leftsib = cpu_to_be32(lbno);
  536. xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
  537. }
  538. /*
  539. * Free the deleting block by putting it on the freelist.
  540. */
  541. error = xfs_alloc_put_freelist(cur->bc_tp,
  542. cur->bc_private.a.agbp, NULL, rbno, 1);
  543. if (error)
  544. return error;
  545. /*
  546. * Since blocks move to the free list without the coordination
  547. * used in xfs_bmap_finish, we can't allow block to be available
  548. * for reallocation and non-transaction writing (user data)
  549. * until we know that the transaction that moved it to the free
  550. * list is permanently on disk. We track the blocks by declaring
  551. * these blocks as "busy"; the busy list is maintained on a
  552. * per-ag basis and each transaction records which entries
  553. * should be removed when the iclog commits to disk. If a
  554. * busy block is allocated, the iclog is pushed up to the
  555. * LSN that freed the block.
  556. */
  557. xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
  558. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  559. /*
  560. * Adjust the current level's cursor so that we're left referring
  561. * to the right node, after we're done.
  562. * If this leaves the ptr value 0 our caller will fix it up.
  563. */
  564. if (level > 0)
  565. cur->bc_ptrs[level]--;
  566. /*
  567. * Return value means the next level up has something to do.
  568. */
  569. *stat = 2;
  570. return 0;
  571. error0:
  572. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  573. return error;
  574. }
  575. /*
  576. * Insert one record/level. Return information to the caller
  577. * allowing the next level up to proceed if necessary.
  578. */
  579. STATIC int /* error */
  580. xfs_alloc_insrec(
  581. xfs_btree_cur_t *cur, /* btree cursor */
  582. int level, /* level to insert record at */
  583. xfs_agblock_t *bnop, /* i/o: block number inserted */
  584. xfs_alloc_rec_t *recp, /* i/o: record data inserted */
  585. xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
  586. int *stat) /* output: success/failure */
  587. {
  588. xfs_agf_t *agf; /* allocation group freelist header */
  589. xfs_alloc_block_t *block; /* btree block record/key lives in */
  590. xfs_buf_t *bp; /* buffer for block */
  591. int error; /* error return value */
  592. int i; /* loop index */
  593. xfs_alloc_key_t key; /* key value being inserted */
  594. xfs_alloc_key_t *kp; /* pointer to btree keys */
  595. xfs_agblock_t nbno; /* block number of allocated block */
  596. xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
  597. xfs_alloc_key_t nkey; /* new key value, from split */
  598. xfs_alloc_rec_t nrec; /* new record value, for caller */
  599. int numrecs;
  600. int optr; /* old ptr value */
  601. xfs_alloc_ptr_t *pp; /* pointer to btree addresses */
  602. int ptr; /* index in btree block for this rec */
  603. xfs_alloc_rec_t *rp; /* pointer to btree records */
  604. ASSERT(be32_to_cpu(recp->ar_blockcount) > 0);
  605. /*
  606. * GCC doesn't understand the (arguably complex) control flow in
  607. * this function and complains about uninitialized structure fields
  608. * without this.
  609. */
  610. memset(&nrec, 0, sizeof(nrec));
  611. /*
  612. * If we made it to the root level, allocate a new root block
  613. * and we're done.
  614. */
  615. if (level >= cur->bc_nlevels) {
  616. XFS_STATS_INC(xs_abt_insrec);
  617. if ((error = xfs_alloc_newroot(cur, &i)))
  618. return error;
  619. *bnop = NULLAGBLOCK;
  620. *stat = i;
  621. return 0;
  622. }
  623. /*
  624. * Make a key out of the record data to be inserted, and save it.
  625. */
  626. key.ar_startblock = recp->ar_startblock;
  627. key.ar_blockcount = recp->ar_blockcount;
  628. optr = ptr = cur->bc_ptrs[level];
  629. /*
  630. * If we're off the left edge, return failure.
  631. */
  632. if (ptr == 0) {
  633. *stat = 0;
  634. return 0;
  635. }
  636. XFS_STATS_INC(xs_abt_insrec);
  637. /*
  638. * Get pointers to the btree buffer and block.
  639. */
  640. bp = cur->bc_bufs[level];
  641. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  642. numrecs = be16_to_cpu(block->bb_numrecs);
  643. #ifdef DEBUG
  644. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  645. return error;
  646. /*
  647. * Check that the new entry is being inserted in the right place.
  648. */
  649. if (ptr <= numrecs) {
  650. if (level == 0) {
  651. rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
  652. xfs_btree_check_rec(cur->bc_btnum, recp, rp);
  653. } else {
  654. kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
  655. xfs_btree_check_key(cur->bc_btnum, &key, kp);
  656. }
  657. }
  658. #endif
  659. nbno = NULLAGBLOCK;
  660. ncur = NULL;
  661. /*
  662. * If the block is full, we can't insert the new entry until we
  663. * make the block un-full.
  664. */
  665. if (numrecs == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  666. /*
  667. * First, try shifting an entry to the right neighbor.
  668. */
  669. if ((error = xfs_btree_rshift(cur, level, &i)))
  670. return error;
  671. if (i) {
  672. /* nothing */
  673. }
  674. /*
  675. * Next, try shifting an entry to the left neighbor.
  676. */
  677. else {
  678. if ((error = xfs_btree_lshift(cur, level, &i)))
  679. return error;
  680. if (i)
  681. optr = ptr = cur->bc_ptrs[level];
  682. else {
  683. union xfs_btree_ptr bno = { .s = cpu_to_be32(nbno) };
  684. /*
  685. * Next, try splitting the current block in
  686. * half. If this works we have to re-set our
  687. * variables because we could be in a
  688. * different block now.
  689. */
  690. if ((error = xfs_btree_split(cur, level, &bno,
  691. (union xfs_btree_key *)&nkey,
  692. &ncur, &i)))
  693. return error;
  694. nbno = be32_to_cpu(bno.s);
  695. if (i) {
  696. bp = cur->bc_bufs[level];
  697. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  698. #ifdef DEBUG
  699. if ((error =
  700. xfs_btree_check_sblock(cur,
  701. block, level, bp)))
  702. return error;
  703. #endif
  704. ptr = cur->bc_ptrs[level];
  705. nrec.ar_startblock = nkey.ar_startblock;
  706. nrec.ar_blockcount = nkey.ar_blockcount;
  707. }
  708. /*
  709. * Otherwise the insert fails.
  710. */
  711. else {
  712. *stat = 0;
  713. return 0;
  714. }
  715. }
  716. }
  717. }
  718. /*
  719. * At this point we know there's room for our new entry in the block
  720. * we're pointing at.
  721. */
  722. numrecs = be16_to_cpu(block->bb_numrecs);
  723. if (level > 0) {
  724. /*
  725. * It's a non-leaf entry. Make a hole for the new data
  726. * in the key and ptr regions of the block.
  727. */
  728. kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
  729. pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
  730. #ifdef DEBUG
  731. for (i = numrecs; i >= ptr; i--) {
  732. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
  733. return error;
  734. }
  735. #endif
  736. memmove(&kp[ptr], &kp[ptr - 1],
  737. (numrecs - ptr + 1) * sizeof(*kp));
  738. memmove(&pp[ptr], &pp[ptr - 1],
  739. (numrecs - ptr + 1) * sizeof(*pp));
  740. #ifdef DEBUG
  741. if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
  742. return error;
  743. #endif
  744. /*
  745. * Now stuff the new data in, bump numrecs and log the new data.
  746. */
  747. kp[ptr - 1] = key;
  748. pp[ptr - 1] = cpu_to_be32(*bnop);
  749. numrecs++;
  750. block->bb_numrecs = cpu_to_be16(numrecs);
  751. xfs_alloc_log_keys(cur, bp, ptr, numrecs);
  752. xfs_alloc_log_ptrs(cur, bp, ptr, numrecs);
  753. #ifdef DEBUG
  754. if (ptr < numrecs)
  755. xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
  756. kp + ptr);
  757. #endif
  758. } else {
  759. /*
  760. * It's a leaf entry. Make a hole for the new record.
  761. */
  762. rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
  763. memmove(&rp[ptr], &rp[ptr - 1],
  764. (numrecs - ptr + 1) * sizeof(*rp));
  765. /*
  766. * Now stuff the new record in, bump numrecs
  767. * and log the new data.
  768. */
  769. rp[ptr - 1] = *recp;
  770. numrecs++;
  771. block->bb_numrecs = cpu_to_be16(numrecs);
  772. xfs_alloc_log_recs(cur, bp, ptr, numrecs);
  773. #ifdef DEBUG
  774. if (ptr < numrecs)
  775. xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
  776. rp + ptr);
  777. #endif
  778. }
  779. /*
  780. * Log the new number of records in the btree header.
  781. */
  782. xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
  783. /*
  784. * If we inserted at the start of a block, update the parents' keys.
  785. */
  786. if (optr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)&key, level + 1)))
  787. return error;
  788. /*
  789. * Look to see if the longest extent in the allocation group
  790. * needs to be updated.
  791. */
  792. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  793. if (level == 0 &&
  794. cur->bc_btnum == XFS_BTNUM_CNT &&
  795. be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
  796. be32_to_cpu(recp->ar_blockcount) > be32_to_cpu(agf->agf_longest)) {
  797. /*
  798. * If this is a leaf in the by-size btree and there
  799. * is no right sibling block and this block is bigger
  800. * than the previous longest block, update it.
  801. */
  802. agf->agf_longest = recp->ar_blockcount;
  803. cur->bc_mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest
  804. = be32_to_cpu(recp->ar_blockcount);
  805. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  806. XFS_AGF_LONGEST);
  807. }
  808. /*
  809. * Return the new block number, if any.
  810. * If there is one, give back a record value and a cursor too.
  811. */
  812. *bnop = nbno;
  813. if (nbno != NULLAGBLOCK) {
  814. *recp = nrec;
  815. *curp = ncur;
  816. }
  817. *stat = 1;
  818. return 0;
  819. }
  820. /*
  821. * Log header fields from a btree block.
  822. */
  823. STATIC void
  824. xfs_alloc_log_block(
  825. xfs_trans_t *tp, /* transaction pointer */
  826. xfs_buf_t *bp, /* buffer containing btree block */
  827. int fields) /* mask of fields: XFS_BB_... */
  828. {
  829. int first; /* first byte offset logged */
  830. int last; /* last byte offset logged */
  831. static const short offsets[] = { /* table of offsets */
  832. offsetof(xfs_alloc_block_t, bb_magic),
  833. offsetof(xfs_alloc_block_t, bb_level),
  834. offsetof(xfs_alloc_block_t, bb_numrecs),
  835. offsetof(xfs_alloc_block_t, bb_leftsib),
  836. offsetof(xfs_alloc_block_t, bb_rightsib),
  837. sizeof(xfs_alloc_block_t)
  838. };
  839. xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
  840. xfs_trans_log_buf(tp, bp, first, last);
  841. }
  842. /*
  843. * Log keys from a btree block (nonleaf).
  844. */
  845. STATIC void
  846. xfs_alloc_log_keys(
  847. xfs_btree_cur_t *cur, /* btree cursor */
  848. xfs_buf_t *bp, /* buffer containing btree block */
  849. int kfirst, /* index of first key to log */
  850. int klast) /* index of last key to log */
  851. {
  852. xfs_alloc_block_t *block; /* btree block to log from */
  853. int first; /* first byte offset logged */
  854. xfs_alloc_key_t *kp; /* key pointer in btree block */
  855. int last; /* last byte offset logged */
  856. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  857. kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
  858. first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
  859. last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
  860. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  861. }
  862. /*
  863. * Log block pointer fields from a btree block (nonleaf).
  864. */
  865. STATIC void
  866. xfs_alloc_log_ptrs(
  867. xfs_btree_cur_t *cur, /* btree cursor */
  868. xfs_buf_t *bp, /* buffer containing btree block */
  869. int pfirst, /* index of first pointer to log */
  870. int plast) /* index of last pointer to log */
  871. {
  872. xfs_alloc_block_t *block; /* btree block to log from */
  873. int first; /* first byte offset logged */
  874. int last; /* last byte offset logged */
  875. xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
  876. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  877. pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
  878. first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
  879. last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
  880. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  881. }
  882. /*
  883. * Log records from a btree block (leaf).
  884. */
  885. STATIC void
  886. xfs_alloc_log_recs(
  887. xfs_btree_cur_t *cur, /* btree cursor */
  888. xfs_buf_t *bp, /* buffer containing btree block */
  889. int rfirst, /* index of first record to log */
  890. int rlast) /* index of last record to log */
  891. {
  892. xfs_alloc_block_t *block; /* btree block to log from */
  893. int first; /* first byte offset logged */
  894. int last; /* last byte offset logged */
  895. xfs_alloc_rec_t *rp; /* record pointer for btree block */
  896. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  897. rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
  898. #ifdef DEBUG
  899. {
  900. xfs_agf_t *agf;
  901. xfs_alloc_rec_t *p;
  902. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  903. for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
  904. ASSERT(be32_to_cpu(p->ar_startblock) +
  905. be32_to_cpu(p->ar_blockcount) <=
  906. be32_to_cpu(agf->agf_length));
  907. }
  908. #endif
  909. first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
  910. last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
  911. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  912. }
  913. /*
  914. * Allocate a new root block, fill it in.
  915. */
  916. STATIC int /* error */
  917. xfs_alloc_newroot(
  918. xfs_btree_cur_t *cur, /* btree cursor */
  919. int *stat) /* success/failure */
  920. {
  921. int error; /* error return value */
  922. xfs_agblock_t lbno; /* left block number */
  923. xfs_buf_t *lbp; /* left btree buffer */
  924. xfs_alloc_block_t *left; /* left btree block */
  925. xfs_mount_t *mp; /* mount structure */
  926. xfs_agblock_t nbno; /* new block number */
  927. xfs_buf_t *nbp; /* new (root) buffer */
  928. xfs_alloc_block_t *new; /* new (root) btree block */
  929. int nptr; /* new value for key index, 1 or 2 */
  930. xfs_agblock_t rbno; /* right block number */
  931. xfs_buf_t *rbp; /* right btree buffer */
  932. xfs_alloc_block_t *right; /* right btree block */
  933. mp = cur->bc_mp;
  934. ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
  935. /*
  936. * Get a buffer from the freelist blocks, for the new root.
  937. */
  938. error = xfs_alloc_get_freelist(cur->bc_tp,
  939. cur->bc_private.a.agbp, &nbno, 1);
  940. if (error)
  941. return error;
  942. /*
  943. * None available, we fail.
  944. */
  945. if (nbno == NULLAGBLOCK) {
  946. *stat = 0;
  947. return 0;
  948. }
  949. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  950. nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
  951. 0);
  952. new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
  953. /*
  954. * Set the root data in the a.g. freespace structure.
  955. */
  956. {
  957. xfs_agf_t *agf; /* a.g. freespace header */
  958. xfs_agnumber_t seqno;
  959. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  960. agf->agf_roots[cur->bc_btnum] = cpu_to_be32(nbno);
  961. be32_add_cpu(&agf->agf_levels[cur->bc_btnum], 1);
  962. seqno = be32_to_cpu(agf->agf_seqno);
  963. mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
  964. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  965. XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  966. }
  967. /*
  968. * At the previous root level there are now two blocks: the old
  969. * root, and the new block generated when it was split.
  970. * We don't know which one the cursor is pointing at, so we
  971. * set up variables "left" and "right" for each case.
  972. */
  973. lbp = cur->bc_bufs[cur->bc_nlevels - 1];
  974. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  975. #ifdef DEBUG
  976. if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
  977. return error;
  978. #endif
  979. if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
  980. /*
  981. * Our block is left, pick up the right block.
  982. */
  983. lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
  984. rbno = be32_to_cpu(left->bb_rightsib);
  985. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  986. cur->bc_private.a.agno, rbno, 0, &rbp,
  987. XFS_ALLOC_BTREE_REF)))
  988. return error;
  989. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  990. if ((error = xfs_btree_check_sblock(cur, right,
  991. cur->bc_nlevels - 1, rbp)))
  992. return error;
  993. nptr = 1;
  994. } else {
  995. /*
  996. * Our block is right, pick up the left block.
  997. */
  998. rbp = lbp;
  999. right = left;
  1000. rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
  1001. lbno = be32_to_cpu(right->bb_leftsib);
  1002. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  1003. cur->bc_private.a.agno, lbno, 0, &lbp,
  1004. XFS_ALLOC_BTREE_REF)))
  1005. return error;
  1006. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1007. if ((error = xfs_btree_check_sblock(cur, left,
  1008. cur->bc_nlevels - 1, lbp)))
  1009. return error;
  1010. nptr = 2;
  1011. }
  1012. /*
  1013. * Fill in the new block's btree header and log it.
  1014. */
  1015. new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  1016. new->bb_level = cpu_to_be16(cur->bc_nlevels);
  1017. new->bb_numrecs = cpu_to_be16(2);
  1018. new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  1019. new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  1020. xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
  1021. ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
  1022. /*
  1023. * Fill in the key data in the new root.
  1024. */
  1025. {
  1026. xfs_alloc_key_t *kp; /* btree key pointer */
  1027. kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
  1028. if (be16_to_cpu(left->bb_level) > 0) {
  1029. kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur);
  1030. kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);
  1031. } else {
  1032. xfs_alloc_rec_t *rp; /* btree record pointer */
  1033. rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
  1034. kp[0].ar_startblock = rp->ar_startblock;
  1035. kp[0].ar_blockcount = rp->ar_blockcount;
  1036. rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  1037. kp[1].ar_startblock = rp->ar_startblock;
  1038. kp[1].ar_blockcount = rp->ar_blockcount;
  1039. }
  1040. }
  1041. xfs_alloc_log_keys(cur, nbp, 1, 2);
  1042. /*
  1043. * Fill in the pointer data in the new root.
  1044. */
  1045. {
  1046. xfs_alloc_ptr_t *pp; /* btree address pointer */
  1047. pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
  1048. pp[0] = cpu_to_be32(lbno);
  1049. pp[1] = cpu_to_be32(rbno);
  1050. }
  1051. xfs_alloc_log_ptrs(cur, nbp, 1, 2);
  1052. /*
  1053. * Fix up the cursor.
  1054. */
  1055. xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
  1056. cur->bc_ptrs[cur->bc_nlevels] = nptr;
  1057. cur->bc_nlevels++;
  1058. *stat = 1;
  1059. return 0;
  1060. }
  1061. /*
  1062. * Externally visible routines.
  1063. */
  1064. /*
  1065. * Delete the record pointed to by cur.
  1066. * The cursor refers to the place where the record was (could be inserted)
  1067. * when the operation returns.
  1068. */
  1069. int /* error */
  1070. xfs_alloc_delete(
  1071. xfs_btree_cur_t *cur, /* btree cursor */
  1072. int *stat) /* success/failure */
  1073. {
  1074. int error; /* error return value */
  1075. int i; /* result code */
  1076. int level; /* btree level */
  1077. /*
  1078. * Go up the tree, starting at leaf level.
  1079. * If 2 is returned then a join was done; go to the next level.
  1080. * Otherwise we are done.
  1081. */
  1082. for (level = 0, i = 2; i == 2; level++) {
  1083. if ((error = xfs_alloc_delrec(cur, level, &i)))
  1084. return error;
  1085. }
  1086. if (i == 0) {
  1087. for (level = 1; level < cur->bc_nlevels; level++) {
  1088. if (cur->bc_ptrs[level] == 0) {
  1089. if ((error = xfs_btree_decrement(cur, level, &i)))
  1090. return error;
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. *stat = i;
  1096. return 0;
  1097. }
  1098. /*
  1099. * Get the data from the pointed-to record.
  1100. */
  1101. int /* error */
  1102. xfs_alloc_get_rec(
  1103. xfs_btree_cur_t *cur, /* btree cursor */
  1104. xfs_agblock_t *bno, /* output: starting block of extent */
  1105. xfs_extlen_t *len, /* output: length of extent */
  1106. int *stat) /* output: success/failure */
  1107. {
  1108. xfs_alloc_block_t *block; /* btree block */
  1109. #ifdef DEBUG
  1110. int error; /* error return value */
  1111. #endif
  1112. int ptr; /* record number */
  1113. ptr = cur->bc_ptrs[0];
  1114. block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
  1115. #ifdef DEBUG
  1116. if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
  1117. return error;
  1118. #endif
  1119. /*
  1120. * Off the right end or left end, return failure.
  1121. */
  1122. if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
  1123. *stat = 0;
  1124. return 0;
  1125. }
  1126. /*
  1127. * Point to the record and extract its data.
  1128. */
  1129. {
  1130. xfs_alloc_rec_t *rec; /* record data */
  1131. rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
  1132. *bno = be32_to_cpu(rec->ar_startblock);
  1133. *len = be32_to_cpu(rec->ar_blockcount);
  1134. }
  1135. *stat = 1;
  1136. return 0;
  1137. }
  1138. /*
  1139. * Insert the current record at the point referenced by cur.
  1140. * The cursor may be inconsistent on return if splits have been done.
  1141. */
  1142. int /* error */
  1143. xfs_alloc_insert(
  1144. xfs_btree_cur_t *cur, /* btree cursor */
  1145. int *stat) /* success/failure */
  1146. {
  1147. int error; /* error return value */
  1148. int i; /* result value, 0 for failure */
  1149. int level; /* current level number in btree */
  1150. xfs_agblock_t nbno; /* new block number (split result) */
  1151. xfs_btree_cur_t *ncur; /* new cursor (split result) */
  1152. xfs_alloc_rec_t nrec; /* record being inserted this level */
  1153. xfs_btree_cur_t *pcur; /* previous level's cursor */
  1154. level = 0;
  1155. nbno = NULLAGBLOCK;
  1156. nrec.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  1157. nrec.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  1158. ncur = NULL;
  1159. pcur = cur;
  1160. /*
  1161. * Loop going up the tree, starting at the leaf level.
  1162. * Stop when we don't get a split block, that must mean that
  1163. * the insert is finished with this level.
  1164. */
  1165. do {
  1166. /*
  1167. * Insert nrec/nbno into this level of the tree.
  1168. * Note if we fail, nbno will be null.
  1169. */
  1170. if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
  1171. &i))) {
  1172. if (pcur != cur)
  1173. xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
  1174. return error;
  1175. }
  1176. /*
  1177. * See if the cursor we just used is trash.
  1178. * Can't trash the caller's cursor, but otherwise we should
  1179. * if ncur is a new cursor or we're about to be done.
  1180. */
  1181. if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
  1182. cur->bc_nlevels = pcur->bc_nlevels;
  1183. xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
  1184. }
  1185. /*
  1186. * If we got a new cursor, switch to it.
  1187. */
  1188. if (ncur) {
  1189. pcur = ncur;
  1190. ncur = NULL;
  1191. }
  1192. } while (nbno != NULLAGBLOCK);
  1193. *stat = i;
  1194. return 0;
  1195. }
  1196. STATIC struct xfs_btree_cur *
  1197. xfs_allocbt_dup_cursor(
  1198. struct xfs_btree_cur *cur)
  1199. {
  1200. return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
  1201. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  1202. cur->bc_btnum);
  1203. }
  1204. STATIC int
  1205. xfs_allocbt_alloc_block(
  1206. struct xfs_btree_cur *cur,
  1207. union xfs_btree_ptr *start,
  1208. union xfs_btree_ptr *new,
  1209. int length,
  1210. int *stat)
  1211. {
  1212. int error;
  1213. xfs_agblock_t bno;
  1214. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1215. /* Allocate the new block from the freelist. If we can't, give up. */
  1216. error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  1217. &bno, 1);
  1218. if (error) {
  1219. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1220. return error;
  1221. }
  1222. if (bno == NULLAGBLOCK) {
  1223. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1224. *stat = 0;
  1225. return 0;
  1226. }
  1227. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  1228. new->s = cpu_to_be32(bno);
  1229. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1230. *stat = 1;
  1231. return 0;
  1232. }
  1233. /*
  1234. * Update the longest extent in the AGF
  1235. */
  1236. STATIC void
  1237. xfs_allocbt_update_lastrec(
  1238. struct xfs_btree_cur *cur,
  1239. struct xfs_btree_block *block,
  1240. union xfs_btree_rec *rec,
  1241. int ptr,
  1242. int reason)
  1243. {
  1244. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  1245. xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
  1246. __be32 len;
  1247. ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
  1248. switch (reason) {
  1249. case LASTREC_UPDATE:
  1250. /*
  1251. * If this is the last leaf block and it's the last record,
  1252. * then update the size of the longest extent in the AG.
  1253. */
  1254. if (ptr != xfs_btree_get_numrecs(block))
  1255. return;
  1256. len = rec->alloc.ar_blockcount;
  1257. break;
  1258. default:
  1259. ASSERT(0);
  1260. return;
  1261. }
  1262. agf->agf_longest = len;
  1263. cur->bc_mp->m_perag[seqno].pagf_longest = be32_to_cpu(len);
  1264. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
  1265. }
  1266. STATIC int
  1267. xfs_allocbt_get_maxrecs(
  1268. struct xfs_btree_cur *cur,
  1269. int level)
  1270. {
  1271. return cur->bc_mp->m_alloc_mxr[level != 0];
  1272. }
  1273. STATIC void
  1274. xfs_allocbt_init_key_from_rec(
  1275. union xfs_btree_key *key,
  1276. union xfs_btree_rec *rec)
  1277. {
  1278. ASSERT(rec->alloc.ar_startblock != 0);
  1279. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  1280. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  1281. }
  1282. STATIC void
  1283. xfs_allocbt_init_ptr_from_cur(
  1284. struct xfs_btree_cur *cur,
  1285. union xfs_btree_ptr *ptr)
  1286. {
  1287. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  1288. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  1289. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  1290. ptr->s = agf->agf_roots[cur->bc_btnum];
  1291. }
  1292. STATIC __int64_t
  1293. xfs_allocbt_key_diff(
  1294. struct xfs_btree_cur *cur,
  1295. union xfs_btree_key *key)
  1296. {
  1297. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  1298. xfs_alloc_key_t *kp = &key->alloc;
  1299. __int64_t diff;
  1300. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  1301. return (__int64_t)be32_to_cpu(kp->ar_startblock) -
  1302. rec->ar_startblock;
  1303. }
  1304. diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  1305. if (diff)
  1306. return diff;
  1307. return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  1308. }
  1309. #ifdef XFS_BTREE_TRACE
  1310. ktrace_t *xfs_allocbt_trace_buf;
  1311. STATIC void
  1312. xfs_allocbt_trace_enter(
  1313. struct xfs_btree_cur *cur,
  1314. const char *func,
  1315. char *s,
  1316. int type,
  1317. int line,
  1318. __psunsigned_t a0,
  1319. __psunsigned_t a1,
  1320. __psunsigned_t a2,
  1321. __psunsigned_t a3,
  1322. __psunsigned_t a4,
  1323. __psunsigned_t a5,
  1324. __psunsigned_t a6,
  1325. __psunsigned_t a7,
  1326. __psunsigned_t a8,
  1327. __psunsigned_t a9,
  1328. __psunsigned_t a10)
  1329. {
  1330. ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
  1331. (void *)func, (void *)s, NULL, (void *)cur,
  1332. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  1333. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  1334. (void *)a8, (void *)a9, (void *)a10);
  1335. }
  1336. STATIC void
  1337. xfs_allocbt_trace_cursor(
  1338. struct xfs_btree_cur *cur,
  1339. __uint32_t *s0,
  1340. __uint64_t *l0,
  1341. __uint64_t *l1)
  1342. {
  1343. *s0 = cur->bc_private.a.agno;
  1344. *l0 = cur->bc_rec.a.ar_startblock;
  1345. *l1 = cur->bc_rec.a.ar_blockcount;
  1346. }
  1347. STATIC void
  1348. xfs_allocbt_trace_key(
  1349. struct xfs_btree_cur *cur,
  1350. union xfs_btree_key *key,
  1351. __uint64_t *l0,
  1352. __uint64_t *l1)
  1353. {
  1354. *l0 = be32_to_cpu(key->alloc.ar_startblock);
  1355. *l1 = be32_to_cpu(key->alloc.ar_blockcount);
  1356. }
  1357. STATIC void
  1358. xfs_allocbt_trace_record(
  1359. struct xfs_btree_cur *cur,
  1360. union xfs_btree_rec *rec,
  1361. __uint64_t *l0,
  1362. __uint64_t *l1,
  1363. __uint64_t *l2)
  1364. {
  1365. *l0 = be32_to_cpu(rec->alloc.ar_startblock);
  1366. *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
  1367. *l2 = 0;
  1368. }
  1369. #endif /* XFS_BTREE_TRACE */
  1370. static const struct xfs_btree_ops xfs_allocbt_ops = {
  1371. .rec_len = sizeof(xfs_alloc_rec_t),
  1372. .key_len = sizeof(xfs_alloc_key_t),
  1373. .dup_cursor = xfs_allocbt_dup_cursor,
  1374. .alloc_block = xfs_allocbt_alloc_block,
  1375. .update_lastrec = xfs_allocbt_update_lastrec,
  1376. .get_maxrecs = xfs_allocbt_get_maxrecs,
  1377. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  1378. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  1379. .key_diff = xfs_allocbt_key_diff,
  1380. #ifdef XFS_BTREE_TRACE
  1381. .trace_enter = xfs_allocbt_trace_enter,
  1382. .trace_cursor = xfs_allocbt_trace_cursor,
  1383. .trace_key = xfs_allocbt_trace_key,
  1384. .trace_record = xfs_allocbt_trace_record,
  1385. #endif
  1386. };
  1387. /*
  1388. * Allocate a new allocation btree cursor.
  1389. */
  1390. struct xfs_btree_cur * /* new alloc btree cursor */
  1391. xfs_allocbt_init_cursor(
  1392. struct xfs_mount *mp, /* file system mount point */
  1393. struct xfs_trans *tp, /* transaction pointer */
  1394. struct xfs_buf *agbp, /* buffer for agf structure */
  1395. xfs_agnumber_t agno, /* allocation group number */
  1396. xfs_btnum_t btnum) /* btree identifier */
  1397. {
  1398. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  1399. struct xfs_btree_cur *cur;
  1400. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  1401. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  1402. cur->bc_tp = tp;
  1403. cur->bc_mp = mp;
  1404. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
  1405. cur->bc_btnum = btnum;
  1406. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  1407. cur->bc_ops = &xfs_allocbt_ops;
  1408. if (btnum == XFS_BTNUM_CNT)
  1409. cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
  1410. cur->bc_private.a.agbp = agbp;
  1411. cur->bc_private.a.agno = agno;
  1412. return cur;
  1413. }