xfs_alloc_btree.c 37 KB

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