xfs_alloc_btree.c 67 KB

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