xfs_ialloc_btree.c 37 KB

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