xfs_alloc_btree.c 68 KB

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