xfs_ialloc_btree.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. /*
  2. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_error.h"
  41. STATIC void xfs_inobt_log_block(xfs_trans_t *, xfs_buf_t *, int);
  42. STATIC void xfs_inobt_log_keys(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  43. STATIC void xfs_inobt_log_ptrs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  44. STATIC void xfs_inobt_log_recs(xfs_btree_cur_t *, xfs_buf_t *, int, int);
  45. STATIC int xfs_inobt_lshift(xfs_btree_cur_t *, int, int *);
  46. STATIC int xfs_inobt_newroot(xfs_btree_cur_t *, int *);
  47. STATIC int xfs_inobt_rshift(xfs_btree_cur_t *, int, int *);
  48. STATIC int xfs_inobt_split(xfs_btree_cur_t *, int, xfs_agblock_t *,
  49. xfs_inobt_key_t *, xfs_btree_cur_t **, int *);
  50. STATIC int xfs_inobt_updkey(xfs_btree_cur_t *, xfs_inobt_key_t *, int);
  51. /*
  52. * Single level of the xfs_inobt_delete record deletion routine.
  53. * Delete record pointed to by cur/level.
  54. * Remove the record from its block then rebalance the tree.
  55. * Return 0 for error, 1 for done, 2 to go on to the next level.
  56. */
  57. STATIC int /* error */
  58. xfs_inobt_delrec(
  59. xfs_btree_cur_t *cur, /* btree cursor */
  60. int level, /* level removing record from */
  61. int *stat) /* fail/done/go-on */
  62. {
  63. xfs_buf_t *agbp; /* buffer for a.g. inode header */
  64. xfs_mount_t *mp; /* mount structure */
  65. xfs_agi_t *agi; /* allocation group inode header */
  66. xfs_inobt_block_t *block; /* btree block record/key lives in */
  67. xfs_agblock_t bno; /* btree block number */
  68. xfs_buf_t *bp; /* buffer for block */
  69. int error; /* error return value */
  70. int i; /* loop index */
  71. xfs_inobt_key_t key; /* kp points here if block is level 0 */
  72. xfs_inobt_key_t *kp = NULL; /* pointer to btree keys */
  73. xfs_agblock_t lbno; /* left block's block number */
  74. xfs_buf_t *lbp; /* left block's buffer pointer */
  75. xfs_inobt_block_t *left; /* left btree block */
  76. xfs_inobt_key_t *lkp; /* left block key pointer */
  77. xfs_inobt_ptr_t *lpp; /* left block address pointer */
  78. int lrecs = 0; /* number of records in left block */
  79. xfs_inobt_rec_t *lrp; /* left block record pointer */
  80. xfs_inobt_ptr_t *pp = NULL; /* pointer to btree addresses */
  81. int ptr; /* index in btree block for this rec */
  82. xfs_agblock_t rbno; /* right block's block number */
  83. xfs_buf_t *rbp; /* right block's buffer pointer */
  84. xfs_inobt_block_t *right; /* right btree block */
  85. xfs_inobt_key_t *rkp; /* right block key pointer */
  86. xfs_inobt_rec_t *rp; /* pointer to btree records */
  87. xfs_inobt_ptr_t *rpp; /* right block address pointer */
  88. int rrecs = 0; /* number of records in right block */
  89. int numrecs;
  90. xfs_inobt_rec_t *rrp; /* right block record pointer */
  91. xfs_btree_cur_t *tcur; /* temporary btree cursor */
  92. mp = cur->bc_mp;
  93. /*
  94. * Get the index of the entry being deleted, check for nothing there.
  95. */
  96. ptr = cur->bc_ptrs[level];
  97. if (ptr == 0) {
  98. *stat = 0;
  99. return 0;
  100. }
  101. /*
  102. * Get the buffer & block containing the record or key/ptr.
  103. */
  104. bp = cur->bc_bufs[level];
  105. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  106. #ifdef DEBUG
  107. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  108. return error;
  109. #endif
  110. /*
  111. * Fail if we're off the end of the block.
  112. */
  113. numrecs = be16_to_cpu(block->bb_numrecs);
  114. if (ptr > numrecs) {
  115. *stat = 0;
  116. return 0;
  117. }
  118. /*
  119. * It's a nonleaf. Excise the key and ptr being deleted, by
  120. * sliding the entries past them down one.
  121. * Log the changed areas of the block.
  122. */
  123. if (level > 0) {
  124. kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
  125. pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
  126. #ifdef DEBUG
  127. for (i = ptr; i < numrecs; i++) {
  128. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i]), level)))
  129. return error;
  130. }
  131. #endif
  132. if (ptr < numrecs) {
  133. memmove(&kp[ptr - 1], &kp[ptr],
  134. (numrecs - ptr) * sizeof(*kp));
  135. memmove(&pp[ptr - 1], &pp[ptr],
  136. (numrecs - ptr) * sizeof(*kp));
  137. xfs_inobt_log_keys(cur, bp, ptr, numrecs - 1);
  138. xfs_inobt_log_ptrs(cur, bp, ptr, numrecs - 1);
  139. }
  140. }
  141. /*
  142. * It's a leaf. Excise the record being deleted, by sliding the
  143. * entries past it down one. Log the changed areas of the block.
  144. */
  145. else {
  146. rp = XFS_INOBT_REC_ADDR(block, 1, cur);
  147. if (ptr < numrecs) {
  148. memmove(&rp[ptr - 1], &rp[ptr],
  149. (numrecs - ptr) * sizeof(*rp));
  150. xfs_inobt_log_recs(cur, bp, ptr, numrecs - 1);
  151. }
  152. /*
  153. * If it's the first record in the block, we'll need a key
  154. * structure to pass up to the next level (updkey).
  155. */
  156. if (ptr == 1) {
  157. key.ir_startino = rp->ir_startino;
  158. kp = &key;
  159. }
  160. }
  161. /*
  162. * Decrement and log the number of entries in the block.
  163. */
  164. numrecs--;
  165. block->bb_numrecs = cpu_to_be16(numrecs);
  166. xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
  167. /*
  168. * Is this the root level? If so, we're almost done.
  169. */
  170. if (level == cur->bc_nlevels - 1) {
  171. /*
  172. * If this is the root level,
  173. * and there's only one entry left,
  174. * and it's NOT the leaf level,
  175. * then we can get rid of this level.
  176. */
  177. if (numrecs == 1 && level > 0) {
  178. agbp = cur->bc_private.i.agbp;
  179. agi = XFS_BUF_TO_AGI(agbp);
  180. /*
  181. * pp is still set to the first pointer in the block.
  182. * Make it the new root of the btree.
  183. */
  184. bno = be32_to_cpu(agi->agi_root);
  185. agi->agi_root = *pp;
  186. be32_add(&agi->agi_level, -1);
  187. /*
  188. * Free the block.
  189. */
  190. if ((error = xfs_free_extent(cur->bc_tp,
  191. XFS_AGB_TO_FSB(mp, cur->bc_private.i.agno, bno), 1)))
  192. return error;
  193. xfs_trans_binval(cur->bc_tp, bp);
  194. xfs_ialloc_log_agi(cur->bc_tp, agbp,
  195. XFS_AGI_ROOT | XFS_AGI_LEVEL);
  196. /*
  197. * Update the cursor so there's one fewer level.
  198. */
  199. cur->bc_bufs[level] = NULL;
  200. cur->bc_nlevels--;
  201. } else if (level > 0 &&
  202. (error = xfs_inobt_decrement(cur, level, &i)))
  203. return error;
  204. *stat = 1;
  205. return 0;
  206. }
  207. /*
  208. * If we deleted the leftmost entry in the block, update the
  209. * key values above us in the tree.
  210. */
  211. if (ptr == 1 && (error = xfs_inobt_updkey(cur, kp, level + 1)))
  212. return error;
  213. /*
  214. * If the number of records remaining in the block is at least
  215. * the minimum, we're done.
  216. */
  217. if (numrecs >= XFS_INOBT_BLOCK_MINRECS(level, cur)) {
  218. if (level > 0 &&
  219. (error = xfs_inobt_decrement(cur, level, &i)))
  220. return error;
  221. *stat = 1;
  222. return 0;
  223. }
  224. /*
  225. * Otherwise, we have to move some records around to keep the
  226. * tree balanced. Look at the left and right sibling blocks to
  227. * see if we can re-balance by moving only one record.
  228. */
  229. rbno = be32_to_cpu(block->bb_rightsib);
  230. lbno = be32_to_cpu(block->bb_leftsib);
  231. bno = NULLAGBLOCK;
  232. ASSERT(rbno != NULLAGBLOCK || lbno != NULLAGBLOCK);
  233. /*
  234. * Duplicate the cursor so our btree manipulations here won't
  235. * disrupt the next level up.
  236. */
  237. if ((error = xfs_btree_dup_cursor(cur, &tcur)))
  238. return error;
  239. /*
  240. * If there's a right sibling, see if it's ok to shift an entry
  241. * out of it.
  242. */
  243. if (rbno != NULLAGBLOCK) {
  244. /*
  245. * Move the temp cursor to the last entry in the next block.
  246. * Actually any entry but the first would suffice.
  247. */
  248. i = xfs_btree_lastrec(tcur, level);
  249. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  250. if ((error = xfs_inobt_increment(tcur, level, &i)))
  251. goto error0;
  252. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  253. i = xfs_btree_lastrec(tcur, level);
  254. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  255. /*
  256. * Grab a pointer to the block.
  257. */
  258. rbp = tcur->bc_bufs[level];
  259. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  260. #ifdef DEBUG
  261. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  262. goto error0;
  263. #endif
  264. /*
  265. * Grab the current block number, for future use.
  266. */
  267. bno = be32_to_cpu(right->bb_leftsib);
  268. /*
  269. * If right block is full enough so that removing one entry
  270. * won't make it too empty, and left-shifting an entry out
  271. * of right to us works, we're done.
  272. */
  273. if (be16_to_cpu(right->bb_numrecs) - 1 >=
  274. XFS_INOBT_BLOCK_MINRECS(level, cur)) {
  275. if ((error = xfs_inobt_lshift(tcur, level, &i)))
  276. goto error0;
  277. if (i) {
  278. ASSERT(be16_to_cpu(block->bb_numrecs) >=
  279. XFS_INOBT_BLOCK_MINRECS(level, cur));
  280. xfs_btree_del_cursor(tcur,
  281. XFS_BTREE_NOERROR);
  282. if (level > 0 &&
  283. (error = xfs_inobt_decrement(cur, level,
  284. &i)))
  285. return error;
  286. *stat = 1;
  287. return 0;
  288. }
  289. }
  290. /*
  291. * Otherwise, grab the number of records in right for
  292. * future reference, and fix up the temp cursor to point
  293. * to our block again (last record).
  294. */
  295. rrecs = be16_to_cpu(right->bb_numrecs);
  296. if (lbno != NULLAGBLOCK) {
  297. xfs_btree_firstrec(tcur, level);
  298. if ((error = xfs_inobt_decrement(tcur, level, &i)))
  299. goto error0;
  300. }
  301. }
  302. /*
  303. * If there's a left sibling, see if it's ok to shift an entry
  304. * out of it.
  305. */
  306. if (lbno != NULLAGBLOCK) {
  307. /*
  308. * Move the temp cursor to the first entry in the
  309. * previous block.
  310. */
  311. xfs_btree_firstrec(tcur, level);
  312. if ((error = xfs_inobt_decrement(tcur, level, &i)))
  313. goto error0;
  314. xfs_btree_firstrec(tcur, level);
  315. /*
  316. * Grab a pointer to the block.
  317. */
  318. lbp = tcur->bc_bufs[level];
  319. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  320. #ifdef DEBUG
  321. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  322. goto error0;
  323. #endif
  324. /*
  325. * Grab the current block number, for future use.
  326. */
  327. bno = be32_to_cpu(left->bb_rightsib);
  328. /*
  329. * If left block is full enough so that removing one entry
  330. * won't make it too empty, and right-shifting an entry out
  331. * of left to us works, we're done.
  332. */
  333. if (be16_to_cpu(left->bb_numrecs) - 1 >=
  334. XFS_INOBT_BLOCK_MINRECS(level, cur)) {
  335. if ((error = xfs_inobt_rshift(tcur, level, &i)))
  336. goto error0;
  337. if (i) {
  338. ASSERT(be16_to_cpu(block->bb_numrecs) >=
  339. XFS_INOBT_BLOCK_MINRECS(level, cur));
  340. xfs_btree_del_cursor(tcur,
  341. XFS_BTREE_NOERROR);
  342. if (level == 0)
  343. cur->bc_ptrs[0]++;
  344. *stat = 1;
  345. return 0;
  346. }
  347. }
  348. /*
  349. * Otherwise, grab the number of records in right for
  350. * future reference.
  351. */
  352. lrecs = be16_to_cpu(left->bb_numrecs);
  353. }
  354. /*
  355. * Delete the temp cursor, we're done with it.
  356. */
  357. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  358. /*
  359. * If here, we need to do a join to keep the tree balanced.
  360. */
  361. ASSERT(bno != NULLAGBLOCK);
  362. /*
  363. * See if we can join with the left neighbor block.
  364. */
  365. if (lbno != NULLAGBLOCK &&
  366. lrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
  367. /*
  368. * Set "right" to be the starting block,
  369. * "left" to be the left neighbor.
  370. */
  371. rbno = bno;
  372. right = block;
  373. rrecs = be16_to_cpu(right->bb_numrecs);
  374. rbp = bp;
  375. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  376. cur->bc_private.i.agno, lbno, 0, &lbp,
  377. XFS_INO_BTREE_REF)))
  378. return error;
  379. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  380. lrecs = be16_to_cpu(left->bb_numrecs);
  381. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  382. return error;
  383. }
  384. /*
  385. * If that won't work, see if we can join with the right neighbor block.
  386. */
  387. else if (rbno != NULLAGBLOCK &&
  388. rrecs + numrecs <= XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
  389. /*
  390. * Set "left" to be the starting block,
  391. * "right" to be the right neighbor.
  392. */
  393. lbno = bno;
  394. left = block;
  395. lrecs = be16_to_cpu(left->bb_numrecs);
  396. lbp = bp;
  397. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  398. cur->bc_private.i.agno, rbno, 0, &rbp,
  399. XFS_INO_BTREE_REF)))
  400. return error;
  401. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  402. rrecs = be16_to_cpu(right->bb_numrecs);
  403. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  404. return error;
  405. }
  406. /*
  407. * Otherwise, we can't fix the imbalance.
  408. * Just return. This is probably a logic error, but it's not fatal.
  409. */
  410. else {
  411. if (level > 0 && (error = xfs_inobt_decrement(cur, level, &i)))
  412. return error;
  413. *stat = 1;
  414. return 0;
  415. }
  416. /*
  417. * We're now going to join "left" and "right" by moving all the stuff
  418. * in "right" to "left" and deleting "right".
  419. */
  420. if (level > 0) {
  421. /*
  422. * It's a non-leaf. Move keys and pointers.
  423. */
  424. lkp = XFS_INOBT_KEY_ADDR(left, lrecs + 1, cur);
  425. lpp = XFS_INOBT_PTR_ADDR(left, lrecs + 1, cur);
  426. rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
  427. rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
  428. #ifdef DEBUG
  429. for (i = 0; i < rrecs; i++) {
  430. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
  431. return error;
  432. }
  433. #endif
  434. memcpy(lkp, rkp, rrecs * sizeof(*lkp));
  435. memcpy(lpp, rpp, rrecs * sizeof(*lpp));
  436. xfs_inobt_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
  437. xfs_inobt_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
  438. } else {
  439. /*
  440. * It's a leaf. Move records.
  441. */
  442. lrp = XFS_INOBT_REC_ADDR(left, lrecs + 1, cur);
  443. rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
  444. memcpy(lrp, rrp, rrecs * sizeof(*lrp));
  445. xfs_inobt_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
  446. }
  447. /*
  448. * If we joined with the left neighbor, set the buffer in the
  449. * cursor to the left block, and fix up the index.
  450. */
  451. if (bp != lbp) {
  452. xfs_btree_setbuf(cur, level, lbp);
  453. cur->bc_ptrs[level] += lrecs;
  454. }
  455. /*
  456. * If we joined with the right neighbor and there's a level above
  457. * us, increment the cursor at that level.
  458. */
  459. else if (level + 1 < cur->bc_nlevels &&
  460. (error = xfs_alloc_increment(cur, level + 1, &i)))
  461. return error;
  462. /*
  463. * Fix up the number of records in the surviving block.
  464. */
  465. lrecs += rrecs;
  466. left->bb_numrecs = cpu_to_be16(lrecs);
  467. /*
  468. * Fix up the right block pointer in the surviving block, and log it.
  469. */
  470. left->bb_rightsib = right->bb_rightsib;
  471. xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  472. /*
  473. * If there is a right sibling now, make it point to the
  474. * remaining block.
  475. */
  476. if (be32_to_cpu(left->bb_rightsib) != NULLAGBLOCK) {
  477. xfs_inobt_block_t *rrblock;
  478. xfs_buf_t *rrbp;
  479. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  480. cur->bc_private.i.agno, be32_to_cpu(left->bb_rightsib), 0,
  481. &rrbp, XFS_INO_BTREE_REF)))
  482. return error;
  483. rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
  484. if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
  485. return error;
  486. rrblock->bb_leftsib = cpu_to_be32(lbno);
  487. xfs_inobt_log_block(cur->bc_tp, rrbp, XFS_BB_LEFTSIB);
  488. }
  489. /*
  490. * Free the deleting block.
  491. */
  492. if ((error = xfs_free_extent(cur->bc_tp, XFS_AGB_TO_FSB(mp,
  493. cur->bc_private.i.agno, rbno), 1)))
  494. return error;
  495. xfs_trans_binval(cur->bc_tp, rbp);
  496. /*
  497. * Readjust the ptr at this level if it's not a leaf, since it's
  498. * still pointing at the deletion point, which makes the cursor
  499. * inconsistent. If this makes the ptr 0, the caller fixes it up.
  500. * We can't use decrement because it would change the next level up.
  501. */
  502. if (level > 0)
  503. cur->bc_ptrs[level]--;
  504. /*
  505. * Return value means the next level up has something to do.
  506. */
  507. *stat = 2;
  508. return 0;
  509. error0:
  510. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  511. return error;
  512. }
  513. /*
  514. * Insert one record/level. Return information to the caller
  515. * allowing the next level up to proceed if necessary.
  516. */
  517. STATIC int /* error */
  518. xfs_inobt_insrec(
  519. xfs_btree_cur_t *cur, /* btree cursor */
  520. int level, /* level to insert record at */
  521. xfs_agblock_t *bnop, /* i/o: block number inserted */
  522. xfs_inobt_rec_t *recp, /* i/o: record data inserted */
  523. xfs_btree_cur_t **curp, /* output: new cursor replacing cur */
  524. int *stat) /* success/failure */
  525. {
  526. xfs_inobt_block_t *block; /* btree block record/key lives in */
  527. xfs_buf_t *bp; /* buffer for block */
  528. int error; /* error return value */
  529. int i; /* loop index */
  530. xfs_inobt_key_t key; /* key value being inserted */
  531. xfs_inobt_key_t *kp=NULL; /* pointer to btree keys */
  532. xfs_agblock_t nbno; /* block number of allocated block */
  533. xfs_btree_cur_t *ncur; /* new cursor to be used at next lvl */
  534. xfs_inobt_key_t nkey; /* new key value, from split */
  535. xfs_inobt_rec_t nrec; /* new record value, for caller */
  536. int numrecs;
  537. int optr; /* old ptr value */
  538. xfs_inobt_ptr_t *pp; /* pointer to btree addresses */
  539. int ptr; /* index in btree block for this rec */
  540. xfs_inobt_rec_t *rp=NULL; /* pointer to btree records */
  541. /*
  542. * GCC doesn't understand the (arguably complex) control flow in
  543. * this function and complains about uninitialized structure fields
  544. * without this.
  545. */
  546. memset(&nrec, 0, sizeof(nrec));
  547. /*
  548. * If we made it to the root level, allocate a new root block
  549. * and we're done.
  550. */
  551. if (level >= cur->bc_nlevels) {
  552. error = xfs_inobt_newroot(cur, &i);
  553. *bnop = NULLAGBLOCK;
  554. *stat = i;
  555. return error;
  556. }
  557. /*
  558. * Make a key out of the record data to be inserted, and save it.
  559. */
  560. key.ir_startino = recp->ir_startino;
  561. optr = ptr = cur->bc_ptrs[level];
  562. /*
  563. * If we're off the left edge, return failure.
  564. */
  565. if (ptr == 0) {
  566. *stat = 0;
  567. return 0;
  568. }
  569. /*
  570. * Get pointers to the btree buffer and block.
  571. */
  572. bp = cur->bc_bufs[level];
  573. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  574. numrecs = be16_to_cpu(block->bb_numrecs);
  575. #ifdef DEBUG
  576. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  577. return error;
  578. /*
  579. * Check that the new entry is being inserted in the right place.
  580. */
  581. if (ptr <= numrecs) {
  582. if (level == 0) {
  583. rp = XFS_INOBT_REC_ADDR(block, ptr, cur);
  584. xfs_btree_check_rec(cur->bc_btnum, recp, rp);
  585. } else {
  586. kp = XFS_INOBT_KEY_ADDR(block, ptr, cur);
  587. xfs_btree_check_key(cur->bc_btnum, &key, kp);
  588. }
  589. }
  590. #endif
  591. nbno = NULLAGBLOCK;
  592. ncur = NULL;
  593. /*
  594. * If the block is full, we can't insert the new entry until we
  595. * make the block un-full.
  596. */
  597. if (numrecs == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
  598. /*
  599. * First, try shifting an entry to the right neighbor.
  600. */
  601. if ((error = xfs_inobt_rshift(cur, level, &i)))
  602. return error;
  603. if (i) {
  604. /* nothing */
  605. }
  606. /*
  607. * Next, try shifting an entry to the left neighbor.
  608. */
  609. else {
  610. if ((error = xfs_inobt_lshift(cur, level, &i)))
  611. return error;
  612. if (i) {
  613. optr = ptr = cur->bc_ptrs[level];
  614. } else {
  615. /*
  616. * Next, try splitting the current block
  617. * in half. If this works we have to
  618. * re-set our variables because
  619. * we could be in a different block now.
  620. */
  621. if ((error = xfs_inobt_split(cur, level, &nbno,
  622. &nkey, &ncur, &i)))
  623. return error;
  624. if (i) {
  625. bp = cur->bc_bufs[level];
  626. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  627. #ifdef DEBUG
  628. if ((error = xfs_btree_check_sblock(cur,
  629. block, level, bp)))
  630. return error;
  631. #endif
  632. ptr = cur->bc_ptrs[level];
  633. nrec.ir_startino = nkey.ir_startino;
  634. } else {
  635. /*
  636. * Otherwise the insert fails.
  637. */
  638. *stat = 0;
  639. return 0;
  640. }
  641. }
  642. }
  643. }
  644. /*
  645. * At this point we know there's room for our new entry in the block
  646. * we're pointing at.
  647. */
  648. numrecs = be16_to_cpu(block->bb_numrecs);
  649. if (level > 0) {
  650. /*
  651. * It's a non-leaf entry. Make a hole for the new data
  652. * in the key and ptr regions of the block.
  653. */
  654. kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
  655. pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
  656. #ifdef DEBUG
  657. for (i = numrecs; i >= ptr; i--) {
  658. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(pp[i - 1]), level)))
  659. return error;
  660. }
  661. #endif
  662. memmove(&kp[ptr], &kp[ptr - 1],
  663. (numrecs - ptr + 1) * sizeof(*kp));
  664. memmove(&pp[ptr], &pp[ptr - 1],
  665. (numrecs - ptr + 1) * sizeof(*pp));
  666. /*
  667. * Now stuff the new data in, bump numrecs and log the new data.
  668. */
  669. #ifdef DEBUG
  670. if ((error = xfs_btree_check_sptr(cur, *bnop, level)))
  671. return error;
  672. #endif
  673. kp[ptr - 1] = key;
  674. pp[ptr - 1] = cpu_to_be32(*bnop);
  675. numrecs++;
  676. block->bb_numrecs = cpu_to_be16(numrecs);
  677. xfs_inobt_log_keys(cur, bp, ptr, numrecs);
  678. xfs_inobt_log_ptrs(cur, bp, ptr, numrecs);
  679. } else {
  680. /*
  681. * It's a leaf entry. Make a hole for the new record.
  682. */
  683. rp = XFS_INOBT_REC_ADDR(block, 1, cur);
  684. memmove(&rp[ptr], &rp[ptr - 1],
  685. (numrecs - ptr + 1) * sizeof(*rp));
  686. /*
  687. * Now stuff the new record in, bump numrecs
  688. * and log the new data.
  689. */
  690. rp[ptr - 1] = *recp;
  691. numrecs++;
  692. block->bb_numrecs = cpu_to_be16(numrecs);
  693. xfs_inobt_log_recs(cur, bp, ptr, numrecs);
  694. }
  695. /*
  696. * Log the new number of records in the btree header.
  697. */
  698. xfs_inobt_log_block(cur->bc_tp, bp, XFS_BB_NUMRECS);
  699. #ifdef DEBUG
  700. /*
  701. * Check that the key/record is in the right place, now.
  702. */
  703. if (ptr < numrecs) {
  704. if (level == 0)
  705. xfs_btree_check_rec(cur->bc_btnum, rp + ptr - 1,
  706. rp + ptr);
  707. else
  708. xfs_btree_check_key(cur->bc_btnum, kp + ptr - 1,
  709. kp + ptr);
  710. }
  711. #endif
  712. /*
  713. * If we inserted at the start of a block, update the parents' keys.
  714. */
  715. if (optr == 1 && (error = xfs_inobt_updkey(cur, &key, level + 1)))
  716. return error;
  717. /*
  718. * Return the new block number, if any.
  719. * If there is one, give back a record value and a cursor too.
  720. */
  721. *bnop = nbno;
  722. if (nbno != NULLAGBLOCK) {
  723. *recp = nrec;
  724. *curp = ncur;
  725. }
  726. *stat = 1;
  727. return 0;
  728. }
  729. /*
  730. * Log header fields from a btree block.
  731. */
  732. STATIC void
  733. xfs_inobt_log_block(
  734. xfs_trans_t *tp, /* transaction pointer */
  735. xfs_buf_t *bp, /* buffer containing btree block */
  736. int fields) /* mask of fields: XFS_BB_... */
  737. {
  738. int first; /* first byte offset logged */
  739. int last; /* last byte offset logged */
  740. static const short offsets[] = { /* table of offsets */
  741. offsetof(xfs_inobt_block_t, bb_magic),
  742. offsetof(xfs_inobt_block_t, bb_level),
  743. offsetof(xfs_inobt_block_t, bb_numrecs),
  744. offsetof(xfs_inobt_block_t, bb_leftsib),
  745. offsetof(xfs_inobt_block_t, bb_rightsib),
  746. sizeof(xfs_inobt_block_t)
  747. };
  748. xfs_btree_offsets(fields, offsets, XFS_BB_NUM_BITS, &first, &last);
  749. xfs_trans_log_buf(tp, bp, first, last);
  750. }
  751. /*
  752. * Log keys from a btree block (nonleaf).
  753. */
  754. STATIC void
  755. xfs_inobt_log_keys(
  756. xfs_btree_cur_t *cur, /* btree cursor */
  757. xfs_buf_t *bp, /* buffer containing btree block */
  758. int kfirst, /* index of first key to log */
  759. int klast) /* index of last key to log */
  760. {
  761. xfs_inobt_block_t *block; /* btree block to log from */
  762. int first; /* first byte offset logged */
  763. xfs_inobt_key_t *kp; /* key pointer in btree block */
  764. int last; /* last byte offset logged */
  765. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  766. kp = XFS_INOBT_KEY_ADDR(block, 1, cur);
  767. first = (int)((xfs_caddr_t)&kp[kfirst - 1] - (xfs_caddr_t)block);
  768. last = (int)(((xfs_caddr_t)&kp[klast] - 1) - (xfs_caddr_t)block);
  769. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  770. }
  771. /*
  772. * Log block pointer fields from a btree block (nonleaf).
  773. */
  774. STATIC void
  775. xfs_inobt_log_ptrs(
  776. xfs_btree_cur_t *cur, /* btree cursor */
  777. xfs_buf_t *bp, /* buffer containing btree block */
  778. int pfirst, /* index of first pointer to log */
  779. int plast) /* index of last pointer to log */
  780. {
  781. xfs_inobt_block_t *block; /* btree block to log from */
  782. int first; /* first byte offset logged */
  783. int last; /* last byte offset logged */
  784. xfs_inobt_ptr_t *pp; /* block-pointer pointer in btree blk */
  785. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  786. pp = XFS_INOBT_PTR_ADDR(block, 1, cur);
  787. first = (int)((xfs_caddr_t)&pp[pfirst - 1] - (xfs_caddr_t)block);
  788. last = (int)(((xfs_caddr_t)&pp[plast] - 1) - (xfs_caddr_t)block);
  789. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  790. }
  791. /*
  792. * Log records from a btree block (leaf).
  793. */
  794. STATIC void
  795. xfs_inobt_log_recs(
  796. xfs_btree_cur_t *cur, /* btree cursor */
  797. xfs_buf_t *bp, /* buffer containing btree block */
  798. int rfirst, /* index of first record to log */
  799. int rlast) /* index of last record to log */
  800. {
  801. xfs_inobt_block_t *block; /* btree block to log from */
  802. int first; /* first byte offset logged */
  803. int last; /* last byte offset logged */
  804. xfs_inobt_rec_t *rp; /* record pointer for btree block */
  805. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  806. rp = XFS_INOBT_REC_ADDR(block, 1, cur);
  807. first = (int)((xfs_caddr_t)&rp[rfirst - 1] - (xfs_caddr_t)block);
  808. last = (int)(((xfs_caddr_t)&rp[rlast] - 1) - (xfs_caddr_t)block);
  809. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  810. }
  811. /*
  812. * Lookup the record. The cursor is made to point to it, based on dir.
  813. * Return 0 if can't find any such record, 1 for success.
  814. */
  815. STATIC int /* error */
  816. xfs_inobt_lookup(
  817. xfs_btree_cur_t *cur, /* btree cursor */
  818. xfs_lookup_t dir, /* <=, ==, or >= */
  819. int *stat) /* success/failure */
  820. {
  821. xfs_agblock_t agbno; /* a.g. relative btree block number */
  822. xfs_agnumber_t agno; /* allocation group number */
  823. xfs_inobt_block_t *block=NULL; /* current btree block */
  824. __int64_t diff; /* difference for the current key */
  825. int error; /* error return value */
  826. int keyno=0; /* current key number */
  827. int level; /* level in the btree */
  828. xfs_mount_t *mp; /* file system mount point */
  829. /*
  830. * Get the allocation group header, and the root block number.
  831. */
  832. mp = cur->bc_mp;
  833. {
  834. xfs_agi_t *agi; /* a.g. inode header */
  835. agi = XFS_BUF_TO_AGI(cur->bc_private.i.agbp);
  836. agno = be32_to_cpu(agi->agi_seqno);
  837. agbno = be32_to_cpu(agi->agi_root);
  838. }
  839. /*
  840. * Iterate over each level in the btree, starting at the root.
  841. * For each level above the leaves, find the key we need, based
  842. * on the lookup record, then follow the corresponding block
  843. * pointer down to the next level.
  844. */
  845. for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
  846. xfs_buf_t *bp; /* buffer pointer for btree block */
  847. xfs_daddr_t d; /* disk address of btree block */
  848. /*
  849. * Get the disk address we're looking for.
  850. */
  851. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  852. /*
  853. * If the old buffer at this level is for a different block,
  854. * throw it away, otherwise just use it.
  855. */
  856. bp = cur->bc_bufs[level];
  857. if (bp && XFS_BUF_ADDR(bp) != d)
  858. bp = NULL;
  859. if (!bp) {
  860. /*
  861. * Need to get a new buffer. Read it, then
  862. * set it in the cursor, releasing the old one.
  863. */
  864. if ((error = xfs_btree_read_bufs(mp, cur->bc_tp,
  865. agno, agbno, 0, &bp, XFS_INO_BTREE_REF)))
  866. return error;
  867. xfs_btree_setbuf(cur, level, bp);
  868. /*
  869. * Point to the btree block, now that we have the buffer
  870. */
  871. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  872. if ((error = xfs_btree_check_sblock(cur, block, level,
  873. bp)))
  874. return error;
  875. } else
  876. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  877. /*
  878. * If we already had a key match at a higher level, we know
  879. * we need to use the first entry in this block.
  880. */
  881. if (diff == 0)
  882. keyno = 1;
  883. /*
  884. * Otherwise we need to search this block. Do a binary search.
  885. */
  886. else {
  887. int high; /* high entry number */
  888. xfs_inobt_key_t *kkbase=NULL;/* base of keys in block */
  889. xfs_inobt_rec_t *krbase=NULL;/* base of records in block */
  890. int low; /* low entry number */
  891. /*
  892. * Get a pointer to keys or records.
  893. */
  894. if (level > 0)
  895. kkbase = XFS_INOBT_KEY_ADDR(block, 1, cur);
  896. else
  897. krbase = XFS_INOBT_REC_ADDR(block, 1, cur);
  898. /*
  899. * Set low and high entry numbers, 1-based.
  900. */
  901. low = 1;
  902. if (!(high = be16_to_cpu(block->bb_numrecs))) {
  903. /*
  904. * If the block is empty, the tree must
  905. * be an empty leaf.
  906. */
  907. ASSERT(level == 0 && cur->bc_nlevels == 1);
  908. cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
  909. *stat = 0;
  910. return 0;
  911. }
  912. /*
  913. * Binary search the block.
  914. */
  915. while (low <= high) {
  916. xfs_agino_t startino; /* key value */
  917. /*
  918. * keyno is average of low and high.
  919. */
  920. keyno = (low + high) >> 1;
  921. /*
  922. * Get startino.
  923. */
  924. if (level > 0) {
  925. xfs_inobt_key_t *kkp;
  926. kkp = kkbase + keyno - 1;
  927. startino = be32_to_cpu(kkp->ir_startino);
  928. } else {
  929. xfs_inobt_rec_t *krp;
  930. krp = krbase + keyno - 1;
  931. startino = be32_to_cpu(krp->ir_startino);
  932. }
  933. /*
  934. * Compute difference to get next direction.
  935. */
  936. diff = (__int64_t)
  937. startino - cur->bc_rec.i.ir_startino;
  938. /*
  939. * Less than, move right.
  940. */
  941. if (diff < 0)
  942. low = keyno + 1;
  943. /*
  944. * Greater than, move left.
  945. */
  946. else if (diff > 0)
  947. high = keyno - 1;
  948. /*
  949. * Equal, we're done.
  950. */
  951. else
  952. break;
  953. }
  954. }
  955. /*
  956. * If there are more levels, set up for the next level
  957. * by getting the block number and filling in the cursor.
  958. */
  959. if (level > 0) {
  960. /*
  961. * If we moved left, need the previous key number,
  962. * unless there isn't one.
  963. */
  964. if (diff > 0 && --keyno < 1)
  965. keyno = 1;
  966. agbno = be32_to_cpu(*XFS_INOBT_PTR_ADDR(block, keyno, cur));
  967. #ifdef DEBUG
  968. if ((error = xfs_btree_check_sptr(cur, agbno, level)))
  969. return error;
  970. #endif
  971. cur->bc_ptrs[level] = keyno;
  972. }
  973. }
  974. /*
  975. * Done with the search.
  976. * See if we need to adjust the results.
  977. */
  978. if (dir != XFS_LOOKUP_LE && diff < 0) {
  979. keyno++;
  980. /*
  981. * If ge search and we went off the end of the block, but it's
  982. * not the last block, we're in the wrong block.
  983. */
  984. if (dir == XFS_LOOKUP_GE &&
  985. keyno > be16_to_cpu(block->bb_numrecs) &&
  986. be32_to_cpu(block->bb_rightsib) != NULLAGBLOCK) {
  987. int i;
  988. cur->bc_ptrs[0] = keyno;
  989. if ((error = xfs_inobt_increment(cur, 0, &i)))
  990. return error;
  991. ASSERT(i == 1);
  992. *stat = 1;
  993. return 0;
  994. }
  995. }
  996. else if (dir == XFS_LOOKUP_LE && diff > 0)
  997. keyno--;
  998. cur->bc_ptrs[0] = keyno;
  999. /*
  1000. * Return if we succeeded or not.
  1001. */
  1002. if (keyno == 0 || keyno > be16_to_cpu(block->bb_numrecs))
  1003. *stat = 0;
  1004. else
  1005. *stat = ((dir != XFS_LOOKUP_EQ) || (diff == 0));
  1006. return 0;
  1007. }
  1008. /*
  1009. * Move 1 record left from cur/level if possible.
  1010. * Update cur to reflect the new path.
  1011. */
  1012. STATIC int /* error */
  1013. xfs_inobt_lshift(
  1014. xfs_btree_cur_t *cur, /* btree cursor */
  1015. int level, /* level to shift record on */
  1016. int *stat) /* success/failure */
  1017. {
  1018. int error; /* error return value */
  1019. #ifdef DEBUG
  1020. int i; /* loop index */
  1021. #endif
  1022. xfs_inobt_key_t key; /* key value for leaf level upward */
  1023. xfs_buf_t *lbp; /* buffer for left neighbor block */
  1024. xfs_inobt_block_t *left; /* left neighbor btree block */
  1025. xfs_inobt_key_t *lkp=NULL; /* key pointer for left block */
  1026. xfs_inobt_ptr_t *lpp; /* address pointer for left block */
  1027. xfs_inobt_rec_t *lrp=NULL; /* record pointer for left block */
  1028. int nrec; /* new number of left block entries */
  1029. xfs_buf_t *rbp; /* buffer for right (current) block */
  1030. xfs_inobt_block_t *right; /* right (current) btree block */
  1031. xfs_inobt_key_t *rkp=NULL; /* key pointer for right block */
  1032. xfs_inobt_ptr_t *rpp=NULL; /* address pointer for right block */
  1033. xfs_inobt_rec_t *rrp=NULL; /* record pointer for right block */
  1034. /*
  1035. * Set up variables for this block as "right".
  1036. */
  1037. rbp = cur->bc_bufs[level];
  1038. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  1039. #ifdef DEBUG
  1040. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  1041. return error;
  1042. #endif
  1043. /*
  1044. * If we've got no left sibling then we can't shift an entry left.
  1045. */
  1046. if (be32_to_cpu(right->bb_leftsib) == NULLAGBLOCK) {
  1047. *stat = 0;
  1048. return 0;
  1049. }
  1050. /*
  1051. * If the cursor entry is the one that would be moved, don't
  1052. * do it... it's too complicated.
  1053. */
  1054. if (cur->bc_ptrs[level] <= 1) {
  1055. *stat = 0;
  1056. return 0;
  1057. }
  1058. /*
  1059. * Set up the left neighbor as "left".
  1060. */
  1061. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1062. cur->bc_private.i.agno, be32_to_cpu(right->bb_leftsib),
  1063. 0, &lbp, XFS_INO_BTREE_REF)))
  1064. return error;
  1065. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  1066. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  1067. return error;
  1068. /*
  1069. * If it's full, it can't take another entry.
  1070. */
  1071. if (be16_to_cpu(left->bb_numrecs) == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
  1072. *stat = 0;
  1073. return 0;
  1074. }
  1075. nrec = be16_to_cpu(left->bb_numrecs) + 1;
  1076. /*
  1077. * If non-leaf, copy a key and a ptr to the left block.
  1078. */
  1079. if (level > 0) {
  1080. lkp = XFS_INOBT_KEY_ADDR(left, nrec, cur);
  1081. rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
  1082. *lkp = *rkp;
  1083. xfs_inobt_log_keys(cur, lbp, nrec, nrec);
  1084. lpp = XFS_INOBT_PTR_ADDR(left, nrec, cur);
  1085. rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
  1086. #ifdef DEBUG
  1087. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*rpp), level)))
  1088. return error;
  1089. #endif
  1090. *lpp = *rpp;
  1091. xfs_inobt_log_ptrs(cur, lbp, nrec, nrec);
  1092. }
  1093. /*
  1094. * If leaf, copy a record to the left block.
  1095. */
  1096. else {
  1097. lrp = XFS_INOBT_REC_ADDR(left, nrec, cur);
  1098. rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
  1099. *lrp = *rrp;
  1100. xfs_inobt_log_recs(cur, lbp, nrec, nrec);
  1101. }
  1102. /*
  1103. * Bump and log left's numrecs, decrement and log right's numrecs.
  1104. */
  1105. be16_add(&left->bb_numrecs, 1);
  1106. xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
  1107. #ifdef DEBUG
  1108. if (level > 0)
  1109. xfs_btree_check_key(cur->bc_btnum, lkp - 1, lkp);
  1110. else
  1111. xfs_btree_check_rec(cur->bc_btnum, lrp - 1, lrp);
  1112. #endif
  1113. be16_add(&right->bb_numrecs, -1);
  1114. xfs_inobt_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
  1115. /*
  1116. * Slide the contents of right down one entry.
  1117. */
  1118. if (level > 0) {
  1119. #ifdef DEBUG
  1120. for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
  1121. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i + 1]),
  1122. level)))
  1123. return error;
  1124. }
  1125. #endif
  1126. memmove(rkp, rkp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1127. memmove(rpp, rpp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1128. xfs_inobt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1129. xfs_inobt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1130. } else {
  1131. memmove(rrp, rrp + 1, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1132. xfs_inobt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1133. key.ir_startino = rrp->ir_startino;
  1134. rkp = &key;
  1135. }
  1136. /*
  1137. * Update the parent key values of right.
  1138. */
  1139. if ((error = xfs_inobt_updkey(cur, rkp, level + 1)))
  1140. return error;
  1141. /*
  1142. * Slide the cursor value left one.
  1143. */
  1144. cur->bc_ptrs[level]--;
  1145. *stat = 1;
  1146. return 0;
  1147. }
  1148. /*
  1149. * Allocate a new root block, fill it in.
  1150. */
  1151. STATIC int /* error */
  1152. xfs_inobt_newroot(
  1153. xfs_btree_cur_t *cur, /* btree cursor */
  1154. int *stat) /* success/failure */
  1155. {
  1156. xfs_agi_t *agi; /* a.g. inode header */
  1157. xfs_alloc_arg_t args; /* allocation argument structure */
  1158. xfs_inobt_block_t *block; /* one half of the old root block */
  1159. xfs_buf_t *bp; /* buffer containing block */
  1160. int error; /* error return value */
  1161. xfs_inobt_key_t *kp; /* btree key pointer */
  1162. xfs_agblock_t lbno; /* left block number */
  1163. xfs_buf_t *lbp; /* left buffer pointer */
  1164. xfs_inobt_block_t *left; /* left btree block */
  1165. xfs_buf_t *nbp; /* new (root) buffer */
  1166. xfs_inobt_block_t *new; /* new (root) btree block */
  1167. int nptr; /* new value for key index, 1 or 2 */
  1168. xfs_inobt_ptr_t *pp; /* btree address pointer */
  1169. xfs_agblock_t rbno; /* right block number */
  1170. xfs_buf_t *rbp; /* right buffer pointer */
  1171. xfs_inobt_block_t *right; /* right btree block */
  1172. xfs_inobt_rec_t *rp; /* btree record pointer */
  1173. ASSERT(cur->bc_nlevels < XFS_IN_MAXLEVELS(cur->bc_mp));
  1174. /*
  1175. * Get a block & a buffer.
  1176. */
  1177. agi = XFS_BUF_TO_AGI(cur->bc_private.i.agbp);
  1178. args.tp = cur->bc_tp;
  1179. args.mp = cur->bc_mp;
  1180. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.i.agno,
  1181. be32_to_cpu(agi->agi_root));
  1182. args.mod = args.minleft = args.alignment = args.total = args.wasdel =
  1183. args.isfl = args.userdata = args.minalignslop = 0;
  1184. args.minlen = args.maxlen = args.prod = 1;
  1185. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  1186. if ((error = xfs_alloc_vextent(&args)))
  1187. return error;
  1188. /*
  1189. * None available, we fail.
  1190. */
  1191. if (args.fsbno == NULLFSBLOCK) {
  1192. *stat = 0;
  1193. return 0;
  1194. }
  1195. ASSERT(args.len == 1);
  1196. nbp = xfs_btree_get_bufs(args.mp, args.tp, args.agno, args.agbno, 0);
  1197. new = XFS_BUF_TO_INOBT_BLOCK(nbp);
  1198. /*
  1199. * Set the root data in the a.g. inode structure.
  1200. */
  1201. agi->agi_root = cpu_to_be32(args.agbno);
  1202. be32_add(&agi->agi_level, 1);
  1203. xfs_ialloc_log_agi(args.tp, cur->bc_private.i.agbp,
  1204. XFS_AGI_ROOT | XFS_AGI_LEVEL);
  1205. /*
  1206. * At the previous root level there are now two blocks: the old
  1207. * root, and the new block generated when it was split.
  1208. * We don't know which one the cursor is pointing at, so we
  1209. * set up variables "left" and "right" for each case.
  1210. */
  1211. bp = cur->bc_bufs[cur->bc_nlevels - 1];
  1212. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1213. #ifdef DEBUG
  1214. if ((error = xfs_btree_check_sblock(cur, block, cur->bc_nlevels - 1, bp)))
  1215. return error;
  1216. #endif
  1217. if (be32_to_cpu(block->bb_rightsib) != NULLAGBLOCK) {
  1218. /*
  1219. * Our block is left, pick up the right block.
  1220. */
  1221. lbp = bp;
  1222. lbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(lbp));
  1223. left = block;
  1224. rbno = be32_to_cpu(left->bb_rightsib);
  1225. if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
  1226. rbno, 0, &rbp, XFS_INO_BTREE_REF)))
  1227. return error;
  1228. bp = rbp;
  1229. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  1230. if ((error = xfs_btree_check_sblock(cur, right,
  1231. cur->bc_nlevels - 1, rbp)))
  1232. return error;
  1233. nptr = 1;
  1234. } else {
  1235. /*
  1236. * Our block is right, pick up the left block.
  1237. */
  1238. rbp = bp;
  1239. rbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(rbp));
  1240. right = block;
  1241. lbno = be32_to_cpu(right->bb_leftsib);
  1242. if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
  1243. lbno, 0, &lbp, XFS_INO_BTREE_REF)))
  1244. return error;
  1245. bp = lbp;
  1246. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  1247. if ((error = xfs_btree_check_sblock(cur, left,
  1248. cur->bc_nlevels - 1, lbp)))
  1249. return error;
  1250. nptr = 2;
  1251. }
  1252. /*
  1253. * Fill in the new block's btree header and log it.
  1254. */
  1255. new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  1256. new->bb_level = cpu_to_be16(cur->bc_nlevels);
  1257. new->bb_numrecs = cpu_to_be16(2);
  1258. new->bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  1259. new->bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  1260. xfs_inobt_log_block(args.tp, nbp, XFS_BB_ALL_BITS);
  1261. ASSERT(lbno != NULLAGBLOCK && rbno != NULLAGBLOCK);
  1262. /*
  1263. * Fill in the key data in the new root.
  1264. */
  1265. kp = XFS_INOBT_KEY_ADDR(new, 1, cur);
  1266. if (be16_to_cpu(left->bb_level) > 0) {
  1267. kp[0] = *XFS_INOBT_KEY_ADDR(left, 1, cur);
  1268. kp[1] = *XFS_INOBT_KEY_ADDR(right, 1, cur);
  1269. } else {
  1270. rp = XFS_INOBT_REC_ADDR(left, 1, cur);
  1271. kp[0].ir_startino = rp->ir_startino;
  1272. rp = XFS_INOBT_REC_ADDR(right, 1, cur);
  1273. kp[1].ir_startino = rp->ir_startino;
  1274. }
  1275. xfs_inobt_log_keys(cur, nbp, 1, 2);
  1276. /*
  1277. * Fill in the pointer data in the new root.
  1278. */
  1279. pp = XFS_INOBT_PTR_ADDR(new, 1, cur);
  1280. pp[0] = cpu_to_be32(lbno);
  1281. pp[1] = cpu_to_be32(rbno);
  1282. xfs_inobt_log_ptrs(cur, nbp, 1, 2);
  1283. /*
  1284. * Fix up the cursor.
  1285. */
  1286. xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
  1287. cur->bc_ptrs[cur->bc_nlevels] = nptr;
  1288. cur->bc_nlevels++;
  1289. *stat = 1;
  1290. return 0;
  1291. }
  1292. /*
  1293. * Move 1 record right from cur/level if possible.
  1294. * Update cur to reflect the new path.
  1295. */
  1296. STATIC int /* error */
  1297. xfs_inobt_rshift(
  1298. xfs_btree_cur_t *cur, /* btree cursor */
  1299. int level, /* level to shift record on */
  1300. int *stat) /* success/failure */
  1301. {
  1302. int error; /* error return value */
  1303. int i; /* loop index */
  1304. xfs_inobt_key_t key; /* key value for leaf level upward */
  1305. xfs_buf_t *lbp; /* buffer for left (current) block */
  1306. xfs_inobt_block_t *left; /* left (current) btree block */
  1307. xfs_inobt_key_t *lkp; /* key pointer for left block */
  1308. xfs_inobt_ptr_t *lpp; /* address pointer for left block */
  1309. xfs_inobt_rec_t *lrp; /* record pointer for left block */
  1310. xfs_buf_t *rbp; /* buffer for right neighbor block */
  1311. xfs_inobt_block_t *right; /* right neighbor btree block */
  1312. xfs_inobt_key_t *rkp; /* key pointer for right block */
  1313. xfs_inobt_ptr_t *rpp; /* address pointer for right block */
  1314. xfs_inobt_rec_t *rrp=NULL; /* record pointer for right block */
  1315. xfs_btree_cur_t *tcur; /* temporary cursor */
  1316. /*
  1317. * Set up variables for this block as "left".
  1318. */
  1319. lbp = cur->bc_bufs[level];
  1320. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  1321. #ifdef DEBUG
  1322. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  1323. return error;
  1324. #endif
  1325. /*
  1326. * If we've got no right sibling then we can't shift an entry right.
  1327. */
  1328. if (be32_to_cpu(left->bb_rightsib) == NULLAGBLOCK) {
  1329. *stat = 0;
  1330. return 0;
  1331. }
  1332. /*
  1333. * If the cursor entry is the one that would be moved, don't
  1334. * do it... it's too complicated.
  1335. */
  1336. if (cur->bc_ptrs[level] >= be16_to_cpu(left->bb_numrecs)) {
  1337. *stat = 0;
  1338. return 0;
  1339. }
  1340. /*
  1341. * Set up the right neighbor as "right".
  1342. */
  1343. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1344. cur->bc_private.i.agno, be32_to_cpu(left->bb_rightsib),
  1345. 0, &rbp, XFS_INO_BTREE_REF)))
  1346. return error;
  1347. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  1348. if ((error = xfs_btree_check_sblock(cur, right, level, rbp)))
  1349. return error;
  1350. /*
  1351. * If it's full, it can't take another entry.
  1352. */
  1353. if (be16_to_cpu(right->bb_numrecs) == XFS_INOBT_BLOCK_MAXRECS(level, cur)) {
  1354. *stat = 0;
  1355. return 0;
  1356. }
  1357. /*
  1358. * Make a hole at the start of the right neighbor block, then
  1359. * copy the last left block entry to the hole.
  1360. */
  1361. if (level > 0) {
  1362. lkp = XFS_INOBT_KEY_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
  1363. lpp = XFS_INOBT_PTR_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
  1364. rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
  1365. rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
  1366. #ifdef DEBUG
  1367. for (i = be16_to_cpu(right->bb_numrecs) - 1; i >= 0; i--) {
  1368. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(rpp[i]), level)))
  1369. return error;
  1370. }
  1371. #endif
  1372. memmove(rkp + 1, rkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1373. memmove(rpp + 1, rpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1374. #ifdef DEBUG
  1375. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(*lpp), level)))
  1376. return error;
  1377. #endif
  1378. *rkp = *lkp;
  1379. *rpp = *lpp;
  1380. xfs_inobt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
  1381. xfs_inobt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
  1382. } else {
  1383. lrp = XFS_INOBT_REC_ADDR(left, be16_to_cpu(left->bb_numrecs), cur);
  1384. rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
  1385. memmove(rrp + 1, rrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1386. *rrp = *lrp;
  1387. xfs_inobt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs) + 1);
  1388. key.ir_startino = rrp->ir_startino;
  1389. rkp = &key;
  1390. }
  1391. /*
  1392. * Decrement and log left's numrecs, bump and log right's numrecs.
  1393. */
  1394. be16_add(&left->bb_numrecs, -1);
  1395. xfs_inobt_log_block(cur->bc_tp, lbp, XFS_BB_NUMRECS);
  1396. be16_add(&right->bb_numrecs, 1);
  1397. #ifdef DEBUG
  1398. if (level > 0)
  1399. xfs_btree_check_key(cur->bc_btnum, rkp, rkp + 1);
  1400. else
  1401. xfs_btree_check_rec(cur->bc_btnum, rrp, rrp + 1);
  1402. #endif
  1403. xfs_inobt_log_block(cur->bc_tp, rbp, XFS_BB_NUMRECS);
  1404. /*
  1405. * Using a temporary cursor, update the parent key values of the
  1406. * block on the right.
  1407. */
  1408. if ((error = xfs_btree_dup_cursor(cur, &tcur)))
  1409. return error;
  1410. xfs_btree_lastrec(tcur, level);
  1411. if ((error = xfs_inobt_increment(tcur, level, &i)) ||
  1412. (error = xfs_inobt_updkey(tcur, rkp, level + 1))) {
  1413. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  1414. return error;
  1415. }
  1416. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  1417. *stat = 1;
  1418. return 0;
  1419. }
  1420. /*
  1421. * Split cur/level block in half.
  1422. * Return new block number and its first record (to be inserted into parent).
  1423. */
  1424. STATIC int /* error */
  1425. xfs_inobt_split(
  1426. xfs_btree_cur_t *cur, /* btree cursor */
  1427. int level, /* level to split */
  1428. xfs_agblock_t *bnop, /* output: block number allocated */
  1429. xfs_inobt_key_t *keyp, /* output: first key of new block */
  1430. xfs_btree_cur_t **curp, /* output: new cursor */
  1431. int *stat) /* success/failure */
  1432. {
  1433. xfs_alloc_arg_t args; /* allocation argument structure */
  1434. int error; /* error return value */
  1435. int i; /* loop index/record number */
  1436. xfs_agblock_t lbno; /* left (current) block number */
  1437. xfs_buf_t *lbp; /* buffer for left block */
  1438. xfs_inobt_block_t *left; /* left (current) btree block */
  1439. xfs_inobt_key_t *lkp; /* left btree key pointer */
  1440. xfs_inobt_ptr_t *lpp; /* left btree address pointer */
  1441. xfs_inobt_rec_t *lrp; /* left btree record pointer */
  1442. xfs_buf_t *rbp; /* buffer for right block */
  1443. xfs_inobt_block_t *right; /* right (new) btree block */
  1444. xfs_inobt_key_t *rkp; /* right btree key pointer */
  1445. xfs_inobt_ptr_t *rpp; /* right btree address pointer */
  1446. xfs_inobt_rec_t *rrp; /* right btree record pointer */
  1447. /*
  1448. * Set up left block (current one).
  1449. */
  1450. lbp = cur->bc_bufs[level];
  1451. args.tp = cur->bc_tp;
  1452. args.mp = cur->bc_mp;
  1453. lbno = XFS_DADDR_TO_AGBNO(args.mp, XFS_BUF_ADDR(lbp));
  1454. /*
  1455. * Allocate the new block.
  1456. * If we can't do it, we're toast. Give up.
  1457. */
  1458. args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_private.i.agno, lbno);
  1459. args.mod = args.minleft = args.alignment = args.total = args.wasdel =
  1460. args.isfl = args.userdata = args.minalignslop = 0;
  1461. args.minlen = args.maxlen = args.prod = 1;
  1462. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  1463. if ((error = xfs_alloc_vextent(&args)))
  1464. return error;
  1465. if (args.fsbno == NULLFSBLOCK) {
  1466. *stat = 0;
  1467. return 0;
  1468. }
  1469. ASSERT(args.len == 1);
  1470. rbp = xfs_btree_get_bufs(args.mp, args.tp, args.agno, args.agbno, 0);
  1471. /*
  1472. * Set up the new block as "right".
  1473. */
  1474. right = XFS_BUF_TO_INOBT_BLOCK(rbp);
  1475. /*
  1476. * "Left" is the current (according to the cursor) block.
  1477. */
  1478. left = XFS_BUF_TO_INOBT_BLOCK(lbp);
  1479. #ifdef DEBUG
  1480. if ((error = xfs_btree_check_sblock(cur, left, level, lbp)))
  1481. return error;
  1482. #endif
  1483. /*
  1484. * Fill in the btree header for the new block.
  1485. */
  1486. right->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  1487. right->bb_level = left->bb_level;
  1488. right->bb_numrecs = cpu_to_be16(be16_to_cpu(left->bb_numrecs) / 2);
  1489. /*
  1490. * Make sure that if there's an odd number of entries now, that
  1491. * each new block will have the same number of entries.
  1492. */
  1493. if ((be16_to_cpu(left->bb_numrecs) & 1) &&
  1494. cur->bc_ptrs[level] <= be16_to_cpu(right->bb_numrecs) + 1)
  1495. be16_add(&right->bb_numrecs, 1);
  1496. i = be16_to_cpu(left->bb_numrecs) - be16_to_cpu(right->bb_numrecs) + 1;
  1497. /*
  1498. * For non-leaf blocks, copy keys and addresses over to the new block.
  1499. */
  1500. if (level > 0) {
  1501. lkp = XFS_INOBT_KEY_ADDR(left, i, cur);
  1502. lpp = XFS_INOBT_PTR_ADDR(left, i, cur);
  1503. rkp = XFS_INOBT_KEY_ADDR(right, 1, cur);
  1504. rpp = XFS_INOBT_PTR_ADDR(right, 1, cur);
  1505. #ifdef DEBUG
  1506. for (i = 0; i < be16_to_cpu(right->bb_numrecs); i++) {
  1507. if ((error = xfs_btree_check_sptr(cur, be32_to_cpu(lpp[i]), level)))
  1508. return error;
  1509. }
  1510. #endif
  1511. memcpy(rkp, lkp, be16_to_cpu(right->bb_numrecs) * sizeof(*rkp));
  1512. memcpy(rpp, lpp, be16_to_cpu(right->bb_numrecs) * sizeof(*rpp));
  1513. xfs_inobt_log_keys(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1514. xfs_inobt_log_ptrs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1515. *keyp = *rkp;
  1516. }
  1517. /*
  1518. * For leaf blocks, copy records over to the new block.
  1519. */
  1520. else {
  1521. lrp = XFS_INOBT_REC_ADDR(left, i, cur);
  1522. rrp = XFS_INOBT_REC_ADDR(right, 1, cur);
  1523. memcpy(rrp, lrp, be16_to_cpu(right->bb_numrecs) * sizeof(*rrp));
  1524. xfs_inobt_log_recs(cur, rbp, 1, be16_to_cpu(right->bb_numrecs));
  1525. keyp->ir_startino = rrp->ir_startino;
  1526. }
  1527. /*
  1528. * Find the left block number by looking in the buffer.
  1529. * Adjust numrecs, sibling pointers.
  1530. */
  1531. be16_add(&left->bb_numrecs, -(be16_to_cpu(right->bb_numrecs)));
  1532. right->bb_rightsib = left->bb_rightsib;
  1533. left->bb_rightsib = cpu_to_be32(args.agbno);
  1534. right->bb_leftsib = cpu_to_be32(lbno);
  1535. xfs_inobt_log_block(args.tp, rbp, XFS_BB_ALL_BITS);
  1536. xfs_inobt_log_block(args.tp, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  1537. /*
  1538. * If there's a block to the new block's right, make that block
  1539. * point back to right instead of to left.
  1540. */
  1541. if (be32_to_cpu(right->bb_rightsib) != NULLAGBLOCK) {
  1542. xfs_inobt_block_t *rrblock; /* rr btree block */
  1543. xfs_buf_t *rrbp; /* buffer for rrblock */
  1544. if ((error = xfs_btree_read_bufs(args.mp, args.tp, args.agno,
  1545. be32_to_cpu(right->bb_rightsib), 0, &rrbp,
  1546. XFS_INO_BTREE_REF)))
  1547. return error;
  1548. rrblock = XFS_BUF_TO_INOBT_BLOCK(rrbp);
  1549. if ((error = xfs_btree_check_sblock(cur, rrblock, level, rrbp)))
  1550. return error;
  1551. rrblock->bb_leftsib = cpu_to_be32(args.agbno);
  1552. xfs_inobt_log_block(args.tp, rrbp, XFS_BB_LEFTSIB);
  1553. }
  1554. /*
  1555. * If the cursor is really in the right block, move it there.
  1556. * If it's just pointing past the last entry in left, then we'll
  1557. * insert there, so don't change anything in that case.
  1558. */
  1559. if (cur->bc_ptrs[level] > be16_to_cpu(left->bb_numrecs) + 1) {
  1560. xfs_btree_setbuf(cur, level, rbp);
  1561. cur->bc_ptrs[level] -= be16_to_cpu(left->bb_numrecs);
  1562. }
  1563. /*
  1564. * If there are more levels, we'll need another cursor which refers
  1565. * the right block, no matter where this cursor was.
  1566. */
  1567. if (level + 1 < cur->bc_nlevels) {
  1568. if ((error = xfs_btree_dup_cursor(cur, curp)))
  1569. return error;
  1570. (*curp)->bc_ptrs[level + 1]++;
  1571. }
  1572. *bnop = args.agbno;
  1573. *stat = 1;
  1574. return 0;
  1575. }
  1576. /*
  1577. * Update keys at all levels from here to the root along the cursor's path.
  1578. */
  1579. STATIC int /* error */
  1580. xfs_inobt_updkey(
  1581. xfs_btree_cur_t *cur, /* btree cursor */
  1582. xfs_inobt_key_t *keyp, /* new key value to update to */
  1583. int level) /* starting level for update */
  1584. {
  1585. int ptr; /* index of key in block */
  1586. /*
  1587. * Go up the tree from this level toward the root.
  1588. * At each level, update the key value to the value input.
  1589. * Stop when we reach a level where the cursor isn't pointing
  1590. * at the first entry in the block.
  1591. */
  1592. for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
  1593. xfs_buf_t *bp; /* buffer for block */
  1594. xfs_inobt_block_t *block; /* btree block */
  1595. #ifdef DEBUG
  1596. int error; /* error return value */
  1597. #endif
  1598. xfs_inobt_key_t *kp; /* ptr to btree block keys */
  1599. bp = cur->bc_bufs[level];
  1600. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1601. #ifdef DEBUG
  1602. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  1603. return error;
  1604. #endif
  1605. ptr = cur->bc_ptrs[level];
  1606. kp = XFS_INOBT_KEY_ADDR(block, ptr, cur);
  1607. *kp = *keyp;
  1608. xfs_inobt_log_keys(cur, bp, ptr, ptr);
  1609. }
  1610. return 0;
  1611. }
  1612. /*
  1613. * Externally visible routines.
  1614. */
  1615. /*
  1616. * Decrement cursor by one record at the level.
  1617. * For nonzero levels the leaf-ward information is untouched.
  1618. */
  1619. int /* error */
  1620. xfs_inobt_decrement(
  1621. xfs_btree_cur_t *cur, /* btree cursor */
  1622. int level, /* level in btree, 0 is leaf */
  1623. int *stat) /* success/failure */
  1624. {
  1625. xfs_inobt_block_t *block; /* btree block */
  1626. int error;
  1627. int lev; /* btree level */
  1628. ASSERT(level < cur->bc_nlevels);
  1629. /*
  1630. * Read-ahead to the left at this level.
  1631. */
  1632. xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
  1633. /*
  1634. * Decrement the ptr at this level. If we're still in the block
  1635. * then we're done.
  1636. */
  1637. if (--cur->bc_ptrs[level] > 0) {
  1638. *stat = 1;
  1639. return 0;
  1640. }
  1641. /*
  1642. * Get a pointer to the btree block.
  1643. */
  1644. block = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[level]);
  1645. #ifdef DEBUG
  1646. if ((error = xfs_btree_check_sblock(cur, block, level,
  1647. cur->bc_bufs[level])))
  1648. return error;
  1649. #endif
  1650. /*
  1651. * If we just went off the left edge of the tree, return failure.
  1652. */
  1653. if (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK) {
  1654. *stat = 0;
  1655. return 0;
  1656. }
  1657. /*
  1658. * March up the tree decrementing pointers.
  1659. * Stop when we don't go off the left edge of a block.
  1660. */
  1661. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1662. if (--cur->bc_ptrs[lev] > 0)
  1663. break;
  1664. /*
  1665. * Read-ahead the left block, we're going to read it
  1666. * in the next loop.
  1667. */
  1668. xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
  1669. }
  1670. /*
  1671. * If we went off the root then we are seriously confused.
  1672. */
  1673. ASSERT(lev < cur->bc_nlevels);
  1674. /*
  1675. * Now walk back down the tree, fixing up the cursor's buffer
  1676. * pointers and key numbers.
  1677. */
  1678. for (block = XFS_BUF_TO_INOBT_BLOCK(cur->bc_bufs[lev]); lev > level; ) {
  1679. xfs_agblock_t agbno; /* block number of btree block */
  1680. xfs_buf_t *bp; /* buffer containing btree block */
  1681. agbno = be32_to_cpu(*XFS_INOBT_PTR_ADDR(block, cur->bc_ptrs[lev], cur));
  1682. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1683. cur->bc_private.i.agno, agbno, 0, &bp,
  1684. XFS_INO_BTREE_REF)))
  1685. return error;
  1686. lev--;
  1687. xfs_btree_setbuf(cur, lev, bp);
  1688. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1689. if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
  1690. return error;
  1691. cur->bc_ptrs[lev] = be16_to_cpu(block->bb_numrecs);
  1692. }
  1693. *stat = 1;
  1694. return 0;
  1695. }
  1696. /*
  1697. * Delete the record pointed to by cur.
  1698. * The cursor refers to the place where the record was (could be inserted)
  1699. * when the operation returns.
  1700. */
  1701. int /* error */
  1702. xfs_inobt_delete(
  1703. xfs_btree_cur_t *cur, /* btree cursor */
  1704. int *stat) /* success/failure */
  1705. {
  1706. int error;
  1707. int i; /* result code */
  1708. int level; /* btree level */
  1709. /*
  1710. * Go up the tree, starting at leaf level.
  1711. * If 2 is returned then a join was done; go to the next level.
  1712. * Otherwise we are done.
  1713. */
  1714. for (level = 0, i = 2; i == 2; level++) {
  1715. if ((error = xfs_inobt_delrec(cur, level, &i)))
  1716. return error;
  1717. }
  1718. if (i == 0) {
  1719. for (level = 1; level < cur->bc_nlevels; level++) {
  1720. if (cur->bc_ptrs[level] == 0) {
  1721. if ((error = xfs_inobt_decrement(cur, level, &i)))
  1722. return error;
  1723. break;
  1724. }
  1725. }
  1726. }
  1727. *stat = i;
  1728. return 0;
  1729. }
  1730. /*
  1731. * Get the data from the pointed-to record.
  1732. */
  1733. int /* error */
  1734. xfs_inobt_get_rec(
  1735. xfs_btree_cur_t *cur, /* btree cursor */
  1736. xfs_agino_t *ino, /* output: starting inode of chunk */
  1737. __int32_t *fcnt, /* output: number of free inodes */
  1738. xfs_inofree_t *free, /* output: free inode mask */
  1739. int *stat) /* output: success/failure */
  1740. {
  1741. xfs_inobt_block_t *block; /* btree block */
  1742. xfs_buf_t *bp; /* buffer containing btree block */
  1743. #ifdef DEBUG
  1744. int error; /* error return value */
  1745. #endif
  1746. int ptr; /* record number */
  1747. xfs_inobt_rec_t *rec; /* record data */
  1748. bp = cur->bc_bufs[0];
  1749. ptr = cur->bc_ptrs[0];
  1750. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1751. #ifdef DEBUG
  1752. if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
  1753. return error;
  1754. #endif
  1755. /*
  1756. * Off the right end or left end, return failure.
  1757. */
  1758. if (ptr > be16_to_cpu(block->bb_numrecs) || ptr <= 0) {
  1759. *stat = 0;
  1760. return 0;
  1761. }
  1762. /*
  1763. * Point to the record and extract its data.
  1764. */
  1765. rec = XFS_INOBT_REC_ADDR(block, ptr, cur);
  1766. *ino = be32_to_cpu(rec->ir_startino);
  1767. *fcnt = be32_to_cpu(rec->ir_freecount);
  1768. *free = be64_to_cpu(rec->ir_free);
  1769. *stat = 1;
  1770. return 0;
  1771. }
  1772. /*
  1773. * Increment cursor by one record at the level.
  1774. * For nonzero levels the leaf-ward information is untouched.
  1775. */
  1776. int /* error */
  1777. xfs_inobt_increment(
  1778. xfs_btree_cur_t *cur, /* btree cursor */
  1779. int level, /* level in btree, 0 is leaf */
  1780. int *stat) /* success/failure */
  1781. {
  1782. xfs_inobt_block_t *block; /* btree block */
  1783. xfs_buf_t *bp; /* buffer containing btree block */
  1784. int error; /* error return value */
  1785. int lev; /* btree level */
  1786. ASSERT(level < cur->bc_nlevels);
  1787. /*
  1788. * Read-ahead to the right at this level.
  1789. */
  1790. xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
  1791. /*
  1792. * Get a pointer to the btree block.
  1793. */
  1794. bp = cur->bc_bufs[level];
  1795. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1796. #ifdef DEBUG
  1797. if ((error = xfs_btree_check_sblock(cur, block, level, bp)))
  1798. return error;
  1799. #endif
  1800. /*
  1801. * Increment the ptr at this level. If we're still in the block
  1802. * then we're done.
  1803. */
  1804. if (++cur->bc_ptrs[level] <= be16_to_cpu(block->bb_numrecs)) {
  1805. *stat = 1;
  1806. return 0;
  1807. }
  1808. /*
  1809. * If we just went off the right edge of the tree, return failure.
  1810. */
  1811. if (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK) {
  1812. *stat = 0;
  1813. return 0;
  1814. }
  1815. /*
  1816. * March up the tree incrementing pointers.
  1817. * Stop when we don't go off the right edge of a block.
  1818. */
  1819. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1820. bp = cur->bc_bufs[lev];
  1821. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1822. #ifdef DEBUG
  1823. if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
  1824. return error;
  1825. #endif
  1826. if (++cur->bc_ptrs[lev] <= be16_to_cpu(block->bb_numrecs))
  1827. break;
  1828. /*
  1829. * Read-ahead the right block, we're going to read it
  1830. * in the next loop.
  1831. */
  1832. xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
  1833. }
  1834. /*
  1835. * If we went off the root then we are seriously confused.
  1836. */
  1837. ASSERT(lev < cur->bc_nlevels);
  1838. /*
  1839. * Now walk back down the tree, fixing up the cursor's buffer
  1840. * pointers and key numbers.
  1841. */
  1842. for (bp = cur->bc_bufs[lev], block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1843. lev > level; ) {
  1844. xfs_agblock_t agbno; /* block number of btree block */
  1845. agbno = be32_to_cpu(*XFS_INOBT_PTR_ADDR(block, cur->bc_ptrs[lev], cur));
  1846. if ((error = xfs_btree_read_bufs(cur->bc_mp, cur->bc_tp,
  1847. cur->bc_private.i.agno, agbno, 0, &bp,
  1848. XFS_INO_BTREE_REF)))
  1849. return error;
  1850. lev--;
  1851. xfs_btree_setbuf(cur, lev, bp);
  1852. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1853. if ((error = xfs_btree_check_sblock(cur, block, lev, bp)))
  1854. return error;
  1855. cur->bc_ptrs[lev] = 1;
  1856. }
  1857. *stat = 1;
  1858. return 0;
  1859. }
  1860. /*
  1861. * Insert the current record at the point referenced by cur.
  1862. * The cursor may be inconsistent on return if splits have been done.
  1863. */
  1864. int /* error */
  1865. xfs_inobt_insert(
  1866. xfs_btree_cur_t *cur, /* btree cursor */
  1867. int *stat) /* success/failure */
  1868. {
  1869. int error; /* error return value */
  1870. int i; /* result value, 0 for failure */
  1871. int level; /* current level number in btree */
  1872. xfs_agblock_t nbno; /* new block number (split result) */
  1873. xfs_btree_cur_t *ncur; /* new cursor (split result) */
  1874. xfs_inobt_rec_t nrec; /* record being inserted this level */
  1875. xfs_btree_cur_t *pcur; /* previous level's cursor */
  1876. level = 0;
  1877. nbno = NULLAGBLOCK;
  1878. nrec.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
  1879. nrec.ir_freecount = cpu_to_be32(cur->bc_rec.i.ir_freecount);
  1880. nrec.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
  1881. ncur = NULL;
  1882. pcur = cur;
  1883. /*
  1884. * Loop going up the tree, starting at the leaf level.
  1885. * Stop when we don't get a split block, that must mean that
  1886. * the insert is finished with this level.
  1887. */
  1888. do {
  1889. /*
  1890. * Insert nrec/nbno into this level of the tree.
  1891. * Note if we fail, nbno will be null.
  1892. */
  1893. if ((error = xfs_inobt_insrec(pcur, level++, &nbno, &nrec, &ncur,
  1894. &i))) {
  1895. if (pcur != cur)
  1896. xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
  1897. return error;
  1898. }
  1899. /*
  1900. * See if the cursor we just used is trash.
  1901. * Can't trash the caller's cursor, but otherwise we should
  1902. * if ncur is a new cursor or we're about to be done.
  1903. */
  1904. if (pcur != cur && (ncur || nbno == NULLAGBLOCK)) {
  1905. cur->bc_nlevels = pcur->bc_nlevels;
  1906. xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
  1907. }
  1908. /*
  1909. * If we got a new cursor, switch to it.
  1910. */
  1911. if (ncur) {
  1912. pcur = ncur;
  1913. ncur = NULL;
  1914. }
  1915. } while (nbno != NULLAGBLOCK);
  1916. *stat = i;
  1917. return 0;
  1918. }
  1919. /*
  1920. * Lookup the record equal to ino in the btree given by cur.
  1921. */
  1922. int /* error */
  1923. xfs_inobt_lookup_eq(
  1924. xfs_btree_cur_t *cur, /* btree cursor */
  1925. xfs_agino_t ino, /* starting inode of chunk */
  1926. __int32_t fcnt, /* free inode count */
  1927. xfs_inofree_t free, /* free inode mask */
  1928. int *stat) /* success/failure */
  1929. {
  1930. cur->bc_rec.i.ir_startino = ino;
  1931. cur->bc_rec.i.ir_freecount = fcnt;
  1932. cur->bc_rec.i.ir_free = free;
  1933. return xfs_inobt_lookup(cur, XFS_LOOKUP_EQ, stat);
  1934. }
  1935. /*
  1936. * Lookup the first record greater than or equal to ino
  1937. * in the btree given by cur.
  1938. */
  1939. int /* error */
  1940. xfs_inobt_lookup_ge(
  1941. xfs_btree_cur_t *cur, /* btree cursor */
  1942. xfs_agino_t ino, /* starting inode of chunk */
  1943. __int32_t fcnt, /* free inode count */
  1944. xfs_inofree_t free, /* free inode mask */
  1945. int *stat) /* success/failure */
  1946. {
  1947. cur->bc_rec.i.ir_startino = ino;
  1948. cur->bc_rec.i.ir_freecount = fcnt;
  1949. cur->bc_rec.i.ir_free = free;
  1950. return xfs_inobt_lookup(cur, XFS_LOOKUP_GE, stat);
  1951. }
  1952. /*
  1953. * Lookup the first record less than or equal to ino
  1954. * in the btree given by cur.
  1955. */
  1956. int /* error */
  1957. xfs_inobt_lookup_le(
  1958. xfs_btree_cur_t *cur, /* btree cursor */
  1959. xfs_agino_t ino, /* starting inode of chunk */
  1960. __int32_t fcnt, /* free inode count */
  1961. xfs_inofree_t free, /* free inode mask */
  1962. int *stat) /* success/failure */
  1963. {
  1964. cur->bc_rec.i.ir_startino = ino;
  1965. cur->bc_rec.i.ir_freecount = fcnt;
  1966. cur->bc_rec.i.ir_free = free;
  1967. return xfs_inobt_lookup(cur, XFS_LOOKUP_LE, stat);
  1968. }
  1969. /*
  1970. * Update the record referred to by cur, to the value given
  1971. * by [ino, fcnt, free].
  1972. * This either works (return 0) or gets an EFSCORRUPTED error.
  1973. */
  1974. int /* error */
  1975. xfs_inobt_update(
  1976. xfs_btree_cur_t *cur, /* btree cursor */
  1977. xfs_agino_t ino, /* starting inode of chunk */
  1978. __int32_t fcnt, /* free inode count */
  1979. xfs_inofree_t free) /* free inode mask */
  1980. {
  1981. xfs_inobt_block_t *block; /* btree block to update */
  1982. xfs_buf_t *bp; /* buffer containing btree block */
  1983. int error; /* error return value */
  1984. int ptr; /* current record number (updating) */
  1985. xfs_inobt_rec_t *rp; /* pointer to updated record */
  1986. /*
  1987. * Pick up the current block.
  1988. */
  1989. bp = cur->bc_bufs[0];
  1990. block = XFS_BUF_TO_INOBT_BLOCK(bp);
  1991. #ifdef DEBUG
  1992. if ((error = xfs_btree_check_sblock(cur, block, 0, bp)))
  1993. return error;
  1994. #endif
  1995. /*
  1996. * Get the address of the rec to be updated.
  1997. */
  1998. ptr = cur->bc_ptrs[0];
  1999. rp = XFS_INOBT_REC_ADDR(block, ptr, cur);
  2000. /*
  2001. * Fill in the new contents and log them.
  2002. */
  2003. rp->ir_startino = cpu_to_be32(ino);
  2004. rp->ir_freecount = cpu_to_be32(fcnt);
  2005. rp->ir_free = cpu_to_be64(free);
  2006. xfs_inobt_log_recs(cur, bp, ptr, ptr);
  2007. /*
  2008. * Updating first record in leaf. Pass new key value up to our parent.
  2009. */
  2010. if (ptr == 1) {
  2011. xfs_inobt_key_t key; /* key containing [ino] */
  2012. key.ir_startino = cpu_to_be32(ino);
  2013. if ((error = xfs_inobt_updkey(cur, &key, 1)))
  2014. return error;
  2015. }
  2016. return 0;
  2017. }