xfs_alloc_btree.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  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_ialloc.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_error.h"
  41. /*
  42. * Prototypes for internal functions.
  43. */
  44. STATIC void xfs_alloc_log_block(xfs_trans_t *, xfs_buf_t *, int);
  45. STATIC void xfs_alloc_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  46. STATIC void xfs_alloc_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  47. STATIC void xfs_alloc_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  48. STATIC int xfs_alloc_lshift(xfs_btree_cur_t *, int, int *);
  49. STATIC int xfs_alloc_newroot(xfs_btree_cur_t *, int *);
  50. STATIC int xfs_alloc_rshift(xfs_btree_cur_t *, int, int *);
  51. STATIC int xfs_alloc_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
  52. xfs_alloc_key_t *, xfs_btree_cur_t **, int *);
  53. STATIC int xfs_alloc_updkey(xfs_btree_cur_t *, xfs_alloc_key_t *, int);
  54. /*
  55. * Internal functions.
  56. */
  57. /*
  58. * Single level of the xfs_alloc_delete record deletion routine.
  59. * Delete record pointed to by cur/level.
  60. * Remove the record from its block then rebalance the tree.
  61. * Return 0 for error, 1 for done, 2 to go on to the next level.
  62. */
  63. STATIC int /* error */
  64. xfs_alloc_delrec(
  65. xfs_btree_cur_t *cur, /* btree cursor */
  66. int level, /* level removing record from */
  67. int *stat) /* fail/done/go-on */
  68. {
  69. xfs_agf_t *agf; /* allocation group freelist header */
  70. xfs_alloc_block_t *block; /* btree block record/key lives in */
  71. xfs_agblock_t bno; /* btree block number */
  72. xfs_buf_t *bp; /* buffer for block */
  73. int error; /* error return value */
  74. int i; /* loop index */
  75. xfs_alloc_key_t key; /* kp points here if block is level 0 */
  76. xfs_agblock_t lbno; /* left block's block number */
  77. xfs_buf_t *lbp; /* left block's buffer pointer */
  78. xfs_alloc_block_t *left; /* left btree block */
  79. xfs_alloc_key_t *lkp=NULL; /* left block key pointer */
  80. xfs_alloc_ptr_t *lpp=NULL; /* left block address pointer */
  81. int lrecs=0; /* number of records in left block */
  82. xfs_alloc_rec_t *lrp; /* left block record pointer */
  83. xfs_mount_t *mp; /* mount structure */
  84. int ptr; /* index in btree block for this rec */
  85. xfs_agblock_t rbno; /* right block's block number */
  86. xfs_buf_t *rbp; /* right block's buffer pointer */
  87. xfs_alloc_block_t *right; /* right btree block */
  88. xfs_alloc_key_t *rkp; /* right block key pointer */
  89. xfs_alloc_ptr_t *rpp; /* right block address pointer */
  90. int rrecs=0; /* number of records in right block */
  91. xfs_alloc_rec_t *rrp; /* right block record pointer */
  92. xfs_btree_cur_t *tcur; /* temporary btree cursor */
  93. /*
  94. * Get the index of the entry being deleted, check for nothing there.
  95. */
  96. ptr = cur->bc_ptrs[level];
  97. if (ptr == 0) {
  98. *stat = 0;
  99. return 0;
  100. }
  101. /*
  102. * Get the buffer & block containing the record or key/ptr.
  103. */
  104. bp = cur->bc_bufs[level];
  105. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  106. #ifdef DEBUG
  107. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  108. return error;
  109. #endif
  110. /*
  111. * Fail if we're off the end of the block.
  112. */
  113. if (ptr > be16_to_cpu(block->bb_numrecs)) {
  114. *stat = 0;
  115. return 0;
  116. }
  117. XFS_STATS_INC(xs_abt_delrec);
  118. /*
  119. * It's a nonleaf. Excise the key and ptr being deleted, by
  120. * sliding the entries past them down one.
  121. * Log the changed areas of the block.
  122. */
  123. if (level > 0) {
  124. lkp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
  125. lpp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
  126. #ifdef DEBUG
  127. for (i = ptr; i < be16_to_cpu(block->bb_numrecs); i++) {
  128. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
  129. return error;
  130. }
  131. #endif
  132. if (ptr < be16_to_cpu(block->bb_numrecs)) {
  133. memmove(&lkp[ptr - 1], &lkp[ptr],
  134. (be16_to_cpu(block->bb_numrecs) - ptr) * sizeof(*lkp));
  135. memmove(&lpp[ptr - 1], &lpp[ptr],
  136. (be16_to_cpu(block->bb_numrecs) - ptr) * sizeof(*lpp));
  137. xfs_alloc_log_ptrs(cur, bp, ptr, be16_to_cpu(block->bb_numrecs) - 1);
  138. xfs_alloc_log_keys(cur, bp, ptr, be16_to_cpu(block->bb_numrecs) - 1);
  139. }
  140. }
  141. /*
  142. * It's a leaf. Excise the record being deleted, by sliding the
  143. * entries past it down one. Log the changed areas of the block.
  144. */
  145. else {
  146. lrp = XFS_ALLOC_REC_ADDR(block, 1, cur);
  147. if (ptr < be16_to_cpu(block->bb_numrecs)) {
  148. memmove(&lrp[ptr - 1], &lrp[ptr],
  149. (be16_to_cpu(block->bb_numrecs) - ptr) * sizeof(*lrp));
  150. xfs_alloc_log_recs(cur, bp, ptr, be16_to_cpu(block->bb_numrecs) - 1);
  151. }
  152. /*
  153. * If it's the first record in the block, we'll need a key
  154. * structure to pass up to the next level (updkey).
  155. */
  156. if (ptr == 1) {
  157. key.ar_startblock = lrp->ar_startblock;
  158. key.ar_blockcount = lrp->ar_blockcount;
  159. lkp = &key;
  160. }
  161. }
  162. /*
  163. * Decrement and log the number of entries in the block.
  164. */
  165. be16_add(&block->bb_numrecs, -1);
  166. xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
  167. /*
  168. * See if the longest free extent in the allocation group was
  169. * changed by this operation. True if it's the by-size btree, and
  170. * this is the leaf level, and there is no right sibling block,
  171. * and this was the last record.
  172. */
  173. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  174. mp = cur->bc_mp;
  175. if (level == 0 &&
  176. cur->bc_btnum == XFS_BTNUM_CNT &&
  177. be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
  178. ptr > be16_to_cpu(block->bb_numrecs)) {
  179. ASSERT(ptr == be16_to_cpu(block->bb_numrecs) + 1);
  180. /*
  181. * There are still records in the block. Grab the size
  182. * from the last one.
  183. */
  184. if (be16_to_cpu(block->bb_numrecs)) {
  185. rrp = XFS_ALLOC_REC_ADDR(block, be16_to_cpu(block->bb_numrecs), cur);
  186. agf->agf_longest = rrp->ar_blockcount;
  187. }
  188. /*
  189. * No free extents left.
  190. */
  191. else
  192. agf->agf_longest = 0;
  193. mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest =
  194. be32_to_cpu(agf->agf_longest);
  195. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  196. XFS_AGF_LONGEST);
  197. }
  198. /*
  199. * Is this the root level? If so, we're almost done.
  200. */
  201. if (level == cur->bc_nlevels - 1) {
  202. /*
  203. * If this is the root level,
  204. * and there's only one entry left,
  205. * and it's NOT the leaf level,
  206. * then we can get rid of this level.
  207. */
  208. if (be16_to_cpu(block->bb_numrecs) == 1 && level > 0) {
  209. /*
  210. * lpp is still set to the first pointer in the block.
  211. * Make it the new root of the btree.
  212. */
  213. bno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
  214. agf->agf_roots[cur->bc_btnum] = *lpp;
  215. be32_add(&agf->agf_levels[cur->bc_btnum], -1);
  216. mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_levels[cur->bc_btnum]--;
  217. /*
  218. * Put this buffer/block on the ag's freelist.
  219. */
  220. if ((error = xfs_alloc_put_freelist(cur->bc_tp,
  221. cur->bc_private.a.agbp, NULL, bno)))
  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_alloc_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_alloc_updkey(cur, 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 (be16_to_cpu(block->bb_numrecs) >= XFS_ALLOC_BLOCK_MINRECS(level, cur)) {
  264. if (level > 0 && (error = xfs_alloc_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_alloc_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_alloc_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_alloc_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_alloc_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_alloc_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_alloc_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 + be16_to_cpu(block->bb_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. 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. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  429. return error;
  430. }
  431. /*
  432. * If that won't work, see if we can join with the right neighbor block.
  433. */
  434. else if (rbno != NULLAGBLOCK &&
  435. rrecs + be16_to_cpu(block->bb_numrecs) <=
  436. 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. lbp = bp;
  444. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  445. cur->bc_private.a.agno, rbno, 0, &rbp,
  446. XFS_ALLOC_BTREE_REF)))
  447. return error;
  448. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  449. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  450. return error;
  451. }
  452. /*
  453. * Otherwise, we can't fix the imbalance.
  454. * Just return. This is probably a logic error, but it's not fatal.
  455. */
  456. else {
  457. if (level > 0 && (error = xfs_alloc_decrement(cur, level, &i)))
  458. return error;
  459. *stat = 1;
  460. return 0;
  461. }
  462. /*
  463. * We're now going to join "left" and "right" by moving all the stuff
  464. * in "right" to "left" and deleting "right".
  465. */
  466. if (level > 0) {
  467. /*
  468. * It's a non-leaf. Move keys and pointers.
  469. */
  470. lkp = XFS_ALLOC_KEY_ADDR(left, be16_to_cpu(left->bb_numrecs) + 1, cur);
  471. lpp = XFS_ALLOC_PTR_ADDR(left, be16_to_cpu(left->bb_numrecs) + 1, cur);
  472. rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
  473. rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
  474. #ifdef DEBUG
  475. for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
  476. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
  477. return error;
  478. }
  479. #endif
  480. memcpy(lkp, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*lkp));
  481. memcpy(lpp, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*lpp));
  482. xfs_alloc_log_keys(cur, lbp, be16_to_cpu(left->bb_numrecs) + 1,
  483. be16_to_cpu(left->bb_numrecs) +
  484. be16_to_cpu(right->bb_numrecs));
  485. xfs_alloc_log_ptrs(cur, lbp, be16_to_cpu(left->bb_numrecs) + 1,
  486. be16_to_cpu(left->bb_numrecs) +
  487. be16_to_cpu(right->bb_numrecs));
  488. } else {
  489. /*
  490. * It's a leaf. Move records.
  491. */
  492. lrp = XFS_ALLOC_REC_ADDR(left, be16_to_cpu(left->bb_numrecs) + 1, cur);
  493. rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  494. memcpy(lrp, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*lrp));
  495. xfs_alloc_log_recs(cur, lbp, be16_to_cpu(left->bb_numrecs) + 1,
  496. be16_to_cpu(left->bb_numrecs) +
  497. be16_to_cpu(right->bb_numrecs));
  498. }
  499. /*
  500. * If we joined with the left neighbor, set the buffer in the
  501. * cursor to the left block, and fix up the index.
  502. */
  503. if (bp != lbp) {
  504. xfs_btree_setbuf(cur, level, lbp);
  505. cur->bc_ptrs[level] += be16_to_cpu(left->bb_numrecs);
  506. }
  507. /*
  508. * If we joined with the right neighbor and there's a level above
  509. * us, increment the cursor at that level.
  510. */
  511. else if (level + 1 < cur->bc_nlevels &&
  512. (error = xfs_alloc_increment(cur, level + 1, &i)))
  513. return error;
  514. /*
  515. * Fix up the number of records in the surviving block.
  516. */
  517. be16_add(&left->bb_numrecs, be16_to_cpu(right->bb_numrecs));
  518. /*
  519. * Fix up the right block pointer in the surviving block, and log it.
  520. */
  521. left->bb_rightsib = right->bb_rightsib;
  522. xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  523. /*
  524. * If there is a right sibling now, make it point to the
  525. * remaining block.
  526. */
  527. if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
  528. xfs_alloc_block_t *rrblock;
  529. xfs_buf_t *rrbp;
  530. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  531. cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
  532. &rrbp, XFS_ALLOC_BTREE_REF)))
  533. return error;
  534. rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
  535. if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
  536. return error;
  537. rrblock->bb_leftsib = cpu_to_be32(lbno);
  538. xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
  539. }
  540. /*
  541. * Free the deleting block by putting it on the freelist.
  542. */
  543. if ((error = xfs_alloc_put_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  544. NULL, rbno)))
  545. return error;
  546. /*
  547. * Since blocks move to the free list without the coordination
  548. * used in xfs_bmap_finish, we can't allow block to be available
  549. * for reallocation and non-transaction writing (user data)
  550. * until we know that the transaction that moved it to the free
  551. * list is permanently on disk. We track the blocks by declaring
  552. * these blocks as "busy"; the busy list is maintained on a
  553. * per-ag basis and each transaction records which entries
  554. * should be removed when the iclog commits to disk. If a
  555. * busy block is allocated, the iclog is pushed up to the
  556. * LSN that freed the block.
  557. */
  558. xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
  559. xfs_trans_agbtree_delta(cur->bc_tp, -1);
  560. /*
  561. * Adjust the current level's cursor so that we're left referring
  562. * to the right node, after we're done.
  563. * If this leaves the ptr value 0 our caller will fix it up.
  564. */
  565. if (level > 0)
  566. cur->bc_ptrs[level]--;
  567. /*
  568. * Return value means the next level up has something to do.
  569. */
  570. *stat = 2;
  571. return 0;
  572. error0:
  573. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  574. return error;
  575. }
  576. /*
  577. * Insert one record/level. Return information to the caller
  578. * allowing the next level up to proceed if necessary.
  579. */
  580. STATIC int /* error */
  581. xfs_alloc_insrec(
  582. xfs_btree_cur_t *cur, /* btree cursor */
  583. int level, /* level to insert record at */
  584. xfs_agblock_t *bnop, /* i/o: block number inserted */
  585. xfs_alloc_rec_t *recp, /* i/o: record data inserted */
  586. xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
  587. int *stat) /* output: success/failure */
  588. {
  589. xfs_agf_t *agf; /* allocation group freelist header */
  590. xfs_alloc_block_t *block; /* btree block record/key lives in */
  591. xfs_buf_t *bp; /* buffer for block */
  592. int error; /* error return value */
  593. int i; /* loop index */
  594. xfs_alloc_key_t key; /* key value being inserted */
  595. xfs_alloc_key_t *kp; /* pointer to btree keys */
  596. xfs_agblock_t nbno; /* block number of allocated block */
  597. xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
  598. xfs_alloc_key_t nkey; /* new key value, from split */
  599. xfs_alloc_rec_t nrec; /* new record value, for caller */
  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. #ifdef DEBUG
  643. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  644. return error;
  645. /*
  646. * Check that the new entry is being inserted in the right place.
  647. */
  648. if (ptr <= be16_to_cpu(block->bb_numrecs)) {
  649. if (level == 0) {
  650. rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
  651. xfs_btree_check_rec(cur->bc_btnum, recp, rp);
  652. } else {
  653. kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
  654. xfs_btree_check_key(cur->bc_btnum, &key, kp);
  655. }
  656. }
  657. #endif
  658. nbno = NULLAGBLOCK;
  659. ncur = (xfs_btree_cur_t *)0;
  660. /*
  661. * If the block is full, we can't insert the new entry until we
  662. * make the block un-full.
  663. */
  664. if (be16_to_cpu(block->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  665. /*
  666. * First, try shifting an entry to the right neighbor.
  667. */
  668. if ((error = xfs_alloc_rshift(cur, level, &i)))
  669. return error;
  670. if (i) {
  671. /* nothing */
  672. }
  673. /*
  674. * Next, try shifting an entry to the left neighbor.
  675. */
  676. else {
  677. if ((error = xfs_alloc_lshift(cur, level, &i)))
  678. return error;
  679. if (i)
  680. optr = ptr = cur->bc_ptrs[level];
  681. else {
  682. /*
  683. * Next, try splitting the current block in
  684. * half. If this works we have to re-set our
  685. * variables because we could be in a
  686. * different block now.
  687. */
  688. if ((error = xfs_alloc_split(cur, level, &nbno,
  689. &nkey, &ncur, &i)))
  690. return error;
  691. if (i) {
  692. bp = cur->bc_bufs[level];
  693. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  694. #ifdef DEBUG
  695. if ((error =
  696. xfs_btree_check_sblock(cur,
  697. block, level, bp)))
  698. return error;
  699. #endif
  700. ptr = cur->bc_ptrs[level];
  701. nrec.ar_startblock = nkey.ar_startblock;
  702. nrec.ar_blockcount = nkey.ar_blockcount;
  703. }
  704. /*
  705. * Otherwise the insert fails.
  706. */
  707. else {
  708. *stat = 0;
  709. return 0;
  710. }
  711. }
  712. }
  713. }
  714. /*
  715. * At this point we know there's room for our new entry in the block
  716. * we're pointing at.
  717. */
  718. if (level > 0) {
  719. /*
  720. * It's a non-leaf entry. Make a hole for the new data
  721. * in the key and ptr regions of the block.
  722. */
  723. kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
  724. pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
  725. #ifdef DEBUG
  726. for (i = be16_to_cpu(block->bb_numrecs); i >= ptr; i--) {
  727. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
  728. return error;
  729. }
  730. #endif
  731. memmove(&kp[ptr], &kp[ptr - 1],
  732. (be16_to_cpu(block->bb_numrecs) - ptr + 1) * sizeof(*kp));
  733. memmove(&pp[ptr], &pp[ptr - 1],
  734. (be16_to_cpu(block->bb_numrecs) - ptr + 1) * sizeof(*pp));
  735. #ifdef DEBUG
  736. if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
  737. return error;
  738. #endif
  739. /*
  740. * Now stuff the new data in, bump numrecs and log the new data.
  741. */
  742. kp[ptr - 1] = key;
  743. pp[ptr - 1] = cpu_to_be32(*bnop);
  744. be16_add(&block->bb_numrecs, 1);
  745. xfs_alloc_log_keys(cur, bp, ptr, be16_to_cpu(block->bb_numrecs));
  746. xfs_alloc_log_ptrs(cur, bp, ptr, be16_to_cpu(block->bb_numrecs));
  747. #ifdef DEBUG
  748. if (ptr < be16_to_cpu(block->bb_numrecs))
  749. xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
  750. kp + ptr);
  751. #endif
  752. } else {
  753. /*
  754. * It's a leaf entry. Make a hole for the new record.
  755. */
  756. rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
  757. memmove(&rp[ptr], &rp[ptr - 1],
  758. (be16_to_cpu(block->bb_numrecs) - ptr + 1) * sizeof(*rp));
  759. /*
  760. * Now stuff the new record in, bump numrecs
  761. * and log the new data.
  762. */
  763. rp[ptr - 1] = *recp; /* INT_: struct copy */
  764. be16_add(&block->bb_numrecs, 1);
  765. xfs_alloc_log_recs(cur, bp, ptr, be16_to_cpu(block->bb_numrecs));
  766. #ifdef DEBUG
  767. if (ptr < be16_to_cpu(block->bb_numrecs))
  768. xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
  769. rp + ptr);
  770. #endif
  771. }
  772. /*
  773. * Log the new number of records in the btree header.
  774. */
  775. xfs_alloc_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
  776. /*
  777. * If we inserted at the start of a block, update the parents' keys.
  778. */
  779. if (optr == 1 && (error = xfs_alloc_updkey(cur, &key, level + 1)))
  780. return error;
  781. /*
  782. * Look to see if the longest extent in the allocation group
  783. * needs to be updated.
  784. */
  785. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  786. if (level == 0 &&
  787. cur->bc_btnum == XFS_BTNUM_CNT &&
  788. be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
  789. be32_to_cpu(recp->ar_blockcount) > be32_to_cpu(agf->agf_longest)) {
  790. /*
  791. * If this is a leaf in the by-size btree and there
  792. * is no right sibling block and this block is bigger
  793. * than the previous longest block, update it.
  794. */
  795. agf->agf_longest = recp->ar_blockcount;
  796. cur->bc_mp->m_perag[be32_to_cpu(agf->agf_seqno)].pagf_longest
  797. = be32_to_cpu(recp->ar_blockcount);
  798. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  799. XFS_AGF_LONGEST);
  800. }
  801. /*
  802. * Return the new block number, if any.
  803. * If there is one, give back a record value and a cursor too.
  804. */
  805. *bnop = nbno;
  806. if (nbno != NULLAGBLOCK) {
  807. *recp = nrec; /* INT_: struct copy */
  808. *curp = ncur; /* INT_: struct copy */
  809. }
  810. *stat = 1;
  811. return 0;
  812. }
  813. /*
  814. * Log header fields from a btree block.
  815. */
  816. STATIC void
  817. xfs_alloc_log_block(
  818. xfs_trans_t *tp, /* transaction pointer */
  819. xfs_buf_t *bp, /* buffer containing btree block */
  820. int fields) /* mask of fields: XFS_BB_... */
  821. {
  822. int first; /* first byte offset logged */
  823. int last; /* last byte offset logged */
  824. static const short offsets[] = { /* table of offsets */
  825. offsetof(xfs_alloc_block_t, bb_magic),
  826. offsetof(xfs_alloc_block_t, bb_level),
  827. offsetof(xfs_alloc_block_t, bb_numrecs),
  828. offsetof(xfs_alloc_block_t, bb_leftsib),
  829. offsetof(xfs_alloc_block_t, bb_rightsib),
  830. sizeof(xfs_alloc_block_t)
  831. };
  832. xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
  833. xfs_trans_log_buf(tp, bp, first, last);
  834. }
  835. /*
  836. * Log keys from a btree block (nonleaf).
  837. */
  838. STATIC void
  839. xfs_alloc_log_keys(
  840. xfs_btree_cur_t *cur, /* btree cursor */
  841. xfs_buf_t *bp, /* buffer containing btree block */
  842. int kfirst, /* index of first key to log */
  843. int klast) /* index of last key to log */
  844. {
  845. xfs_alloc_block_t *block; /* btree block to log from */
  846. int first; /* first byte offset logged */
  847. xfs_alloc_key_t *kp; /* key pointer in btree block */
  848. int last; /* last byte offset logged */
  849. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  850. kp = XFS_ALLOC_KEY_ADDR(block, 1, cur);
  851. first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
  852. last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
  853. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  854. }
  855. /*
  856. * Log block pointer fields from a btree block (nonleaf).
  857. */
  858. STATIC void
  859. xfs_alloc_log_ptrs(
  860. xfs_btree_cur_t *cur, /* btree cursor */
  861. xfs_buf_t *bp, /* buffer containing btree block */
  862. int pfirst, /* index of first pointer to log */
  863. int plast) /* index of last pointer to log */
  864. {
  865. xfs_alloc_block_t *block; /* btree block to log from */
  866. int first; /* first byte offset logged */
  867. int last; /* last byte offset logged */
  868. xfs_alloc_ptr_t *pp; /* block-pointer pointer in btree blk */
  869. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  870. pp = XFS_ALLOC_PTR_ADDR(block, 1, cur);
  871. first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
  872. last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
  873. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  874. }
  875. /*
  876. * Log records from a btree block (leaf).
  877. */
  878. STATIC void
  879. xfs_alloc_log_recs(
  880. xfs_btree_cur_t *cur, /* btree cursor */
  881. xfs_buf_t *bp, /* buffer containing btree block */
  882. int rfirst, /* index of first record to log */
  883. int rlast) /* index of last record to log */
  884. {
  885. xfs_alloc_block_t *block; /* btree block to log from */
  886. int first; /* first byte offset logged */
  887. int last; /* last byte offset logged */
  888. xfs_alloc_rec_t *rp; /* record pointer for btree block */
  889. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  890. rp = XFS_ALLOC_REC_ADDR(block, 1, cur);
  891. #ifdef DEBUG
  892. {
  893. xfs_agf_t *agf;
  894. xfs_alloc_rec_t *p;
  895. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  896. for (p = &rp[rfirst - 1]; p <= &rp[rlast - 1]; p++)
  897. ASSERT(be32_to_cpu(p->ar_startblock) +
  898. be32_to_cpu(p->ar_blockcount) <=
  899. be32_to_cpu(agf->agf_length));
  900. }
  901. #endif
  902. first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
  903. last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
  904. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  905. }
  906. /*
  907. * Lookup the record. The cursor is made to point to it, based on dir.
  908. * Return 0 if can't find any such record, 1 for success.
  909. */
  910. STATIC int /* error */
  911. xfs_alloc_lookup(
  912. xfs_btree_cur_t *cur, /* btree cursor */
  913. xfs_lookup_t dir, /* <=, ==, or >= */
  914. int *stat) /* success/failure */
  915. {
  916. xfs_agblock_t agbno; /* a.g. relative btree block number */
  917. xfs_agnumber_t agno; /* allocation group number */
  918. xfs_alloc_block_t *block=NULL; /* current btree block */
  919. int diff; /* difference for the current key */
  920. int error; /* error return value */
  921. int keyno=0; /* current key number */
  922. int level; /* level in the btree */
  923. xfs_mount_t *mp; /* file system mount point */
  924. XFS_STATS_INC(xs_abt_lookup);
  925. /*
  926. * Get the allocation group header, and the root block number.
  927. */
  928. mp = cur->bc_mp;
  929. {
  930. xfs_agf_t *agf; /* a.g. freespace header */
  931. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  932. agno = be32_to_cpu(agf->agf_seqno);
  933. agbno = be32_to_cpu(agf->agf_roots[cur->bc_btnum]);
  934. }
  935. /*
  936. * Iterate over each level in the btree, starting at the root.
  937. * For each level above the leaves, find the key we need, based
  938. * on the lookup record, then follow the corresponding block
  939. * pointer down to the next level.
  940. */
  941. for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
  942. xfs_buf_t *bp; /* buffer pointer for btree block */
  943. xfs_daddr_t d; /* disk address of btree block */
  944. /*
  945. * Get the disk address we're looking for.
  946. */
  947. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  948. /*
  949. * If the old buffer at this level is for a different block,
  950. * throw it away, otherwise just use it.
  951. */
  952. bp = cur->bc_bufs[level];
  953. if (bp && XFS_BUF_ADDR(bp) != d)
  954. bp = (xfs_buf_t *)0;
  955. if (!bp) {
  956. /*
  957. * Need to get a new buffer. Read it, then
  958. * set it in the cursor, releasing the old one.
  959. */
  960. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp, agno,
  961. agbno, 0, &bp, XFS_ALLOC_BTREE_REF)))
  962. return error;
  963. xfs_btree_setbuf(cur, level, bp);
  964. /*
  965. * Point to the btree block, now that we have the buffer
  966. */
  967. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  968. if ((error = xfs_btree_check_sblock(cur, block, level,
  969. bp)))
  970. return error;
  971. } else
  972. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  973. /*
  974. * If we already had a key match at a higher level, we know
  975. * we need to use the first entry in this block.
  976. */
  977. if (diff == 0)
  978. keyno = 1;
  979. /*
  980. * Otherwise we need to search this block. Do a binary search.
  981. */
  982. else {
  983. int high; /* high entry number */
  984. xfs_alloc_key_t *kkbase=NULL;/* base of keys in block */
  985. xfs_alloc_rec_t *krbase=NULL;/* base of records in block */
  986. int low; /* low entry number */
  987. /*
  988. * Get a pointer to keys or records.
  989. */
  990. if (level > 0)
  991. kkbase = XFS_ALLOC_KEY_ADDR(block, 1, cur);
  992. else
  993. krbase = XFS_ALLOC_REC_ADDR(block, 1, cur);
  994. /*
  995. * Set low and high entry numbers, 1-based.
  996. */
  997. low = 1;
  998. if (!(high = be16_to_cpu(block->bb_numrecs))) {
  999. /*
  1000. * If the block is empty, the tree must
  1001. * be an empty leaf.
  1002. */
  1003. ASSERT(level == 0 && cur->bc_nlevels == 1);
  1004. cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
  1005. *stat = 0;
  1006. return 0;
  1007. }
  1008. /*
  1009. * Binary search the block.
  1010. */
  1011. while (low <= high) {
  1012. xfs_extlen_t blockcount; /* key value */
  1013. xfs_agblock_t startblock; /* key value */
  1014. XFS_STATS_INC(xs_abt_compare);
  1015. /*
  1016. * keyno is average of low and high.
  1017. */
  1018. keyno = (low + high) >> 1;
  1019. /*
  1020. * Get startblock & blockcount.
  1021. */
  1022. if (level > 0) {
  1023. xfs_alloc_key_t *kkp;
  1024. kkp = kkbase + keyno - 1;
  1025. startblock = be32_to_cpu(kkp->ar_startblock);
  1026. blockcount = be32_to_cpu(kkp->ar_blockcount);
  1027. } else {
  1028. xfs_alloc_rec_t *krp;
  1029. krp = krbase + keyno - 1;
  1030. startblock = be32_to_cpu(krp->ar_startblock);
  1031. blockcount = be32_to_cpu(krp->ar_blockcount);
  1032. }
  1033. /*
  1034. * Compute difference to get next direction.
  1035. */
  1036. if (cur->bc_btnum == XFS_BTNUM_BNO)
  1037. diff = (int)startblock -
  1038. (int)cur->bc_rec.a.ar_startblock;
  1039. else if (!(diff = (int)blockcount -
  1040. (int)cur->bc_rec.a.ar_blockcount))
  1041. diff = (int)startblock -
  1042. (int)cur->bc_rec.a.ar_startblock;
  1043. /*
  1044. * Less than, move right.
  1045. */
  1046. if (diff < 0)
  1047. low = keyno + 1;
  1048. /*
  1049. * Greater than, move left.
  1050. */
  1051. else if (diff > 0)
  1052. high = keyno - 1;
  1053. /*
  1054. * Equal, we're done.
  1055. */
  1056. else
  1057. break;
  1058. }
  1059. }
  1060. /*
  1061. * If there are more levels, set up for the next level
  1062. * by getting the block number and filling in the cursor.
  1063. */
  1064. if (level > 0) {
  1065. /*
  1066. * If we moved left, need the previous key number,
  1067. * unless there isn't one.
  1068. */
  1069. if (diff > 0 && --keyno < 1)
  1070. keyno = 1;
  1071. agbno = be32_to_cpu(*XFS_ALLOC_PTR_ADDR(block, keyno, cur));
  1072. #ifdef DEBUG
  1073. if ((error = xfs_btree_check_sptr(cur, agbno, level)))
  1074. return error;
  1075. #endif
  1076. cur->bc_ptrs[level] = keyno;
  1077. }
  1078. }
  1079. /*
  1080. * Done with the search.
  1081. * See if we need to adjust the results.
  1082. */
  1083. if (dir != XFS_LOOKUP_LE && diff < 0) {
  1084. keyno++;
  1085. /*
  1086. * If ge search and we went off the end of the block, but it's
  1087. * not the last block, we're in the wrong block.
  1088. */
  1089. if (dir == XFS_LOOKUP_GE &&
  1090. keyno > be16_to_cpu(block->bb_numrecs) &&
  1091. be32_to_cpu(block->bb_rightsib) != NULLAGBLOCK) {
  1092. int i;
  1093. cur->bc_ptrs[0] = keyno;
  1094. if ((error = xfs_alloc_increment(cur, 0, &i)))
  1095. return error;
  1096. XFS_WANT_CORRUPTED_RETURN(i == 1);
  1097. *stat = 1;
  1098. return 0;
  1099. }
  1100. }
  1101. else if (dir == XFS_LOOKUP_LE && diff > 0)
  1102. keyno--;
  1103. cur->bc_ptrs[0] = keyno;
  1104. /*
  1105. * Return if we succeeded or not.
  1106. */
  1107. if (keyno == 0 || keyno > be16_to_cpu(block->bb_numrecs))
  1108. *stat = 0;
  1109. else
  1110. *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
  1111. return 0;
  1112. }
  1113. /*
  1114. * Move 1 record left from cur/level if possible.
  1115. * Update cur to reflect the new path.
  1116. */
  1117. STATIC int /* error */
  1118. xfs_alloc_lshift(
  1119. xfs_btree_cur_t *cur, /* btree cursor */
  1120. int level, /* level to shift record on */
  1121. int *stat) /* success/failure */
  1122. {
  1123. int error; /* error return value */
  1124. #ifdef DEBUG
  1125. int i; /* loop index */
  1126. #endif
  1127. xfs_alloc_key_t key; /* key value for leaf level upward */
  1128. xfs_buf_t *lbp; /* buffer for left neighbor block */
  1129. xfs_alloc_block_t *left; /* left neighbor btree block */
  1130. int nrec; /* new number of left block entries */
  1131. xfs_buf_t *rbp; /* buffer for right (current) block */
  1132. xfs_alloc_block_t *right; /* right (current) btree block */
  1133. xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
  1134. xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
  1135. xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */
  1136. /*
  1137. * Set up variables for this block as "right".
  1138. */
  1139. rbp = cur->bc_bufs[level];
  1140. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  1141. #ifdef DEBUG
  1142. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  1143. return error;
  1144. #endif
  1145. /*
  1146. * If we've got no left sibling then we can't shift an entry left.
  1147. */
  1148. if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) {
  1149. *stat = 0;
  1150. return 0;
  1151. }
  1152. /*
  1153. * If the cursor entry is the one that would be moved, don't
  1154. * do it... it's too complicated.
  1155. */
  1156. if (cur->bc_ptrs[level] <= 1) {
  1157. *stat = 0;
  1158. return 0;
  1159. }
  1160. /*
  1161. * Set up the left neighbor as "left".
  1162. */
  1163. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1164. cur->bc_private.a.agno, be32_to_cpu(right->bb_leftsib),
  1165. 0, &lbp, XFS_ALLOC_BTREE_REF)))
  1166. return error;
  1167. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1168. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  1169. return error;
  1170. /*
  1171. * If it's full, it can't take another entry.
  1172. */
  1173. if (be16_to_cpu(left->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  1174. *stat = 0;
  1175. return 0;
  1176. }
  1177. nrec = be16_to_cpu(left->bb_numrecs) + 1;
  1178. /*
  1179. * If non-leaf, copy a key and a ptr to the left block.
  1180. */
  1181. if (level > 0) {
  1182. xfs_alloc_key_t *lkp; /* key pointer for left block */
  1183. xfs_alloc_ptr_t *lpp; /* address pointer for left block */
  1184. lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
  1185. rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
  1186. *lkp = *rkp;
  1187. xfs_alloc_log_keys(cur, lbp, nrec, nrec);
  1188. lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
  1189. rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
  1190. #ifdef DEBUG
  1191. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level)))
  1192. return error;
  1193. #endif
  1194. *lpp = *rpp; /* INT_: copy */
  1195. xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
  1196. xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
  1197. }
  1198. /*
  1199. * If leaf, copy a record to the left block.
  1200. */
  1201. else {
  1202. xfs_alloc_rec_t *lrp; /* record pointer for left block */
  1203. lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
  1204. rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  1205. *lrp = *rrp;
  1206. xfs_alloc_log_recs(cur, lbp, nrec, nrec);
  1207. xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
  1208. }
  1209. /*
  1210. * Bump and log left's numrecs, decrement and log right's numrecs.
  1211. */
  1212. be16_add(&left->bb_numrecs, 1);
  1213. xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
  1214. be16_add(&right->bb_numrecs, -1);
  1215. xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
  1216. /*
  1217. * Slide the contents of right down one entry.
  1218. */
  1219. if (level > 0) {
  1220. #ifdef DEBUG
  1221. for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
  1222. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]),
  1223. level)))
  1224. return error;
  1225. }
  1226. #endif
  1227. memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1228. memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1229. xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1230. xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1231. } else {
  1232. memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1233. xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1234. key.ar_startblock = rrp->ar_startblock;
  1235. key.ar_blockcount = rrp->ar_blockcount;
  1236. rkp = &key;
  1237. }
  1238. /*
  1239. * Update the parent key values of right.
  1240. */
  1241. if ((error = xfs_alloc_updkey(cur, rkp, level + 1)))
  1242. return error;
  1243. /*
  1244. * Slide the cursor value left one.
  1245. */
  1246. cur->bc_ptrs[level]--;
  1247. *stat = 1;
  1248. return 0;
  1249. }
  1250. /*
  1251. * Allocate a new root block, fill it in.
  1252. */
  1253. STATIC int /* error */
  1254. xfs_alloc_newroot(
  1255. xfs_btree_cur_t *cur, /* btree cursor */
  1256. int *stat) /* success/failure */
  1257. {
  1258. int error; /* error return value */
  1259. xfs_agblock_t lbno; /* left block number */
  1260. xfs_buf_t *lbp; /* left btree buffer */
  1261. xfs_alloc_block_t *left; /* left btree block */
  1262. xfs_mount_t *mp; /* mount structure */
  1263. xfs_agblock_t nbno; /* new block number */
  1264. xfs_buf_t *nbp; /* new (root) buffer */
  1265. xfs_alloc_block_t *new; /* new (root) btree block */
  1266. int nptr; /* new value for key index, 1 or 2 */
  1267. xfs_agblock_t rbno; /* right block number */
  1268. xfs_buf_t *rbp; /* right btree buffer */
  1269. xfs_alloc_block_t *right; /* right btree block */
  1270. mp = cur->bc_mp;
  1271. ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
  1272. /*
  1273. * Get a buffer from the freelist blocks, for the new root.
  1274. */
  1275. if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  1276. &nbno)))
  1277. return error;
  1278. /*
  1279. * None available, we fail.
  1280. */
  1281. if (nbno == NULLAGBLOCK) {
  1282. *stat = 0;
  1283. return 0;
  1284. }
  1285. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  1286. nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
  1287. 0);
  1288. new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
  1289. /*
  1290. * Set the root data in the a.g. freespace structure.
  1291. */
  1292. {
  1293. xfs_agf_t *agf; /* a.g. freespace header */
  1294. xfs_agnumber_t seqno;
  1295. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  1296. agf->agf_roots[cur->bc_btnum] = cpu_to_be32(nbno);
  1297. be32_add(&agf->agf_levels[cur->bc_btnum], 1);
  1298. seqno = be32_to_cpu(agf->agf_seqno);
  1299. mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
  1300. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  1301. XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  1302. }
  1303. /*
  1304. * At the previous root level there are now two blocks: the old
  1305. * root, and the new block generated when it was split.
  1306. * We don't know which one the cursor is pointing at, so we
  1307. * set up variables "left" and "right" for each case.
  1308. */
  1309. lbp = cur->bc_bufs[cur->bc_nlevels - 1];
  1310. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1311. #ifdef DEBUG
  1312. if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
  1313. return error;
  1314. #endif
  1315. if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
  1316. /*
  1317. * Our block is left, pick up the right block.
  1318. */
  1319. lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
  1320. rbno = be32_to_cpu(left->bb_rightsib);
  1321. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  1322. cur->bc_private.a.agno, rbno, 0, &rbp,
  1323. XFS_ALLOC_BTREE_REF)))
  1324. return error;
  1325. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  1326. if ((error = xfs_btree_check_sblock(cur, right,
  1327. cur->bc_nlevels - 1, rbp)))
  1328. return error;
  1329. nptr = 1;
  1330. } else {
  1331. /*
  1332. * Our block is right, pick up the left block.
  1333. */
  1334. rbp = lbp;
  1335. right = left;
  1336. rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
  1337. lbno = be32_to_cpu(right->bb_leftsib);
  1338. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  1339. cur->bc_private.a.agno, lbno, 0, &lbp,
  1340. XFS_ALLOC_BTREE_REF)))
  1341. return error;
  1342. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1343. if ((error = xfs_btree_check_sblock(cur, left,
  1344. cur->bc_nlevels - 1, lbp)))
  1345. return error;
  1346. nptr = 2;
  1347. }
  1348. /*
  1349. * Fill in the new block's btree header and log it.
  1350. */
  1351. new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  1352. new->bb_level = cpu_to_be16(cur->bc_nlevels);
  1353. new->bb_numrecs = cpu_to_be16(2);
  1354. new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  1355. new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  1356. xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
  1357. ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
  1358. /*
  1359. * Fill in the key data in the new root.
  1360. */
  1361. {
  1362. xfs_alloc_key_t *kp; /* btree key pointer */
  1363. kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
  1364. if (be16_to_cpu(left->bb_level) > 0) {
  1365. kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur); /* INT_: structure copy */
  1366. kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);/* INT_: structure copy */
  1367. } else {
  1368. xfs_alloc_rec_t *rp; /* btree record pointer */
  1369. rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
  1370. kp[0].ar_startblock = rp->ar_startblock;
  1371. kp[0].ar_blockcount = rp->ar_blockcount;
  1372. rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  1373. kp[1].ar_startblock = rp->ar_startblock;
  1374. kp[1].ar_blockcount = rp->ar_blockcount;
  1375. }
  1376. }
  1377. xfs_alloc_log_keys(cur, nbp, 1, 2);
  1378. /*
  1379. * Fill in the pointer data in the new root.
  1380. */
  1381. {
  1382. xfs_alloc_ptr_t *pp; /* btree address pointer */
  1383. pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
  1384. pp[0] = cpu_to_be32(lbno);
  1385. pp[1] = cpu_to_be32(rbno);
  1386. }
  1387. xfs_alloc_log_ptrs(cur, nbp, 1, 2);
  1388. /*
  1389. * Fix up the cursor.
  1390. */
  1391. xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
  1392. cur->bc_ptrs[cur->bc_nlevels] = nptr;
  1393. cur->bc_nlevels++;
  1394. *stat = 1;
  1395. return 0;
  1396. }
  1397. /*
  1398. * Move 1 record right from cur/level if possible.
  1399. * Update cur to reflect the new path.
  1400. */
  1401. STATIC int /* error */
  1402. xfs_alloc_rshift(
  1403. xfs_btree_cur_t *cur, /* btree cursor */
  1404. int level, /* level to shift record on */
  1405. int *stat) /* success/failure */
  1406. {
  1407. int error; /* error return value */
  1408. int i; /* loop index */
  1409. xfs_alloc_key_t key; /* key value for leaf level upward */
  1410. xfs_buf_t *lbp; /* buffer for left (current) block */
  1411. xfs_alloc_block_t *left; /* left (current) btree block */
  1412. xfs_buf_t *rbp; /* buffer for right neighbor block */
  1413. xfs_alloc_block_t *right; /* right neighbor btree block */
  1414. xfs_alloc_key_t *rkp; /* key pointer for right block */
  1415. xfs_btree_cur_t *tcur; /* temporary cursor */
  1416. /*
  1417. * Set up variables for this block as "left".
  1418. */
  1419. lbp = cur->bc_bufs[level];
  1420. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1421. #ifdef DEBUG
  1422. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  1423. return error;
  1424. #endif
  1425. /*
  1426. * If we've got no right sibling then we can't shift an entry right.
  1427. */
  1428. if (be32_to_cpu(left->bb_rightsib) == NULLAGBLOCK) {
  1429. *stat = 0;
  1430. return 0;
  1431. }
  1432. /*
  1433. * If the cursor entry is the one that would be moved, don't
  1434. * do it... it's too complicated.
  1435. */
  1436. if (cur->bc_ptrs[level] >= be16_to_cpu(left->bb_numrecs)) {
  1437. *stat = 0;
  1438. return 0;
  1439. }
  1440. /*
  1441. * Set up the right neighbor as "right".
  1442. */
  1443. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1444. cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib),
  1445. 0, &rbp, XFS_ALLOC_BTREE_REF)))
  1446. return error;
  1447. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  1448. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  1449. return error;
  1450. /*
  1451. * If it's full, it can't take another entry.
  1452. */
  1453. if (be16_to_cpu(right->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  1454. *stat = 0;
  1455. return 0;
  1456. }
  1457. /*
  1458. * Make a hole at the start of the right neighbor block, then
  1459. * copy the last left block entry to the hole.
  1460. */
  1461. if (level > 0) {
  1462. xfs_alloc_key_t *lkp; /* key pointer for left block */
  1463. xfs_alloc_ptr_t *lpp; /* address pointer for left block */
  1464. xfs_alloc_ptr_t *rpp; /* address pointer for right block */
  1465. lkp = XFS_ALLOC_KEY_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
  1466. lpp = XFS_ALLOC_PTR_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
  1467. rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
  1468. rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
  1469. #ifdef DEBUG
  1470. for (i = be16_to_cpu(right->bb_numrecs) - 1; i >= 0; i--) {
  1471. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
  1472. return error;
  1473. }
  1474. #endif
  1475. memmove(rkp + 1, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1476. memmove(rpp + 1, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1477. #ifdef DEBUG
  1478. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*lpp), level)))
  1479. return error;
  1480. #endif
  1481. *rkp = *lkp; /* INT_: copy */
  1482. *rpp = *lpp; /* INT_: copy */
  1483. xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
  1484. xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
  1485. xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
  1486. } else {
  1487. xfs_alloc_rec_t *lrp; /* record pointer for left block */
  1488. xfs_alloc_rec_t *rrp; /* record pointer for right block */
  1489. lrp = XFS_ALLOC_REC_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
  1490. rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  1491. memmove(rrp + 1, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1492. *rrp = *lrp;
  1493. xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
  1494. key.ar_startblock = rrp->ar_startblock;
  1495. key.ar_blockcount = rrp->ar_blockcount;
  1496. rkp = &key;
  1497. xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
  1498. }
  1499. /*
  1500. * Decrement and log left's numrecs, bump and log right's numrecs.
  1501. */
  1502. be16_add(&left->bb_numrecs, -1);
  1503. xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
  1504. be16_add(&right->bb_numrecs, 1);
  1505. xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
  1506. /*
  1507. * Using a temporary cursor, update the parent key values of the
  1508. * block on the right.
  1509. */
  1510. if ((error = xfs_btree_dup_cursor(cur, &tcur)))
  1511. return error;
  1512. i = xfs_btree_lastrec(tcur, level);
  1513. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1514. if ((error = xfs_alloc_increment(tcur, level, &i)) ||
  1515. (error = xfs_alloc_updkey(tcur, rkp, level + 1)))
  1516. goto error0;
  1517. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  1518. *stat = 1;
  1519. return 0;
  1520. error0:
  1521. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  1522. return error;
  1523. }
  1524. /*
  1525. * Split cur/level block in half.
  1526. * Return new block number and its first record (to be inserted into parent).
  1527. */
  1528. STATIC int /* error */
  1529. xfs_alloc_split(
  1530. xfs_btree_cur_t *cur, /* btree cursor */
  1531. int level, /* level to split */
  1532. xfs_agblock_t *bnop, /* output: block number allocated */
  1533. xfs_alloc_key_t *keyp, /* output: first key of new block */
  1534. xfs_btree_cur_t **curp, /* output: new cursor */
  1535. int *stat) /* success/failure */
  1536. {
  1537. int error; /* error return value */
  1538. int i; /* loop index/record number */
  1539. xfs_agblock_t lbno; /* left (current) block number */
  1540. xfs_buf_t *lbp; /* buffer for left block */
  1541. xfs_alloc_block_t *left; /* left (current) btree block */
  1542. xfs_agblock_t rbno; /* right (new) block number */
  1543. xfs_buf_t *rbp; /* buffer for right block */
  1544. xfs_alloc_block_t *right; /* right (new) btree block */
  1545. /*
  1546. * Allocate the new block from the freelist.
  1547. * If we can't do it, we're toast. Give up.
  1548. */
  1549. if ((error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
  1550. &rbno)))
  1551. return error;
  1552. if (rbno == NULLAGBLOCK) {
  1553. *stat = 0;
  1554. return 0;
  1555. }
  1556. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  1557. rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
  1558. rbno, 0);
  1559. /*
  1560. * Set up the new block as "right".
  1561. */
  1562. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  1563. /*
  1564. * "Left" is the current (according to the cursor) block.
  1565. */
  1566. lbp = cur->bc_bufs[level];
  1567. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1568. #ifdef DEBUG
  1569. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  1570. return error;
  1571. #endif
  1572. /*
  1573. * Fill in the btree header for the new block.
  1574. */
  1575. right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  1576. right->bb_level = left->bb_level;
  1577. right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
  1578. /*
  1579. * Make sure that if there's an odd number of entries now, that
  1580. * each new block will have the same number of entries.
  1581. */
  1582. if ((be16_to_cpu(left->bb_numrecs) & 1) &&
  1583. cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
  1584. be16_add(&right->bb_numrecs, 1);
  1585. i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
  1586. /*
  1587. * For non-leaf blocks, copy keys and addresses over to the new block.
  1588. */
  1589. if (level > 0) {
  1590. xfs_alloc_key_t *lkp; /* left btree key pointer */
  1591. xfs_alloc_ptr_t *lpp; /* left btree address pointer */
  1592. xfs_alloc_key_t *rkp; /* right btree key pointer */
  1593. xfs_alloc_ptr_t *rpp; /* right btree address pointer */
  1594. lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
  1595. lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
  1596. rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
  1597. rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
  1598. #ifdef DEBUG
  1599. for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
  1600. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
  1601. return error;
  1602. }
  1603. #endif
  1604. memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1605. memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1606. xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1607. xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1608. *keyp = *rkp;
  1609. }
  1610. /*
  1611. * For leaf blocks, copy records over to the new block.
  1612. */
  1613. else {
  1614. xfs_alloc_rec_t *lrp; /* left btree record pointer */
  1615. xfs_alloc_rec_t *rrp; /* right btree record pointer */
  1616. lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
  1617. rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  1618. memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1619. xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1620. keyp->ar_startblock = rrp->ar_startblock;
  1621. keyp->ar_blockcount = rrp->ar_blockcount;
  1622. }
  1623. /*
  1624. * Find the left block number by looking in the buffer.
  1625. * Adjust numrecs, sibling pointers.
  1626. */
  1627. lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
  1628. be16_add(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
  1629. right->bb_rightsib = left->bb_rightsib;
  1630. left->bb_rightsib = cpu_to_be32(rbno);
  1631. right->bb_leftsib = cpu_to_be32(lbno);
  1632. xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
  1633. xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  1634. /*
  1635. * If there's a block to the new block's right, make that block
  1636. * point back to right instead of to left.
  1637. */
  1638. if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) {
  1639. xfs_alloc_block_t *rrblock; /* rr btree block */
  1640. xfs_buf_t *rrbp; /* buffer for rrblock */
  1641. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1642. cur->bc_private.a.agno, be32_to_cpu(right->bb_rightsib), 0,
  1643. &rrbp, XFS_ALLOC_BTREE_REF)))
  1644. return error;
  1645. rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
  1646. if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
  1647. return error;
  1648. rrblock->bb_leftsib = cpu_to_be32(rbno);
  1649. xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
  1650. }
  1651. /*
  1652. * If the cursor is really in the right block, move it there.
  1653. * If it's just pointing past the last entry in left, then we'll
  1654. * insert there, so don't change anything in that case.
  1655. */
  1656. if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
  1657. xfs_btree_setbuf(cur, level, rbp);
  1658. cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
  1659. }
  1660. /*
  1661. * If there are more levels, we'll need another cursor which refers to
  1662. * the right block, no matter where this cursor was.
  1663. */
  1664. if (level + 1 < cur->bc_nlevels) {
  1665. if ((error = xfs_btree_dup_cursor(cur, curp)))
  1666. return error;
  1667. (*curp)->bc_ptrs[level + 1]++;
  1668. }
  1669. *bnop = rbno;
  1670. *stat = 1;
  1671. return 0;
  1672. }
  1673. /*
  1674. * Update keys at all levels from here to the root along the cursor's path.
  1675. */
  1676. STATIC int /* error */
  1677. xfs_alloc_updkey(
  1678. xfs_btree_cur_t *cur, /* btree cursor */
  1679. xfs_alloc_key_t *keyp, /* new key value to update to */
  1680. int level) /* starting level for update */
  1681. {
  1682. int ptr; /* index of key in block */
  1683. /*
  1684. * Go up the tree from this level toward the root.
  1685. * At each level, update the key value to the value input.
  1686. * Stop when we reach a level where the cursor isn't pointing
  1687. * at the first entry in the block.
  1688. */
  1689. for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
  1690. xfs_alloc_block_t *block; /* btree block */
  1691. xfs_buf_t *bp; /* buffer for block */
  1692. #ifdef DEBUG
  1693. int error; /* error return value */
  1694. #endif
  1695. xfs_alloc_key_t *kp; /* ptr to btree block keys */
  1696. bp = cur->bc_bufs[level];
  1697. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  1698. #ifdef DEBUG
  1699. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  1700. return error;
  1701. #endif
  1702. ptr = cur->bc_ptrs[level];
  1703. kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
  1704. *kp = *keyp;
  1705. xfs_alloc_log_keys(cur, bp, ptr, ptr);
  1706. }
  1707. return 0;
  1708. }
  1709. /*
  1710. * Externally visible routines.
  1711. */
  1712. /*
  1713. * Decrement cursor by one record at the level.
  1714. * For nonzero levels the leaf-ward information is untouched.
  1715. */
  1716. int /* error */
  1717. xfs_alloc_decrement(
  1718. xfs_btree_cur_t *cur, /* btree cursor */
  1719. int level, /* level in btree, 0 is leaf */
  1720. int *stat) /* success/failure */
  1721. {
  1722. xfs_alloc_block_t *block; /* btree block */
  1723. int error; /* error return value */
  1724. int lev; /* btree level */
  1725. ASSERT(level < cur->bc_nlevels);
  1726. /*
  1727. * Read-ahead to the left at this level.
  1728. */
  1729. xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
  1730. /*
  1731. * Decrement the ptr at this level. If we're still in the block
  1732. * then we're done.
  1733. */
  1734. if (--cur->bc_ptrs[level] > 0) {
  1735. *stat = 1;
  1736. return 0;
  1737. }
  1738. /*
  1739. * Get a pointer to the btree block.
  1740. */
  1741. block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[level]);
  1742. #ifdef DEBUG
  1743. if ((error = xfs_btree_check_sblock(cur, block, level,
  1744. cur->bc_bufs[level])))
  1745. return error;
  1746. #endif
  1747. /*
  1748. * If we just went off the left edge of the tree, return failure.
  1749. */
  1750. if (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK) {
  1751. *stat = 0;
  1752. return 0;
  1753. }
  1754. /*
  1755. * March up the tree decrementing pointers.
  1756. * Stop when we don't go off the left edge of a block.
  1757. */
  1758. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1759. if (--cur->bc_ptrs[lev] > 0)
  1760. break;
  1761. /*
  1762. * Read-ahead the left block, we're going to read it
  1763. * in the next loop.
  1764. */
  1765. xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
  1766. }
  1767. /*
  1768. * If we went off the root then we are seriously confused.
  1769. */
  1770. ASSERT(lev < cur->bc_nlevels);
  1771. /*
  1772. * Now walk back down the tree, fixing up the cursor's buffer
  1773. * pointers and key numbers.
  1774. */
  1775. for (block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[lev]); lev > level; ) {
  1776. xfs_agblock_t agbno; /* block number of btree block */
  1777. xfs_buf_t *bp; /* buffer pointer for block */
  1778. agbno = be32_to_cpu(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur));
  1779. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1780. cur->bc_private.a.agno, agbno, 0, &bp,
  1781. XFS_ALLOC_BTREE_REF)))
  1782. return error;
  1783. lev--;
  1784. xfs_btree_setbuf(cur, lev, bp);
  1785. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  1786. if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
  1787. return error;
  1788. cur->bc_ptrs[lev] = be16_to_cpu(block->bb_numrecs);
  1789. }
  1790. *stat = 1;
  1791. return 0;
  1792. }
  1793. /*
  1794. * Delete the record pointed to by cur.
  1795. * The cursor refers to the place where the record was (could be inserted)
  1796. * when the operation returns.
  1797. */
  1798. int /* error */
  1799. xfs_alloc_delete(
  1800. xfs_btree_cur_t *cur, /* btree cursor */
  1801. int *stat) /* success/failure */
  1802. {
  1803. int error; /* error return value */
  1804. int i; /* result code */
  1805. int level; /* btree level */
  1806. /*
  1807. * Go up the tree, starting at leaf level.
  1808. * If 2 is returned then a join was done; go to the next level.
  1809. * Otherwise we are done.
  1810. */
  1811. for (level = 0, i = 2; i == 2; level++) {
  1812. if ((error = xfs_alloc_delrec(cur, level, &i)))
  1813. return error;
  1814. }
  1815. if (i == 0) {
  1816. for (level = 1; level < cur->bc_nlevels; level++) {
  1817. if (cur->bc_ptrs[level] == 0) {
  1818. if ((error = xfs_alloc_decrement(cur, level, &i)))
  1819. return error;
  1820. break;
  1821. }
  1822. }
  1823. }
  1824. *stat = i;
  1825. return 0;
  1826. }
  1827. /*
  1828. * Get the data from the pointed-to record.
  1829. */
  1830. int /* error */
  1831. xfs_alloc_get_rec(
  1832. xfs_btree_cur_t *cur, /* btree cursor */
  1833. xfs_agblock_t *bno, /* output: starting block of extent */
  1834. xfs_extlen_t *len, /* output: length of extent */
  1835. int *stat) /* output: success/failure */
  1836. {
  1837. xfs_alloc_block_t *block; /* btree block */
  1838. #ifdef DEBUG
  1839. int error; /* error return value */
  1840. #endif
  1841. int ptr; /* record number */
  1842. ptr = cur->bc_ptrs[0];
  1843. block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
  1844. #ifdef DEBUG
  1845. if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
  1846. return error;
  1847. #endif
  1848. /*
  1849. * Off the right end or left end, return failure.
  1850. */
  1851. if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
  1852. *stat = 0;
  1853. return 0;
  1854. }
  1855. /*
  1856. * Point to the record and extract its data.
  1857. */
  1858. {
  1859. xfs_alloc_rec_t *rec; /* record data */
  1860. rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
  1861. *bno = be32_to_cpu(rec->ar_startblock);
  1862. *len = be32_to_cpu(rec->ar_blockcount);
  1863. }
  1864. *stat = 1;
  1865. return 0;
  1866. }
  1867. /*
  1868. * Increment cursor by one record at the level.
  1869. * For nonzero levels the leaf-ward information is untouched.
  1870. */
  1871. int /* error */
  1872. xfs_alloc_increment(
  1873. xfs_btree_cur_t *cur, /* btree cursor */
  1874. int level, /* level in btree, 0 is leaf */
  1875. int *stat) /* success/failure */
  1876. {
  1877. xfs_alloc_block_t *block; /* btree block */
  1878. xfs_buf_t *bp; /* tree block buffer */
  1879. int error; /* error return value */
  1880. int lev; /* btree level */
  1881. ASSERT(level < cur->bc_nlevels);
  1882. /*
  1883. * Read-ahead to the right at this level.
  1884. */
  1885. xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
  1886. /*
  1887. * Get a pointer to the btree block.
  1888. */
  1889. bp = cur->bc_bufs[level];
  1890. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  1891. #ifdef DEBUG
  1892. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  1893. return error;
  1894. #endif
  1895. /*
  1896. * Increment the ptr at this level. If we're still in the block
  1897. * then we're done.
  1898. */
  1899. if (++cur->bc_ptrs[level] <= be16_to_cpu(block->bb_numrecs)) {
  1900. *stat = 1;
  1901. return 0;
  1902. }
  1903. /*
  1904. * If we just went off the right edge of the tree, return failure.
  1905. */
  1906. if (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK) {
  1907. *stat = 0;
  1908. return 0;
  1909. }
  1910. /*
  1911. * March up the tree incrementing pointers.
  1912. * Stop when we don't go off the right edge of a block.
  1913. */
  1914. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1915. bp = cur->bc_bufs[lev];
  1916. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  1917. #ifdef DEBUG
  1918. if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
  1919. return error;
  1920. #endif
  1921. if (++cur->bc_ptrs[lev] <= be16_to_cpu(block->bb_numrecs))
  1922. break;
  1923. /*
  1924. * Read-ahead the right block, we're going to read it
  1925. * in the next loop.
  1926. */
  1927. xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
  1928. }
  1929. /*
  1930. * If we went off the root then we are seriously confused.
  1931. */
  1932. ASSERT(lev < cur->bc_nlevels);
  1933. /*
  1934. * Now walk back down the tree, fixing up the cursor's buffer
  1935. * pointers and key numbers.
  1936. */
  1937. for (bp = cur->bc_bufs[lev], block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  1938. lev > level; ) {
  1939. xfs_agblock_t agbno; /* block number of btree block */
  1940. agbno = be32_to_cpu(*XFS_ALLOC_PTR_ADDR(block, cur->bc_ptrs[lev], cur));
  1941. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1942. cur->bc_private.a.agno, agbno, 0, &bp,
  1943. XFS_ALLOC_BTREE_REF)))
  1944. return error;
  1945. lev--;
  1946. xfs_btree_setbuf(cur, lev, bp);
  1947. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  1948. if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
  1949. return error;
  1950. cur->bc_ptrs[lev] = 1;
  1951. }
  1952. *stat = 1;
  1953. return 0;
  1954. }
  1955. /*
  1956. * Insert the current record at the point referenced by cur.
  1957. * The cursor may be inconsistent on return if splits have been done.
  1958. */
  1959. int /* error */
  1960. xfs_alloc_insert(
  1961. xfs_btree_cur_t *cur, /* btree cursor */
  1962. int *stat) /* success/failure */
  1963. {
  1964. int error; /* error return value */
  1965. int i; /* result value, 0 for failure */
  1966. int level; /* current level number in btree */
  1967. xfs_agblock_t nbno; /* new block number (split result) */
  1968. xfs_btree_cur_t *ncur; /* new cursor (split result) */
  1969. xfs_alloc_rec_t nrec; /* record being inserted this level */
  1970. xfs_btree_cur_t *pcur; /* previous level's cursor */
  1971. level = 0;
  1972. nbno = NULLAGBLOCK;
  1973. nrec.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  1974. nrec.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  1975. ncur = (xfs_btree_cur_t *)0;
  1976. pcur = cur;
  1977. /*
  1978. * Loop going up the tree, starting at the leaf level.
  1979. * Stop when we don't get a split block, that must mean that
  1980. * the insert is finished with this level.
  1981. */
  1982. do {
  1983. /*
  1984. * Insert nrec/nbno into this level of the tree.
  1985. * Note if we fail, nbno will be null.
  1986. */
  1987. if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
  1988. &i))) {
  1989. if (pcur != cur)
  1990. xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
  1991. return error;
  1992. }
  1993. /*
  1994. * See if the cursor we just used is trash.
  1995. * Can't trash the caller's cursor, but otherwise we should
  1996. * if ncur is a new cursor or we're about to be done.
  1997. */
  1998. if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
  1999. cur->bc_nlevels = pcur->bc_nlevels;
  2000. xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
  2001. }
  2002. /*
  2003. * If we got a new cursor, switch to it.
  2004. */
  2005. if (ncur) {
  2006. pcur = ncur;
  2007. ncur = (xfs_btree_cur_t *)0;
  2008. }
  2009. } while (nbno != NULLAGBLOCK);
  2010. *stat = i;
  2011. return 0;
  2012. }
  2013. /*
  2014. * Lookup the record equal to [bno, len] in the btree given by cur.
  2015. */
  2016. int /* error */
  2017. xfs_alloc_lookup_eq(
  2018. xfs_btree_cur_t *cur, /* btree cursor */
  2019. xfs_agblock_t bno, /* starting block of extent */
  2020. xfs_extlen_t len, /* length of extent */
  2021. int *stat) /* success/failure */
  2022. {
  2023. cur->bc_rec.a.ar_startblock = bno;
  2024. cur->bc_rec.a.ar_blockcount = len;
  2025. return xfs_alloc_lookup(cur, XFS_LOOKUP_EQ, stat);
  2026. }
  2027. /*
  2028. * Lookup the first record greater than or equal to [bno, len]
  2029. * in the btree given by cur.
  2030. */
  2031. int /* error */
  2032. xfs_alloc_lookup_ge(
  2033. xfs_btree_cur_t *cur, /* btree cursor */
  2034. xfs_agblock_t bno, /* starting block of extent */
  2035. xfs_extlen_t len, /* length of extent */
  2036. int *stat) /* success/failure */
  2037. {
  2038. cur->bc_rec.a.ar_startblock = bno;
  2039. cur->bc_rec.a.ar_blockcount = len;
  2040. return xfs_alloc_lookup(cur, XFS_LOOKUP_GE, stat);
  2041. }
  2042. /*
  2043. * Lookup the first record less than or equal to [bno, len]
  2044. * in the btree given by cur.
  2045. */
  2046. int /* error */
  2047. xfs_alloc_lookup_le(
  2048. xfs_btree_cur_t *cur, /* btree cursor */
  2049. xfs_agblock_t bno, /* starting block of extent */
  2050. xfs_extlen_t len, /* length of extent */
  2051. int *stat) /* success/failure */
  2052. {
  2053. cur->bc_rec.a.ar_startblock = bno;
  2054. cur->bc_rec.a.ar_blockcount = len;
  2055. return xfs_alloc_lookup(cur, XFS_LOOKUP_LE, stat);
  2056. }
  2057. /*
  2058. * Update the record referred to by cur, to the value given by [bno, len].
  2059. * This either works (return 0) or gets an EFSCORRUPTED error.
  2060. */
  2061. int /* error */
  2062. xfs_alloc_update(
  2063. xfs_btree_cur_t *cur, /* btree cursor */
  2064. xfs_agblock_t bno, /* starting block of extent */
  2065. xfs_extlen_t len) /* length of extent */
  2066. {
  2067. xfs_alloc_block_t *block; /* btree block to update */
  2068. int error; /* error return value */
  2069. int ptr; /* current record number (updating) */
  2070. ASSERT(len > 0);
  2071. /*
  2072. * Pick up the a.g. freelist struct and the current block.
  2073. */
  2074. block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
  2075. #ifdef DEBUG
  2076. if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
  2077. return error;
  2078. #endif
  2079. /*
  2080. * Get the address of the rec to be updated.
  2081. */
  2082. ptr = cur->bc_ptrs[0];
  2083. {
  2084. xfs_alloc_rec_t *rp; /* pointer to updated record */
  2085. rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
  2086. /*
  2087. * Fill in the new contents and log them.
  2088. */
  2089. rp->ar_startblock = cpu_to_be32(bno);
  2090. rp->ar_blockcount = cpu_to_be32(len);
  2091. xfs_alloc_log_recs(cur, cur->bc_bufs[0], ptr, ptr);
  2092. }
  2093. /*
  2094. * If it's the by-size btree and it's the last leaf block and
  2095. * it's the last record... then update the size of the longest
  2096. * extent in the a.g., which we cache in the a.g. freelist header.
  2097. */
  2098. if (cur->bc_btnum == XFS_BTNUM_CNT &&
  2099. be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
  2100. ptr == be16_to_cpu(block->bb_numrecs)) {
  2101. xfs_agf_t *agf; /* a.g. freespace header */
  2102. xfs_agnumber_t seqno;
  2103. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  2104. seqno = be32_to_cpu(agf->agf_seqno);
  2105. cur->bc_mp->m_perag[seqno].pagf_longest = len;
  2106. agf->agf_longest = cpu_to_be32(len);
  2107. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  2108. XFS_AGF_LONGEST);
  2109. }
  2110. /*
  2111. * Updating first record in leaf. Pass new key value up to our parent.
  2112. */
  2113. if (ptr == 1) {
  2114. xfs_alloc_key_t key; /* key containing [bno, len] */
  2115. key.ar_startblock = cpu_to_be32(bno);
  2116. key.ar_blockcount = cpu_to_be32(len);
  2117. if ((error = xfs_alloc_updkey(cur, &key, 1)))
  2118. return error;
  2119. }
  2120. return 0;
  2121. }