xfs_ialloc_btree.c 60 KB

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