xfs_alloc_btree.c 67 KB

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