xfs_alloc_btree.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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_cpu(&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_btree_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_btree_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_btree_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_btree_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_btree_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_btree_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_btree_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_btree_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. * Move 1 record left from cur/level if possible.
  916. * Update cur to reflect the new path.
  917. */
  918. STATIC int /* error */
  919. xfs_alloc_lshift(
  920. xfs_btree_cur_t *cur, /* btree cursor */
  921. int level, /* level to shift record on */
  922. int *stat) /* success/failure */
  923. {
  924. int error; /* error return value */
  925. #ifdef DEBUG
  926. int i; /* loop index */
  927. #endif
  928. xfs_alloc_key_t key; /* key value for leaf level upward */
  929. xfs_buf_t *lbp; /* buffer for left neighbor block */
  930. xfs_alloc_block_t *left; /* left neighbor btree block */
  931. int nrec; /* new number of left block entries */
  932. xfs_buf_t *rbp; /* buffer for right (current) block */
  933. xfs_alloc_block_t *right; /* right (current) btree block */
  934. xfs_alloc_key_t *rkp=NULL; /* key pointer for right block */
  935. xfs_alloc_ptr_t *rpp=NULL; /* address pointer for right block */
  936. xfs_alloc_rec_t *rrp=NULL; /* record pointer for right block */
  937. /*
  938. * Set up variables for this block as "right".
  939. */
  940. rbp = cur->bc_bufs[level];
  941. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  942. #ifdef DEBUG
  943. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  944. return error;
  945. #endif
  946. /*
  947. * If we've got no left sibling then we can't shift an entry left.
  948. */
  949. if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) {
  950. *stat = 0;
  951. return 0;
  952. }
  953. /*
  954. * If the cursor entry is the one that would be moved, don't
  955. * do it... it's too complicated.
  956. */
  957. if (cur->bc_ptrs[level] <= 1) {
  958. *stat = 0;
  959. return 0;
  960. }
  961. /*
  962. * Set up the left neighbor as "left".
  963. */
  964. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  965. cur->bc_private.a.agno, be32_to_cpu(right->bb_leftsib),
  966. 0, &lbp, XFS_ALLOC_BTREE_REF)))
  967. return error;
  968. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  969. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  970. return error;
  971. /*
  972. * If it's full, it can't take another entry.
  973. */
  974. if (be16_to_cpu(left->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  975. *stat = 0;
  976. return 0;
  977. }
  978. nrec = be16_to_cpu(left->bb_numrecs) + 1;
  979. /*
  980. * If non-leaf, copy a key and a ptr to the left block.
  981. */
  982. if (level > 0) {
  983. xfs_alloc_key_t *lkp; /* key pointer for left block */
  984. xfs_alloc_ptr_t *lpp; /* address pointer for left block */
  985. lkp = XFS_ALLOC_KEY_ADDR(left, nrec, cur);
  986. rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
  987. *lkp = *rkp;
  988. xfs_alloc_log_keys(cur, lbp, nrec, nrec);
  989. lpp = XFS_ALLOC_PTR_ADDR(left, nrec, cur);
  990. rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
  991. #ifdef DEBUG
  992. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level)))
  993. return error;
  994. #endif
  995. *lpp = *rpp;
  996. xfs_alloc_log_ptrs(cur, lbp, nrec, nrec);
  997. xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
  998. }
  999. /*
  1000. * If leaf, copy a record to the left block.
  1001. */
  1002. else {
  1003. xfs_alloc_rec_t *lrp; /* record pointer for left block */
  1004. lrp = XFS_ALLOC_REC_ADDR(left, nrec, cur);
  1005. rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  1006. *lrp = *rrp;
  1007. xfs_alloc_log_recs(cur, lbp, nrec, nrec);
  1008. xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
  1009. }
  1010. /*
  1011. * Bump and log left's numrecs, decrement and log right's numrecs.
  1012. */
  1013. be16_add_cpu(&left->bb_numrecs, 1);
  1014. xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
  1015. be16_add_cpu(&right->bb_numrecs, -1);
  1016. xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
  1017. /*
  1018. * Slide the contents of right down one entry.
  1019. */
  1020. if (level > 0) {
  1021. #ifdef DEBUG
  1022. for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
  1023. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]),
  1024. level)))
  1025. return error;
  1026. }
  1027. #endif
  1028. memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1029. memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1030. xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1031. xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1032. } else {
  1033. memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1034. xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1035. key.ar_startblock = rrp->ar_startblock;
  1036. key.ar_blockcount = rrp->ar_blockcount;
  1037. rkp = &key;
  1038. }
  1039. /*
  1040. * Update the parent key values of right.
  1041. */
  1042. if ((error = xfs_alloc_updkey(cur, rkp, level + 1)))
  1043. return error;
  1044. /*
  1045. * Slide the cursor value left one.
  1046. */
  1047. cur->bc_ptrs[level]--;
  1048. *stat = 1;
  1049. return 0;
  1050. }
  1051. /*
  1052. * Allocate a new root block, fill it in.
  1053. */
  1054. STATIC int /* error */
  1055. xfs_alloc_newroot(
  1056. xfs_btree_cur_t *cur, /* btree cursor */
  1057. int *stat) /* success/failure */
  1058. {
  1059. int error; /* error return value */
  1060. xfs_agblock_t lbno; /* left block number */
  1061. xfs_buf_t *lbp; /* left btree buffer */
  1062. xfs_alloc_block_t *left; /* left btree block */
  1063. xfs_mount_t *mp; /* mount structure */
  1064. xfs_agblock_t nbno; /* new block number */
  1065. xfs_buf_t *nbp; /* new (root) buffer */
  1066. xfs_alloc_block_t *new; /* new (root) btree block */
  1067. int nptr; /* new value for key index, 1 or 2 */
  1068. xfs_agblock_t rbno; /* right block number */
  1069. xfs_buf_t *rbp; /* right btree buffer */
  1070. xfs_alloc_block_t *right; /* right btree block */
  1071. mp = cur->bc_mp;
  1072. ASSERT(cur->bc_nlevels < XFS_AG_MAXLEVELS(mp));
  1073. /*
  1074. * Get a buffer from the freelist blocks, for the new root.
  1075. */
  1076. error = xfs_alloc_get_freelist(cur->bc_tp,
  1077. cur->bc_private.a.agbp, &nbno, 1);
  1078. if (error)
  1079. return error;
  1080. /*
  1081. * None available, we fail.
  1082. */
  1083. if (nbno == NULLAGBLOCK) {
  1084. *stat = 0;
  1085. return 0;
  1086. }
  1087. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  1088. nbp = xfs_btree_get_bufs(mp, cur->bc_tp, cur->bc_private.a.agno, nbno,
  1089. 0);
  1090. new = XFS_BUF_TO_ALLOC_BLOCK(nbp);
  1091. /*
  1092. * Set the root data in the a.g. freespace structure.
  1093. */
  1094. {
  1095. xfs_agf_t *agf; /* a.g. freespace header */
  1096. xfs_agnumber_t seqno;
  1097. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  1098. agf->agf_roots[cur->bc_btnum] = cpu_to_be32(nbno);
  1099. be32_add_cpu(&agf->agf_levels[cur->bc_btnum], 1);
  1100. seqno = be32_to_cpu(agf->agf_seqno);
  1101. mp->m_perag[seqno].pagf_levels[cur->bc_btnum]++;
  1102. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  1103. XFS_AGF_ROOTS | XFS_AGF_LEVELS);
  1104. }
  1105. /*
  1106. * At the previous root level there are now two blocks: the old
  1107. * root, and the new block generated when it was split.
  1108. * We don't know which one the cursor is pointing at, so we
  1109. * set up variables "left" and "right" for each case.
  1110. */
  1111. lbp = cur->bc_bufs[cur->bc_nlevels - 1];
  1112. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1113. #ifdef DEBUG
  1114. if ((error = xfs_btree_check_sblock(cur, left, cur->bc_nlevels - 1, lbp)))
  1115. return error;
  1116. #endif
  1117. if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
  1118. /*
  1119. * Our block is left, pick up the right block.
  1120. */
  1121. lbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(lbp));
  1122. rbno = be32_to_cpu(left->bb_rightsib);
  1123. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  1124. cur->bc_private.a.agno, rbno, 0, &rbp,
  1125. XFS_ALLOC_BTREE_REF)))
  1126. return error;
  1127. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  1128. if ((error = xfs_btree_check_sblock(cur, right,
  1129. cur->bc_nlevels - 1, rbp)))
  1130. return error;
  1131. nptr = 1;
  1132. } else {
  1133. /*
  1134. * Our block is right, pick up the left block.
  1135. */
  1136. rbp = lbp;
  1137. right = left;
  1138. rbno = XFS_DADDR_TO_AGBNO(mp, XFS_BUF_ADDR(rbp));
  1139. lbno = be32_to_cpu(right->bb_leftsib);
  1140. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  1141. cur->bc_private.a.agno, lbno, 0, &lbp,
  1142. XFS_ALLOC_BTREE_REF)))
  1143. return error;
  1144. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1145. if ((error = xfs_btree_check_sblock(cur, left,
  1146. cur->bc_nlevels - 1, lbp)))
  1147. return error;
  1148. nptr = 2;
  1149. }
  1150. /*
  1151. * Fill in the new block's btree header and log it.
  1152. */
  1153. new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  1154. new->bb_level = cpu_to_be16(cur->bc_nlevels);
  1155. new->bb_numrecs = cpu_to_be16(2);
  1156. new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  1157. new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  1158. xfs_alloc_log_block(cur->bc_tp, nbp, XFS_BB_ALL_BITS);
  1159. ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
  1160. /*
  1161. * Fill in the key data in the new root.
  1162. */
  1163. {
  1164. xfs_alloc_key_t *kp; /* btree key pointer */
  1165. kp = XFS_ALLOC_KEY_ADDR(new, 1, cur);
  1166. if (be16_to_cpu(left->bb_level) > 0) {
  1167. kp[0] = *XFS_ALLOC_KEY_ADDR(left, 1, cur);
  1168. kp[1] = *XFS_ALLOC_KEY_ADDR(right, 1, cur);
  1169. } else {
  1170. xfs_alloc_rec_t *rp; /* btree record pointer */
  1171. rp = XFS_ALLOC_REC_ADDR(left, 1, cur);
  1172. kp[0].ar_startblock = rp->ar_startblock;
  1173. kp[0].ar_blockcount = rp->ar_blockcount;
  1174. rp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  1175. kp[1].ar_startblock = rp->ar_startblock;
  1176. kp[1].ar_blockcount = rp->ar_blockcount;
  1177. }
  1178. }
  1179. xfs_alloc_log_keys(cur, nbp, 1, 2);
  1180. /*
  1181. * Fill in the pointer data in the new root.
  1182. */
  1183. {
  1184. xfs_alloc_ptr_t *pp; /* btree address pointer */
  1185. pp = XFS_ALLOC_PTR_ADDR(new, 1, cur);
  1186. pp[0] = cpu_to_be32(lbno);
  1187. pp[1] = cpu_to_be32(rbno);
  1188. }
  1189. xfs_alloc_log_ptrs(cur, nbp, 1, 2);
  1190. /*
  1191. * Fix up the cursor.
  1192. */
  1193. xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
  1194. cur->bc_ptrs[cur->bc_nlevels] = nptr;
  1195. cur->bc_nlevels++;
  1196. *stat = 1;
  1197. return 0;
  1198. }
  1199. /*
  1200. * Move 1 record right from cur/level if possible.
  1201. * Update cur to reflect the new path.
  1202. */
  1203. STATIC int /* error */
  1204. xfs_alloc_rshift(
  1205. xfs_btree_cur_t *cur, /* btree cursor */
  1206. int level, /* level to shift record on */
  1207. int *stat) /* success/failure */
  1208. {
  1209. int error; /* error return value */
  1210. int i; /* loop index */
  1211. xfs_alloc_key_t key; /* key value for leaf level upward */
  1212. xfs_buf_t *lbp; /* buffer for left (current) block */
  1213. xfs_alloc_block_t *left; /* left (current) btree block */
  1214. xfs_buf_t *rbp; /* buffer for right neighbor block */
  1215. xfs_alloc_block_t *right; /* right neighbor btree block */
  1216. xfs_alloc_key_t *rkp; /* key pointer for right block */
  1217. xfs_btree_cur_t *tcur; /* temporary cursor */
  1218. /*
  1219. * Set up variables for this block as "left".
  1220. */
  1221. lbp = cur->bc_bufs[level];
  1222. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1223. #ifdef DEBUG
  1224. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  1225. return error;
  1226. #endif
  1227. /*
  1228. * If we've got no right sibling then we can't shift an entry right.
  1229. */
  1230. if (be32_to_cpu(left->bb_rightsib) == NULLAGBLOCK) {
  1231. *stat = 0;
  1232. return 0;
  1233. }
  1234. /*
  1235. * If the cursor entry is the one that would be moved, don't
  1236. * do it... it's too complicated.
  1237. */
  1238. if (cur->bc_ptrs[level] >= be16_to_cpu(left->bb_numrecs)) {
  1239. *stat = 0;
  1240. return 0;
  1241. }
  1242. /*
  1243. * Set up the right neighbor as "right".
  1244. */
  1245. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1246. cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib),
  1247. 0, &rbp, XFS_ALLOC_BTREE_REF)))
  1248. return error;
  1249. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  1250. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  1251. return error;
  1252. /*
  1253. * If it's full, it can't take another entry.
  1254. */
  1255. if (be16_to_cpu(right->bb_numrecs) == XFS_ALLOC_BLOCK_MAXRECS(level, cur)) {
  1256. *stat = 0;
  1257. return 0;
  1258. }
  1259. /*
  1260. * Make a hole at the start of the right neighbor block, then
  1261. * copy the last left block entry to the hole.
  1262. */
  1263. if (level > 0) {
  1264. xfs_alloc_key_t *lkp; /* key pointer for left block */
  1265. xfs_alloc_ptr_t *lpp; /* address pointer for left block */
  1266. xfs_alloc_ptr_t *rpp; /* address pointer for right block */
  1267. lkp = XFS_ALLOC_KEY_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
  1268. lpp = XFS_ALLOC_PTR_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
  1269. rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
  1270. rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
  1271. #ifdef DEBUG
  1272. for (i = be16_to_cpu(right->bb_numrecs) - 1; i >= 0; i--) {
  1273. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
  1274. return error;
  1275. }
  1276. #endif
  1277. memmove(rkp + 1, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1278. memmove(rpp + 1, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1279. #ifdef DEBUG
  1280. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*lpp), level)))
  1281. return error;
  1282. #endif
  1283. *rkp = *lkp;
  1284. *rpp = *lpp;
  1285. xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
  1286. xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
  1287. xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
  1288. } else {
  1289. xfs_alloc_rec_t *lrp; /* record pointer for left block */
  1290. xfs_alloc_rec_t *rrp; /* record pointer for right block */
  1291. lrp = XFS_ALLOC_REC_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
  1292. rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  1293. memmove(rrp + 1, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1294. *rrp = *lrp;
  1295. xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
  1296. key.ar_startblock = rrp->ar_startblock;
  1297. key.ar_blockcount = rrp->ar_blockcount;
  1298. rkp = &key;
  1299. xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
  1300. }
  1301. /*
  1302. * Decrement and log left's numrecs, bump and log right's numrecs.
  1303. */
  1304. be16_add_cpu(&left->bb_numrecs, -1);
  1305. xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
  1306. be16_add_cpu(&right->bb_numrecs, 1);
  1307. xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
  1308. /*
  1309. * Using a temporary cursor, update the parent key values of the
  1310. * block on the right.
  1311. */
  1312. if ((error = xfs_btree_dup_cursor(cur, &tcur)))
  1313. return error;
  1314. i = xfs_btree_lastrec(tcur, level);
  1315. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1316. if ((error = xfs_btree_increment(tcur, level, &i)) ||
  1317. (error = xfs_alloc_updkey(tcur, rkp, level + 1)))
  1318. goto error0;
  1319. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  1320. *stat = 1;
  1321. return 0;
  1322. error0:
  1323. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  1324. return error;
  1325. }
  1326. /*
  1327. * Split cur/level block in half.
  1328. * Return new block number and its first record (to be inserted into parent).
  1329. */
  1330. STATIC int /* error */
  1331. xfs_alloc_split(
  1332. xfs_btree_cur_t *cur, /* btree cursor */
  1333. int level, /* level to split */
  1334. xfs_agblock_t *bnop, /* output: block number allocated */
  1335. xfs_alloc_key_t *keyp, /* output: first key of new block */
  1336. xfs_btree_cur_t **curp, /* output: new cursor */
  1337. int *stat) /* success/failure */
  1338. {
  1339. int error; /* error return value */
  1340. int i; /* loop index/record number */
  1341. xfs_agblock_t lbno; /* left (current) block number */
  1342. xfs_buf_t *lbp; /* buffer for left block */
  1343. xfs_alloc_block_t *left; /* left (current) btree block */
  1344. xfs_agblock_t rbno; /* right (new) block number */
  1345. xfs_buf_t *rbp; /* buffer for right block */
  1346. xfs_alloc_block_t *right; /* right (new) btree block */
  1347. /*
  1348. * Allocate the new block from the freelist.
  1349. * If we can't do it, we're toast. Give up.
  1350. */
  1351. error = xfs_alloc_get_freelist(cur->bc_tp,
  1352. cur->bc_private.a.agbp, &rbno, 1);
  1353. if (error)
  1354. return error;
  1355. if (rbno == NULLAGBLOCK) {
  1356. *stat = 0;
  1357. return 0;
  1358. }
  1359. xfs_trans_agbtree_delta(cur->bc_tp, 1);
  1360. rbp = xfs_btree_get_bufs(cur->bc_mp, cur->bc_tp, cur->bc_private.a.agno,
  1361. rbno, 0);
  1362. /*
  1363. * Set up the new block as "right".
  1364. */
  1365. right = XFS_BUF_TO_ALLOC_BLOCK(rbp);
  1366. /*
  1367. * "Left" is the current (according to the cursor) block.
  1368. */
  1369. lbp = cur->bc_bufs[level];
  1370. left = XFS_BUF_TO_ALLOC_BLOCK(lbp);
  1371. #ifdef DEBUG
  1372. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  1373. return error;
  1374. #endif
  1375. /*
  1376. * Fill in the btree header for the new block.
  1377. */
  1378. right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  1379. right->bb_level = left->bb_level;
  1380. right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
  1381. /*
  1382. * Make sure that if there's an odd number of entries now, that
  1383. * each new block will have the same number of entries.
  1384. */
  1385. if ((be16_to_cpu(left->bb_numrecs) & 1) &&
  1386. cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
  1387. be16_add_cpu(&right->bb_numrecs, 1);
  1388. i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
  1389. /*
  1390. * For non-leaf blocks, copy keys and addresses over to the new block.
  1391. */
  1392. if (level > 0) {
  1393. xfs_alloc_key_t *lkp; /* left btree key pointer */
  1394. xfs_alloc_ptr_t *lpp; /* left btree address pointer */
  1395. xfs_alloc_key_t *rkp; /* right btree key pointer */
  1396. xfs_alloc_ptr_t *rpp; /* right btree address pointer */
  1397. lkp = XFS_ALLOC_KEY_ADDR(left, i, cur);
  1398. lpp = XFS_ALLOC_PTR_ADDR(left, i, cur);
  1399. rkp = XFS_ALLOC_KEY_ADDR(right, 1, cur);
  1400. rpp = XFS_ALLOC_PTR_ADDR(right, 1, cur);
  1401. #ifdef DEBUG
  1402. for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
  1403. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
  1404. return error;
  1405. }
  1406. #endif
  1407. memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1408. memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1409. xfs_alloc_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1410. xfs_alloc_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1411. *keyp = *rkp;
  1412. }
  1413. /*
  1414. * For leaf blocks, copy records over to the new block.
  1415. */
  1416. else {
  1417. xfs_alloc_rec_t *lrp; /* left btree record pointer */
  1418. xfs_alloc_rec_t *rrp; /* right btree record pointer */
  1419. lrp = XFS_ALLOC_REC_ADDR(left, i, cur);
  1420. rrp = XFS_ALLOC_REC_ADDR(right, 1, cur);
  1421. memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1422. xfs_alloc_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1423. keyp->ar_startblock = rrp->ar_startblock;
  1424. keyp->ar_blockcount = rrp->ar_blockcount;
  1425. }
  1426. /*
  1427. * Find the left block number by looking in the buffer.
  1428. * Adjust numrecs, sibling pointers.
  1429. */
  1430. lbno = XFS_DADDR_TO_AGBNO(cur->bc_mp, XFS_BUF_ADDR(lbp));
  1431. be16_add_cpu(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
  1432. right->bb_rightsib = left->bb_rightsib;
  1433. left->bb_rightsib = cpu_to_be32(rbno);
  1434. right->bb_leftsib = cpu_to_be32(lbno);
  1435. xfs_alloc_log_block(cur->bc_tp, rbp, XFS_BB_ALL_BITS);
  1436. xfs_alloc_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  1437. /*
  1438. * If there's a block to the new block's right, make that block
  1439. * point back to right instead of to left.
  1440. */
  1441. if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) {
  1442. xfs_alloc_block_t *rrblock; /* rr btree block */
  1443. xfs_buf_t *rrbp; /* buffer for rrblock */
  1444. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1445. cur->bc_private.a.agno, be32_to_cpu(right->bb_rightsib), 0,
  1446. &rrbp, XFS_ALLOC_BTREE_REF)))
  1447. return error;
  1448. rrblock = XFS_BUF_TO_ALLOC_BLOCK(rrbp);
  1449. if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
  1450. return error;
  1451. rrblock->bb_leftsib = cpu_to_be32(rbno);
  1452. xfs_alloc_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
  1453. }
  1454. /*
  1455. * If the cursor is really in the right block, move it there.
  1456. * If it's just pointing past the last entry in left, then we'll
  1457. * insert there, so don't change anything in that case.
  1458. */
  1459. if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
  1460. xfs_btree_setbuf(cur, level, rbp);
  1461. cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
  1462. }
  1463. /*
  1464. * If there are more levels, we'll need another cursor which refers to
  1465. * the right block, no matter where this cursor was.
  1466. */
  1467. if (level + 1 < cur->bc_nlevels) {
  1468. if ((error = xfs_btree_dup_cursor(cur, curp)))
  1469. return error;
  1470. (*curp)->bc_ptrs[level + 1]++;
  1471. }
  1472. *bnop = rbno;
  1473. *stat = 1;
  1474. return 0;
  1475. }
  1476. /*
  1477. * Update keys at all levels from here to the root along the cursor's path.
  1478. */
  1479. STATIC int /* error */
  1480. xfs_alloc_updkey(
  1481. xfs_btree_cur_t *cur, /* btree cursor */
  1482. xfs_alloc_key_t *keyp, /* new key value to update to */
  1483. int level) /* starting level for update */
  1484. {
  1485. int ptr; /* index of key in block */
  1486. /*
  1487. * Go up the tree from this level toward the root.
  1488. * At each level, update the key value to the value input.
  1489. * Stop when we reach a level where the cursor isn't pointing
  1490. * at the first entry in the block.
  1491. */
  1492. for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
  1493. xfs_alloc_block_t *block; /* btree block */
  1494. xfs_buf_t *bp; /* buffer for block */
  1495. #ifdef DEBUG
  1496. int error; /* error return value */
  1497. #endif
  1498. xfs_alloc_key_t *kp; /* ptr to btree block keys */
  1499. bp = cur->bc_bufs[level];
  1500. block = XFS_BUF_TO_ALLOC_BLOCK(bp);
  1501. #ifdef DEBUG
  1502. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  1503. return error;
  1504. #endif
  1505. ptr = cur->bc_ptrs[level];
  1506. kp = XFS_ALLOC_KEY_ADDR(block, ptr, cur);
  1507. *kp = *keyp;
  1508. xfs_alloc_log_keys(cur, bp, ptr, ptr);
  1509. }
  1510. return 0;
  1511. }
  1512. /*
  1513. * Externally visible routines.
  1514. */
  1515. /*
  1516. * Delete the record pointed to by cur.
  1517. * The cursor refers to the place where the record was (could be inserted)
  1518. * when the operation returns.
  1519. */
  1520. int /* error */
  1521. xfs_alloc_delete(
  1522. xfs_btree_cur_t *cur, /* btree cursor */
  1523. int *stat) /* success/failure */
  1524. {
  1525. int error; /* error return value */
  1526. int i; /* result code */
  1527. int level; /* btree level */
  1528. /*
  1529. * Go up the tree, starting at leaf level.
  1530. * If 2 is returned then a join was done; go to the next level.
  1531. * Otherwise we are done.
  1532. */
  1533. for (level = 0, i = 2; i == 2; level++) {
  1534. if ((error = xfs_alloc_delrec(cur, level, &i)))
  1535. return error;
  1536. }
  1537. if (i == 0) {
  1538. for (level = 1; level < cur->bc_nlevels; level++) {
  1539. if (cur->bc_ptrs[level] == 0) {
  1540. if ((error = xfs_btree_decrement(cur, level, &i)))
  1541. return error;
  1542. break;
  1543. }
  1544. }
  1545. }
  1546. *stat = i;
  1547. return 0;
  1548. }
  1549. /*
  1550. * Get the data from the pointed-to record.
  1551. */
  1552. int /* error */
  1553. xfs_alloc_get_rec(
  1554. xfs_btree_cur_t *cur, /* btree cursor */
  1555. xfs_agblock_t *bno, /* output: starting block of extent */
  1556. xfs_extlen_t *len, /* output: length of extent */
  1557. int *stat) /* output: success/failure */
  1558. {
  1559. xfs_alloc_block_t *block; /* btree block */
  1560. #ifdef DEBUG
  1561. int error; /* error return value */
  1562. #endif
  1563. int ptr; /* record number */
  1564. ptr = cur->bc_ptrs[0];
  1565. block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
  1566. #ifdef DEBUG
  1567. if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
  1568. return error;
  1569. #endif
  1570. /*
  1571. * Off the right end or left end, return failure.
  1572. */
  1573. if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
  1574. *stat = 0;
  1575. return 0;
  1576. }
  1577. /*
  1578. * Point to the record and extract its data.
  1579. */
  1580. {
  1581. xfs_alloc_rec_t *rec; /* record data */
  1582. rec = XFS_ALLOC_REC_ADDR(block, ptr, cur);
  1583. *bno = be32_to_cpu(rec->ar_startblock);
  1584. *len = be32_to_cpu(rec->ar_blockcount);
  1585. }
  1586. *stat = 1;
  1587. return 0;
  1588. }
  1589. /*
  1590. * Insert the current record at the point referenced by cur.
  1591. * The cursor may be inconsistent on return if splits have been done.
  1592. */
  1593. int /* error */
  1594. xfs_alloc_insert(
  1595. xfs_btree_cur_t *cur, /* btree cursor */
  1596. int *stat) /* success/failure */
  1597. {
  1598. int error; /* error return value */
  1599. int i; /* result value, 0 for failure */
  1600. int level; /* current level number in btree */
  1601. xfs_agblock_t nbno; /* new block number (split result) */
  1602. xfs_btree_cur_t *ncur; /* new cursor (split result) */
  1603. xfs_alloc_rec_t nrec; /* record being inserted this level */
  1604. xfs_btree_cur_t *pcur; /* previous level's cursor */
  1605. level = 0;
  1606. nbno = NULLAGBLOCK;
  1607. nrec.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
  1608. nrec.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
  1609. ncur = NULL;
  1610. pcur = cur;
  1611. /*
  1612. * Loop going up the tree, starting at the leaf level.
  1613. * Stop when we don't get a split block, that must mean that
  1614. * the insert is finished with this level.
  1615. */
  1616. do {
  1617. /*
  1618. * Insert nrec/nbno into this level of the tree.
  1619. * Note if we fail, nbno will be null.
  1620. */
  1621. if ((error = xfs_alloc_insrec(pcur, level++, &nbno, &nrec, &ncur,
  1622. &i))) {
  1623. if (pcur != cur)
  1624. xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
  1625. return error;
  1626. }
  1627. /*
  1628. * See if the cursor we just used is trash.
  1629. * Can't trash the caller's cursor, but otherwise we should
  1630. * if ncur is a new cursor or we're about to be done.
  1631. */
  1632. if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
  1633. cur->bc_nlevels = pcur->bc_nlevels;
  1634. xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
  1635. }
  1636. /*
  1637. * If we got a new cursor, switch to it.
  1638. */
  1639. if (ncur) {
  1640. pcur = ncur;
  1641. ncur = NULL;
  1642. }
  1643. } while (nbno != NULLAGBLOCK);
  1644. *stat = i;
  1645. return 0;
  1646. }
  1647. /*
  1648. * Update the record referred to by cur, to the value given by [bno, len].
  1649. * This either works (return 0) or gets an EFSCORRUPTED error.
  1650. */
  1651. int /* error */
  1652. xfs_alloc_update(
  1653. xfs_btree_cur_t *cur, /* btree cursor */
  1654. xfs_agblock_t bno, /* starting block of extent */
  1655. xfs_extlen_t len) /* length of extent */
  1656. {
  1657. xfs_alloc_block_t *block; /* btree block to update */
  1658. int error; /* error return value */
  1659. int ptr; /* current record number (updating) */
  1660. ASSERT(len > 0);
  1661. /*
  1662. * Pick up the a.g. freelist struct and the current block.
  1663. */
  1664. block = XFS_BUF_TO_ALLOC_BLOCK(cur->bc_bufs[0]);
  1665. #ifdef DEBUG
  1666. if ((error = xfs_btree_check_sblock(cur, block, 0, cur->bc_bufs[0])))
  1667. return error;
  1668. #endif
  1669. /*
  1670. * Get the address of the rec to be updated.
  1671. */
  1672. ptr = cur->bc_ptrs[0];
  1673. {
  1674. xfs_alloc_rec_t *rp; /* pointer to updated record */
  1675. rp = XFS_ALLOC_REC_ADDR(block, ptr, cur);
  1676. /*
  1677. * Fill in the new contents and log them.
  1678. */
  1679. rp->ar_startblock = cpu_to_be32(bno);
  1680. rp->ar_blockcount = cpu_to_be32(len);
  1681. xfs_alloc_log_recs(cur, cur->bc_bufs[0], ptr, ptr);
  1682. }
  1683. /*
  1684. * If it's the by-size btree and it's the last leaf block and
  1685. * it's the last record... then update the size of the longest
  1686. * extent in the a.g., which we cache in the a.g. freelist header.
  1687. */
  1688. if (cur->bc_btnum == XFS_BTNUM_CNT &&
  1689. be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK &&
  1690. ptr == be16_to_cpu(block->bb_numrecs)) {
  1691. xfs_agf_t *agf; /* a.g. freespace header */
  1692. xfs_agnumber_t seqno;
  1693. agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  1694. seqno = be32_to_cpu(agf->agf_seqno);
  1695. cur->bc_mp->m_perag[seqno].pagf_longest = len;
  1696. agf->agf_longest = cpu_to_be32(len);
  1697. xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp,
  1698. XFS_AGF_LONGEST);
  1699. }
  1700. /*
  1701. * Updating first record in leaf. Pass new key value up to our parent.
  1702. */
  1703. if (ptr == 1) {
  1704. xfs_alloc_key_t key; /* key containing [bno, len] */
  1705. key.ar_startblock = cpu_to_be32(bno);
  1706. key.ar_blockcount = cpu_to_be32(len);
  1707. if ((error = xfs_alloc_updkey(cur, &key, 1)))
  1708. return error;
  1709. }
  1710. return 0;
  1711. }
  1712. STATIC struct xfs_btree_cur *
  1713. xfs_allocbt_dup_cursor(
  1714. struct xfs_btree_cur *cur)
  1715. {
  1716. return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
  1717. cur->bc_private.a.agbp, cur->bc_private.a.agno,
  1718. cur->bc_btnum);
  1719. }
  1720. STATIC int
  1721. xfs_allocbt_get_maxrecs(
  1722. struct xfs_btree_cur *cur,
  1723. int level)
  1724. {
  1725. return cur->bc_mp->m_alloc_mxr[level != 0];
  1726. }
  1727. STATIC void
  1728. xfs_allocbt_init_key_from_rec(
  1729. union xfs_btree_key *key,
  1730. union xfs_btree_rec *rec)
  1731. {
  1732. ASSERT(rec->alloc.ar_startblock != 0);
  1733. key->alloc.ar_startblock = rec->alloc.ar_startblock;
  1734. key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
  1735. }
  1736. STATIC void
  1737. xfs_allocbt_init_ptr_from_cur(
  1738. struct xfs_btree_cur *cur,
  1739. union xfs_btree_ptr *ptr)
  1740. {
  1741. struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
  1742. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
  1743. ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
  1744. ptr->s = agf->agf_roots[cur->bc_btnum];
  1745. }
  1746. STATIC __int64_t
  1747. xfs_allocbt_key_diff(
  1748. struct xfs_btree_cur *cur,
  1749. union xfs_btree_key *key)
  1750. {
  1751. xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
  1752. xfs_alloc_key_t *kp = &key->alloc;
  1753. __int64_t diff;
  1754. if (cur->bc_btnum == XFS_BTNUM_BNO) {
  1755. return (__int64_t)be32_to_cpu(kp->ar_startblock) -
  1756. rec->ar_startblock;
  1757. }
  1758. diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
  1759. if (diff)
  1760. return diff;
  1761. return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
  1762. }
  1763. #ifdef XFS_BTREE_TRACE
  1764. ktrace_t *xfs_allocbt_trace_buf;
  1765. STATIC void
  1766. xfs_allocbt_trace_enter(
  1767. struct xfs_btree_cur *cur,
  1768. const char *func,
  1769. char *s,
  1770. int type,
  1771. int line,
  1772. __psunsigned_t a0,
  1773. __psunsigned_t a1,
  1774. __psunsigned_t a2,
  1775. __psunsigned_t a3,
  1776. __psunsigned_t a4,
  1777. __psunsigned_t a5,
  1778. __psunsigned_t a6,
  1779. __psunsigned_t a7,
  1780. __psunsigned_t a8,
  1781. __psunsigned_t a9,
  1782. __psunsigned_t a10)
  1783. {
  1784. ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
  1785. (void *)func, (void *)s, NULL, (void *)cur,
  1786. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  1787. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  1788. (void *)a8, (void *)a9, (void *)a10);
  1789. }
  1790. STATIC void
  1791. xfs_allocbt_trace_cursor(
  1792. struct xfs_btree_cur *cur,
  1793. __uint32_t *s0,
  1794. __uint64_t *l0,
  1795. __uint64_t *l1)
  1796. {
  1797. *s0 = cur->bc_private.a.agno;
  1798. *l0 = cur->bc_rec.a.ar_startblock;
  1799. *l1 = cur->bc_rec.a.ar_blockcount;
  1800. }
  1801. STATIC void
  1802. xfs_allocbt_trace_key(
  1803. struct xfs_btree_cur *cur,
  1804. union xfs_btree_key *key,
  1805. __uint64_t *l0,
  1806. __uint64_t *l1)
  1807. {
  1808. *l0 = be32_to_cpu(key->alloc.ar_startblock);
  1809. *l1 = be32_to_cpu(key->alloc.ar_blockcount);
  1810. }
  1811. STATIC void
  1812. xfs_allocbt_trace_record(
  1813. struct xfs_btree_cur *cur,
  1814. union xfs_btree_rec *rec,
  1815. __uint64_t *l0,
  1816. __uint64_t *l1,
  1817. __uint64_t *l2)
  1818. {
  1819. *l0 = be32_to_cpu(rec->alloc.ar_startblock);
  1820. *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
  1821. *l2 = 0;
  1822. }
  1823. #endif /* XFS_BTREE_TRACE */
  1824. static const struct xfs_btree_ops xfs_allocbt_ops = {
  1825. .rec_len = sizeof(xfs_alloc_rec_t),
  1826. .key_len = sizeof(xfs_alloc_key_t),
  1827. .dup_cursor = xfs_allocbt_dup_cursor,
  1828. .get_maxrecs = xfs_allocbt_get_maxrecs,
  1829. .init_key_from_rec = xfs_allocbt_init_key_from_rec,
  1830. .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
  1831. .key_diff = xfs_allocbt_key_diff,
  1832. #ifdef XFS_BTREE_TRACE
  1833. .trace_enter = xfs_allocbt_trace_enter,
  1834. .trace_cursor = xfs_allocbt_trace_cursor,
  1835. .trace_key = xfs_allocbt_trace_key,
  1836. .trace_record = xfs_allocbt_trace_record,
  1837. #endif
  1838. };
  1839. /*
  1840. * Allocate a new allocation btree cursor.
  1841. */
  1842. struct xfs_btree_cur * /* new alloc btree cursor */
  1843. xfs_allocbt_init_cursor(
  1844. struct xfs_mount *mp, /* file system mount point */
  1845. struct xfs_trans *tp, /* transaction pointer */
  1846. struct xfs_buf *agbp, /* buffer for agf structure */
  1847. xfs_agnumber_t agno, /* allocation group number */
  1848. xfs_btnum_t btnum) /* btree identifier */
  1849. {
  1850. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  1851. struct xfs_btree_cur *cur;
  1852. ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
  1853. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  1854. cur->bc_tp = tp;
  1855. cur->bc_mp = mp;
  1856. cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
  1857. cur->bc_btnum = btnum;
  1858. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  1859. cur->bc_ops = &xfs_allocbt_ops;
  1860. cur->bc_private.a.agbp = agbp;
  1861. cur->bc_private.a.agno = agno;
  1862. return cur;
  1863. }