xfs_ialloc_btree.c 62 KB

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