xfs_alloc_btree.c 64 KB

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