xfs_alloc_btree.c 67 KB

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