xfs_ialloc_btree.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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. STATIC void xfs_inobt_log_block(xfs_trans_t *, xfs_buf_t *, int);
  42. STATIC void xfs_inobt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  43. STATIC void xfs_inobt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  44. STATIC void xfs_inobt_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  45. STATIC int xfs_inobt_lshift(xfs_btree_cur_t *, int, int *);
  46. STATIC int xfs_inobt_newroot(xfs_btree_cur_t *, int *);
  47. STATIC int xfs_inobt_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
  48. xfs_inobt_key_t *, xfs_btree_cur_t **, int *);
  49. /*
  50. * Single level of the xfs_inobt_delete record deletion routine.
  51. * Delete record pointed to by cur/level.
  52. * Remove the record from its block then rebalance the tree.
  53. * Return 0 for error, 1 for done, 2 to go on to the next level.
  54. */
  55. STATIC int /* error */
  56. xfs_inobt_delrec(
  57. xfs_btree_cur_t *cur, /* btree cursor */
  58. int level, /* level removing record from */
  59. int *stat) /* fail/done/go-on */
  60. {
  61. xfs_buf_t *agbp; /* buffer for a.g. inode header */
  62. xfs_mount_t *mp; /* mount structure */
  63. xfs_agi_t *agi; /* allocation group inode header */
  64. xfs_inobt_block_t *block; /* btree block record/key lives in */
  65. xfs_agblock_t bno; /* btree block number */
  66. xfs_buf_t *bp; /* buffer for block */
  67. int error; /* error return value */
  68. int i; /* loop index */
  69. xfs_inobt_key_t key; /* kp points here if block is level 0 */
  70. xfs_inobt_key_t *kp = NULL; /* pointer to btree keys */
  71. xfs_agblock_t lbno; /* left block's block number */
  72. xfs_buf_t *lbp; /* left block's buffer pointer */
  73. xfs_inobt_block_t *left; /* left btree block */
  74. xfs_inobt_key_t *lkp; /* left block key pointer */
  75. xfs_inobt_ptr_t *lpp; /* left block address pointer */
  76. int lrecs = 0; /* number of records in left block */
  77. xfs_inobt_rec_t *lrp; /* left block record pointer */
  78. xfs_inobt_ptr_t *pp = NULL; /* pointer to btree addresses */
  79. int ptr; /* index in btree block for this rec */
  80. xfs_agblock_t rbno; /* right block's block number */
  81. xfs_buf_t *rbp; /* right block's buffer pointer */
  82. xfs_inobt_block_t *right; /* right btree block */
  83. xfs_inobt_key_t *rkp; /* right block key pointer */
  84. xfs_inobt_rec_t *rp; /* pointer to btree records */
  85. xfs_inobt_ptr_t *rpp; /* right block address pointer */
  86. int rrecs = 0; /* number of records in right block */
  87. int numrecs;
  88. xfs_inobt_rec_t *rrp; /* right block record pointer */
  89. xfs_btree_cur_t *tcur; /* temporary btree cursor */
  90. mp = cur->bc_mp;
  91. /*
  92. * Get the index of the entry being deleted, check for nothing there.
  93. */
  94. ptr = cur->bc_ptrs[level];
  95. if (ptr == 0) {
  96. *stat = 0;
  97. return 0;
  98. }
  99. /*
  100. * Get the buffer & block containing the record or key/ptr.
  101. */
  102. bp = cur->bc_bufs[level];
  103. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  104. #ifdef DEBUG
  105. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  106. return error;
  107. #endif
  108. /*
  109. * Fail if we're off the end of the block.
  110. */
  111. numrecs = be16_to_cpu(block->bb_numrecs);
  112. if (ptr > numrecs) {
  113. *stat = 0;
  114. return 0;
  115. }
  116. /*
  117. * It's a nonleaf. Excise the key and ptr being deleted, by
  118. * sliding the entries past them down one.
  119. * Log the changed areas of the block.
  120. */
  121. if (level > 0) {
  122. kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
  123. pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
  124. #ifdef DEBUG
  125. for (i = ptr; i < numrecs; i++) {
  126. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i]), level)))
  127. return error;
  128. }
  129. #endif
  130. if (ptr < numrecs) {
  131. memmove(&kp[ptr - 1], &kp[ptr],
  132. (numrecs - ptr) * sizeof(*kp));
  133. memmove(&pp[ptr - 1], &pp[ptr],
  134. (numrecs - ptr) * sizeof(*kp));
  135. xfs_inobt_log_keys(cur, bp, ptr, numrecs - 1);
  136. xfs_inobt_log_ptrs(cur, bp, ptr, numrecs - 1);
  137. }
  138. }
  139. /*
  140. * It's a leaf. Excise the record being deleted, by sliding the
  141. * entries past it down one. Log the changed areas of the block.
  142. */
  143. else {
  144. rp = XFS_INOBT_REC_ADDR(block, 1, cur);
  145. if (ptr < numrecs) {
  146. memmove(&rp[ptr - 1], &rp[ptr],
  147. (numrecs - ptr) * sizeof(*rp));
  148. xfs_inobt_log_recs(cur, bp, ptr, numrecs - 1);
  149. }
  150. /*
  151. * If it's the first record in the block, we'll need a key
  152. * structure to pass up to the next level (updkey).
  153. */
  154. if (ptr == 1) {
  155. key.ir_startino = rp->ir_startino;
  156. kp = &key;
  157. }
  158. }
  159. /*
  160. * Decrement and log the number of entries in the block.
  161. */
  162. numrecs--;
  163. block->bb_numrecs = cpu_to_be16(numrecs);
  164. xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
  165. /*
  166. * Is this the root level? If so, we're almost done.
  167. */
  168. if (level == cur->bc_nlevels - 1) {
  169. /*
  170. * If this is the root level,
  171. * and there's only one entry left,
  172. * and it's NOT the leaf level,
  173. * then we can get rid of this level.
  174. */
  175. if (numrecs == 1 && level > 0) {
  176. agbp = cur->bc_private.a.agbp;
  177. agi = XFS_BUF_TO_AGI(agbp);
  178. /*
  179. * pp is still set to the first pointer in the block.
  180. * Make it the new root of the btree.
  181. */
  182. bno = be32_to_cpu(agi->agi_root);
  183. agi->agi_root = *pp;
  184. be32_add_cpu(&agi->agi_level, -1);
  185. /*
  186. * Free the block.
  187. */
  188. if ((error = xfs_free_extent(cur->bc_tp,
  189. XFS_AGB_TO_FSB(mp, cur->bc_private.a.agno, bno), 1)))
  190. return error;
  191. xfs_trans_binval(cur->bc_tp, bp);
  192. xfs_ialloc_log_agi(cur->bc_tp, agbp,
  193. XFS_AGI_ROOT | XFS_AGI_LEVEL);
  194. /*
  195. * Update the cursor so there's one fewer level.
  196. */
  197. cur->bc_bufs[level] = NULL;
  198. cur->bc_nlevels--;
  199. } else if (level > 0 &&
  200. (error = xfs_btree_decrement(cur, level, &i)))
  201. return error;
  202. *stat = 1;
  203. return 0;
  204. }
  205. /*
  206. * If we deleted the leftmost entry in the block, update the
  207. * key values above us in the tree.
  208. */
  209. if (ptr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)kp, level + 1)))
  210. return error;
  211. /*
  212. * If the number of records remaining in the block is at least
  213. * the minimum, we're done.
  214. */
  215. if (numrecs >= XFS_INOBT_BLOCK_MINRECS(level, cur)) {
  216. if (level > 0 &&
  217. (error = xfs_btree_decrement(cur, level, &i)))
  218. return error;
  219. *stat = 1;
  220. return 0;
  221. }
  222. /*
  223. * Otherwise, we have to move some records around to keep the
  224. * tree balanced. Look at the left and right sibling blocks to
  225. * see if we can re-balance by moving only one record.
  226. */
  227. rbno = be32_to_cpu(block->bb_rightsib);
  228. lbno = be32_to_cpu(block->bb_leftsib);
  229. bno = NULLAGBLOCK;
  230. ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
  231. /*
  232. * Duplicate the cursor so our btree manipulations here won't
  233. * disrupt the next level up.
  234. */
  235. if ((error = xfs_btree_dup_cursor(cur, &tcur)))
  236. return error;
  237. /*
  238. * If there's a right sibling, see if it's ok to shift an entry
  239. * out of it.
  240. */
  241. if (rbno != NULLAGBLOCK) {
  242. /*
  243. * Move the temp cursor to the last entry in the next block.
  244. * Actually any entry but the first would suffice.
  245. */
  246. i = xfs_btree_lastrec(tcur, level);
  247. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  248. if ((error = xfs_btree_increment(tcur, level, &i)))
  249. goto error0;
  250. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  251. i = xfs_btree_lastrec(tcur, level);
  252. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  253. /*
  254. * Grab a pointer to the block.
  255. */
  256. rbp = tcur->bc_bufs[level];
  257. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  258. #ifdef DEBUG
  259. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  260. goto error0;
  261. #endif
  262. /*
  263. * Grab the current block number, for future use.
  264. */
  265. bno = be32_to_cpu(right->bb_leftsib);
  266. /*
  267. * If right block is full enough so that removing one entry
  268. * won't make it too empty, and left-shifting an entry out
  269. * of right to us works, we're done.
  270. */
  271. if (be16_to_cpu(right->bb_numrecs) - 1 >=
  272. XFS_INOBT_BLOCK_MINRECS(level, cur)) {
  273. if ((error = xfs_inobt_lshift(tcur, level, &i)))
  274. goto error0;
  275. if (i) {
  276. ASSERT(be16_to_cpu(block->bb_numrecs) >=
  277. XFS_INOBT_BLOCK_MINRECS(level, cur));
  278. xfs_btree_del_cursor(tcur,
  279. XFS_BTREE_NOERROR);
  280. if (level > 0 &&
  281. (error = xfs_btree_decrement(cur, level,
  282. &i)))
  283. return error;
  284. *stat = 1;
  285. return 0;
  286. }
  287. }
  288. /*
  289. * Otherwise, grab the number of records in right for
  290. * future reference, and fix up the temp cursor to point
  291. * to our block again (last record).
  292. */
  293. rrecs = be16_to_cpu(right->bb_numrecs);
  294. if (lbno != NULLAGBLOCK) {
  295. xfs_btree_firstrec(tcur, level);
  296. if ((error = xfs_btree_decrement(tcur, level, &i)))
  297. goto error0;
  298. }
  299. }
  300. /*
  301. * If there's a left sibling, see if it's ok to shift an entry
  302. * out of it.
  303. */
  304. if (lbno != NULLAGBLOCK) {
  305. /*
  306. * Move the temp cursor to the first entry in the
  307. * previous block.
  308. */
  309. xfs_btree_firstrec(tcur, level);
  310. if ((error = xfs_btree_decrement(tcur, level, &i)))
  311. goto error0;
  312. xfs_btree_firstrec(tcur, level);
  313. /*
  314. * Grab a pointer to the block.
  315. */
  316. lbp = tcur->bc_bufs[level];
  317. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  318. #ifdef DEBUG
  319. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  320. goto error0;
  321. #endif
  322. /*
  323. * Grab the current block number, for future use.
  324. */
  325. bno = be32_to_cpu(left->bb_rightsib);
  326. /*
  327. * If left block is full enough so that removing one entry
  328. * won't make it too empty, and right-shifting an entry out
  329. * of left to us works, we're done.
  330. */
  331. if (be16_to_cpu(left->bb_numrecs) - 1 >=
  332. XFS_INOBT_BLOCK_MINRECS(level, cur)) {
  333. if ((error = xfs_btree_rshift(tcur, level, &i)))
  334. goto error0;
  335. if (i) {
  336. ASSERT(be16_to_cpu(block->bb_numrecs) >=
  337. XFS_INOBT_BLOCK_MINRECS(level, cur));
  338. xfs_btree_del_cursor(tcur,
  339. XFS_BTREE_NOERROR);
  340. if (level == 0)
  341. cur->bc_ptrs[0]++;
  342. *stat = 1;
  343. return 0;
  344. }
  345. }
  346. /*
  347. * Otherwise, grab the number of records in right for
  348. * future reference.
  349. */
  350. lrecs = be16_to_cpu(left->bb_numrecs);
  351. }
  352. /*
  353. * Delete the temp cursor, we're done with it.
  354. */
  355. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  356. /*
  357. * If here, we need to do a join to keep the tree balanced.
  358. */
  359. ASSERT(bno != NULLAGBLOCK);
  360. /*
  361. * See if we can join with the left neighbor block.
  362. */
  363. if (lbno != NULLAGBLOCK &&
  364. lrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
  365. /*
  366. * Set "right" to be the starting block,
  367. * "left" to be the left neighbor.
  368. */
  369. rbno = bno;
  370. right = block;
  371. rrecs = be16_to_cpu(right->bb_numrecs);
  372. rbp = bp;
  373. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  374. cur->bc_private.a.agno, lbno, 0, &lbp,
  375. XFS_INO_BTREE_REF)))
  376. return error;
  377. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  378. lrecs = be16_to_cpu(left->bb_numrecs);
  379. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  380. return error;
  381. }
  382. /*
  383. * If that won't work, see if we can join with the right neighbor block.
  384. */
  385. else if (rbno != NULLAGBLOCK &&
  386. rrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
  387. /*
  388. * Set "left" to be the starting block,
  389. * "right" to be the right neighbor.
  390. */
  391. lbno = bno;
  392. left = block;
  393. lrecs = be16_to_cpu(left->bb_numrecs);
  394. lbp = bp;
  395. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  396. cur->bc_private.a.agno, rbno, 0, &rbp,
  397. XFS_INO_BTREE_REF)))
  398. return error;
  399. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  400. rrecs = be16_to_cpu(right->bb_numrecs);
  401. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  402. return error;
  403. }
  404. /*
  405. * Otherwise, we can't fix the imbalance.
  406. * Just return. This is probably a logic error, but it's not fatal.
  407. */
  408. else {
  409. if (level > 0 && (error = xfs_btree_decrement(cur, level, &i)))
  410. return error;
  411. *stat = 1;
  412. return 0;
  413. }
  414. /*
  415. * We're now going to join "left" and "right" by moving all the stuff
  416. * in "right" to "left" and deleting "right".
  417. */
  418. if (level > 0) {
  419. /*
  420. * It's a non-leaf. Move keys and pointers.
  421. */
  422. lkp = XFS_INOBT_KEY_ADDR(left, lrecs + 1, cur);
  423. lpp = XFS_INOBT_PTR_ADDR(left, lrecs + 1, cur);
  424. rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
  425. rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
  426. #ifdef DEBUG
  427. for (i = 0; i < rrecs; i++) {
  428. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
  429. return error;
  430. }
  431. #endif
  432. memcpy(lkp, rkp, rrecs * sizeof(*lkp));
  433. memcpy(lpp, rpp, rrecs * sizeof(*lpp));
  434. xfs_inobt_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
  435. xfs_inobt_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
  436. } else {
  437. /*
  438. * It's a leaf. Move records.
  439. */
  440. lrp = XFS_INOBT_REC_ADDR(left, lrecs + 1, cur);
  441. rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
  442. memcpy(lrp, rrp, rrecs * sizeof(*lrp));
  443. xfs_inobt_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
  444. }
  445. /*
  446. * If we joined with the left neighbor, set the buffer in the
  447. * cursor to the left block, and fix up the index.
  448. */
  449. if (bp != lbp) {
  450. xfs_btree_setbuf(cur, level, lbp);
  451. cur->bc_ptrs[level] += lrecs;
  452. }
  453. /*
  454. * If we joined with the right neighbor and there's a level above
  455. * us, increment the cursor at that level.
  456. */
  457. else if (level + 1 < cur->bc_nlevels &&
  458. (error = xfs_btree_increment(cur, level + 1, &i)))
  459. return error;
  460. /*
  461. * Fix up the number of records in the surviving block.
  462. */
  463. lrecs += rrecs;
  464. left->bb_numrecs = cpu_to_be16(lrecs);
  465. /*
  466. * Fix up the right block pointer in the surviving block, and log it.
  467. */
  468. left->bb_rightsib = right->bb_rightsib;
  469. xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  470. /*
  471. * If there is a right sibling now, make it point to the
  472. * remaining block.
  473. */
  474. if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
  475. xfs_inobt_block_t *rrblock;
  476. xfs_buf_t *rrbp;
  477. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  478. cur->bc_private.a.agno, be32_to_cpu(left->bb_rightsib), 0,
  479. &rrbp, XFS_INO_BTREE_REF)))
  480. return error;
  481. rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
  482. if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
  483. return error;
  484. rrblock->bb_leftsib = cpu_to_be32(lbno);
  485. xfs_inobt_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
  486. }
  487. /*
  488. * Free the deleting block.
  489. */
  490. if ((error = xfs_free_extent(cur->bc_tp, XFS_AGB_TO_FSB(mp,
  491. cur->bc_private.a.agno, rbno), 1)))
  492. return error;
  493. xfs_trans_binval(cur->bc_tp, rbp);
  494. /*
  495. * Readjust the ptr at this level if it's not a leaf, since it's
  496. * still pointing at the deletion point, which makes the cursor
  497. * inconsistent. If this makes the ptr 0, the caller fixes it up.
  498. * We can't use decrement because it would change the next level up.
  499. */
  500. if (level > 0)
  501. cur->bc_ptrs[level]--;
  502. /*
  503. * Return value means the next level up has something to do.
  504. */
  505. *stat = 2;
  506. return 0;
  507. error0:
  508. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  509. return error;
  510. }
  511. /*
  512. * Insert one record/level. Return information to the caller
  513. * allowing the next level up to proceed if necessary.
  514. */
  515. STATIC int /* error */
  516. xfs_inobt_insrec(
  517. xfs_btree_cur_t *cur, /* btree cursor */
  518. int level, /* level to insert record at */
  519. xfs_agblock_t *bnop, /* i/o: block number inserted */
  520. xfs_inobt_rec_t *recp, /* i/o: record data inserted */
  521. xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
  522. int *stat) /* success/failure */
  523. {
  524. xfs_inobt_block_t *block; /* btree block record/key lives in */
  525. xfs_buf_t *bp; /* buffer for block */
  526. int error; /* error return value */
  527. int i; /* loop index */
  528. xfs_inobt_key_t key; /* key value being inserted */
  529. xfs_inobt_key_t *kp=NULL; /* pointer to btree keys */
  530. xfs_agblock_t nbno; /* block number of allocated block */
  531. xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
  532. xfs_inobt_key_t nkey; /* new key value, from split */
  533. xfs_inobt_rec_t nrec; /* new record value, for caller */
  534. int numrecs;
  535. int optr; /* old ptr value */
  536. xfs_inobt_ptr_t *pp; /* pointer to btree addresses */
  537. int ptr; /* index in btree block for this rec */
  538. xfs_inobt_rec_t *rp=NULL; /* pointer to btree records */
  539. /*
  540. * GCC doesn't understand the (arguably complex) control flow in
  541. * this function and complains about uninitialized structure fields
  542. * without this.
  543. */
  544. memset(&nrec, 0, sizeof(nrec));
  545. /*
  546. * If we made it to the root level, allocate a new root block
  547. * and we're done.
  548. */
  549. if (level >= cur->bc_nlevels) {
  550. error = xfs_inobt_newroot(cur, &i);
  551. *bnop = NULLAGBLOCK;
  552. *stat = i;
  553. return error;
  554. }
  555. /*
  556. * Make a key out of the record data to be inserted, and save it.
  557. */
  558. key.ir_startino = recp->ir_startino;
  559. optr = ptr = cur->bc_ptrs[level];
  560. /*
  561. * If we're off the left edge, return failure.
  562. */
  563. if (ptr == 0) {
  564. *stat = 0;
  565. return 0;
  566. }
  567. /*
  568. * Get pointers to the btree buffer and block.
  569. */
  570. bp = cur->bc_bufs[level];
  571. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  572. numrecs = be16_to_cpu(block->bb_numrecs);
  573. #ifdef DEBUG
  574. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  575. return error;
  576. /*
  577. * Check that the new entry is being inserted in the right place.
  578. */
  579. if (ptr <= numrecs) {
  580. if (level == 0) {
  581. rp = XFS_INOBT_REC_ADDR(block, ptr, cur);
  582. xfs_btree_check_rec(cur->bc_btnum, recp, rp);
  583. } else {
  584. kp = XFS_INOBT_KEY_ADDR(block, ptr, cur);
  585. xfs_btree_check_key(cur->bc_btnum, &key, kp);
  586. }
  587. }
  588. #endif
  589. nbno = NULLAGBLOCK;
  590. ncur = NULL;
  591. /*
  592. * If the block is full, we can't insert the new entry until we
  593. * make the block un-full.
  594. */
  595. if (numrecs == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
  596. /*
  597. * First, try shifting an entry to the right neighbor.
  598. */
  599. if ((error = xfs_btree_rshift(cur, level, &i)))
  600. return error;
  601. if (i) {
  602. /* nothing */
  603. }
  604. /*
  605. * Next, try shifting an entry to the left neighbor.
  606. */
  607. else {
  608. if ((error = xfs_inobt_lshift(cur, level, &i)))
  609. return error;
  610. if (i) {
  611. optr = ptr = cur->bc_ptrs[level];
  612. } else {
  613. /*
  614. * Next, try splitting the current block
  615. * in half. If this works we have to
  616. * re-set our variables because
  617. * we could be in a different block now.
  618. */
  619. if ((error = xfs_inobt_split(cur, level, &nbno,
  620. &nkey, &ncur, &i)))
  621. return error;
  622. if (i) {
  623. bp = cur->bc_bufs[level];
  624. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  625. #ifdef DEBUG
  626. if ((error = xfs_btree_check_sblock(cur,
  627. block, level, bp)))
  628. return error;
  629. #endif
  630. ptr = cur->bc_ptrs[level];
  631. nrec.ir_startino = nkey.ir_startino;
  632. } else {
  633. /*
  634. * Otherwise the insert fails.
  635. */
  636. *stat = 0;
  637. return 0;
  638. }
  639. }
  640. }
  641. }
  642. /*
  643. * At this point we know there's room for our new entry in the block
  644. * we're pointing at.
  645. */
  646. numrecs = be16_to_cpu(block->bb_numrecs);
  647. if (level > 0) {
  648. /*
  649. * It's a non-leaf entry. Make a hole for the new data
  650. * in the key and ptr regions of the block.
  651. */
  652. kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
  653. pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
  654. #ifdef DEBUG
  655. for (i = numrecs; i >= ptr; i--) {
  656. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
  657. return error;
  658. }
  659. #endif
  660. memmove(&kp[ptr], &kp[ptr - 1],
  661. (numrecs - ptr + 1) * sizeof(*kp));
  662. memmove(&pp[ptr], &pp[ptr - 1],
  663. (numrecs - ptr + 1) * sizeof(*pp));
  664. /*
  665. * Now stuff the new data in, bump numrecs and log the new data.
  666. */
  667. #ifdef DEBUG
  668. if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
  669. return error;
  670. #endif
  671. kp[ptr - 1] = key;
  672. pp[ptr - 1] = cpu_to_be32(*bnop);
  673. numrecs++;
  674. block->bb_numrecs = cpu_to_be16(numrecs);
  675. xfs_inobt_log_keys(cur, bp, ptr, numrecs);
  676. xfs_inobt_log_ptrs(cur, bp, ptr, numrecs);
  677. } else {
  678. /*
  679. * It's a leaf entry. Make a hole for the new record.
  680. */
  681. rp = XFS_INOBT_REC_ADDR(block, 1, cur);
  682. memmove(&rp[ptr], &rp[ptr - 1],
  683. (numrecs - ptr + 1) * sizeof(*rp));
  684. /*
  685. * Now stuff the new record in, bump numrecs
  686. * and log the new data.
  687. */
  688. rp[ptr - 1] = *recp;
  689. numrecs++;
  690. block->bb_numrecs = cpu_to_be16(numrecs);
  691. xfs_inobt_log_recs(cur, bp, ptr, numrecs);
  692. }
  693. /*
  694. * Log the new number of records in the btree header.
  695. */
  696. xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
  697. #ifdef DEBUG
  698. /*
  699. * Check that the key/record is in the right place, now.
  700. */
  701. if (ptr < numrecs) {
  702. if (level == 0)
  703. xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
  704. rp + ptr);
  705. else
  706. xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
  707. kp + ptr);
  708. }
  709. #endif
  710. /*
  711. * If we inserted at the start of a block, update the parents' keys.
  712. */
  713. if (optr == 1 && (error = xfs_btree_updkey(cur, (union xfs_btree_key *)&key, level + 1)))
  714. return error;
  715. /*
  716. * Return the new block number, if any.
  717. * If there is one, give back a record value and a cursor too.
  718. */
  719. *bnop = nbno;
  720. if (nbno != NULLAGBLOCK) {
  721. *recp = nrec;
  722. *curp = ncur;
  723. }
  724. *stat = 1;
  725. return 0;
  726. }
  727. /*
  728. * Log header fields from a btree block.
  729. */
  730. STATIC void
  731. xfs_inobt_log_block(
  732. xfs_trans_t *tp, /* transaction pointer */
  733. xfs_buf_t *bp, /* buffer containing btree block */
  734. int fields) /* mask of fields: XFS_BB_... */
  735. {
  736. int first; /* first byte offset logged */
  737. int last; /* last byte offset logged */
  738. static const short offsets[] = { /* table of offsets */
  739. offsetof(xfs_inobt_block_t, bb_magic),
  740. offsetof(xfs_inobt_block_t, bb_level),
  741. offsetof(xfs_inobt_block_t, bb_numrecs),
  742. offsetof(xfs_inobt_block_t, bb_leftsib),
  743. offsetof(xfs_inobt_block_t, bb_rightsib),
  744. sizeof(xfs_inobt_block_t)
  745. };
  746. xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
  747. xfs_trans_log_buf(tp, bp, first, last);
  748. }
  749. /*
  750. * Log keys from a btree block (nonleaf).
  751. */
  752. STATIC void
  753. xfs_inobt_log_keys(
  754. xfs_btree_cur_t *cur, /* btree cursor */
  755. xfs_buf_t *bp, /* buffer containing btree block */
  756. int kfirst, /* index of first key to log */
  757. int klast) /* index of last key to log */
  758. {
  759. xfs_inobt_block_t *block; /* btree block to log from */
  760. int first; /* first byte offset logged */
  761. xfs_inobt_key_t *kp; /* key pointer in btree block */
  762. int last; /* last byte offset logged */
  763. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  764. kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
  765. first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
  766. last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
  767. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  768. }
  769. /*
  770. * Log block pointer fields from a btree block (nonleaf).
  771. */
  772. STATIC void
  773. xfs_inobt_log_ptrs(
  774. xfs_btree_cur_t *cur, /* btree cursor */
  775. xfs_buf_t *bp, /* buffer containing btree block */
  776. int pfirst, /* index of first pointer to log */
  777. int plast) /* index of last pointer to log */
  778. {
  779. xfs_inobt_block_t *block; /* btree block to log from */
  780. int first; /* first byte offset logged */
  781. int last; /* last byte offset logged */
  782. xfs_inobt_ptr_t *pp; /* block-pointer pointer in btree blk */
  783. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  784. pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
  785. first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
  786. last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
  787. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  788. }
  789. /*
  790. * Log records from a btree block (leaf).
  791. */
  792. STATIC void
  793. xfs_inobt_log_recs(
  794. xfs_btree_cur_t *cur, /* btree cursor */
  795. xfs_buf_t *bp, /* buffer containing btree block */
  796. int rfirst, /* index of first record to log */
  797. int rlast) /* index of last record to log */
  798. {
  799. xfs_inobt_block_t *block; /* btree block to log from */
  800. int first; /* first byte offset logged */
  801. int last; /* last byte offset logged */
  802. xfs_inobt_rec_t *rp; /* record pointer for btree block */
  803. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  804. rp = XFS_INOBT_REC_ADDR(block, 1, cur);
  805. first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
  806. last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
  807. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  808. }
  809. /*
  810. * Move 1 record left from cur/level if possible.
  811. * Update cur to reflect the new path.
  812. */
  813. STATIC int /* error */
  814. xfs_inobt_lshift(
  815. xfs_btree_cur_t *cur, /* btree cursor */
  816. int level, /* level to shift record on */
  817. int *stat) /* success/failure */
  818. {
  819. int error; /* error return value */
  820. #ifdef DEBUG
  821. int i; /* loop index */
  822. #endif
  823. xfs_inobt_key_t key; /* key value for leaf level upward */
  824. xfs_buf_t *lbp; /* buffer for left neighbor block */
  825. xfs_inobt_block_t *left; /* left neighbor btree block */
  826. xfs_inobt_key_t *lkp=NULL; /* key pointer for left block */
  827. xfs_inobt_ptr_t *lpp; /* address pointer for left block */
  828. xfs_inobt_rec_t *lrp=NULL; /* record pointer for left block */
  829. int nrec; /* new number of left block entries */
  830. xfs_buf_t *rbp; /* buffer for right (current) block */
  831. xfs_inobt_block_t *right; /* right (current) btree block */
  832. xfs_inobt_key_t *rkp=NULL; /* key pointer for right block */
  833. xfs_inobt_ptr_t *rpp=NULL; /* address pointer for right block */
  834. xfs_inobt_rec_t *rrp=NULL; /* record pointer for right block */
  835. /*
  836. * Set up variables for this block as "right".
  837. */
  838. rbp = cur->bc_bufs[level];
  839. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  840. #ifdef DEBUG
  841. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  842. return error;
  843. #endif
  844. /*
  845. * If we've got no left sibling then we can't shift an entry left.
  846. */
  847. if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) {
  848. *stat = 0;
  849. return 0;
  850. }
  851. /*
  852. * If the cursor entry is the one that would be moved, don't
  853. * do it... it's too complicated.
  854. */
  855. if (cur->bc_ptrs[level] <= 1) {
  856. *stat = 0;
  857. return 0;
  858. }
  859. /*
  860. * Set up the left neighbor as "left".
  861. */
  862. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  863. cur->bc_private.a.agno, be32_to_cpu(right->bb_leftsib),
  864. 0, &lbp, XFS_INO_BTREE_REF)))
  865. return error;
  866. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  867. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  868. return error;
  869. /*
  870. * If it's full, it can't take another entry.
  871. */
  872. if (be16_to_cpu(left->bb_numrecs) == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
  873. *stat = 0;
  874. return 0;
  875. }
  876. nrec = be16_to_cpu(left->bb_numrecs) + 1;
  877. /*
  878. * If non-leaf, copy a key and a ptr to the left block.
  879. */
  880. if (level > 0) {
  881. lkp = XFS_INOBT_KEY_ADDR(left, nrec, cur);
  882. rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
  883. *lkp = *rkp;
  884. xfs_inobt_log_keys(cur, lbp, nrec, nrec);
  885. lpp = XFS_INOBT_PTR_ADDR(left, nrec, cur);
  886. rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
  887. #ifdef DEBUG
  888. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level)))
  889. return error;
  890. #endif
  891. *lpp = *rpp;
  892. xfs_inobt_log_ptrs(cur, lbp, nrec, nrec);
  893. }
  894. /*
  895. * If leaf, copy a record to the left block.
  896. */
  897. else {
  898. lrp = XFS_INOBT_REC_ADDR(left, nrec, cur);
  899. rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
  900. *lrp = *rrp;
  901. xfs_inobt_log_recs(cur, lbp, nrec, nrec);
  902. }
  903. /*
  904. * Bump and log left's numrecs, decrement and log right's numrecs.
  905. */
  906. be16_add_cpu(&left->bb_numrecs, 1);
  907. xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
  908. #ifdef DEBUG
  909. if (level > 0)
  910. xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
  911. else
  912. xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
  913. #endif
  914. be16_add_cpu(&right->bb_numrecs, -1);
  915. xfs_inobt_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
  916. /*
  917. * Slide the contents of right down one entry.
  918. */
  919. if (level > 0) {
  920. #ifdef DEBUG
  921. for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
  922. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]),
  923. level)))
  924. return error;
  925. }
  926. #endif
  927. memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  928. memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  929. xfs_inobt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  930. xfs_inobt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  931. } else {
  932. memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  933. xfs_inobt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  934. key.ir_startino = rrp->ir_startino;
  935. rkp = &key;
  936. }
  937. /*
  938. * Update the parent key values of right.
  939. */
  940. if ((error = xfs_btree_updkey(cur, (union xfs_btree_key *)rkp, level + 1)))
  941. return error;
  942. /*
  943. * Slide the cursor value left one.
  944. */
  945. cur->bc_ptrs[level]--;
  946. *stat = 1;
  947. return 0;
  948. }
  949. /*
  950. * Allocate a new root block, fill it in.
  951. */
  952. STATIC int /* error */
  953. xfs_inobt_newroot(
  954. xfs_btree_cur_t *cur, /* btree cursor */
  955. int *stat) /* success/failure */
  956. {
  957. xfs_agi_t *agi; /* a.g. inode header */
  958. xfs_alloc_arg_t args; /* allocation argument structure */
  959. xfs_inobt_block_t *block; /* one half of the old root block */
  960. xfs_buf_t *bp; /* buffer containing block */
  961. int error; /* error return value */
  962. xfs_inobt_key_t *kp; /* btree key pointer */
  963. xfs_agblock_t lbno; /* left block number */
  964. xfs_buf_t *lbp; /* left buffer pointer */
  965. xfs_inobt_block_t *left; /* left btree block */
  966. xfs_buf_t *nbp; /* new (root) buffer */
  967. xfs_inobt_block_t *new; /* new (root) btree block */
  968. int nptr; /* new value for key index, 1 or 2 */
  969. xfs_inobt_ptr_t *pp; /* btree address pointer */
  970. xfs_agblock_t rbno; /* right block number */
  971. xfs_buf_t *rbp; /* right buffer pointer */
  972. xfs_inobt_block_t *right; /* right btree block */
  973. xfs_inobt_rec_t *rp; /* btree record pointer */
  974. ASSERT(cur->bc_nlevels < XFS_IN_MAXLEVELS(cur->bc_mp));
  975. /*
  976. * Get a block & a buffer.
  977. */
  978. agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  979. args.tp = cur->bc_tp;
  980. args.mp = cur->bc_mp;
  981. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno,
  982. be32_to_cpu(agi->agi_root));
  983. args.mod = args.minleft = args.alignment = args.total = args.wasdel =
  984. args.isfl = args.userdata = args.minalignslop = 0;
  985. args.minlen = args.maxlen = args.prod = 1;
  986. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  987. if ((error = xfs_alloc_vextent(&args)))
  988. return error;
  989. /*
  990. * None available, we fail.
  991. */
  992. if (args.fsbno == NULLFSBLOCK) {
  993. *stat = 0;
  994. return 0;
  995. }
  996. ASSERT(args.len == 1);
  997. nbp = xfs_btree_get_bufs(args.mp, args.tp, args.agno, args.agbno, 0);
  998. new = XFS_BUF_TO_INOBT_BLOCK(nbp);
  999. /*
  1000. * Set the root data in the a.g. inode structure.
  1001. */
  1002. agi->agi_root = cpu_to_be32(args.agbno);
  1003. be32_add_cpu(&agi->agi_level, 1);
  1004. xfs_ialloc_log_agi(args.tp, cur->bc_private.a.agbp,
  1005. XFS_AGI_ROOT | XFS_AGI_LEVEL);
  1006. /*
  1007. * At the previous root level there are now two blocks: the old
  1008. * root, and the new block generated when it was split.
  1009. * We don't know which one the cursor is pointing at, so we
  1010. * set up variables "left" and "right" for each case.
  1011. */
  1012. bp = cur->bc_bufs[cur->bc_nlevels - 1];
  1013. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1014. #ifdef DEBUG
  1015. if ((error = xfs_btree_check_sblock(cur, block, cur->bc_nlevels - 1, bp)))
  1016. return error;
  1017. #endif
  1018. if (be32_to_cpu(block->bb_rightsib) != NULLAGBLOCK) {
  1019. /*
  1020. * Our block is left, pick up the right block.
  1021. */
  1022. lbp = bp;
  1023. lbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(lbp));
  1024. left = block;
  1025. rbno = be32_to_cpu(left->bb_rightsib);
  1026. if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
  1027. rbno, 0, &rbp, XFS_INO_BTREE_REF)))
  1028. return error;
  1029. bp = rbp;
  1030. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  1031. if ((error = xfs_btree_check_sblock(cur, right,
  1032. cur->bc_nlevels - 1, rbp)))
  1033. return error;
  1034. nptr = 1;
  1035. } else {
  1036. /*
  1037. * Our block is right, pick up the left block.
  1038. */
  1039. rbp = bp;
  1040. rbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(rbp));
  1041. right = block;
  1042. lbno = be32_to_cpu(right->bb_leftsib);
  1043. if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
  1044. lbno, 0, &lbp, XFS_INO_BTREE_REF)))
  1045. return error;
  1046. bp = lbp;
  1047. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  1048. if ((error = xfs_btree_check_sblock(cur, left,
  1049. cur->bc_nlevels - 1, lbp)))
  1050. return error;
  1051. nptr = 2;
  1052. }
  1053. /*
  1054. * Fill in the new block's btree header and log it.
  1055. */
  1056. new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  1057. new->bb_level = cpu_to_be16(cur->bc_nlevels);
  1058. new->bb_numrecs = cpu_to_be16(2);
  1059. new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  1060. new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  1061. xfs_inobt_log_block(args.tp, nbp, XFS_BB_ALL_BITS);
  1062. ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
  1063. /*
  1064. * Fill in the key data in the new root.
  1065. */
  1066. kp = XFS_INOBT_KEY_ADDR(new, 1, cur);
  1067. if (be16_to_cpu(left->bb_level) > 0) {
  1068. kp[0] = *XFS_INOBT_KEY_ADDR(left, 1, cur);
  1069. kp[1] = *XFS_INOBT_KEY_ADDR(right, 1, cur);
  1070. } else {
  1071. rp = XFS_INOBT_REC_ADDR(left, 1, cur);
  1072. kp[0].ir_startino = rp->ir_startino;
  1073. rp = XFS_INOBT_REC_ADDR(right, 1, cur);
  1074. kp[1].ir_startino = rp->ir_startino;
  1075. }
  1076. xfs_inobt_log_keys(cur, nbp, 1, 2);
  1077. /*
  1078. * Fill in the pointer data in the new root.
  1079. */
  1080. pp = XFS_INOBT_PTR_ADDR(new, 1, cur);
  1081. pp[0] = cpu_to_be32(lbno);
  1082. pp[1] = cpu_to_be32(rbno);
  1083. xfs_inobt_log_ptrs(cur, nbp, 1, 2);
  1084. /*
  1085. * Fix up the cursor.
  1086. */
  1087. xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
  1088. cur->bc_ptrs[cur->bc_nlevels] = nptr;
  1089. cur->bc_nlevels++;
  1090. *stat = 1;
  1091. return 0;
  1092. }
  1093. /*
  1094. * Split cur/level block in half.
  1095. * Return new block number and its first record (to be inserted into parent).
  1096. */
  1097. STATIC int /* error */
  1098. xfs_inobt_split(
  1099. xfs_btree_cur_t *cur, /* btree cursor */
  1100. int level, /* level to split */
  1101. xfs_agblock_t *bnop, /* output: block number allocated */
  1102. xfs_inobt_key_t *keyp, /* output: first key of new block */
  1103. xfs_btree_cur_t **curp, /* output: new cursor */
  1104. int *stat) /* success/failure */
  1105. {
  1106. xfs_alloc_arg_t args; /* allocation argument structure */
  1107. int error; /* error return value */
  1108. int i; /* loop index/record number */
  1109. xfs_agblock_t lbno; /* left (current) block number */
  1110. xfs_buf_t *lbp; /* buffer for left block */
  1111. xfs_inobt_block_t *left; /* left (current) btree block */
  1112. xfs_inobt_key_t *lkp; /* left btree key pointer */
  1113. xfs_inobt_ptr_t *lpp; /* left btree address pointer */
  1114. xfs_inobt_rec_t *lrp; /* left btree record pointer */
  1115. xfs_buf_t *rbp; /* buffer for right block */
  1116. xfs_inobt_block_t *right; /* right (new) btree block */
  1117. xfs_inobt_key_t *rkp; /* right btree key pointer */
  1118. xfs_inobt_ptr_t *rpp; /* right btree address pointer */
  1119. xfs_inobt_rec_t *rrp; /* right btree record pointer */
  1120. /*
  1121. * Set up left block (current one).
  1122. */
  1123. lbp = cur->bc_bufs[level];
  1124. args.tp = cur->bc_tp;
  1125. args.mp = cur->bc_mp;
  1126. lbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(lbp));
  1127. /*
  1128. * Allocate the new block.
  1129. * If we can't do it, we're toast. Give up.
  1130. */
  1131. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, lbno);
  1132. args.mod = args.minleft = args.alignment = args.total = args.wasdel =
  1133. args.isfl = args.userdata = args.minalignslop = 0;
  1134. args.minlen = args.maxlen = args.prod = 1;
  1135. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  1136. if ((error = xfs_alloc_vextent(&args)))
  1137. return error;
  1138. if (args.fsbno == NULLFSBLOCK) {
  1139. *stat = 0;
  1140. return 0;
  1141. }
  1142. ASSERT(args.len == 1);
  1143. rbp = xfs_btree_get_bufs(args.mp, args.tp, args.agno, args.agbno, 0);
  1144. /*
  1145. * Set up the new block as "right".
  1146. */
  1147. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  1148. /*
  1149. * "Left" is the current (according to the cursor) block.
  1150. */
  1151. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  1152. #ifdef DEBUG
  1153. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  1154. return error;
  1155. #endif
  1156. /*
  1157. * Fill in the btree header for the new block.
  1158. */
  1159. right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  1160. right->bb_level = left->bb_level;
  1161. right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
  1162. /*
  1163. * Make sure that if there's an odd number of entries now, that
  1164. * each new block will have the same number of entries.
  1165. */
  1166. if ((be16_to_cpu(left->bb_numrecs) & 1) &&
  1167. cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
  1168. be16_add_cpu(&right->bb_numrecs, 1);
  1169. i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
  1170. /*
  1171. * For non-leaf blocks, copy keys and addresses over to the new block.
  1172. */
  1173. if (level > 0) {
  1174. lkp = XFS_INOBT_KEY_ADDR(left, i, cur);
  1175. lpp = XFS_INOBT_PTR_ADDR(left, i, cur);
  1176. rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
  1177. rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
  1178. #ifdef DEBUG
  1179. for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
  1180. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
  1181. return error;
  1182. }
  1183. #endif
  1184. memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1185. memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1186. xfs_inobt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1187. xfs_inobt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1188. *keyp = *rkp;
  1189. }
  1190. /*
  1191. * For leaf blocks, copy records over to the new block.
  1192. */
  1193. else {
  1194. lrp = XFS_INOBT_REC_ADDR(left, i, cur);
  1195. rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
  1196. memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1197. xfs_inobt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1198. keyp->ir_startino = rrp->ir_startino;
  1199. }
  1200. /*
  1201. * Find the left block number by looking in the buffer.
  1202. * Adjust numrecs, sibling pointers.
  1203. */
  1204. be16_add_cpu(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
  1205. right->bb_rightsib = left->bb_rightsib;
  1206. left->bb_rightsib = cpu_to_be32(args.agbno);
  1207. right->bb_leftsib = cpu_to_be32(lbno);
  1208. xfs_inobt_log_block(args.tp, rbp, XFS_BB_ALL_BITS);
  1209. xfs_inobt_log_block(args.tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  1210. /*
  1211. * If there's a block to the new block's right, make that block
  1212. * point back to right instead of to left.
  1213. */
  1214. if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) {
  1215. xfs_inobt_block_t *rrblock; /* rr btree block */
  1216. xfs_buf_t *rrbp; /* buffer for rrblock */
  1217. if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
  1218. be32_to_cpu(right->bb_rightsib), 0, &rrbp,
  1219. XFS_INO_BTREE_REF)))
  1220. return error;
  1221. rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
  1222. if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
  1223. return error;
  1224. rrblock->bb_leftsib = cpu_to_be32(args.agbno);
  1225. xfs_inobt_log_block(args.tp, rrbp, XFS_BB_LEFTSIB);
  1226. }
  1227. /*
  1228. * If the cursor is really in the right block, move it there.
  1229. * If it's just pointing past the last entry in left, then we'll
  1230. * insert there, so don't change anything in that case.
  1231. */
  1232. if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
  1233. xfs_btree_setbuf(cur, level, rbp);
  1234. cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
  1235. }
  1236. /*
  1237. * If there are more levels, we'll need another cursor which refers
  1238. * the right block, no matter where this cursor was.
  1239. */
  1240. if (level + 1 < cur->bc_nlevels) {
  1241. if ((error = xfs_btree_dup_cursor(cur, curp)))
  1242. return error;
  1243. (*curp)->bc_ptrs[level + 1]++;
  1244. }
  1245. *bnop = args.agbno;
  1246. *stat = 1;
  1247. return 0;
  1248. }
  1249. /*
  1250. * Externally visible routines.
  1251. */
  1252. /*
  1253. * Delete the record pointed to by cur.
  1254. * The cursor refers to the place where the record was (could be inserted)
  1255. * when the operation returns.
  1256. */
  1257. int /* error */
  1258. xfs_inobt_delete(
  1259. xfs_btree_cur_t *cur, /* btree cursor */
  1260. int *stat) /* success/failure */
  1261. {
  1262. int error;
  1263. int i; /* result code */
  1264. int level; /* btree level */
  1265. /*
  1266. * Go up the tree, starting at leaf level.
  1267. * If 2 is returned then a join was done; go to the next level.
  1268. * Otherwise we are done.
  1269. */
  1270. for (level = 0, i = 2; i == 2; level++) {
  1271. if ((error = xfs_inobt_delrec(cur, level, &i)))
  1272. return error;
  1273. }
  1274. if (i == 0) {
  1275. for (level = 1; level < cur->bc_nlevels; level++) {
  1276. if (cur->bc_ptrs[level] == 0) {
  1277. if ((error = xfs_btree_decrement(cur, level, &i)))
  1278. return error;
  1279. break;
  1280. }
  1281. }
  1282. }
  1283. *stat = i;
  1284. return 0;
  1285. }
  1286. /*
  1287. * Get the data from the pointed-to record.
  1288. */
  1289. int /* error */
  1290. xfs_inobt_get_rec(
  1291. xfs_btree_cur_t *cur, /* btree cursor */
  1292. xfs_agino_t *ino, /* output: starting inode of chunk */
  1293. __int32_t *fcnt, /* output: number of free inodes */
  1294. xfs_inofree_t *free, /* output: free inode mask */
  1295. int *stat) /* output: success/failure */
  1296. {
  1297. xfs_inobt_block_t *block; /* btree block */
  1298. xfs_buf_t *bp; /* buffer containing btree block */
  1299. #ifdef DEBUG
  1300. int error; /* error return value */
  1301. #endif
  1302. int ptr; /* record number */
  1303. xfs_inobt_rec_t *rec; /* record data */
  1304. bp = cur->bc_bufs[0];
  1305. ptr = cur->bc_ptrs[0];
  1306. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1307. #ifdef DEBUG
  1308. if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
  1309. return error;
  1310. #endif
  1311. /*
  1312. * Off the right end or left end, return failure.
  1313. */
  1314. if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
  1315. *stat = 0;
  1316. return 0;
  1317. }
  1318. /*
  1319. * Point to the record and extract its data.
  1320. */
  1321. rec = XFS_INOBT_REC_ADDR(block, ptr, cur);
  1322. *ino = be32_to_cpu(rec->ir_startino);
  1323. *fcnt = be32_to_cpu(rec->ir_freecount);
  1324. *free = be64_to_cpu(rec->ir_free);
  1325. *stat = 1;
  1326. return 0;
  1327. }
  1328. /*
  1329. * Insert the current record at the point referenced by cur.
  1330. * The cursor may be inconsistent on return if splits have been done.
  1331. */
  1332. int /* error */
  1333. xfs_inobt_insert(
  1334. xfs_btree_cur_t *cur, /* btree cursor */
  1335. int *stat) /* success/failure */
  1336. {
  1337. int error; /* error return value */
  1338. int i; /* result value, 0 for failure */
  1339. int level; /* current level number in btree */
  1340. xfs_agblock_t nbno; /* new block number (split result) */
  1341. xfs_btree_cur_t *ncur; /* new cursor (split result) */
  1342. xfs_inobt_rec_t nrec; /* record being inserted this level */
  1343. xfs_btree_cur_t *pcur; /* previous level's cursor */
  1344. level = 0;
  1345. nbno = NULLAGBLOCK;
  1346. nrec.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
  1347. nrec.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
  1348. nrec.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
  1349. ncur = NULL;
  1350. pcur = cur;
  1351. /*
  1352. * Loop going up the tree, starting at the leaf level.
  1353. * Stop when we don't get a split block, that must mean that
  1354. * the insert is finished with this level.
  1355. */
  1356. do {
  1357. /*
  1358. * Insert nrec/nbno into this level of the tree.
  1359. * Note if we fail, nbno will be null.
  1360. */
  1361. if ((error = xfs_inobt_insrec(pcur, level++, &nbno, &nrec, &ncur,
  1362. &i))) {
  1363. if (pcur != cur)
  1364. xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
  1365. return error;
  1366. }
  1367. /*
  1368. * See if the cursor we just used is trash.
  1369. * Can't trash the caller's cursor, but otherwise we should
  1370. * if ncur is a new cursor or we're about to be done.
  1371. */
  1372. if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
  1373. cur->bc_nlevels = pcur->bc_nlevels;
  1374. xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
  1375. }
  1376. /*
  1377. * If we got a new cursor, switch to it.
  1378. */
  1379. if (ncur) {
  1380. pcur = ncur;
  1381. ncur = NULL;
  1382. }
  1383. } while (nbno != NULLAGBLOCK);
  1384. *stat = i;
  1385. return 0;
  1386. }
  1387. STATIC struct xfs_btree_cur *
  1388. xfs_inobt_dup_cursor(
  1389. struct xfs_btree_cur *cur)
  1390. {
  1391. return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
  1392. cur->bc_private.a.agbp, cur->bc_private.a.agno);
  1393. }
  1394. STATIC int
  1395. xfs_inobt_get_maxrecs(
  1396. struct xfs_btree_cur *cur,
  1397. int level)
  1398. {
  1399. return cur->bc_mp->m_inobt_mxr[level != 0];
  1400. }
  1401. STATIC void
  1402. xfs_inobt_init_key_from_rec(
  1403. union xfs_btree_key *key,
  1404. union xfs_btree_rec *rec)
  1405. {
  1406. key->inobt.ir_startino = rec->inobt.ir_startino;
  1407. }
  1408. /*
  1409. * intial value of ptr for lookup
  1410. */
  1411. STATIC void
  1412. xfs_inobt_init_ptr_from_cur(
  1413. struct xfs_btree_cur *cur,
  1414. union xfs_btree_ptr *ptr)
  1415. {
  1416. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  1417. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  1418. ptr->s = agi->agi_root;
  1419. }
  1420. STATIC __int64_t
  1421. xfs_inobt_key_diff(
  1422. struct xfs_btree_cur *cur,
  1423. union xfs_btree_key *key)
  1424. {
  1425. return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
  1426. cur->bc_rec.i.ir_startino;
  1427. }
  1428. #ifdef XFS_BTREE_TRACE
  1429. ktrace_t *xfs_inobt_trace_buf;
  1430. STATIC void
  1431. xfs_inobt_trace_enter(
  1432. struct xfs_btree_cur *cur,
  1433. const char *func,
  1434. char *s,
  1435. int type,
  1436. int line,
  1437. __psunsigned_t a0,
  1438. __psunsigned_t a1,
  1439. __psunsigned_t a2,
  1440. __psunsigned_t a3,
  1441. __psunsigned_t a4,
  1442. __psunsigned_t a5,
  1443. __psunsigned_t a6,
  1444. __psunsigned_t a7,
  1445. __psunsigned_t a8,
  1446. __psunsigned_t a9,
  1447. __psunsigned_t a10)
  1448. {
  1449. ktrace_enter(xfs_inobt_trace_buf, (void *)(__psint_t)type,
  1450. (void *)func, (void *)s, NULL, (void *)cur,
  1451. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  1452. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  1453. (void *)a8, (void *)a9, (void *)a10);
  1454. }
  1455. STATIC void
  1456. xfs_inobt_trace_cursor(
  1457. struct xfs_btree_cur *cur,
  1458. __uint32_t *s0,
  1459. __uint64_t *l0,
  1460. __uint64_t *l1)
  1461. {
  1462. *s0 = cur->bc_private.a.agno;
  1463. *l0 = cur->bc_rec.i.ir_startino;
  1464. *l1 = cur->bc_rec.i.ir_free;
  1465. }
  1466. STATIC void
  1467. xfs_inobt_trace_key(
  1468. struct xfs_btree_cur *cur,
  1469. union xfs_btree_key *key,
  1470. __uint64_t *l0,
  1471. __uint64_t *l1)
  1472. {
  1473. *l0 = be32_to_cpu(key->inobt.ir_startino);
  1474. *l1 = 0;
  1475. }
  1476. STATIC void
  1477. xfs_inobt_trace_record(
  1478. struct xfs_btree_cur *cur,
  1479. union xfs_btree_rec *rec,
  1480. __uint64_t *l0,
  1481. __uint64_t *l1,
  1482. __uint64_t *l2)
  1483. {
  1484. *l0 = be32_to_cpu(rec->inobt.ir_startino);
  1485. *l1 = be32_to_cpu(rec->inobt.ir_freecount);
  1486. *l2 = be64_to_cpu(rec->inobt.ir_free);
  1487. }
  1488. #endif /* XFS_BTREE_TRACE */
  1489. static const struct xfs_btree_ops xfs_inobt_ops = {
  1490. .rec_len = sizeof(xfs_inobt_rec_t),
  1491. .key_len = sizeof(xfs_inobt_key_t),
  1492. .dup_cursor = xfs_inobt_dup_cursor,
  1493. .get_maxrecs = xfs_inobt_get_maxrecs,
  1494. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  1495. .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
  1496. .key_diff = xfs_inobt_key_diff,
  1497. #ifdef XFS_BTREE_TRACE
  1498. .trace_enter = xfs_inobt_trace_enter,
  1499. .trace_cursor = xfs_inobt_trace_cursor,
  1500. .trace_key = xfs_inobt_trace_key,
  1501. .trace_record = xfs_inobt_trace_record,
  1502. #endif
  1503. };
  1504. /*
  1505. * Allocate a new inode btree cursor.
  1506. */
  1507. struct xfs_btree_cur * /* new inode btree cursor */
  1508. xfs_inobt_init_cursor(
  1509. struct xfs_mount *mp, /* file system mount point */
  1510. struct xfs_trans *tp, /* transaction pointer */
  1511. struct xfs_buf *agbp, /* buffer for agi structure */
  1512. xfs_agnumber_t agno) /* allocation group number */
  1513. {
  1514. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1515. struct xfs_btree_cur *cur;
  1516. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  1517. cur->bc_tp = tp;
  1518. cur->bc_mp = mp;
  1519. cur->bc_nlevels = be32_to_cpu(agi->agi_level);
  1520. cur->bc_btnum = XFS_BTNUM_INO;
  1521. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  1522. cur->bc_ops = &xfs_inobt_ops;
  1523. cur->bc_private.a.agbp = agbp;
  1524. cur->bc_private.a.agno = agno;
  1525. return cur;
  1526. }