xfs_alloc_btree.c 68 KB

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