xfs_ialloc_btree.c 41 KB

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