xfs_ialloc_btree.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. * Log header fields from a btree block.
  510. */
  511. STATIC void
  512. xfs_inobt_log_block(
  513. xfs_trans_t *tp, /* transaction pointer */
  514. xfs_buf_t *bp, /* buffer containing btree block */
  515. int fields) /* mask of fields: XFS_BB_... */
  516. {
  517. int first; /* first byte offset logged */
  518. int last; /* last byte offset logged */
  519. static const short offsets[] = { /* table of offsets */
  520. offsetof(xfs_inobt_block_t, bb_magic),
  521. offsetof(xfs_inobt_block_t, bb_level),
  522. offsetof(xfs_inobt_block_t, bb_numrecs),
  523. offsetof(xfs_inobt_block_t, bb_leftsib),
  524. offsetof(xfs_inobt_block_t, bb_rightsib),
  525. sizeof(xfs_inobt_block_t)
  526. };
  527. xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
  528. xfs_trans_log_buf(tp, bp, first, last);
  529. }
  530. /*
  531. * Log keys from a btree block (nonleaf).
  532. */
  533. STATIC void
  534. xfs_inobt_log_keys(
  535. xfs_btree_cur_t *cur, /* btree cursor */
  536. xfs_buf_t *bp, /* buffer containing btree block */
  537. int kfirst, /* index of first key to log */
  538. int klast) /* index of last key to log */
  539. {
  540. xfs_inobt_block_t *block; /* btree block to log from */
  541. int first; /* first byte offset logged */
  542. xfs_inobt_key_t *kp; /* key pointer in btree block */
  543. int last; /* last byte offset logged */
  544. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  545. kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
  546. first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
  547. last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
  548. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  549. }
  550. /*
  551. * Log block pointer fields from a btree block (nonleaf).
  552. */
  553. STATIC void
  554. xfs_inobt_log_ptrs(
  555. xfs_btree_cur_t *cur, /* btree cursor */
  556. xfs_buf_t *bp, /* buffer containing btree block */
  557. int pfirst, /* index of first pointer to log */
  558. int plast) /* index of last pointer to log */
  559. {
  560. xfs_inobt_block_t *block; /* btree block to log from */
  561. int first; /* first byte offset logged */
  562. int last; /* last byte offset logged */
  563. xfs_inobt_ptr_t *pp; /* block-pointer pointer in btree blk */
  564. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  565. pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
  566. first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
  567. last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
  568. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  569. }
  570. /*
  571. * Log records from a btree block (leaf).
  572. */
  573. STATIC void
  574. xfs_inobt_log_recs(
  575. xfs_btree_cur_t *cur, /* btree cursor */
  576. xfs_buf_t *bp, /* buffer containing btree block */
  577. int rfirst, /* index of first record to log */
  578. int rlast) /* index of last record to log */
  579. {
  580. xfs_inobt_block_t *block; /* btree block to log from */
  581. int first; /* first byte offset logged */
  582. int last; /* last byte offset logged */
  583. xfs_inobt_rec_t *rp; /* record pointer for btree block */
  584. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  585. rp = XFS_INOBT_REC_ADDR(block, 1, cur);
  586. first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
  587. last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
  588. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  589. }
  590. /*
  591. * Externally visible routines.
  592. */
  593. /*
  594. * Delete the record pointed to by cur.
  595. * The cursor refers to the place where the record was (could be inserted)
  596. * when the operation returns.
  597. */
  598. int /* error */
  599. xfs_inobt_delete(
  600. xfs_btree_cur_t *cur, /* btree cursor */
  601. int *stat) /* success/failure */
  602. {
  603. int error;
  604. int i; /* result code */
  605. int level; /* btree level */
  606. /*
  607. * Go up the tree, starting at leaf level.
  608. * If 2 is returned then a join was done; go to the next level.
  609. * Otherwise we are done.
  610. */
  611. for (level = 0, i = 2; i == 2; level++) {
  612. if ((error = xfs_inobt_delrec(cur, level, &i)))
  613. return error;
  614. }
  615. if (i == 0) {
  616. for (level = 1; level < cur->bc_nlevels; level++) {
  617. if (cur->bc_ptrs[level] == 0) {
  618. if ((error = xfs_btree_decrement(cur, level, &i)))
  619. return error;
  620. break;
  621. }
  622. }
  623. }
  624. *stat = i;
  625. return 0;
  626. }
  627. /*
  628. * Get the data from the pointed-to record.
  629. */
  630. int /* error */
  631. xfs_inobt_get_rec(
  632. xfs_btree_cur_t *cur, /* btree cursor */
  633. xfs_agino_t *ino, /* output: starting inode of chunk */
  634. __int32_t *fcnt, /* output: number of free inodes */
  635. xfs_inofree_t *free, /* output: free inode mask */
  636. int *stat) /* output: success/failure */
  637. {
  638. xfs_inobt_block_t *block; /* btree block */
  639. xfs_buf_t *bp; /* buffer containing btree block */
  640. #ifdef DEBUG
  641. int error; /* error return value */
  642. #endif
  643. int ptr; /* record number */
  644. xfs_inobt_rec_t *rec; /* record data */
  645. bp = cur->bc_bufs[0];
  646. ptr = cur->bc_ptrs[0];
  647. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  648. #ifdef DEBUG
  649. if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
  650. return error;
  651. #endif
  652. /*
  653. * Off the right end or left end, return failure.
  654. */
  655. if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
  656. *stat = 0;
  657. return 0;
  658. }
  659. /*
  660. * Point to the record and extract its data.
  661. */
  662. rec = XFS_INOBT_REC_ADDR(block, ptr, cur);
  663. *ino = be32_to_cpu(rec->ir_startino);
  664. *fcnt = be32_to_cpu(rec->ir_freecount);
  665. *free = be64_to_cpu(rec->ir_free);
  666. *stat = 1;
  667. return 0;
  668. }
  669. STATIC struct xfs_btree_cur *
  670. xfs_inobt_dup_cursor(
  671. struct xfs_btree_cur *cur)
  672. {
  673. return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
  674. cur->bc_private.a.agbp, cur->bc_private.a.agno);
  675. }
  676. STATIC void
  677. xfs_inobt_set_root(
  678. struct xfs_btree_cur *cur,
  679. union xfs_btree_ptr *nptr,
  680. int inc) /* level change */
  681. {
  682. struct xfs_buf *agbp = cur->bc_private.a.agbp;
  683. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  684. agi->agi_root = nptr->s;
  685. be32_add_cpu(&agi->agi_level, inc);
  686. xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
  687. }
  688. STATIC int
  689. xfs_inobt_alloc_block(
  690. struct xfs_btree_cur *cur,
  691. union xfs_btree_ptr *start,
  692. union xfs_btree_ptr *new,
  693. int length,
  694. int *stat)
  695. {
  696. xfs_alloc_arg_t args; /* block allocation args */
  697. int error; /* error return value */
  698. xfs_agblock_t sbno = be32_to_cpu(start->s);
  699. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  700. memset(&args, 0, sizeof(args));
  701. args.tp = cur->bc_tp;
  702. args.mp = cur->bc_mp;
  703. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.a.agno, sbno);
  704. args.minlen = 1;
  705. args.maxlen = 1;
  706. args.prod = 1;
  707. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  708. error = xfs_alloc_vextent(&args);
  709. if (error) {
  710. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  711. return error;
  712. }
  713. if (args.fsbno == NULLFSBLOCK) {
  714. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  715. *stat = 0;
  716. return 0;
  717. }
  718. ASSERT(args.len == 1);
  719. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  720. new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
  721. *stat = 1;
  722. return 0;
  723. }
  724. STATIC int
  725. xfs_inobt_free_block(
  726. struct xfs_btree_cur *cur,
  727. struct xfs_buf *bp)
  728. {
  729. xfs_fsblock_t fsbno;
  730. int error;
  731. fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, XFS_BUF_ADDR(bp));
  732. error = xfs_free_extent(cur->bc_tp, fsbno, 1);
  733. if (error)
  734. return error;
  735. xfs_trans_binval(cur->bc_tp, bp);
  736. return error;
  737. }
  738. STATIC int
  739. xfs_inobt_get_maxrecs(
  740. struct xfs_btree_cur *cur,
  741. int level)
  742. {
  743. return cur->bc_mp->m_inobt_mxr[level != 0];
  744. }
  745. STATIC void
  746. xfs_inobt_init_key_from_rec(
  747. union xfs_btree_key *key,
  748. union xfs_btree_rec *rec)
  749. {
  750. key->inobt.ir_startino = rec->inobt.ir_startino;
  751. }
  752. STATIC void
  753. xfs_inobt_init_rec_from_key(
  754. union xfs_btree_key *key,
  755. union xfs_btree_rec *rec)
  756. {
  757. rec->inobt.ir_startino = key->inobt.ir_startino;
  758. }
  759. STATIC void
  760. xfs_inobt_init_rec_from_cur(
  761. struct xfs_btree_cur *cur,
  762. union xfs_btree_rec *rec)
  763. {
  764. rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
  765. rec->inobt.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
  766. rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
  767. }
  768. /*
  769. * intial value of ptr for lookup
  770. */
  771. STATIC void
  772. xfs_inobt_init_ptr_from_cur(
  773. struct xfs_btree_cur *cur,
  774. union xfs_btree_ptr *ptr)
  775. {
  776. struct xfs_agi *agi = XFS_BUF_TO_AGI(cur->bc_private.a.agbp);
  777. ASSERT(cur->bc_private.a.agno == be32_to_cpu(agi->agi_seqno));
  778. ptr->s = agi->agi_root;
  779. }
  780. STATIC __int64_t
  781. xfs_inobt_key_diff(
  782. struct xfs_btree_cur *cur,
  783. union xfs_btree_key *key)
  784. {
  785. return (__int64_t)be32_to_cpu(key->inobt.ir_startino) -
  786. cur->bc_rec.i.ir_startino;
  787. }
  788. #ifdef XFS_BTREE_TRACE
  789. ktrace_t *xfs_inobt_trace_buf;
  790. STATIC void
  791. xfs_inobt_trace_enter(
  792. struct xfs_btree_cur *cur,
  793. const char *func,
  794. char *s,
  795. int type,
  796. int line,
  797. __psunsigned_t a0,
  798. __psunsigned_t a1,
  799. __psunsigned_t a2,
  800. __psunsigned_t a3,
  801. __psunsigned_t a4,
  802. __psunsigned_t a5,
  803. __psunsigned_t a6,
  804. __psunsigned_t a7,
  805. __psunsigned_t a8,
  806. __psunsigned_t a9,
  807. __psunsigned_t a10)
  808. {
  809. ktrace_enter(xfs_inobt_trace_buf, (void *)(__psint_t)type,
  810. (void *)func, (void *)s, NULL, (void *)cur,
  811. (void *)a0, (void *)a1, (void *)a2, (void *)a3,
  812. (void *)a4, (void *)a5, (void *)a6, (void *)a7,
  813. (void *)a8, (void *)a9, (void *)a10);
  814. }
  815. STATIC void
  816. xfs_inobt_trace_cursor(
  817. struct xfs_btree_cur *cur,
  818. __uint32_t *s0,
  819. __uint64_t *l0,
  820. __uint64_t *l1)
  821. {
  822. *s0 = cur->bc_private.a.agno;
  823. *l0 = cur->bc_rec.i.ir_startino;
  824. *l1 = cur->bc_rec.i.ir_free;
  825. }
  826. STATIC void
  827. xfs_inobt_trace_key(
  828. struct xfs_btree_cur *cur,
  829. union xfs_btree_key *key,
  830. __uint64_t *l0,
  831. __uint64_t *l1)
  832. {
  833. *l0 = be32_to_cpu(key->inobt.ir_startino);
  834. *l1 = 0;
  835. }
  836. STATIC void
  837. xfs_inobt_trace_record(
  838. struct xfs_btree_cur *cur,
  839. union xfs_btree_rec *rec,
  840. __uint64_t *l0,
  841. __uint64_t *l1,
  842. __uint64_t *l2)
  843. {
  844. *l0 = be32_to_cpu(rec->inobt.ir_startino);
  845. *l1 = be32_to_cpu(rec->inobt.ir_freecount);
  846. *l2 = be64_to_cpu(rec->inobt.ir_free);
  847. }
  848. #endif /* XFS_BTREE_TRACE */
  849. static const struct xfs_btree_ops xfs_inobt_ops = {
  850. .rec_len = sizeof(xfs_inobt_rec_t),
  851. .key_len = sizeof(xfs_inobt_key_t),
  852. .dup_cursor = xfs_inobt_dup_cursor,
  853. .set_root = xfs_inobt_set_root,
  854. .alloc_block = xfs_inobt_alloc_block,
  855. .free_block = xfs_inobt_free_block,
  856. .get_maxrecs = xfs_inobt_get_maxrecs,
  857. .init_key_from_rec = xfs_inobt_init_key_from_rec,
  858. .init_rec_from_key = xfs_inobt_init_rec_from_key,
  859. .init_rec_from_cur = xfs_inobt_init_rec_from_cur,
  860. .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur,
  861. .key_diff = xfs_inobt_key_diff,
  862. #ifdef XFS_BTREE_TRACE
  863. .trace_enter = xfs_inobt_trace_enter,
  864. .trace_cursor = xfs_inobt_trace_cursor,
  865. .trace_key = xfs_inobt_trace_key,
  866. .trace_record = xfs_inobt_trace_record,
  867. #endif
  868. };
  869. /*
  870. * Allocate a new inode btree cursor.
  871. */
  872. struct xfs_btree_cur * /* new inode btree cursor */
  873. xfs_inobt_init_cursor(
  874. struct xfs_mount *mp, /* file system mount point */
  875. struct xfs_trans *tp, /* transaction pointer */
  876. struct xfs_buf *agbp, /* buffer for agi structure */
  877. xfs_agnumber_t agno) /* allocation group number */
  878. {
  879. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  880. struct xfs_btree_cur *cur;
  881. cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
  882. cur->bc_tp = tp;
  883. cur->bc_mp = mp;
  884. cur->bc_nlevels = be32_to_cpu(agi->agi_level);
  885. cur->bc_btnum = XFS_BTNUM_INO;
  886. cur->bc_blocklog = mp->m_sb.sb_blocklog;
  887. cur->bc_ops = &xfs_inobt_ops;
  888. cur->bc_private.a.agbp = agbp;
  889. cur->bc_private.a.agno = agno;
  890. return cur;
  891. }