xfs_ialloc_btree.c 32 KB

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