xfs_btree.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001
  1. /*
  2. * Copyright (c) 2000-2002,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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_inode_item.h"
  31. #include "xfs_buf_item.h"
  32. #include "xfs_btree.h"
  33. #include "xfs_error.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_cksum.h"
  36. /*
  37. * Cursor allocation zone.
  38. */
  39. kmem_zone_t *xfs_btree_cur_zone;
  40. /*
  41. * Btree magic numbers.
  42. */
  43. static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
  44. { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC },
  45. { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC,
  46. XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC }
  47. };
  48. #define xfs_btree_magic(cur) \
  49. xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
  50. STATIC int /* error (0 or EFSCORRUPTED) */
  51. xfs_btree_check_lblock(
  52. struct xfs_btree_cur *cur, /* btree cursor */
  53. struct xfs_btree_block *block, /* btree long form block pointer */
  54. int level, /* level of the btree block */
  55. struct xfs_buf *bp) /* buffer for block, if any */
  56. {
  57. int lblock_ok = 1; /* block passes checks */
  58. struct xfs_mount *mp; /* file system mount point */
  59. mp = cur->bc_mp;
  60. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  61. lblock_ok = lblock_ok &&
  62. uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid) &&
  63. block->bb_u.l.bb_blkno == cpu_to_be64(
  64. bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
  65. }
  66. lblock_ok = lblock_ok &&
  67. be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
  68. be16_to_cpu(block->bb_level) == level &&
  69. be16_to_cpu(block->bb_numrecs) <=
  70. cur->bc_ops->get_maxrecs(cur, level) &&
  71. block->bb_u.l.bb_leftsib &&
  72. (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO) ||
  73. XFS_FSB_SANITY_CHECK(mp,
  74. be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
  75. block->bb_u.l.bb_rightsib &&
  76. (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO) ||
  77. XFS_FSB_SANITY_CHECK(mp,
  78. be64_to_cpu(block->bb_u.l.bb_rightsib)));
  79. if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
  80. XFS_ERRTAG_BTREE_CHECK_LBLOCK,
  81. XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
  82. if (bp)
  83. trace_xfs_btree_corrupt(bp, _RET_IP_);
  84. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
  85. return XFS_ERROR(EFSCORRUPTED);
  86. }
  87. return 0;
  88. }
  89. STATIC int /* error (0 or EFSCORRUPTED) */
  90. xfs_btree_check_sblock(
  91. struct xfs_btree_cur *cur, /* btree cursor */
  92. struct xfs_btree_block *block, /* btree short form block pointer */
  93. int level, /* level of the btree block */
  94. struct xfs_buf *bp) /* buffer containing block */
  95. {
  96. struct xfs_mount *mp; /* file system mount point */
  97. struct xfs_buf *agbp; /* buffer for ag. freespace struct */
  98. struct xfs_agf *agf; /* ag. freespace structure */
  99. xfs_agblock_t agflen; /* native ag. freespace length */
  100. int sblock_ok = 1; /* block passes checks */
  101. mp = cur->bc_mp;
  102. agbp = cur->bc_private.a.agbp;
  103. agf = XFS_BUF_TO_AGF(agbp);
  104. agflen = be32_to_cpu(agf->agf_length);
  105. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  106. sblock_ok = sblock_ok &&
  107. uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid) &&
  108. block->bb_u.s.bb_blkno == cpu_to_be64(
  109. bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
  110. }
  111. sblock_ok = sblock_ok &&
  112. be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
  113. be16_to_cpu(block->bb_level) == level &&
  114. be16_to_cpu(block->bb_numrecs) <=
  115. cur->bc_ops->get_maxrecs(cur, level) &&
  116. (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
  117. be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
  118. block->bb_u.s.bb_leftsib &&
  119. (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
  120. be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
  121. block->bb_u.s.bb_rightsib;
  122. if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
  123. XFS_ERRTAG_BTREE_CHECK_SBLOCK,
  124. XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
  125. if (bp)
  126. trace_xfs_btree_corrupt(bp, _RET_IP_);
  127. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
  128. return XFS_ERROR(EFSCORRUPTED);
  129. }
  130. return 0;
  131. }
  132. /*
  133. * Debug routine: check that block header is ok.
  134. */
  135. int
  136. xfs_btree_check_block(
  137. struct xfs_btree_cur *cur, /* btree cursor */
  138. struct xfs_btree_block *block, /* generic btree block pointer */
  139. int level, /* level of the btree block */
  140. struct xfs_buf *bp) /* buffer containing block, if any */
  141. {
  142. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  143. return xfs_btree_check_lblock(cur, block, level, bp);
  144. else
  145. return xfs_btree_check_sblock(cur, block, level, bp);
  146. }
  147. /*
  148. * Check that (long) pointer is ok.
  149. */
  150. int /* error (0 or EFSCORRUPTED) */
  151. xfs_btree_check_lptr(
  152. struct xfs_btree_cur *cur, /* btree cursor */
  153. xfs_dfsbno_t bno, /* btree block disk address */
  154. int level) /* btree block level */
  155. {
  156. XFS_WANT_CORRUPTED_RETURN(
  157. level > 0 &&
  158. bno != NULLDFSBNO &&
  159. XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
  160. return 0;
  161. }
  162. #ifdef DEBUG
  163. /*
  164. * Check that (short) pointer is ok.
  165. */
  166. STATIC int /* error (0 or EFSCORRUPTED) */
  167. xfs_btree_check_sptr(
  168. struct xfs_btree_cur *cur, /* btree cursor */
  169. xfs_agblock_t bno, /* btree block disk address */
  170. int level) /* btree block level */
  171. {
  172. xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
  173. XFS_WANT_CORRUPTED_RETURN(
  174. level > 0 &&
  175. bno != NULLAGBLOCK &&
  176. bno != 0 &&
  177. bno < agblocks);
  178. return 0;
  179. }
  180. /*
  181. * Check that block ptr is ok.
  182. */
  183. STATIC int /* error (0 or EFSCORRUPTED) */
  184. xfs_btree_check_ptr(
  185. struct xfs_btree_cur *cur, /* btree cursor */
  186. union xfs_btree_ptr *ptr, /* btree block disk address */
  187. int index, /* offset from ptr to check */
  188. int level) /* btree block level */
  189. {
  190. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  191. return xfs_btree_check_lptr(cur,
  192. be64_to_cpu((&ptr->l)[index]), level);
  193. } else {
  194. return xfs_btree_check_sptr(cur,
  195. be32_to_cpu((&ptr->s)[index]), level);
  196. }
  197. }
  198. #endif
  199. /*
  200. * Calculate CRC on the whole btree block and stuff it into the
  201. * long-form btree header.
  202. *
  203. * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
  204. * it into the buffer so recovery knows what the last modifcation was that made
  205. * it to disk.
  206. */
  207. void
  208. xfs_btree_lblock_calc_crc(
  209. struct xfs_buf *bp)
  210. {
  211. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  212. struct xfs_buf_log_item *bip = bp->b_fspriv;
  213. if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
  214. return;
  215. if (bip)
  216. block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  217. xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
  218. }
  219. bool
  220. xfs_btree_lblock_verify_crc(
  221. struct xfs_buf *bp)
  222. {
  223. if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
  224. return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
  225. return true;
  226. }
  227. /*
  228. * Calculate CRC on the whole btree block and stuff it into the
  229. * short-form btree header.
  230. *
  231. * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
  232. * it into the buffer so recovery knows what the last modifcation was that made
  233. * it to disk.
  234. */
  235. void
  236. xfs_btree_sblock_calc_crc(
  237. struct xfs_buf *bp)
  238. {
  239. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  240. struct xfs_buf_log_item *bip = bp->b_fspriv;
  241. if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
  242. return;
  243. if (bip)
  244. block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  245. xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
  246. }
  247. bool
  248. xfs_btree_sblock_verify_crc(
  249. struct xfs_buf *bp)
  250. {
  251. if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
  252. return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
  253. return true;
  254. }
  255. /*
  256. * Delete the btree cursor.
  257. */
  258. void
  259. xfs_btree_del_cursor(
  260. xfs_btree_cur_t *cur, /* btree cursor */
  261. int error) /* del because of error */
  262. {
  263. int i; /* btree level */
  264. /*
  265. * Clear the buffer pointers, and release the buffers.
  266. * If we're doing this in the face of an error, we
  267. * need to make sure to inspect all of the entries
  268. * in the bc_bufs array for buffers to be unlocked.
  269. * This is because some of the btree code works from
  270. * level n down to 0, and if we get an error along
  271. * the way we won't have initialized all the entries
  272. * down to 0.
  273. */
  274. for (i = 0; i < cur->bc_nlevels; i++) {
  275. if (cur->bc_bufs[i])
  276. xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
  277. else if (!error)
  278. break;
  279. }
  280. /*
  281. * Can't free a bmap cursor without having dealt with the
  282. * allocated indirect blocks' accounting.
  283. */
  284. ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
  285. cur->bc_private.b.allocated == 0);
  286. /*
  287. * Free the cursor.
  288. */
  289. kmem_zone_free(xfs_btree_cur_zone, cur);
  290. }
  291. /*
  292. * Duplicate the btree cursor.
  293. * Allocate a new one, copy the record, re-get the buffers.
  294. */
  295. int /* error */
  296. xfs_btree_dup_cursor(
  297. xfs_btree_cur_t *cur, /* input cursor */
  298. xfs_btree_cur_t **ncur) /* output cursor */
  299. {
  300. xfs_buf_t *bp; /* btree block's buffer pointer */
  301. int error; /* error return value */
  302. int i; /* level number of btree block */
  303. xfs_mount_t *mp; /* mount structure for filesystem */
  304. xfs_btree_cur_t *new; /* new cursor value */
  305. xfs_trans_t *tp; /* transaction pointer, can be NULL */
  306. tp = cur->bc_tp;
  307. mp = cur->bc_mp;
  308. /*
  309. * Allocate a new cursor like the old one.
  310. */
  311. new = cur->bc_ops->dup_cursor(cur);
  312. /*
  313. * Copy the record currently in the cursor.
  314. */
  315. new->bc_rec = cur->bc_rec;
  316. /*
  317. * For each level current, re-get the buffer and copy the ptr value.
  318. */
  319. for (i = 0; i < new->bc_nlevels; i++) {
  320. new->bc_ptrs[i] = cur->bc_ptrs[i];
  321. new->bc_ra[i] = cur->bc_ra[i];
  322. bp = cur->bc_bufs[i];
  323. if (bp) {
  324. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  325. XFS_BUF_ADDR(bp), mp->m_bsize,
  326. 0, &bp,
  327. cur->bc_ops->buf_ops);
  328. if (error) {
  329. xfs_btree_del_cursor(new, error);
  330. *ncur = NULL;
  331. return error;
  332. }
  333. }
  334. new->bc_bufs[i] = bp;
  335. }
  336. *ncur = new;
  337. return 0;
  338. }
  339. /*
  340. * XFS btree block layout and addressing:
  341. *
  342. * There are two types of blocks in the btree: leaf and non-leaf blocks.
  343. *
  344. * The leaf record start with a header then followed by records containing
  345. * the values. A non-leaf block also starts with the same header, and
  346. * then first contains lookup keys followed by an equal number of pointers
  347. * to the btree blocks at the previous level.
  348. *
  349. * +--------+-------+-------+-------+-------+-------+-------+
  350. * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
  351. * +--------+-------+-------+-------+-------+-------+-------+
  352. *
  353. * +--------+-------+-------+-------+-------+-------+-------+
  354. * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
  355. * +--------+-------+-------+-------+-------+-------+-------+
  356. *
  357. * The header is called struct xfs_btree_block for reasons better left unknown
  358. * and comes in different versions for short (32bit) and long (64bit) block
  359. * pointers. The record and key structures are defined by the btree instances
  360. * and opaque to the btree core. The block pointers are simple disk endian
  361. * integers, available in a short (32bit) and long (64bit) variant.
  362. *
  363. * The helpers below calculate the offset of a given record, key or pointer
  364. * into a btree block (xfs_btree_*_offset) or return a pointer to the given
  365. * record, key or pointer (xfs_btree_*_addr). Note that all addressing
  366. * inside the btree block is done using indices starting at one, not zero!
  367. */
  368. /*
  369. * Return size of the btree block header for this btree instance.
  370. */
  371. static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
  372. {
  373. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  374. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
  375. return XFS_BTREE_LBLOCK_CRC_LEN;
  376. return XFS_BTREE_LBLOCK_LEN;
  377. }
  378. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
  379. return XFS_BTREE_SBLOCK_CRC_LEN;
  380. return XFS_BTREE_SBLOCK_LEN;
  381. }
  382. /*
  383. * Return size of btree block pointers for this btree instance.
  384. */
  385. static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
  386. {
  387. return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  388. sizeof(__be64) : sizeof(__be32);
  389. }
  390. /*
  391. * Calculate offset of the n-th record in a btree block.
  392. */
  393. STATIC size_t
  394. xfs_btree_rec_offset(
  395. struct xfs_btree_cur *cur,
  396. int n)
  397. {
  398. return xfs_btree_block_len(cur) +
  399. (n - 1) * cur->bc_ops->rec_len;
  400. }
  401. /*
  402. * Calculate offset of the n-th key in a btree block.
  403. */
  404. STATIC size_t
  405. xfs_btree_key_offset(
  406. struct xfs_btree_cur *cur,
  407. int n)
  408. {
  409. return xfs_btree_block_len(cur) +
  410. (n - 1) * cur->bc_ops->key_len;
  411. }
  412. /*
  413. * Calculate offset of the n-th block pointer in a btree block.
  414. */
  415. STATIC size_t
  416. xfs_btree_ptr_offset(
  417. struct xfs_btree_cur *cur,
  418. int n,
  419. int level)
  420. {
  421. return xfs_btree_block_len(cur) +
  422. cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
  423. (n - 1) * xfs_btree_ptr_len(cur);
  424. }
  425. /*
  426. * Return a pointer to the n-th record in the btree block.
  427. */
  428. STATIC union xfs_btree_rec *
  429. xfs_btree_rec_addr(
  430. struct xfs_btree_cur *cur,
  431. int n,
  432. struct xfs_btree_block *block)
  433. {
  434. return (union xfs_btree_rec *)
  435. ((char *)block + xfs_btree_rec_offset(cur, n));
  436. }
  437. /*
  438. * Return a pointer to the n-th key in the btree block.
  439. */
  440. STATIC union xfs_btree_key *
  441. xfs_btree_key_addr(
  442. struct xfs_btree_cur *cur,
  443. int n,
  444. struct xfs_btree_block *block)
  445. {
  446. return (union xfs_btree_key *)
  447. ((char *)block + xfs_btree_key_offset(cur, n));
  448. }
  449. /*
  450. * Return a pointer to the n-th block pointer in the btree block.
  451. */
  452. STATIC union xfs_btree_ptr *
  453. xfs_btree_ptr_addr(
  454. struct xfs_btree_cur *cur,
  455. int n,
  456. struct xfs_btree_block *block)
  457. {
  458. int level = xfs_btree_get_level(block);
  459. ASSERT(block->bb_level != 0);
  460. return (union xfs_btree_ptr *)
  461. ((char *)block + xfs_btree_ptr_offset(cur, n, level));
  462. }
  463. /*
  464. * Get the root block which is stored in the inode.
  465. *
  466. * For now this btree implementation assumes the btree root is always
  467. * stored in the if_broot field of an inode fork.
  468. */
  469. STATIC struct xfs_btree_block *
  470. xfs_btree_get_iroot(
  471. struct xfs_btree_cur *cur)
  472. {
  473. struct xfs_ifork *ifp;
  474. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
  475. return (struct xfs_btree_block *)ifp->if_broot;
  476. }
  477. /*
  478. * Retrieve the block pointer from the cursor at the given level.
  479. * This may be an inode btree root or from a buffer.
  480. */
  481. STATIC struct xfs_btree_block * /* generic btree block pointer */
  482. xfs_btree_get_block(
  483. struct xfs_btree_cur *cur, /* btree cursor */
  484. int level, /* level in btree */
  485. struct xfs_buf **bpp) /* buffer containing the block */
  486. {
  487. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  488. (level == cur->bc_nlevels - 1)) {
  489. *bpp = NULL;
  490. return xfs_btree_get_iroot(cur);
  491. }
  492. *bpp = cur->bc_bufs[level];
  493. return XFS_BUF_TO_BLOCK(*bpp);
  494. }
  495. /*
  496. * Get a buffer for the block, return it with no data read.
  497. * Long-form addressing.
  498. */
  499. xfs_buf_t * /* buffer for fsbno */
  500. xfs_btree_get_bufl(
  501. xfs_mount_t *mp, /* file system mount point */
  502. xfs_trans_t *tp, /* transaction pointer */
  503. xfs_fsblock_t fsbno, /* file system block number */
  504. uint lock) /* lock flags for get_buf */
  505. {
  506. xfs_buf_t *bp; /* buffer pointer (return value) */
  507. xfs_daddr_t d; /* real disk block address */
  508. ASSERT(fsbno != NULLFSBLOCK);
  509. d = XFS_FSB_TO_DADDR(mp, fsbno);
  510. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  511. ASSERT(!xfs_buf_geterror(bp));
  512. return bp;
  513. }
  514. /*
  515. * Get a buffer for the block, return it with no data read.
  516. * Short-form addressing.
  517. */
  518. xfs_buf_t * /* buffer for agno/agbno */
  519. xfs_btree_get_bufs(
  520. xfs_mount_t *mp, /* file system mount point */
  521. xfs_trans_t *tp, /* transaction pointer */
  522. xfs_agnumber_t agno, /* allocation group number */
  523. xfs_agblock_t agbno, /* allocation group block number */
  524. uint lock) /* lock flags for get_buf */
  525. {
  526. xfs_buf_t *bp; /* buffer pointer (return value) */
  527. xfs_daddr_t d; /* real disk block address */
  528. ASSERT(agno != NULLAGNUMBER);
  529. ASSERT(agbno != NULLAGBLOCK);
  530. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  531. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  532. ASSERT(!xfs_buf_geterror(bp));
  533. return bp;
  534. }
  535. /*
  536. * Check for the cursor referring to the last block at the given level.
  537. */
  538. int /* 1=is last block, 0=not last block */
  539. xfs_btree_islastblock(
  540. xfs_btree_cur_t *cur, /* btree cursor */
  541. int level) /* level to check */
  542. {
  543. struct xfs_btree_block *block; /* generic btree block pointer */
  544. xfs_buf_t *bp; /* buffer containing block */
  545. block = xfs_btree_get_block(cur, level, &bp);
  546. xfs_btree_check_block(cur, block, level, bp);
  547. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  548. return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO);
  549. else
  550. return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
  551. }
  552. /*
  553. * Change the cursor to point to the first record at the given level.
  554. * Other levels are unaffected.
  555. */
  556. STATIC int /* success=1, failure=0 */
  557. xfs_btree_firstrec(
  558. xfs_btree_cur_t *cur, /* btree cursor */
  559. int level) /* level to change */
  560. {
  561. struct xfs_btree_block *block; /* generic btree block pointer */
  562. xfs_buf_t *bp; /* buffer containing block */
  563. /*
  564. * Get the block pointer for this level.
  565. */
  566. block = xfs_btree_get_block(cur, level, &bp);
  567. xfs_btree_check_block(cur, block, level, bp);
  568. /*
  569. * It's empty, there is no such record.
  570. */
  571. if (!block->bb_numrecs)
  572. return 0;
  573. /*
  574. * Set the ptr value to 1, that's the first record/key.
  575. */
  576. cur->bc_ptrs[level] = 1;
  577. return 1;
  578. }
  579. /*
  580. * Change the cursor to point to the last record in the current block
  581. * at the given level. Other levels are unaffected.
  582. */
  583. STATIC int /* success=1, failure=0 */
  584. xfs_btree_lastrec(
  585. xfs_btree_cur_t *cur, /* btree cursor */
  586. int level) /* level to change */
  587. {
  588. struct xfs_btree_block *block; /* generic btree block pointer */
  589. xfs_buf_t *bp; /* buffer containing block */
  590. /*
  591. * Get the block pointer for this level.
  592. */
  593. block = xfs_btree_get_block(cur, level, &bp);
  594. xfs_btree_check_block(cur, block, level, bp);
  595. /*
  596. * It's empty, there is no such record.
  597. */
  598. if (!block->bb_numrecs)
  599. return 0;
  600. /*
  601. * Set the ptr value to numrecs, that's the last record/key.
  602. */
  603. cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
  604. return 1;
  605. }
  606. /*
  607. * Compute first and last byte offsets for the fields given.
  608. * Interprets the offsets table, which contains struct field offsets.
  609. */
  610. void
  611. xfs_btree_offsets(
  612. __int64_t fields, /* bitmask of fields */
  613. const short *offsets, /* table of field offsets */
  614. int nbits, /* number of bits to inspect */
  615. int *first, /* output: first byte offset */
  616. int *last) /* output: last byte offset */
  617. {
  618. int i; /* current bit number */
  619. __int64_t imask; /* mask for current bit number */
  620. ASSERT(fields != 0);
  621. /*
  622. * Find the lowest bit, so the first byte offset.
  623. */
  624. for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
  625. if (imask & fields) {
  626. *first = offsets[i];
  627. break;
  628. }
  629. }
  630. /*
  631. * Find the highest bit, so the last byte offset.
  632. */
  633. for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
  634. if (imask & fields) {
  635. *last = offsets[i + 1] - 1;
  636. break;
  637. }
  638. }
  639. }
  640. /*
  641. * Get a buffer for the block, return it read in.
  642. * Long-form addressing.
  643. */
  644. int
  645. xfs_btree_read_bufl(
  646. struct xfs_mount *mp, /* file system mount point */
  647. struct xfs_trans *tp, /* transaction pointer */
  648. xfs_fsblock_t fsbno, /* file system block number */
  649. uint lock, /* lock flags for read_buf */
  650. struct xfs_buf **bpp, /* buffer for fsbno */
  651. int refval, /* ref count value for buffer */
  652. const struct xfs_buf_ops *ops)
  653. {
  654. struct xfs_buf *bp; /* return value */
  655. xfs_daddr_t d; /* real disk block address */
  656. int error;
  657. ASSERT(fsbno != NULLFSBLOCK);
  658. d = XFS_FSB_TO_DADDR(mp, fsbno);
  659. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  660. mp->m_bsize, lock, &bp, ops);
  661. if (error)
  662. return error;
  663. ASSERT(!xfs_buf_geterror(bp));
  664. if (bp)
  665. xfs_buf_set_ref(bp, refval);
  666. *bpp = bp;
  667. return 0;
  668. }
  669. /*
  670. * Read-ahead the block, don't wait for it, don't return a buffer.
  671. * Long-form addressing.
  672. */
  673. /* ARGSUSED */
  674. void
  675. xfs_btree_reada_bufl(
  676. struct xfs_mount *mp, /* file system mount point */
  677. xfs_fsblock_t fsbno, /* file system block number */
  678. xfs_extlen_t count, /* count of filesystem blocks */
  679. const struct xfs_buf_ops *ops)
  680. {
  681. xfs_daddr_t d;
  682. ASSERT(fsbno != NULLFSBLOCK);
  683. d = XFS_FSB_TO_DADDR(mp, fsbno);
  684. xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
  685. }
  686. /*
  687. * Read-ahead the block, don't wait for it, don't return a buffer.
  688. * Short-form addressing.
  689. */
  690. /* ARGSUSED */
  691. void
  692. xfs_btree_reada_bufs(
  693. struct xfs_mount *mp, /* file system mount point */
  694. xfs_agnumber_t agno, /* allocation group number */
  695. xfs_agblock_t agbno, /* allocation group block number */
  696. xfs_extlen_t count, /* count of filesystem blocks */
  697. const struct xfs_buf_ops *ops)
  698. {
  699. xfs_daddr_t d;
  700. ASSERT(agno != NULLAGNUMBER);
  701. ASSERT(agbno != NULLAGBLOCK);
  702. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  703. xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
  704. }
  705. STATIC int
  706. xfs_btree_readahead_lblock(
  707. struct xfs_btree_cur *cur,
  708. int lr,
  709. struct xfs_btree_block *block)
  710. {
  711. int rval = 0;
  712. xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
  713. xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
  714. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
  715. xfs_btree_reada_bufl(cur->bc_mp, left, 1,
  716. cur->bc_ops->buf_ops);
  717. rval++;
  718. }
  719. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
  720. xfs_btree_reada_bufl(cur->bc_mp, right, 1,
  721. cur->bc_ops->buf_ops);
  722. rval++;
  723. }
  724. return rval;
  725. }
  726. STATIC int
  727. xfs_btree_readahead_sblock(
  728. struct xfs_btree_cur *cur,
  729. int lr,
  730. struct xfs_btree_block *block)
  731. {
  732. int rval = 0;
  733. xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
  734. xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
  735. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
  736. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  737. left, 1, cur->bc_ops->buf_ops);
  738. rval++;
  739. }
  740. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
  741. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  742. right, 1, cur->bc_ops->buf_ops);
  743. rval++;
  744. }
  745. return rval;
  746. }
  747. /*
  748. * Read-ahead btree blocks, at the given level.
  749. * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
  750. */
  751. STATIC int
  752. xfs_btree_readahead(
  753. struct xfs_btree_cur *cur, /* btree cursor */
  754. int lev, /* level in btree */
  755. int lr) /* left/right bits */
  756. {
  757. struct xfs_btree_block *block;
  758. /*
  759. * No readahead needed if we are at the root level and the
  760. * btree root is stored in the inode.
  761. */
  762. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  763. (lev == cur->bc_nlevels - 1))
  764. return 0;
  765. if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
  766. return 0;
  767. cur->bc_ra[lev] |= lr;
  768. block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
  769. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  770. return xfs_btree_readahead_lblock(cur, lr, block);
  771. return xfs_btree_readahead_sblock(cur, lr, block);
  772. }
  773. STATIC xfs_daddr_t
  774. xfs_btree_ptr_to_daddr(
  775. struct xfs_btree_cur *cur,
  776. union xfs_btree_ptr *ptr)
  777. {
  778. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  779. ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
  780. return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
  781. } else {
  782. ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
  783. ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
  784. return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
  785. be32_to_cpu(ptr->s));
  786. }
  787. }
  788. /*
  789. * Readahead @count btree blocks at the given @ptr location.
  790. *
  791. * We don't need to care about long or short form btrees here as we have a
  792. * method of converting the ptr directly to a daddr available to us.
  793. */
  794. STATIC void
  795. xfs_btree_readahead_ptr(
  796. struct xfs_btree_cur *cur,
  797. union xfs_btree_ptr *ptr,
  798. xfs_extlen_t count)
  799. {
  800. xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
  801. xfs_btree_ptr_to_daddr(cur, ptr),
  802. cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
  803. }
  804. /*
  805. * Set the buffer for level "lev" in the cursor to bp, releasing
  806. * any previous buffer.
  807. */
  808. STATIC void
  809. xfs_btree_setbuf(
  810. xfs_btree_cur_t *cur, /* btree cursor */
  811. int lev, /* level in btree */
  812. xfs_buf_t *bp) /* new buffer to set */
  813. {
  814. struct xfs_btree_block *b; /* btree block */
  815. if (cur->bc_bufs[lev])
  816. xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
  817. cur->bc_bufs[lev] = bp;
  818. cur->bc_ra[lev] = 0;
  819. b = XFS_BUF_TO_BLOCK(bp);
  820. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  821. if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
  822. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  823. if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
  824. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  825. } else {
  826. if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
  827. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  828. if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
  829. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  830. }
  831. }
  832. STATIC int
  833. xfs_btree_ptr_is_null(
  834. struct xfs_btree_cur *cur,
  835. union xfs_btree_ptr *ptr)
  836. {
  837. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  838. return ptr->l == cpu_to_be64(NULLDFSBNO);
  839. else
  840. return ptr->s == cpu_to_be32(NULLAGBLOCK);
  841. }
  842. STATIC void
  843. xfs_btree_set_ptr_null(
  844. struct xfs_btree_cur *cur,
  845. union xfs_btree_ptr *ptr)
  846. {
  847. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  848. ptr->l = cpu_to_be64(NULLDFSBNO);
  849. else
  850. ptr->s = cpu_to_be32(NULLAGBLOCK);
  851. }
  852. /*
  853. * Get/set/init sibling pointers
  854. */
  855. STATIC void
  856. xfs_btree_get_sibling(
  857. struct xfs_btree_cur *cur,
  858. struct xfs_btree_block *block,
  859. union xfs_btree_ptr *ptr,
  860. int lr)
  861. {
  862. ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
  863. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  864. if (lr == XFS_BB_RIGHTSIB)
  865. ptr->l = block->bb_u.l.bb_rightsib;
  866. else
  867. ptr->l = block->bb_u.l.bb_leftsib;
  868. } else {
  869. if (lr == XFS_BB_RIGHTSIB)
  870. ptr->s = block->bb_u.s.bb_rightsib;
  871. else
  872. ptr->s = block->bb_u.s.bb_leftsib;
  873. }
  874. }
  875. STATIC void
  876. xfs_btree_set_sibling(
  877. struct xfs_btree_cur *cur,
  878. struct xfs_btree_block *block,
  879. union xfs_btree_ptr *ptr,
  880. int lr)
  881. {
  882. ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
  883. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  884. if (lr == XFS_BB_RIGHTSIB)
  885. block->bb_u.l.bb_rightsib = ptr->l;
  886. else
  887. block->bb_u.l.bb_leftsib = ptr->l;
  888. } else {
  889. if (lr == XFS_BB_RIGHTSIB)
  890. block->bb_u.s.bb_rightsib = ptr->s;
  891. else
  892. block->bb_u.s.bb_leftsib = ptr->s;
  893. }
  894. }
  895. void
  896. xfs_btree_init_block_int(
  897. struct xfs_mount *mp,
  898. struct xfs_btree_block *buf,
  899. xfs_daddr_t blkno,
  900. __u32 magic,
  901. __u16 level,
  902. __u16 numrecs,
  903. __u64 owner,
  904. unsigned int flags)
  905. {
  906. buf->bb_magic = cpu_to_be32(magic);
  907. buf->bb_level = cpu_to_be16(level);
  908. buf->bb_numrecs = cpu_to_be16(numrecs);
  909. if (flags & XFS_BTREE_LONG_PTRS) {
  910. buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  911. buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  912. if (flags & XFS_BTREE_CRC_BLOCKS) {
  913. buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
  914. buf->bb_u.l.bb_owner = cpu_to_be64(owner);
  915. uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid);
  916. buf->bb_u.l.bb_pad = 0;
  917. buf->bb_u.l.bb_lsn = 0;
  918. }
  919. } else {
  920. /* owner is a 32 bit value on short blocks */
  921. __u32 __owner = (__u32)owner;
  922. buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  923. buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  924. if (flags & XFS_BTREE_CRC_BLOCKS) {
  925. buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
  926. buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
  927. uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid);
  928. buf->bb_u.s.bb_lsn = 0;
  929. }
  930. }
  931. }
  932. void
  933. xfs_btree_init_block(
  934. struct xfs_mount *mp,
  935. struct xfs_buf *bp,
  936. __u32 magic,
  937. __u16 level,
  938. __u16 numrecs,
  939. __u64 owner,
  940. unsigned int flags)
  941. {
  942. xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
  943. magic, level, numrecs, owner, flags);
  944. }
  945. STATIC void
  946. xfs_btree_init_block_cur(
  947. struct xfs_btree_cur *cur,
  948. struct xfs_buf *bp,
  949. int level,
  950. int numrecs)
  951. {
  952. __u64 owner;
  953. /*
  954. * we can pull the owner from the cursor right now as the different
  955. * owners align directly with the pointer size of the btree. This may
  956. * change in future, but is safe for current users of the generic btree
  957. * code.
  958. */
  959. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  960. owner = cur->bc_private.b.ip->i_ino;
  961. else
  962. owner = cur->bc_private.a.agno;
  963. xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
  964. xfs_btree_magic(cur), level, numrecs,
  965. owner, cur->bc_flags);
  966. }
  967. /*
  968. * Return true if ptr is the last record in the btree and
  969. * we need to track updates to this record. The decision
  970. * will be further refined in the update_lastrec method.
  971. */
  972. STATIC int
  973. xfs_btree_is_lastrec(
  974. struct xfs_btree_cur *cur,
  975. struct xfs_btree_block *block,
  976. int level)
  977. {
  978. union xfs_btree_ptr ptr;
  979. if (level > 0)
  980. return 0;
  981. if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
  982. return 0;
  983. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  984. if (!xfs_btree_ptr_is_null(cur, &ptr))
  985. return 0;
  986. return 1;
  987. }
  988. STATIC void
  989. xfs_btree_buf_to_ptr(
  990. struct xfs_btree_cur *cur,
  991. struct xfs_buf *bp,
  992. union xfs_btree_ptr *ptr)
  993. {
  994. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  995. ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
  996. XFS_BUF_ADDR(bp)));
  997. else {
  998. ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
  999. XFS_BUF_ADDR(bp)));
  1000. }
  1001. }
  1002. STATIC void
  1003. xfs_btree_set_refs(
  1004. struct xfs_btree_cur *cur,
  1005. struct xfs_buf *bp)
  1006. {
  1007. switch (cur->bc_btnum) {
  1008. case XFS_BTNUM_BNO:
  1009. case XFS_BTNUM_CNT:
  1010. xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
  1011. break;
  1012. case XFS_BTNUM_INO:
  1013. xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
  1014. break;
  1015. case XFS_BTNUM_BMAP:
  1016. xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
  1017. break;
  1018. default:
  1019. ASSERT(0);
  1020. }
  1021. }
  1022. STATIC int
  1023. xfs_btree_get_buf_block(
  1024. struct xfs_btree_cur *cur,
  1025. union xfs_btree_ptr *ptr,
  1026. int flags,
  1027. struct xfs_btree_block **block,
  1028. struct xfs_buf **bpp)
  1029. {
  1030. struct xfs_mount *mp = cur->bc_mp;
  1031. xfs_daddr_t d;
  1032. /* need to sort out how callers deal with failures first */
  1033. ASSERT(!(flags & XBF_TRYLOCK));
  1034. d = xfs_btree_ptr_to_daddr(cur, ptr);
  1035. *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
  1036. mp->m_bsize, flags);
  1037. if (!*bpp)
  1038. return ENOMEM;
  1039. (*bpp)->b_ops = cur->bc_ops->buf_ops;
  1040. *block = XFS_BUF_TO_BLOCK(*bpp);
  1041. return 0;
  1042. }
  1043. /*
  1044. * Read in the buffer at the given ptr and return the buffer and
  1045. * the block pointer within the buffer.
  1046. */
  1047. STATIC int
  1048. xfs_btree_read_buf_block(
  1049. struct xfs_btree_cur *cur,
  1050. union xfs_btree_ptr *ptr,
  1051. int level,
  1052. int flags,
  1053. struct xfs_btree_block **block,
  1054. struct xfs_buf **bpp)
  1055. {
  1056. struct xfs_mount *mp = cur->bc_mp;
  1057. xfs_daddr_t d;
  1058. int error;
  1059. /* need to sort out how callers deal with failures first */
  1060. ASSERT(!(flags & XBF_TRYLOCK));
  1061. d = xfs_btree_ptr_to_daddr(cur, ptr);
  1062. error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
  1063. mp->m_bsize, flags, bpp,
  1064. cur->bc_ops->buf_ops);
  1065. if (error)
  1066. return error;
  1067. ASSERT(!xfs_buf_geterror(*bpp));
  1068. xfs_btree_set_refs(cur, *bpp);
  1069. *block = XFS_BUF_TO_BLOCK(*bpp);
  1070. return 0;
  1071. }
  1072. /*
  1073. * Copy keys from one btree block to another.
  1074. */
  1075. STATIC void
  1076. xfs_btree_copy_keys(
  1077. struct xfs_btree_cur *cur,
  1078. union xfs_btree_key *dst_key,
  1079. union xfs_btree_key *src_key,
  1080. int numkeys)
  1081. {
  1082. ASSERT(numkeys >= 0);
  1083. memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
  1084. }
  1085. /*
  1086. * Copy records from one btree block to another.
  1087. */
  1088. STATIC void
  1089. xfs_btree_copy_recs(
  1090. struct xfs_btree_cur *cur,
  1091. union xfs_btree_rec *dst_rec,
  1092. union xfs_btree_rec *src_rec,
  1093. int numrecs)
  1094. {
  1095. ASSERT(numrecs >= 0);
  1096. memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
  1097. }
  1098. /*
  1099. * Copy block pointers from one btree block to another.
  1100. */
  1101. STATIC void
  1102. xfs_btree_copy_ptrs(
  1103. struct xfs_btree_cur *cur,
  1104. union xfs_btree_ptr *dst_ptr,
  1105. union xfs_btree_ptr *src_ptr,
  1106. int numptrs)
  1107. {
  1108. ASSERT(numptrs >= 0);
  1109. memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
  1110. }
  1111. /*
  1112. * Shift keys one index left/right inside a single btree block.
  1113. */
  1114. STATIC void
  1115. xfs_btree_shift_keys(
  1116. struct xfs_btree_cur *cur,
  1117. union xfs_btree_key *key,
  1118. int dir,
  1119. int numkeys)
  1120. {
  1121. char *dst_key;
  1122. ASSERT(numkeys >= 0);
  1123. ASSERT(dir == 1 || dir == -1);
  1124. dst_key = (char *)key + (dir * cur->bc_ops->key_len);
  1125. memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
  1126. }
  1127. /*
  1128. * Shift records one index left/right inside a single btree block.
  1129. */
  1130. STATIC void
  1131. xfs_btree_shift_recs(
  1132. struct xfs_btree_cur *cur,
  1133. union xfs_btree_rec *rec,
  1134. int dir,
  1135. int numrecs)
  1136. {
  1137. char *dst_rec;
  1138. ASSERT(numrecs >= 0);
  1139. ASSERT(dir == 1 || dir == -1);
  1140. dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
  1141. memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
  1142. }
  1143. /*
  1144. * Shift block pointers one index left/right inside a single btree block.
  1145. */
  1146. STATIC void
  1147. xfs_btree_shift_ptrs(
  1148. struct xfs_btree_cur *cur,
  1149. union xfs_btree_ptr *ptr,
  1150. int dir,
  1151. int numptrs)
  1152. {
  1153. char *dst_ptr;
  1154. ASSERT(numptrs >= 0);
  1155. ASSERT(dir == 1 || dir == -1);
  1156. dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
  1157. memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
  1158. }
  1159. /*
  1160. * Log key values from the btree block.
  1161. */
  1162. STATIC void
  1163. xfs_btree_log_keys(
  1164. struct xfs_btree_cur *cur,
  1165. struct xfs_buf *bp,
  1166. int first,
  1167. int last)
  1168. {
  1169. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1170. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1171. if (bp) {
  1172. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1173. xfs_trans_log_buf(cur->bc_tp, bp,
  1174. xfs_btree_key_offset(cur, first),
  1175. xfs_btree_key_offset(cur, last + 1) - 1);
  1176. } else {
  1177. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1178. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1179. }
  1180. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1181. }
  1182. /*
  1183. * Log record values from the btree block.
  1184. */
  1185. void
  1186. xfs_btree_log_recs(
  1187. struct xfs_btree_cur *cur,
  1188. struct xfs_buf *bp,
  1189. int first,
  1190. int last)
  1191. {
  1192. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1193. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1194. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1195. xfs_trans_log_buf(cur->bc_tp, bp,
  1196. xfs_btree_rec_offset(cur, first),
  1197. xfs_btree_rec_offset(cur, last + 1) - 1);
  1198. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1199. }
  1200. /*
  1201. * Log block pointer fields from a btree block (nonleaf).
  1202. */
  1203. STATIC void
  1204. xfs_btree_log_ptrs(
  1205. struct xfs_btree_cur *cur, /* btree cursor */
  1206. struct xfs_buf *bp, /* buffer containing btree block */
  1207. int first, /* index of first pointer to log */
  1208. int last) /* index of last pointer to log */
  1209. {
  1210. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1211. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1212. if (bp) {
  1213. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  1214. int level = xfs_btree_get_level(block);
  1215. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1216. xfs_trans_log_buf(cur->bc_tp, bp,
  1217. xfs_btree_ptr_offset(cur, first, level),
  1218. xfs_btree_ptr_offset(cur, last + 1, level) - 1);
  1219. } else {
  1220. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1221. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1222. }
  1223. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1224. }
  1225. /*
  1226. * Log fields from a btree block header.
  1227. */
  1228. void
  1229. xfs_btree_log_block(
  1230. struct xfs_btree_cur *cur, /* btree cursor */
  1231. struct xfs_buf *bp, /* buffer containing btree block */
  1232. int fields) /* mask of fields: XFS_BB_... */
  1233. {
  1234. int first; /* first byte offset logged */
  1235. int last; /* last byte offset logged */
  1236. static const short soffsets[] = { /* table of offsets (short) */
  1237. offsetof(struct xfs_btree_block, bb_magic),
  1238. offsetof(struct xfs_btree_block, bb_level),
  1239. offsetof(struct xfs_btree_block, bb_numrecs),
  1240. offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
  1241. offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
  1242. offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
  1243. offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
  1244. offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
  1245. offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
  1246. offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
  1247. XFS_BTREE_SBLOCK_CRC_LEN
  1248. };
  1249. static const short loffsets[] = { /* table of offsets (long) */
  1250. offsetof(struct xfs_btree_block, bb_magic),
  1251. offsetof(struct xfs_btree_block, bb_level),
  1252. offsetof(struct xfs_btree_block, bb_numrecs),
  1253. offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
  1254. offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
  1255. offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
  1256. offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
  1257. offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
  1258. offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
  1259. offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
  1260. offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
  1261. XFS_BTREE_LBLOCK_CRC_LEN
  1262. };
  1263. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1264. XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
  1265. if (bp) {
  1266. int nbits;
  1267. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
  1268. /*
  1269. * We don't log the CRC when updating a btree
  1270. * block but instead recreate it during log
  1271. * recovery. As the log buffers have checksums
  1272. * of their own this is safe and avoids logging a crc
  1273. * update in a lot of places.
  1274. */
  1275. if (fields == XFS_BB_ALL_BITS)
  1276. fields = XFS_BB_ALL_BITS_CRC;
  1277. nbits = XFS_BB_NUM_BITS_CRC;
  1278. } else {
  1279. nbits = XFS_BB_NUM_BITS;
  1280. }
  1281. xfs_btree_offsets(fields,
  1282. (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  1283. loffsets : soffsets,
  1284. nbits, &first, &last);
  1285. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1286. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  1287. } else {
  1288. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1289. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1290. }
  1291. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1292. }
  1293. /*
  1294. * Increment cursor by one record at the level.
  1295. * For nonzero levels the leaf-ward information is untouched.
  1296. */
  1297. int /* error */
  1298. xfs_btree_increment(
  1299. struct xfs_btree_cur *cur,
  1300. int level,
  1301. int *stat) /* success/failure */
  1302. {
  1303. struct xfs_btree_block *block;
  1304. union xfs_btree_ptr ptr;
  1305. struct xfs_buf *bp;
  1306. int error; /* error return value */
  1307. int lev;
  1308. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1309. XFS_BTREE_TRACE_ARGI(cur, level);
  1310. ASSERT(level < cur->bc_nlevels);
  1311. /* Read-ahead to the right at this level. */
  1312. xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
  1313. /* Get a pointer to the btree block. */
  1314. block = xfs_btree_get_block(cur, level, &bp);
  1315. #ifdef DEBUG
  1316. error = xfs_btree_check_block(cur, block, level, bp);
  1317. if (error)
  1318. goto error0;
  1319. #endif
  1320. /* We're done if we remain in the block after the increment. */
  1321. if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
  1322. goto out1;
  1323. /* Fail if we just went off the right edge of the tree. */
  1324. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1325. if (xfs_btree_ptr_is_null(cur, &ptr))
  1326. goto out0;
  1327. XFS_BTREE_STATS_INC(cur, increment);
  1328. /*
  1329. * March up the tree incrementing pointers.
  1330. * Stop when we don't go off the right edge of a block.
  1331. */
  1332. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1333. block = xfs_btree_get_block(cur, lev, &bp);
  1334. #ifdef DEBUG
  1335. error = xfs_btree_check_block(cur, block, lev, bp);
  1336. if (error)
  1337. goto error0;
  1338. #endif
  1339. if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
  1340. break;
  1341. /* Read-ahead the right block for the next loop. */
  1342. xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
  1343. }
  1344. /*
  1345. * If we went off the root then we are either seriously
  1346. * confused or have the tree root in an inode.
  1347. */
  1348. if (lev == cur->bc_nlevels) {
  1349. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1350. goto out0;
  1351. ASSERT(0);
  1352. error = EFSCORRUPTED;
  1353. goto error0;
  1354. }
  1355. ASSERT(lev < cur->bc_nlevels);
  1356. /*
  1357. * Now walk back down the tree, fixing up the cursor's buffer
  1358. * pointers and key numbers.
  1359. */
  1360. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1361. union xfs_btree_ptr *ptrp;
  1362. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1363. error = xfs_btree_read_buf_block(cur, ptrp, --lev,
  1364. 0, &block, &bp);
  1365. if (error)
  1366. goto error0;
  1367. xfs_btree_setbuf(cur, lev, bp);
  1368. cur->bc_ptrs[lev] = 1;
  1369. }
  1370. out1:
  1371. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1372. *stat = 1;
  1373. return 0;
  1374. out0:
  1375. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1376. *stat = 0;
  1377. return 0;
  1378. error0:
  1379. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1380. return error;
  1381. }
  1382. /*
  1383. * Decrement cursor by one record at the level.
  1384. * For nonzero levels the leaf-ward information is untouched.
  1385. */
  1386. int /* error */
  1387. xfs_btree_decrement(
  1388. struct xfs_btree_cur *cur,
  1389. int level,
  1390. int *stat) /* success/failure */
  1391. {
  1392. struct xfs_btree_block *block;
  1393. xfs_buf_t *bp;
  1394. int error; /* error return value */
  1395. int lev;
  1396. union xfs_btree_ptr ptr;
  1397. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1398. XFS_BTREE_TRACE_ARGI(cur, level);
  1399. ASSERT(level < cur->bc_nlevels);
  1400. /* Read-ahead to the left at this level. */
  1401. xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
  1402. /* We're done if we remain in the block after the decrement. */
  1403. if (--cur->bc_ptrs[level] > 0)
  1404. goto out1;
  1405. /* Get a pointer to the btree block. */
  1406. block = xfs_btree_get_block(cur, level, &bp);
  1407. #ifdef DEBUG
  1408. error = xfs_btree_check_block(cur, block, level, bp);
  1409. if (error)
  1410. goto error0;
  1411. #endif
  1412. /* Fail if we just went off the left edge of the tree. */
  1413. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
  1414. if (xfs_btree_ptr_is_null(cur, &ptr))
  1415. goto out0;
  1416. XFS_BTREE_STATS_INC(cur, decrement);
  1417. /*
  1418. * March up the tree decrementing pointers.
  1419. * Stop when we don't go off the left edge of a block.
  1420. */
  1421. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1422. if (--cur->bc_ptrs[lev] > 0)
  1423. break;
  1424. /* Read-ahead the left block for the next loop. */
  1425. xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
  1426. }
  1427. /*
  1428. * If we went off the root then we are seriously confused.
  1429. * or the root of the tree is in an inode.
  1430. */
  1431. if (lev == cur->bc_nlevels) {
  1432. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1433. goto out0;
  1434. ASSERT(0);
  1435. error = EFSCORRUPTED;
  1436. goto error0;
  1437. }
  1438. ASSERT(lev < cur->bc_nlevels);
  1439. /*
  1440. * Now walk back down the tree, fixing up the cursor's buffer
  1441. * pointers and key numbers.
  1442. */
  1443. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1444. union xfs_btree_ptr *ptrp;
  1445. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1446. error = xfs_btree_read_buf_block(cur, ptrp, --lev,
  1447. 0, &block, &bp);
  1448. if (error)
  1449. goto error0;
  1450. xfs_btree_setbuf(cur, lev, bp);
  1451. cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
  1452. }
  1453. out1:
  1454. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1455. *stat = 1;
  1456. return 0;
  1457. out0:
  1458. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1459. *stat = 0;
  1460. return 0;
  1461. error0:
  1462. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1463. return error;
  1464. }
  1465. STATIC int
  1466. xfs_btree_lookup_get_block(
  1467. struct xfs_btree_cur *cur, /* btree cursor */
  1468. int level, /* level in the btree */
  1469. union xfs_btree_ptr *pp, /* ptr to btree block */
  1470. struct xfs_btree_block **blkp) /* return btree block */
  1471. {
  1472. struct xfs_buf *bp; /* buffer pointer for btree block */
  1473. int error = 0;
  1474. /* special case the root block if in an inode */
  1475. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1476. (level == cur->bc_nlevels - 1)) {
  1477. *blkp = xfs_btree_get_iroot(cur);
  1478. return 0;
  1479. }
  1480. /*
  1481. * If the old buffer at this level for the disk address we are
  1482. * looking for re-use it.
  1483. *
  1484. * Otherwise throw it away and get a new one.
  1485. */
  1486. bp = cur->bc_bufs[level];
  1487. if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
  1488. *blkp = XFS_BUF_TO_BLOCK(bp);
  1489. return 0;
  1490. }
  1491. error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
  1492. if (error)
  1493. return error;
  1494. xfs_btree_setbuf(cur, level, bp);
  1495. return 0;
  1496. }
  1497. /*
  1498. * Get current search key. For level 0 we don't actually have a key
  1499. * structure so we make one up from the record. For all other levels
  1500. * we just return the right key.
  1501. */
  1502. STATIC union xfs_btree_key *
  1503. xfs_lookup_get_search_key(
  1504. struct xfs_btree_cur *cur,
  1505. int level,
  1506. int keyno,
  1507. struct xfs_btree_block *block,
  1508. union xfs_btree_key *kp)
  1509. {
  1510. if (level == 0) {
  1511. cur->bc_ops->init_key_from_rec(kp,
  1512. xfs_btree_rec_addr(cur, keyno, block));
  1513. return kp;
  1514. }
  1515. return xfs_btree_key_addr(cur, keyno, block);
  1516. }
  1517. /*
  1518. * Lookup the record. The cursor is made to point to it, based on dir.
  1519. * stat is set to 0 if can't find any such record, 1 for success.
  1520. */
  1521. int /* error */
  1522. xfs_btree_lookup(
  1523. struct xfs_btree_cur *cur, /* btree cursor */
  1524. xfs_lookup_t dir, /* <=, ==, or >= */
  1525. int *stat) /* success/failure */
  1526. {
  1527. struct xfs_btree_block *block; /* current btree block */
  1528. __int64_t diff; /* difference for the current key */
  1529. int error; /* error return value */
  1530. int keyno; /* current key number */
  1531. int level; /* level in the btree */
  1532. union xfs_btree_ptr *pp; /* ptr to btree block */
  1533. union xfs_btree_ptr ptr; /* ptr to btree block */
  1534. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1535. XFS_BTREE_TRACE_ARGI(cur, dir);
  1536. XFS_BTREE_STATS_INC(cur, lookup);
  1537. block = NULL;
  1538. keyno = 0;
  1539. /* initialise start pointer from cursor */
  1540. cur->bc_ops->init_ptr_from_cur(cur, &ptr);
  1541. pp = &ptr;
  1542. /*
  1543. * Iterate over each level in the btree, starting at the root.
  1544. * For each level above the leaves, find the key we need, based
  1545. * on the lookup record, then follow the corresponding block
  1546. * pointer down to the next level.
  1547. */
  1548. for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
  1549. /* Get the block we need to do the lookup on. */
  1550. error = xfs_btree_lookup_get_block(cur, level, pp, &block);
  1551. if (error)
  1552. goto error0;
  1553. if (diff == 0) {
  1554. /*
  1555. * If we already had a key match at a higher level, we
  1556. * know we need to use the first entry in this block.
  1557. */
  1558. keyno = 1;
  1559. } else {
  1560. /* Otherwise search this block. Do a binary search. */
  1561. int high; /* high entry number */
  1562. int low; /* low entry number */
  1563. /* Set low and high entry numbers, 1-based. */
  1564. low = 1;
  1565. high = xfs_btree_get_numrecs(block);
  1566. if (!high) {
  1567. /* Block is empty, must be an empty leaf. */
  1568. ASSERT(level == 0 && cur->bc_nlevels == 1);
  1569. cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
  1570. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1571. *stat = 0;
  1572. return 0;
  1573. }
  1574. /* Binary search the block. */
  1575. while (low <= high) {
  1576. union xfs_btree_key key;
  1577. union xfs_btree_key *kp;
  1578. XFS_BTREE_STATS_INC(cur, compare);
  1579. /* keyno is average of low and high. */
  1580. keyno = (low + high) >> 1;
  1581. /* Get current search key */
  1582. kp = xfs_lookup_get_search_key(cur, level,
  1583. keyno, block, &key);
  1584. /*
  1585. * Compute difference to get next direction:
  1586. * - less than, move right
  1587. * - greater than, move left
  1588. * - equal, we're done
  1589. */
  1590. diff = cur->bc_ops->key_diff(cur, kp);
  1591. if (diff < 0)
  1592. low = keyno + 1;
  1593. else if (diff > 0)
  1594. high = keyno - 1;
  1595. else
  1596. break;
  1597. }
  1598. }
  1599. /*
  1600. * If there are more levels, set up for the next level
  1601. * by getting the block number and filling in the cursor.
  1602. */
  1603. if (level > 0) {
  1604. /*
  1605. * If we moved left, need the previous key number,
  1606. * unless there isn't one.
  1607. */
  1608. if (diff > 0 && --keyno < 1)
  1609. keyno = 1;
  1610. pp = xfs_btree_ptr_addr(cur, keyno, block);
  1611. #ifdef DEBUG
  1612. error = xfs_btree_check_ptr(cur, pp, 0, level);
  1613. if (error)
  1614. goto error0;
  1615. #endif
  1616. cur->bc_ptrs[level] = keyno;
  1617. }
  1618. }
  1619. /* Done with the search. See if we need to adjust the results. */
  1620. if (dir != XFS_LOOKUP_LE && diff < 0) {
  1621. keyno++;
  1622. /*
  1623. * If ge search and we went off the end of the block, but it's
  1624. * not the last block, we're in the wrong block.
  1625. */
  1626. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1627. if (dir == XFS_LOOKUP_GE &&
  1628. keyno > xfs_btree_get_numrecs(block) &&
  1629. !xfs_btree_ptr_is_null(cur, &ptr)) {
  1630. int i;
  1631. cur->bc_ptrs[0] = keyno;
  1632. error = xfs_btree_increment(cur, 0, &i);
  1633. if (error)
  1634. goto error0;
  1635. XFS_WANT_CORRUPTED_RETURN(i == 1);
  1636. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1637. *stat = 1;
  1638. return 0;
  1639. }
  1640. } else if (dir == XFS_LOOKUP_LE && diff > 0)
  1641. keyno--;
  1642. cur->bc_ptrs[0] = keyno;
  1643. /* Return if we succeeded or not. */
  1644. if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
  1645. *stat = 0;
  1646. else if (dir != XFS_LOOKUP_EQ || diff == 0)
  1647. *stat = 1;
  1648. else
  1649. *stat = 0;
  1650. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1651. return 0;
  1652. error0:
  1653. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1654. return error;
  1655. }
  1656. /*
  1657. * Update keys at all levels from here to the root along the cursor's path.
  1658. */
  1659. STATIC int
  1660. xfs_btree_updkey(
  1661. struct xfs_btree_cur *cur,
  1662. union xfs_btree_key *keyp,
  1663. int level)
  1664. {
  1665. struct xfs_btree_block *block;
  1666. struct xfs_buf *bp;
  1667. union xfs_btree_key *kp;
  1668. int ptr;
  1669. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1670. XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
  1671. ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
  1672. /*
  1673. * Go up the tree from this level toward the root.
  1674. * At each level, update the key value to the value input.
  1675. * Stop when we reach a level where the cursor isn't pointing
  1676. * at the first entry in the block.
  1677. */
  1678. for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
  1679. #ifdef DEBUG
  1680. int error;
  1681. #endif
  1682. block = xfs_btree_get_block(cur, level, &bp);
  1683. #ifdef DEBUG
  1684. error = xfs_btree_check_block(cur, block, level, bp);
  1685. if (error) {
  1686. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1687. return error;
  1688. }
  1689. #endif
  1690. ptr = cur->bc_ptrs[level];
  1691. kp = xfs_btree_key_addr(cur, ptr, block);
  1692. xfs_btree_copy_keys(cur, kp, keyp, 1);
  1693. xfs_btree_log_keys(cur, bp, ptr, ptr);
  1694. }
  1695. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1696. return 0;
  1697. }
  1698. /*
  1699. * Update the record referred to by cur to the value in the
  1700. * given record. This either works (return 0) or gets an
  1701. * EFSCORRUPTED error.
  1702. */
  1703. int
  1704. xfs_btree_update(
  1705. struct xfs_btree_cur *cur,
  1706. union xfs_btree_rec *rec)
  1707. {
  1708. struct xfs_btree_block *block;
  1709. struct xfs_buf *bp;
  1710. int error;
  1711. int ptr;
  1712. union xfs_btree_rec *rp;
  1713. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1714. XFS_BTREE_TRACE_ARGR(cur, rec);
  1715. /* Pick up the current block. */
  1716. block = xfs_btree_get_block(cur, 0, &bp);
  1717. #ifdef DEBUG
  1718. error = xfs_btree_check_block(cur, block, 0, bp);
  1719. if (error)
  1720. goto error0;
  1721. #endif
  1722. /* Get the address of the rec to be updated. */
  1723. ptr = cur->bc_ptrs[0];
  1724. rp = xfs_btree_rec_addr(cur, ptr, block);
  1725. /* Fill in the new contents and log them. */
  1726. xfs_btree_copy_recs(cur, rp, rec, 1);
  1727. xfs_btree_log_recs(cur, bp, ptr, ptr);
  1728. /*
  1729. * If we are tracking the last record in the tree and
  1730. * we are at the far right edge of the tree, update it.
  1731. */
  1732. if (xfs_btree_is_lastrec(cur, block, 0)) {
  1733. cur->bc_ops->update_lastrec(cur, block, rec,
  1734. ptr, LASTREC_UPDATE);
  1735. }
  1736. /* Updating first rec in leaf. Pass new key value up to our parent. */
  1737. if (ptr == 1) {
  1738. union xfs_btree_key key;
  1739. cur->bc_ops->init_key_from_rec(&key, rec);
  1740. error = xfs_btree_updkey(cur, &key, 1);
  1741. if (error)
  1742. goto error0;
  1743. }
  1744. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1745. return 0;
  1746. error0:
  1747. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1748. return error;
  1749. }
  1750. /*
  1751. * Move 1 record left from cur/level if possible.
  1752. * Update cur to reflect the new path.
  1753. */
  1754. STATIC int /* error */
  1755. xfs_btree_lshift(
  1756. struct xfs_btree_cur *cur,
  1757. int level,
  1758. int *stat) /* success/failure */
  1759. {
  1760. union xfs_btree_key key; /* btree key */
  1761. struct xfs_buf *lbp; /* left buffer pointer */
  1762. struct xfs_btree_block *left; /* left btree block */
  1763. int lrecs; /* left record count */
  1764. struct xfs_buf *rbp; /* right buffer pointer */
  1765. struct xfs_btree_block *right; /* right btree block */
  1766. int rrecs; /* right record count */
  1767. union xfs_btree_ptr lptr; /* left btree pointer */
  1768. union xfs_btree_key *rkp = NULL; /* right btree key */
  1769. union xfs_btree_ptr *rpp = NULL; /* right address pointer */
  1770. union xfs_btree_rec *rrp = NULL; /* right record pointer */
  1771. int error; /* error return value */
  1772. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1773. XFS_BTREE_TRACE_ARGI(cur, level);
  1774. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1775. level == cur->bc_nlevels - 1)
  1776. goto out0;
  1777. /* Set up variables for this block as "right". */
  1778. right = xfs_btree_get_block(cur, level, &rbp);
  1779. #ifdef DEBUG
  1780. error = xfs_btree_check_block(cur, right, level, rbp);
  1781. if (error)
  1782. goto error0;
  1783. #endif
  1784. /* If we've got no left sibling then we can't shift an entry left. */
  1785. xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  1786. if (xfs_btree_ptr_is_null(cur, &lptr))
  1787. goto out0;
  1788. /*
  1789. * If the cursor entry is the one that would be moved, don't
  1790. * do it... it's too complicated.
  1791. */
  1792. if (cur->bc_ptrs[level] <= 1)
  1793. goto out0;
  1794. /* Set up the left neighbor as "left". */
  1795. error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
  1796. if (error)
  1797. goto error0;
  1798. /* If it's full, it can't take another entry. */
  1799. lrecs = xfs_btree_get_numrecs(left);
  1800. if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
  1801. goto out0;
  1802. rrecs = xfs_btree_get_numrecs(right);
  1803. /*
  1804. * We add one entry to the left side and remove one for the right side.
  1805. * Account for it here, the changes will be updated on disk and logged
  1806. * later.
  1807. */
  1808. lrecs++;
  1809. rrecs--;
  1810. XFS_BTREE_STATS_INC(cur, lshift);
  1811. XFS_BTREE_STATS_ADD(cur, moves, 1);
  1812. /*
  1813. * If non-leaf, copy a key and a ptr to the left block.
  1814. * Log the changes to the left block.
  1815. */
  1816. if (level > 0) {
  1817. /* It's a non-leaf. Move keys and pointers. */
  1818. union xfs_btree_key *lkp; /* left btree key */
  1819. union xfs_btree_ptr *lpp; /* left address pointer */
  1820. lkp = xfs_btree_key_addr(cur, lrecs, left);
  1821. rkp = xfs_btree_key_addr(cur, 1, right);
  1822. lpp = xfs_btree_ptr_addr(cur, lrecs, left);
  1823. rpp = xfs_btree_ptr_addr(cur, 1, right);
  1824. #ifdef DEBUG
  1825. error = xfs_btree_check_ptr(cur, rpp, 0, level);
  1826. if (error)
  1827. goto error0;
  1828. #endif
  1829. xfs_btree_copy_keys(cur, lkp, rkp, 1);
  1830. xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
  1831. xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
  1832. xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
  1833. ASSERT(cur->bc_ops->keys_inorder(cur,
  1834. xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
  1835. } else {
  1836. /* It's a leaf. Move records. */
  1837. union xfs_btree_rec *lrp; /* left record pointer */
  1838. lrp = xfs_btree_rec_addr(cur, lrecs, left);
  1839. rrp = xfs_btree_rec_addr(cur, 1, right);
  1840. xfs_btree_copy_recs(cur, lrp, rrp, 1);
  1841. xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
  1842. ASSERT(cur->bc_ops->recs_inorder(cur,
  1843. xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
  1844. }
  1845. xfs_btree_set_numrecs(left, lrecs);
  1846. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
  1847. xfs_btree_set_numrecs(right, rrecs);
  1848. xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
  1849. /*
  1850. * Slide the contents of right down one entry.
  1851. */
  1852. XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
  1853. if (level > 0) {
  1854. /* It's a nonleaf. operate on keys and ptrs */
  1855. #ifdef DEBUG
  1856. int i; /* loop index */
  1857. for (i = 0; i < rrecs; i++) {
  1858. error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
  1859. if (error)
  1860. goto error0;
  1861. }
  1862. #endif
  1863. xfs_btree_shift_keys(cur,
  1864. xfs_btree_key_addr(cur, 2, right),
  1865. -1, rrecs);
  1866. xfs_btree_shift_ptrs(cur,
  1867. xfs_btree_ptr_addr(cur, 2, right),
  1868. -1, rrecs);
  1869. xfs_btree_log_keys(cur, rbp, 1, rrecs);
  1870. xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
  1871. } else {
  1872. /* It's a leaf. operate on records */
  1873. xfs_btree_shift_recs(cur,
  1874. xfs_btree_rec_addr(cur, 2, right),
  1875. -1, rrecs);
  1876. xfs_btree_log_recs(cur, rbp, 1, rrecs);
  1877. /*
  1878. * If it's the first record in the block, we'll need a key
  1879. * structure to pass up to the next level (updkey).
  1880. */
  1881. cur->bc_ops->init_key_from_rec(&key,
  1882. xfs_btree_rec_addr(cur, 1, right));
  1883. rkp = &key;
  1884. }
  1885. /* Update the parent key values of right. */
  1886. error = xfs_btree_updkey(cur, rkp, level + 1);
  1887. if (error)
  1888. goto error0;
  1889. /* Slide the cursor value left one. */
  1890. cur->bc_ptrs[level]--;
  1891. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1892. *stat = 1;
  1893. return 0;
  1894. out0:
  1895. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1896. *stat = 0;
  1897. return 0;
  1898. error0:
  1899. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1900. return error;
  1901. }
  1902. /*
  1903. * Move 1 record right from cur/level if possible.
  1904. * Update cur to reflect the new path.
  1905. */
  1906. STATIC int /* error */
  1907. xfs_btree_rshift(
  1908. struct xfs_btree_cur *cur,
  1909. int level,
  1910. int *stat) /* success/failure */
  1911. {
  1912. union xfs_btree_key key; /* btree key */
  1913. struct xfs_buf *lbp; /* left buffer pointer */
  1914. struct xfs_btree_block *left; /* left btree block */
  1915. struct xfs_buf *rbp; /* right buffer pointer */
  1916. struct xfs_btree_block *right; /* right btree block */
  1917. struct xfs_btree_cur *tcur; /* temporary btree cursor */
  1918. union xfs_btree_ptr rptr; /* right block pointer */
  1919. union xfs_btree_key *rkp; /* right btree key */
  1920. int rrecs; /* right record count */
  1921. int lrecs; /* left record count */
  1922. int error; /* error return value */
  1923. int i; /* loop counter */
  1924. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1925. XFS_BTREE_TRACE_ARGI(cur, level);
  1926. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1927. (level == cur->bc_nlevels - 1))
  1928. goto out0;
  1929. /* Set up variables for this block as "left". */
  1930. left = xfs_btree_get_block(cur, level, &lbp);
  1931. #ifdef DEBUG
  1932. error = xfs_btree_check_block(cur, left, level, lbp);
  1933. if (error)
  1934. goto error0;
  1935. #endif
  1936. /* If we've got no right sibling then we can't shift an entry right. */
  1937. xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
  1938. if (xfs_btree_ptr_is_null(cur, &rptr))
  1939. goto out0;
  1940. /*
  1941. * If the cursor entry is the one that would be moved, don't
  1942. * do it... it's too complicated.
  1943. */
  1944. lrecs = xfs_btree_get_numrecs(left);
  1945. if (cur->bc_ptrs[level] >= lrecs)
  1946. goto out0;
  1947. /* Set up the right neighbor as "right". */
  1948. error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
  1949. if (error)
  1950. goto error0;
  1951. /* If it's full, it can't take another entry. */
  1952. rrecs = xfs_btree_get_numrecs(right);
  1953. if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
  1954. goto out0;
  1955. XFS_BTREE_STATS_INC(cur, rshift);
  1956. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  1957. /*
  1958. * Make a hole at the start of the right neighbor block, then
  1959. * copy the last left block entry to the hole.
  1960. */
  1961. if (level > 0) {
  1962. /* It's a nonleaf. make a hole in the keys and ptrs */
  1963. union xfs_btree_key *lkp;
  1964. union xfs_btree_ptr *lpp;
  1965. union xfs_btree_ptr *rpp;
  1966. lkp = xfs_btree_key_addr(cur, lrecs, left);
  1967. lpp = xfs_btree_ptr_addr(cur, lrecs, left);
  1968. rkp = xfs_btree_key_addr(cur, 1, right);
  1969. rpp = xfs_btree_ptr_addr(cur, 1, right);
  1970. #ifdef DEBUG
  1971. for (i = rrecs - 1; i >= 0; i--) {
  1972. error = xfs_btree_check_ptr(cur, rpp, i, level);
  1973. if (error)
  1974. goto error0;
  1975. }
  1976. #endif
  1977. xfs_btree_shift_keys(cur, rkp, 1, rrecs);
  1978. xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
  1979. #ifdef DEBUG
  1980. error = xfs_btree_check_ptr(cur, lpp, 0, level);
  1981. if (error)
  1982. goto error0;
  1983. #endif
  1984. /* Now put the new data in, and log it. */
  1985. xfs_btree_copy_keys(cur, rkp, lkp, 1);
  1986. xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
  1987. xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
  1988. xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
  1989. ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
  1990. xfs_btree_key_addr(cur, 2, right)));
  1991. } else {
  1992. /* It's a leaf. make a hole in the records */
  1993. union xfs_btree_rec *lrp;
  1994. union xfs_btree_rec *rrp;
  1995. lrp = xfs_btree_rec_addr(cur, lrecs, left);
  1996. rrp = xfs_btree_rec_addr(cur, 1, right);
  1997. xfs_btree_shift_recs(cur, rrp, 1, rrecs);
  1998. /* Now put the new data in, and log it. */
  1999. xfs_btree_copy_recs(cur, rrp, lrp, 1);
  2000. xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
  2001. cur->bc_ops->init_key_from_rec(&key, rrp);
  2002. rkp = &key;
  2003. ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
  2004. xfs_btree_rec_addr(cur, 2, right)));
  2005. }
  2006. /*
  2007. * Decrement and log left's numrecs, bump and log right's numrecs.
  2008. */
  2009. xfs_btree_set_numrecs(left, --lrecs);
  2010. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
  2011. xfs_btree_set_numrecs(right, ++rrecs);
  2012. xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
  2013. /*
  2014. * Using a temporary cursor, update the parent key values of the
  2015. * block on the right.
  2016. */
  2017. error = xfs_btree_dup_cursor(cur, &tcur);
  2018. if (error)
  2019. goto error0;
  2020. i = xfs_btree_lastrec(tcur, level);
  2021. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2022. error = xfs_btree_increment(tcur, level, &i);
  2023. if (error)
  2024. goto error1;
  2025. error = xfs_btree_updkey(tcur, rkp, level + 1);
  2026. if (error)
  2027. goto error1;
  2028. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  2029. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2030. *stat = 1;
  2031. return 0;
  2032. out0:
  2033. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2034. *stat = 0;
  2035. return 0;
  2036. error0:
  2037. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2038. return error;
  2039. error1:
  2040. XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
  2041. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  2042. return error;
  2043. }
  2044. /*
  2045. * Split cur/level block in half.
  2046. * Return new block number and the key to its first
  2047. * record (to be inserted into parent).
  2048. */
  2049. STATIC int /* error */
  2050. xfs_btree_split(
  2051. struct xfs_btree_cur *cur,
  2052. int level,
  2053. union xfs_btree_ptr *ptrp,
  2054. union xfs_btree_key *key,
  2055. struct xfs_btree_cur **curp,
  2056. int *stat) /* success/failure */
  2057. {
  2058. union xfs_btree_ptr lptr; /* left sibling block ptr */
  2059. struct xfs_buf *lbp; /* left buffer pointer */
  2060. struct xfs_btree_block *left; /* left btree block */
  2061. union xfs_btree_ptr rptr; /* right sibling block ptr */
  2062. struct xfs_buf *rbp; /* right buffer pointer */
  2063. struct xfs_btree_block *right; /* right btree block */
  2064. union xfs_btree_ptr rrptr; /* right-right sibling ptr */
  2065. struct xfs_buf *rrbp; /* right-right buffer pointer */
  2066. struct xfs_btree_block *rrblock; /* right-right btree block */
  2067. int lrecs;
  2068. int rrecs;
  2069. int src_index;
  2070. int error; /* error return value */
  2071. #ifdef DEBUG
  2072. int i;
  2073. #endif
  2074. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2075. XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
  2076. XFS_BTREE_STATS_INC(cur, split);
  2077. /* Set up left block (current one). */
  2078. left = xfs_btree_get_block(cur, level, &lbp);
  2079. #ifdef DEBUG
  2080. error = xfs_btree_check_block(cur, left, level, lbp);
  2081. if (error)
  2082. goto error0;
  2083. #endif
  2084. xfs_btree_buf_to_ptr(cur, lbp, &lptr);
  2085. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2086. error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
  2087. if (error)
  2088. goto error0;
  2089. if (*stat == 0)
  2090. goto out0;
  2091. XFS_BTREE_STATS_INC(cur, alloc);
  2092. /* Set up the new block as "right". */
  2093. error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
  2094. if (error)
  2095. goto error0;
  2096. /* Fill in the btree header for the new right block. */
  2097. xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
  2098. /*
  2099. * Split the entries between the old and the new block evenly.
  2100. * Make sure that if there's an odd number of entries now, that
  2101. * each new block will have the same number of entries.
  2102. */
  2103. lrecs = xfs_btree_get_numrecs(left);
  2104. rrecs = lrecs / 2;
  2105. if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
  2106. rrecs++;
  2107. src_index = (lrecs - rrecs + 1);
  2108. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  2109. /*
  2110. * Copy btree block entries from the left block over to the
  2111. * new block, the right. Update the right block and log the
  2112. * changes.
  2113. */
  2114. if (level > 0) {
  2115. /* It's a non-leaf. Move keys and pointers. */
  2116. union xfs_btree_key *lkp; /* left btree key */
  2117. union xfs_btree_ptr *lpp; /* left address pointer */
  2118. union xfs_btree_key *rkp; /* right btree key */
  2119. union xfs_btree_ptr *rpp; /* right address pointer */
  2120. lkp = xfs_btree_key_addr(cur, src_index, left);
  2121. lpp = xfs_btree_ptr_addr(cur, src_index, left);
  2122. rkp = xfs_btree_key_addr(cur, 1, right);
  2123. rpp = xfs_btree_ptr_addr(cur, 1, right);
  2124. #ifdef DEBUG
  2125. for (i = src_index; i < rrecs; i++) {
  2126. error = xfs_btree_check_ptr(cur, lpp, i, level);
  2127. if (error)
  2128. goto error0;
  2129. }
  2130. #endif
  2131. xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
  2132. xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
  2133. xfs_btree_log_keys(cur, rbp, 1, rrecs);
  2134. xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
  2135. /* Grab the keys to the entries moved to the right block */
  2136. xfs_btree_copy_keys(cur, key, rkp, 1);
  2137. } else {
  2138. /* It's a leaf. Move records. */
  2139. union xfs_btree_rec *lrp; /* left record pointer */
  2140. union xfs_btree_rec *rrp; /* right record pointer */
  2141. lrp = xfs_btree_rec_addr(cur, src_index, left);
  2142. rrp = xfs_btree_rec_addr(cur, 1, right);
  2143. xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
  2144. xfs_btree_log_recs(cur, rbp, 1, rrecs);
  2145. cur->bc_ops->init_key_from_rec(key,
  2146. xfs_btree_rec_addr(cur, 1, right));
  2147. }
  2148. /*
  2149. * Find the left block number by looking in the buffer.
  2150. * Adjust numrecs, sibling pointers.
  2151. */
  2152. xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
  2153. xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
  2154. xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  2155. xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
  2156. lrecs -= rrecs;
  2157. xfs_btree_set_numrecs(left, lrecs);
  2158. xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
  2159. xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
  2160. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  2161. /*
  2162. * If there's a block to the new block's right, make that block
  2163. * point back to right instead of to left.
  2164. */
  2165. if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
  2166. error = xfs_btree_read_buf_block(cur, &rrptr, level,
  2167. 0, &rrblock, &rrbp);
  2168. if (error)
  2169. goto error0;
  2170. xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
  2171. xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
  2172. }
  2173. /*
  2174. * If the cursor is really in the right block, move it there.
  2175. * If it's just pointing past the last entry in left, then we'll
  2176. * insert there, so don't change anything in that case.
  2177. */
  2178. if (cur->bc_ptrs[level] > lrecs + 1) {
  2179. xfs_btree_setbuf(cur, level, rbp);
  2180. cur->bc_ptrs[level] -= lrecs;
  2181. }
  2182. /*
  2183. * If there are more levels, we'll need another cursor which refers
  2184. * the right block, no matter where this cursor was.
  2185. */
  2186. if (level + 1 < cur->bc_nlevels) {
  2187. error = xfs_btree_dup_cursor(cur, curp);
  2188. if (error)
  2189. goto error0;
  2190. (*curp)->bc_ptrs[level + 1]++;
  2191. }
  2192. *ptrp = rptr;
  2193. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2194. *stat = 1;
  2195. return 0;
  2196. out0:
  2197. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2198. *stat = 0;
  2199. return 0;
  2200. error0:
  2201. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2202. return error;
  2203. }
  2204. /*
  2205. * Copy the old inode root contents into a real block and make the
  2206. * broot point to it.
  2207. */
  2208. int /* error */
  2209. xfs_btree_new_iroot(
  2210. struct xfs_btree_cur *cur, /* btree cursor */
  2211. int *logflags, /* logging flags for inode */
  2212. int *stat) /* return status - 0 fail */
  2213. {
  2214. struct xfs_buf *cbp; /* buffer for cblock */
  2215. struct xfs_btree_block *block; /* btree block */
  2216. struct xfs_btree_block *cblock; /* child btree block */
  2217. union xfs_btree_key *ckp; /* child key pointer */
  2218. union xfs_btree_ptr *cpp; /* child ptr pointer */
  2219. union xfs_btree_key *kp; /* pointer to btree key */
  2220. union xfs_btree_ptr *pp; /* pointer to block addr */
  2221. union xfs_btree_ptr nptr; /* new block addr */
  2222. int level; /* btree level */
  2223. int error; /* error return code */
  2224. #ifdef DEBUG
  2225. int i; /* loop counter */
  2226. #endif
  2227. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2228. XFS_BTREE_STATS_INC(cur, newroot);
  2229. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  2230. level = cur->bc_nlevels - 1;
  2231. block = xfs_btree_get_iroot(cur);
  2232. pp = xfs_btree_ptr_addr(cur, 1, block);
  2233. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2234. error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
  2235. if (error)
  2236. goto error0;
  2237. if (*stat == 0) {
  2238. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2239. return 0;
  2240. }
  2241. XFS_BTREE_STATS_INC(cur, alloc);
  2242. /* Copy the root into a real block. */
  2243. error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
  2244. if (error)
  2245. goto error0;
  2246. /*
  2247. * we can't just memcpy() the root in for CRC enabled btree blocks.
  2248. * In that case have to also ensure the blkno remains correct
  2249. */
  2250. memcpy(cblock, block, xfs_btree_block_len(cur));
  2251. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
  2252. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  2253. cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
  2254. else
  2255. cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
  2256. }
  2257. be16_add_cpu(&block->bb_level, 1);
  2258. xfs_btree_set_numrecs(block, 1);
  2259. cur->bc_nlevels++;
  2260. cur->bc_ptrs[level + 1] = 1;
  2261. kp = xfs_btree_key_addr(cur, 1, block);
  2262. ckp = xfs_btree_key_addr(cur, 1, cblock);
  2263. xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
  2264. cpp = xfs_btree_ptr_addr(cur, 1, cblock);
  2265. #ifdef DEBUG
  2266. for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
  2267. error = xfs_btree_check_ptr(cur, pp, i, level);
  2268. if (error)
  2269. goto error0;
  2270. }
  2271. #endif
  2272. xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
  2273. #ifdef DEBUG
  2274. error = xfs_btree_check_ptr(cur, &nptr, 0, level);
  2275. if (error)
  2276. goto error0;
  2277. #endif
  2278. xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
  2279. xfs_iroot_realloc(cur->bc_private.b.ip,
  2280. 1 - xfs_btree_get_numrecs(cblock),
  2281. cur->bc_private.b.whichfork);
  2282. xfs_btree_setbuf(cur, level, cbp);
  2283. /*
  2284. * Do all this logging at the end so that
  2285. * the root is at the right level.
  2286. */
  2287. xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
  2288. xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
  2289. xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
  2290. *logflags |=
  2291. XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
  2292. *stat = 1;
  2293. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2294. return 0;
  2295. error0:
  2296. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2297. return error;
  2298. }
  2299. /*
  2300. * Allocate a new root block, fill it in.
  2301. */
  2302. STATIC int /* error */
  2303. xfs_btree_new_root(
  2304. struct xfs_btree_cur *cur, /* btree cursor */
  2305. int *stat) /* success/failure */
  2306. {
  2307. struct xfs_btree_block *block; /* one half of the old root block */
  2308. struct xfs_buf *bp; /* buffer containing block */
  2309. int error; /* error return value */
  2310. struct xfs_buf *lbp; /* left buffer pointer */
  2311. struct xfs_btree_block *left; /* left btree block */
  2312. struct xfs_buf *nbp; /* new (root) buffer */
  2313. struct xfs_btree_block *new; /* new (root) btree block */
  2314. int nptr; /* new value for key index, 1 or 2 */
  2315. struct xfs_buf *rbp; /* right buffer pointer */
  2316. struct xfs_btree_block *right; /* right btree block */
  2317. union xfs_btree_ptr rptr;
  2318. union xfs_btree_ptr lptr;
  2319. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2320. XFS_BTREE_STATS_INC(cur, newroot);
  2321. /* initialise our start point from the cursor */
  2322. cur->bc_ops->init_ptr_from_cur(cur, &rptr);
  2323. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2324. error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
  2325. if (error)
  2326. goto error0;
  2327. if (*stat == 0)
  2328. goto out0;
  2329. XFS_BTREE_STATS_INC(cur, alloc);
  2330. /* Set up the new block. */
  2331. error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
  2332. if (error)
  2333. goto error0;
  2334. /* Set the root in the holding structure increasing the level by 1. */
  2335. cur->bc_ops->set_root(cur, &lptr, 1);
  2336. /*
  2337. * At the previous root level there are now two blocks: the old root,
  2338. * and the new block generated when it was split. We don't know which
  2339. * one the cursor is pointing at, so we set up variables "left" and
  2340. * "right" for each case.
  2341. */
  2342. block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
  2343. #ifdef DEBUG
  2344. error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
  2345. if (error)
  2346. goto error0;
  2347. #endif
  2348. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  2349. if (!xfs_btree_ptr_is_null(cur, &rptr)) {
  2350. /* Our block is left, pick up the right block. */
  2351. lbp = bp;
  2352. xfs_btree_buf_to_ptr(cur, lbp, &lptr);
  2353. left = block;
  2354. error = xfs_btree_read_buf_block(cur, &rptr,
  2355. cur->bc_nlevels - 1, 0, &right, &rbp);
  2356. if (error)
  2357. goto error0;
  2358. bp = rbp;
  2359. nptr = 1;
  2360. } else {
  2361. /* Our block is right, pick up the left block. */
  2362. rbp = bp;
  2363. xfs_btree_buf_to_ptr(cur, rbp, &rptr);
  2364. right = block;
  2365. xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  2366. error = xfs_btree_read_buf_block(cur, &lptr,
  2367. cur->bc_nlevels - 1, 0, &left, &lbp);
  2368. if (error)
  2369. goto error0;
  2370. bp = lbp;
  2371. nptr = 2;
  2372. }
  2373. /* Fill in the new block's btree header and log it. */
  2374. xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
  2375. xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
  2376. ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
  2377. !xfs_btree_ptr_is_null(cur, &rptr));
  2378. /* Fill in the key data in the new root. */
  2379. if (xfs_btree_get_level(left) > 0) {
  2380. xfs_btree_copy_keys(cur,
  2381. xfs_btree_key_addr(cur, 1, new),
  2382. xfs_btree_key_addr(cur, 1, left), 1);
  2383. xfs_btree_copy_keys(cur,
  2384. xfs_btree_key_addr(cur, 2, new),
  2385. xfs_btree_key_addr(cur, 1, right), 1);
  2386. } else {
  2387. cur->bc_ops->init_key_from_rec(
  2388. xfs_btree_key_addr(cur, 1, new),
  2389. xfs_btree_rec_addr(cur, 1, left));
  2390. cur->bc_ops->init_key_from_rec(
  2391. xfs_btree_key_addr(cur, 2, new),
  2392. xfs_btree_rec_addr(cur, 1, right));
  2393. }
  2394. xfs_btree_log_keys(cur, nbp, 1, 2);
  2395. /* Fill in the pointer data in the new root. */
  2396. xfs_btree_copy_ptrs(cur,
  2397. xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
  2398. xfs_btree_copy_ptrs(cur,
  2399. xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
  2400. xfs_btree_log_ptrs(cur, nbp, 1, 2);
  2401. /* Fix up the cursor. */
  2402. xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
  2403. cur->bc_ptrs[cur->bc_nlevels] = nptr;
  2404. cur->bc_nlevels++;
  2405. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2406. *stat = 1;
  2407. return 0;
  2408. error0:
  2409. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2410. return error;
  2411. out0:
  2412. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2413. *stat = 0;
  2414. return 0;
  2415. }
  2416. STATIC int
  2417. xfs_btree_make_block_unfull(
  2418. struct xfs_btree_cur *cur, /* btree cursor */
  2419. int level, /* btree level */
  2420. int numrecs,/* # of recs in block */
  2421. int *oindex,/* old tree index */
  2422. int *index, /* new tree index */
  2423. union xfs_btree_ptr *nptr, /* new btree ptr */
  2424. struct xfs_btree_cur **ncur, /* new btree cursor */
  2425. union xfs_btree_rec *nrec, /* new record */
  2426. int *stat)
  2427. {
  2428. union xfs_btree_key key; /* new btree key value */
  2429. int error = 0;
  2430. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2431. level == cur->bc_nlevels - 1) {
  2432. struct xfs_inode *ip = cur->bc_private.b.ip;
  2433. if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
  2434. /* A root block that can be made bigger. */
  2435. xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
  2436. } else {
  2437. /* A root block that needs replacing */
  2438. int logflags = 0;
  2439. error = xfs_btree_new_iroot(cur, &logflags, stat);
  2440. if (error || *stat == 0)
  2441. return error;
  2442. xfs_trans_log_inode(cur->bc_tp, ip, logflags);
  2443. }
  2444. return 0;
  2445. }
  2446. /* First, try shifting an entry to the right neighbor. */
  2447. error = xfs_btree_rshift(cur, level, stat);
  2448. if (error || *stat)
  2449. return error;
  2450. /* Next, try shifting an entry to the left neighbor. */
  2451. error = xfs_btree_lshift(cur, level, stat);
  2452. if (error)
  2453. return error;
  2454. if (*stat) {
  2455. *oindex = *index = cur->bc_ptrs[level];
  2456. return 0;
  2457. }
  2458. /*
  2459. * Next, try splitting the current block in half.
  2460. *
  2461. * If this works we have to re-set our variables because we
  2462. * could be in a different block now.
  2463. */
  2464. error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
  2465. if (error || *stat == 0)
  2466. return error;
  2467. *index = cur->bc_ptrs[level];
  2468. cur->bc_ops->init_rec_from_key(&key, nrec);
  2469. return 0;
  2470. }
  2471. /*
  2472. * Insert one record/level. Return information to the caller
  2473. * allowing the next level up to proceed if necessary.
  2474. */
  2475. STATIC int
  2476. xfs_btree_insrec(
  2477. struct xfs_btree_cur *cur, /* btree cursor */
  2478. int level, /* level to insert record at */
  2479. union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
  2480. union xfs_btree_rec *recp, /* i/o: record data inserted */
  2481. struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
  2482. int *stat) /* success/failure */
  2483. {
  2484. struct xfs_btree_block *block; /* btree block */
  2485. struct xfs_buf *bp; /* buffer for block */
  2486. union xfs_btree_key key; /* btree key */
  2487. union xfs_btree_ptr nptr; /* new block ptr */
  2488. struct xfs_btree_cur *ncur; /* new btree cursor */
  2489. union xfs_btree_rec nrec; /* new record count */
  2490. int optr; /* old key/record index */
  2491. int ptr; /* key/record index */
  2492. int numrecs;/* number of records */
  2493. int error; /* error return value */
  2494. #ifdef DEBUG
  2495. int i;
  2496. #endif
  2497. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2498. XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
  2499. ncur = NULL;
  2500. /*
  2501. * If we have an external root pointer, and we've made it to the
  2502. * root level, allocate a new root block and we're done.
  2503. */
  2504. if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2505. (level >= cur->bc_nlevels)) {
  2506. error = xfs_btree_new_root(cur, stat);
  2507. xfs_btree_set_ptr_null(cur, ptrp);
  2508. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2509. return error;
  2510. }
  2511. /* If we're off the left edge, return failure. */
  2512. ptr = cur->bc_ptrs[level];
  2513. if (ptr == 0) {
  2514. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2515. *stat = 0;
  2516. return 0;
  2517. }
  2518. /* Make a key out of the record data to be inserted, and save it. */
  2519. cur->bc_ops->init_key_from_rec(&key, recp);
  2520. optr = ptr;
  2521. XFS_BTREE_STATS_INC(cur, insrec);
  2522. /* Get pointers to the btree buffer and block. */
  2523. block = xfs_btree_get_block(cur, level, &bp);
  2524. numrecs = xfs_btree_get_numrecs(block);
  2525. #ifdef DEBUG
  2526. error = xfs_btree_check_block(cur, block, level, bp);
  2527. if (error)
  2528. goto error0;
  2529. /* Check that the new entry is being inserted in the right place. */
  2530. if (ptr <= numrecs) {
  2531. if (level == 0) {
  2532. ASSERT(cur->bc_ops->recs_inorder(cur, recp,
  2533. xfs_btree_rec_addr(cur, ptr, block)));
  2534. } else {
  2535. ASSERT(cur->bc_ops->keys_inorder(cur, &key,
  2536. xfs_btree_key_addr(cur, ptr, block)));
  2537. }
  2538. }
  2539. #endif
  2540. /*
  2541. * If the block is full, we can't insert the new entry until we
  2542. * make the block un-full.
  2543. */
  2544. xfs_btree_set_ptr_null(cur, &nptr);
  2545. if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
  2546. error = xfs_btree_make_block_unfull(cur, level, numrecs,
  2547. &optr, &ptr, &nptr, &ncur, &nrec, stat);
  2548. if (error || *stat == 0)
  2549. goto error0;
  2550. }
  2551. /*
  2552. * The current block may have changed if the block was
  2553. * previously full and we have just made space in it.
  2554. */
  2555. block = xfs_btree_get_block(cur, level, &bp);
  2556. numrecs = xfs_btree_get_numrecs(block);
  2557. #ifdef DEBUG
  2558. error = xfs_btree_check_block(cur, block, level, bp);
  2559. if (error)
  2560. return error;
  2561. #endif
  2562. /*
  2563. * At this point we know there's room for our new entry in the block
  2564. * we're pointing at.
  2565. */
  2566. XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
  2567. if (level > 0) {
  2568. /* It's a nonleaf. make a hole in the keys and ptrs */
  2569. union xfs_btree_key *kp;
  2570. union xfs_btree_ptr *pp;
  2571. kp = xfs_btree_key_addr(cur, ptr, block);
  2572. pp = xfs_btree_ptr_addr(cur, ptr, block);
  2573. #ifdef DEBUG
  2574. for (i = numrecs - ptr; i >= 0; i--) {
  2575. error = xfs_btree_check_ptr(cur, pp, i, level);
  2576. if (error)
  2577. return error;
  2578. }
  2579. #endif
  2580. xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
  2581. xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
  2582. #ifdef DEBUG
  2583. error = xfs_btree_check_ptr(cur, ptrp, 0, level);
  2584. if (error)
  2585. goto error0;
  2586. #endif
  2587. /* Now put the new data in, bump numrecs and log it. */
  2588. xfs_btree_copy_keys(cur, kp, &key, 1);
  2589. xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
  2590. numrecs++;
  2591. xfs_btree_set_numrecs(block, numrecs);
  2592. xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
  2593. xfs_btree_log_keys(cur, bp, ptr, numrecs);
  2594. #ifdef DEBUG
  2595. if (ptr < numrecs) {
  2596. ASSERT(cur->bc_ops->keys_inorder(cur, kp,
  2597. xfs_btree_key_addr(cur, ptr + 1, block)));
  2598. }
  2599. #endif
  2600. } else {
  2601. /* It's a leaf. make a hole in the records */
  2602. union xfs_btree_rec *rp;
  2603. rp = xfs_btree_rec_addr(cur, ptr, block);
  2604. xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
  2605. /* Now put the new data in, bump numrecs and log it. */
  2606. xfs_btree_copy_recs(cur, rp, recp, 1);
  2607. xfs_btree_set_numrecs(block, ++numrecs);
  2608. xfs_btree_log_recs(cur, bp, ptr, numrecs);
  2609. #ifdef DEBUG
  2610. if (ptr < numrecs) {
  2611. ASSERT(cur->bc_ops->recs_inorder(cur, rp,
  2612. xfs_btree_rec_addr(cur, ptr + 1, block)));
  2613. }
  2614. #endif
  2615. }
  2616. /* Log the new number of records in the btree header. */
  2617. xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
  2618. /* If we inserted at the start of a block, update the parents' keys. */
  2619. if (optr == 1) {
  2620. error = xfs_btree_updkey(cur, &key, level + 1);
  2621. if (error)
  2622. goto error0;
  2623. }
  2624. /*
  2625. * If we are tracking the last record in the tree and
  2626. * we are at the far right edge of the tree, update it.
  2627. */
  2628. if (xfs_btree_is_lastrec(cur, block, level)) {
  2629. cur->bc_ops->update_lastrec(cur, block, recp,
  2630. ptr, LASTREC_INSREC);
  2631. }
  2632. /*
  2633. * Return the new block number, if any.
  2634. * If there is one, give back a record value and a cursor too.
  2635. */
  2636. *ptrp = nptr;
  2637. if (!xfs_btree_ptr_is_null(cur, &nptr)) {
  2638. *recp = nrec;
  2639. *curp = ncur;
  2640. }
  2641. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2642. *stat = 1;
  2643. return 0;
  2644. error0:
  2645. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2646. return error;
  2647. }
  2648. /*
  2649. * Insert the record at the point referenced by cur.
  2650. *
  2651. * A multi-level split of the tree on insert will invalidate the original
  2652. * cursor. All callers of this function should assume that the cursor is
  2653. * no longer valid and revalidate it.
  2654. */
  2655. int
  2656. xfs_btree_insert(
  2657. struct xfs_btree_cur *cur,
  2658. int *stat)
  2659. {
  2660. int error; /* error return value */
  2661. int i; /* result value, 0 for failure */
  2662. int level; /* current level number in btree */
  2663. union xfs_btree_ptr nptr; /* new block number (split result) */
  2664. struct xfs_btree_cur *ncur; /* new cursor (split result) */
  2665. struct xfs_btree_cur *pcur; /* previous level's cursor */
  2666. union xfs_btree_rec rec; /* record to insert */
  2667. level = 0;
  2668. ncur = NULL;
  2669. pcur = cur;
  2670. xfs_btree_set_ptr_null(cur, &nptr);
  2671. cur->bc_ops->init_rec_from_cur(cur, &rec);
  2672. /*
  2673. * Loop going up the tree, starting at the leaf level.
  2674. * Stop when we don't get a split block, that must mean that
  2675. * the insert is finished with this level.
  2676. */
  2677. do {
  2678. /*
  2679. * Insert nrec/nptr into this level of the tree.
  2680. * Note if we fail, nptr will be null.
  2681. */
  2682. error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
  2683. if (error) {
  2684. if (pcur != cur)
  2685. xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
  2686. goto error0;
  2687. }
  2688. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2689. level++;
  2690. /*
  2691. * See if the cursor we just used is trash.
  2692. * Can't trash the caller's cursor, but otherwise we should
  2693. * if ncur is a new cursor or we're about to be done.
  2694. */
  2695. if (pcur != cur &&
  2696. (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
  2697. /* Save the state from the cursor before we trash it */
  2698. if (cur->bc_ops->update_cursor)
  2699. cur->bc_ops->update_cursor(pcur, cur);
  2700. cur->bc_nlevels = pcur->bc_nlevels;
  2701. xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
  2702. }
  2703. /* If we got a new cursor, switch to it. */
  2704. if (ncur) {
  2705. pcur = ncur;
  2706. ncur = NULL;
  2707. }
  2708. } while (!xfs_btree_ptr_is_null(cur, &nptr));
  2709. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2710. *stat = i;
  2711. return 0;
  2712. error0:
  2713. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2714. return error;
  2715. }
  2716. /*
  2717. * Try to merge a non-leaf block back into the inode root.
  2718. *
  2719. * Note: the killroot names comes from the fact that we're effectively
  2720. * killing the old root block. But because we can't just delete the
  2721. * inode we have to copy the single block it was pointing to into the
  2722. * inode.
  2723. */
  2724. STATIC int
  2725. xfs_btree_kill_iroot(
  2726. struct xfs_btree_cur *cur)
  2727. {
  2728. int whichfork = cur->bc_private.b.whichfork;
  2729. struct xfs_inode *ip = cur->bc_private.b.ip;
  2730. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  2731. struct xfs_btree_block *block;
  2732. struct xfs_btree_block *cblock;
  2733. union xfs_btree_key *kp;
  2734. union xfs_btree_key *ckp;
  2735. union xfs_btree_ptr *pp;
  2736. union xfs_btree_ptr *cpp;
  2737. struct xfs_buf *cbp;
  2738. int level;
  2739. int index;
  2740. int numrecs;
  2741. #ifdef DEBUG
  2742. union xfs_btree_ptr ptr;
  2743. int i;
  2744. #endif
  2745. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2746. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  2747. ASSERT(cur->bc_nlevels > 1);
  2748. /*
  2749. * Don't deal with the root block needs to be a leaf case.
  2750. * We're just going to turn the thing back into extents anyway.
  2751. */
  2752. level = cur->bc_nlevels - 1;
  2753. if (level == 1)
  2754. goto out0;
  2755. /*
  2756. * Give up if the root has multiple children.
  2757. */
  2758. block = xfs_btree_get_iroot(cur);
  2759. if (xfs_btree_get_numrecs(block) != 1)
  2760. goto out0;
  2761. cblock = xfs_btree_get_block(cur, level - 1, &cbp);
  2762. numrecs = xfs_btree_get_numrecs(cblock);
  2763. /*
  2764. * Only do this if the next level will fit.
  2765. * Then the data must be copied up to the inode,
  2766. * instead of freeing the root you free the next level.
  2767. */
  2768. if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
  2769. goto out0;
  2770. XFS_BTREE_STATS_INC(cur, killroot);
  2771. #ifdef DEBUG
  2772. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
  2773. ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
  2774. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  2775. ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
  2776. #endif
  2777. index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
  2778. if (index) {
  2779. xfs_iroot_realloc(cur->bc_private.b.ip, index,
  2780. cur->bc_private.b.whichfork);
  2781. block = ifp->if_broot;
  2782. }
  2783. be16_add_cpu(&block->bb_numrecs, index);
  2784. ASSERT(block->bb_numrecs == cblock->bb_numrecs);
  2785. kp = xfs_btree_key_addr(cur, 1, block);
  2786. ckp = xfs_btree_key_addr(cur, 1, cblock);
  2787. xfs_btree_copy_keys(cur, kp, ckp, numrecs);
  2788. pp = xfs_btree_ptr_addr(cur, 1, block);
  2789. cpp = xfs_btree_ptr_addr(cur, 1, cblock);
  2790. #ifdef DEBUG
  2791. for (i = 0; i < numrecs; i++) {
  2792. int error;
  2793. error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
  2794. if (error) {
  2795. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2796. return error;
  2797. }
  2798. }
  2799. #endif
  2800. xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
  2801. cur->bc_ops->free_block(cur, cbp);
  2802. XFS_BTREE_STATS_INC(cur, free);
  2803. cur->bc_bufs[level - 1] = NULL;
  2804. be16_add_cpu(&block->bb_level, -1);
  2805. xfs_trans_log_inode(cur->bc_tp, ip,
  2806. XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  2807. cur->bc_nlevels--;
  2808. out0:
  2809. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2810. return 0;
  2811. }
  2812. /*
  2813. * Kill the current root node, and replace it with it's only child node.
  2814. */
  2815. STATIC int
  2816. xfs_btree_kill_root(
  2817. struct xfs_btree_cur *cur,
  2818. struct xfs_buf *bp,
  2819. int level,
  2820. union xfs_btree_ptr *newroot)
  2821. {
  2822. int error;
  2823. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2824. XFS_BTREE_STATS_INC(cur, killroot);
  2825. /*
  2826. * Update the root pointer, decreasing the level by 1 and then
  2827. * free the old root.
  2828. */
  2829. cur->bc_ops->set_root(cur, newroot, -1);
  2830. error = cur->bc_ops->free_block(cur, bp);
  2831. if (error) {
  2832. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2833. return error;
  2834. }
  2835. XFS_BTREE_STATS_INC(cur, free);
  2836. cur->bc_bufs[level] = NULL;
  2837. cur->bc_ra[level] = 0;
  2838. cur->bc_nlevels--;
  2839. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2840. return 0;
  2841. }
  2842. STATIC int
  2843. xfs_btree_dec_cursor(
  2844. struct xfs_btree_cur *cur,
  2845. int level,
  2846. int *stat)
  2847. {
  2848. int error;
  2849. int i;
  2850. if (level > 0) {
  2851. error = xfs_btree_decrement(cur, level, &i);
  2852. if (error)
  2853. return error;
  2854. }
  2855. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2856. *stat = 1;
  2857. return 0;
  2858. }
  2859. /*
  2860. * Single level of the btree record deletion routine.
  2861. * Delete record pointed to by cur/level.
  2862. * Remove the record from its block then rebalance the tree.
  2863. * Return 0 for error, 1 for done, 2 to go on to the next level.
  2864. */
  2865. STATIC int /* error */
  2866. xfs_btree_delrec(
  2867. struct xfs_btree_cur *cur, /* btree cursor */
  2868. int level, /* level removing record from */
  2869. int *stat) /* fail/done/go-on */
  2870. {
  2871. struct xfs_btree_block *block; /* btree block */
  2872. union xfs_btree_ptr cptr; /* current block ptr */
  2873. struct xfs_buf *bp; /* buffer for block */
  2874. int error; /* error return value */
  2875. int i; /* loop counter */
  2876. union xfs_btree_key key; /* storage for keyp */
  2877. union xfs_btree_key *keyp = &key; /* passed to the next level */
  2878. union xfs_btree_ptr lptr; /* left sibling block ptr */
  2879. struct xfs_buf *lbp; /* left buffer pointer */
  2880. struct xfs_btree_block *left; /* left btree block */
  2881. int lrecs = 0; /* left record count */
  2882. int ptr; /* key/record index */
  2883. union xfs_btree_ptr rptr; /* right sibling block ptr */
  2884. struct xfs_buf *rbp; /* right buffer pointer */
  2885. struct xfs_btree_block *right; /* right btree block */
  2886. struct xfs_btree_block *rrblock; /* right-right btree block */
  2887. struct xfs_buf *rrbp; /* right-right buffer pointer */
  2888. int rrecs = 0; /* right record count */
  2889. struct xfs_btree_cur *tcur; /* temporary btree cursor */
  2890. int numrecs; /* temporary numrec count */
  2891. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2892. XFS_BTREE_TRACE_ARGI(cur, level);
  2893. tcur = NULL;
  2894. /* Get the index of the entry being deleted, check for nothing there. */
  2895. ptr = cur->bc_ptrs[level];
  2896. if (ptr == 0) {
  2897. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2898. *stat = 0;
  2899. return 0;
  2900. }
  2901. /* Get the buffer & block containing the record or key/ptr. */
  2902. block = xfs_btree_get_block(cur, level, &bp);
  2903. numrecs = xfs_btree_get_numrecs(block);
  2904. #ifdef DEBUG
  2905. error = xfs_btree_check_block(cur, block, level, bp);
  2906. if (error)
  2907. goto error0;
  2908. #endif
  2909. /* Fail if we're off the end of the block. */
  2910. if (ptr > numrecs) {
  2911. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2912. *stat = 0;
  2913. return 0;
  2914. }
  2915. XFS_BTREE_STATS_INC(cur, delrec);
  2916. XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
  2917. /* Excise the entries being deleted. */
  2918. if (level > 0) {
  2919. /* It's a nonleaf. operate on keys and ptrs */
  2920. union xfs_btree_key *lkp;
  2921. union xfs_btree_ptr *lpp;
  2922. lkp = xfs_btree_key_addr(cur, ptr + 1, block);
  2923. lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
  2924. #ifdef DEBUG
  2925. for (i = 0; i < numrecs - ptr; i++) {
  2926. error = xfs_btree_check_ptr(cur, lpp, i, level);
  2927. if (error)
  2928. goto error0;
  2929. }
  2930. #endif
  2931. if (ptr < numrecs) {
  2932. xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
  2933. xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
  2934. xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
  2935. xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
  2936. }
  2937. /*
  2938. * If it's the first record in the block, we'll need to pass a
  2939. * key up to the next level (updkey).
  2940. */
  2941. if (ptr == 1)
  2942. keyp = xfs_btree_key_addr(cur, 1, block);
  2943. } else {
  2944. /* It's a leaf. operate on records */
  2945. if (ptr < numrecs) {
  2946. xfs_btree_shift_recs(cur,
  2947. xfs_btree_rec_addr(cur, ptr + 1, block),
  2948. -1, numrecs - ptr);
  2949. xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
  2950. }
  2951. /*
  2952. * If it's the first record in the block, we'll need a key
  2953. * structure to pass up to the next level (updkey).
  2954. */
  2955. if (ptr == 1) {
  2956. cur->bc_ops->init_key_from_rec(&key,
  2957. xfs_btree_rec_addr(cur, 1, block));
  2958. keyp = &key;
  2959. }
  2960. }
  2961. /*
  2962. * Decrement and log the number of entries in the block.
  2963. */
  2964. xfs_btree_set_numrecs(block, --numrecs);
  2965. xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
  2966. /*
  2967. * If we are tracking the last record in the tree and
  2968. * we are at the far right edge of the tree, update it.
  2969. */
  2970. if (xfs_btree_is_lastrec(cur, block, level)) {
  2971. cur->bc_ops->update_lastrec(cur, block, NULL,
  2972. ptr, LASTREC_DELREC);
  2973. }
  2974. /*
  2975. * We're at the root level. First, shrink the root block in-memory.
  2976. * Try to get rid of the next level down. If we can't then there's
  2977. * nothing left to do.
  2978. */
  2979. if (level == cur->bc_nlevels - 1) {
  2980. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
  2981. xfs_iroot_realloc(cur->bc_private.b.ip, -1,
  2982. cur->bc_private.b.whichfork);
  2983. error = xfs_btree_kill_iroot(cur);
  2984. if (error)
  2985. goto error0;
  2986. error = xfs_btree_dec_cursor(cur, level, stat);
  2987. if (error)
  2988. goto error0;
  2989. *stat = 1;
  2990. return 0;
  2991. }
  2992. /*
  2993. * If this is the root level, and there's only one entry left,
  2994. * and it's NOT the leaf level, then we can get rid of this
  2995. * level.
  2996. */
  2997. if (numrecs == 1 && level > 0) {
  2998. union xfs_btree_ptr *pp;
  2999. /*
  3000. * pp is still set to the first pointer in the block.
  3001. * Make it the new root of the btree.
  3002. */
  3003. pp = xfs_btree_ptr_addr(cur, 1, block);
  3004. error = xfs_btree_kill_root(cur, bp, level, pp);
  3005. if (error)
  3006. goto error0;
  3007. } else if (level > 0) {
  3008. error = xfs_btree_dec_cursor(cur, level, stat);
  3009. if (error)
  3010. goto error0;
  3011. }
  3012. *stat = 1;
  3013. return 0;
  3014. }
  3015. /*
  3016. * If we deleted the leftmost entry in the block, update the
  3017. * key values above us in the tree.
  3018. */
  3019. if (ptr == 1) {
  3020. error = xfs_btree_updkey(cur, keyp, level + 1);
  3021. if (error)
  3022. goto error0;
  3023. }
  3024. /*
  3025. * If the number of records remaining in the block is at least
  3026. * the minimum, we're done.
  3027. */
  3028. if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
  3029. error = xfs_btree_dec_cursor(cur, level, stat);
  3030. if (error)
  3031. goto error0;
  3032. return 0;
  3033. }
  3034. /*
  3035. * Otherwise, we have to move some records around to keep the
  3036. * tree balanced. Look at the left and right sibling blocks to
  3037. * see if we can re-balance by moving only one record.
  3038. */
  3039. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  3040. xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
  3041. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
  3042. /*
  3043. * One child of root, need to get a chance to copy its contents
  3044. * into the root and delete it. Can't go up to next level,
  3045. * there's nothing to delete there.
  3046. */
  3047. if (xfs_btree_ptr_is_null(cur, &rptr) &&
  3048. xfs_btree_ptr_is_null(cur, &lptr) &&
  3049. level == cur->bc_nlevels - 2) {
  3050. error = xfs_btree_kill_iroot(cur);
  3051. if (!error)
  3052. error = xfs_btree_dec_cursor(cur, level, stat);
  3053. if (error)
  3054. goto error0;
  3055. return 0;
  3056. }
  3057. }
  3058. ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
  3059. !xfs_btree_ptr_is_null(cur, &lptr));
  3060. /*
  3061. * Duplicate the cursor so our btree manipulations here won't
  3062. * disrupt the next level up.
  3063. */
  3064. error = xfs_btree_dup_cursor(cur, &tcur);
  3065. if (error)
  3066. goto error0;
  3067. /*
  3068. * If there's a right sibling, see if it's ok to shift an entry
  3069. * out of it.
  3070. */
  3071. if (!xfs_btree_ptr_is_null(cur, &rptr)) {
  3072. /*
  3073. * Move the temp cursor to the last entry in the next block.
  3074. * Actually any entry but the first would suffice.
  3075. */
  3076. i = xfs_btree_lastrec(tcur, level);
  3077. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3078. error = xfs_btree_increment(tcur, level, &i);
  3079. if (error)
  3080. goto error0;
  3081. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3082. i = xfs_btree_lastrec(tcur, level);
  3083. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3084. /* Grab a pointer to the block. */
  3085. right = xfs_btree_get_block(tcur, level, &rbp);
  3086. #ifdef DEBUG
  3087. error = xfs_btree_check_block(tcur, right, level, rbp);
  3088. if (error)
  3089. goto error0;
  3090. #endif
  3091. /* Grab the current block number, for future use. */
  3092. xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
  3093. /*
  3094. * If right block is full enough so that removing one entry
  3095. * won't make it too empty, and left-shifting an entry out
  3096. * of right to us works, we're done.
  3097. */
  3098. if (xfs_btree_get_numrecs(right) - 1 >=
  3099. cur->bc_ops->get_minrecs(tcur, level)) {
  3100. error = xfs_btree_lshift(tcur, level, &i);
  3101. if (error)
  3102. goto error0;
  3103. if (i) {
  3104. ASSERT(xfs_btree_get_numrecs(block) >=
  3105. cur->bc_ops->get_minrecs(tcur, level));
  3106. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  3107. tcur = NULL;
  3108. error = xfs_btree_dec_cursor(cur, level, stat);
  3109. if (error)
  3110. goto error0;
  3111. return 0;
  3112. }
  3113. }
  3114. /*
  3115. * Otherwise, grab the number of records in right for
  3116. * future reference, and fix up the temp cursor to point
  3117. * to our block again (last record).
  3118. */
  3119. rrecs = xfs_btree_get_numrecs(right);
  3120. if (!xfs_btree_ptr_is_null(cur, &lptr)) {
  3121. i = xfs_btree_firstrec(tcur, level);
  3122. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3123. error = xfs_btree_decrement(tcur, level, &i);
  3124. if (error)
  3125. goto error0;
  3126. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3127. }
  3128. }
  3129. /*
  3130. * If there's a left sibling, see if it's ok to shift an entry
  3131. * out of it.
  3132. */
  3133. if (!xfs_btree_ptr_is_null(cur, &lptr)) {
  3134. /*
  3135. * Move the temp cursor to the first entry in the
  3136. * previous block.
  3137. */
  3138. i = xfs_btree_firstrec(tcur, level);
  3139. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3140. error = xfs_btree_decrement(tcur, level, &i);
  3141. if (error)
  3142. goto error0;
  3143. i = xfs_btree_firstrec(tcur, level);
  3144. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3145. /* Grab a pointer to the block. */
  3146. left = xfs_btree_get_block(tcur, level, &lbp);
  3147. #ifdef DEBUG
  3148. error = xfs_btree_check_block(cur, left, level, lbp);
  3149. if (error)
  3150. goto error0;
  3151. #endif
  3152. /* Grab the current block number, for future use. */
  3153. xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
  3154. /*
  3155. * If left block is full enough so that removing one entry
  3156. * won't make it too empty, and right-shifting an entry out
  3157. * of left to us works, we're done.
  3158. */
  3159. if (xfs_btree_get_numrecs(left) - 1 >=
  3160. cur->bc_ops->get_minrecs(tcur, level)) {
  3161. error = xfs_btree_rshift(tcur, level, &i);
  3162. if (error)
  3163. goto error0;
  3164. if (i) {
  3165. ASSERT(xfs_btree_get_numrecs(block) >=
  3166. cur->bc_ops->get_minrecs(tcur, level));
  3167. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  3168. tcur = NULL;
  3169. if (level == 0)
  3170. cur->bc_ptrs[0]++;
  3171. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3172. *stat = 1;
  3173. return 0;
  3174. }
  3175. }
  3176. /*
  3177. * Otherwise, grab the number of records in right for
  3178. * future reference.
  3179. */
  3180. lrecs = xfs_btree_get_numrecs(left);
  3181. }
  3182. /* Delete the temp cursor, we're done with it. */
  3183. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  3184. tcur = NULL;
  3185. /* If here, we need to do a join to keep the tree balanced. */
  3186. ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
  3187. if (!xfs_btree_ptr_is_null(cur, &lptr) &&
  3188. lrecs + xfs_btree_get_numrecs(block) <=
  3189. cur->bc_ops->get_maxrecs(cur, level)) {
  3190. /*
  3191. * Set "right" to be the starting block,
  3192. * "left" to be the left neighbor.
  3193. */
  3194. rptr = cptr;
  3195. right = block;
  3196. rbp = bp;
  3197. error = xfs_btree_read_buf_block(cur, &lptr, level,
  3198. 0, &left, &lbp);
  3199. if (error)
  3200. goto error0;
  3201. /*
  3202. * If that won't work, see if we can join with the right neighbor block.
  3203. */
  3204. } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
  3205. rrecs + xfs_btree_get_numrecs(block) <=
  3206. cur->bc_ops->get_maxrecs(cur, level)) {
  3207. /*
  3208. * Set "left" to be the starting block,
  3209. * "right" to be the right neighbor.
  3210. */
  3211. lptr = cptr;
  3212. left = block;
  3213. lbp = bp;
  3214. error = xfs_btree_read_buf_block(cur, &rptr, level,
  3215. 0, &right, &rbp);
  3216. if (error)
  3217. goto error0;
  3218. /*
  3219. * Otherwise, we can't fix the imbalance.
  3220. * Just return. This is probably a logic error, but it's not fatal.
  3221. */
  3222. } else {
  3223. error = xfs_btree_dec_cursor(cur, level, stat);
  3224. if (error)
  3225. goto error0;
  3226. return 0;
  3227. }
  3228. rrecs = xfs_btree_get_numrecs(right);
  3229. lrecs = xfs_btree_get_numrecs(left);
  3230. /*
  3231. * We're now going to join "left" and "right" by moving all the stuff
  3232. * in "right" to "left" and deleting "right".
  3233. */
  3234. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  3235. if (level > 0) {
  3236. /* It's a non-leaf. Move keys and pointers. */
  3237. union xfs_btree_key *lkp; /* left btree key */
  3238. union xfs_btree_ptr *lpp; /* left address pointer */
  3239. union xfs_btree_key *rkp; /* right btree key */
  3240. union xfs_btree_ptr *rpp; /* right address pointer */
  3241. lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
  3242. lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
  3243. rkp = xfs_btree_key_addr(cur, 1, right);
  3244. rpp = xfs_btree_ptr_addr(cur, 1, right);
  3245. #ifdef DEBUG
  3246. for (i = 1; i < rrecs; i++) {
  3247. error = xfs_btree_check_ptr(cur, rpp, i, level);
  3248. if (error)
  3249. goto error0;
  3250. }
  3251. #endif
  3252. xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
  3253. xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
  3254. xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
  3255. xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
  3256. } else {
  3257. /* It's a leaf. Move records. */
  3258. union xfs_btree_rec *lrp; /* left record pointer */
  3259. union xfs_btree_rec *rrp; /* right record pointer */
  3260. lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
  3261. rrp = xfs_btree_rec_addr(cur, 1, right);
  3262. xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
  3263. xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
  3264. }
  3265. XFS_BTREE_STATS_INC(cur, join);
  3266. /*
  3267. * Fix up the number of records and right block pointer in the
  3268. * surviving block, and log it.
  3269. */
  3270. xfs_btree_set_numrecs(left, lrecs + rrecs);
  3271. xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
  3272. xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
  3273. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  3274. /* If there is a right sibling, point it to the remaining block. */
  3275. xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
  3276. if (!xfs_btree_ptr_is_null(cur, &cptr)) {
  3277. error = xfs_btree_read_buf_block(cur, &cptr, level,
  3278. 0, &rrblock, &rrbp);
  3279. if (error)
  3280. goto error0;
  3281. xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
  3282. xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
  3283. }
  3284. /* Free the deleted block. */
  3285. error = cur->bc_ops->free_block(cur, rbp);
  3286. if (error)
  3287. goto error0;
  3288. XFS_BTREE_STATS_INC(cur, free);
  3289. /*
  3290. * If we joined with the left neighbor, set the buffer in the
  3291. * cursor to the left block, and fix up the index.
  3292. */
  3293. if (bp != lbp) {
  3294. cur->bc_bufs[level] = lbp;
  3295. cur->bc_ptrs[level] += lrecs;
  3296. cur->bc_ra[level] = 0;
  3297. }
  3298. /*
  3299. * If we joined with the right neighbor and there's a level above
  3300. * us, increment the cursor at that level.
  3301. */
  3302. else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
  3303. (level + 1 < cur->bc_nlevels)) {
  3304. error = xfs_btree_increment(cur, level + 1, &i);
  3305. if (error)
  3306. goto error0;
  3307. }
  3308. /*
  3309. * Readjust the ptr at this level if it's not a leaf, since it's
  3310. * still pointing at the deletion point, which makes the cursor
  3311. * inconsistent. If this makes the ptr 0, the caller fixes it up.
  3312. * We can't use decrement because it would change the next level up.
  3313. */
  3314. if (level > 0)
  3315. cur->bc_ptrs[level]--;
  3316. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3317. /* Return value means the next level up has something to do. */
  3318. *stat = 2;
  3319. return 0;
  3320. error0:
  3321. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3322. if (tcur)
  3323. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  3324. return error;
  3325. }
  3326. /*
  3327. * Delete the record pointed to by cur.
  3328. * The cursor refers to the place where the record was (could be inserted)
  3329. * when the operation returns.
  3330. */
  3331. int /* error */
  3332. xfs_btree_delete(
  3333. struct xfs_btree_cur *cur,
  3334. int *stat) /* success/failure */
  3335. {
  3336. int error; /* error return value */
  3337. int level;
  3338. int i;
  3339. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  3340. /*
  3341. * Go up the tree, starting at leaf level.
  3342. *
  3343. * If 2 is returned then a join was done; go to the next level.
  3344. * Otherwise we are done.
  3345. */
  3346. for (level = 0, i = 2; i == 2; level++) {
  3347. error = xfs_btree_delrec(cur, level, &i);
  3348. if (error)
  3349. goto error0;
  3350. }
  3351. if (i == 0) {
  3352. for (level = 1; level < cur->bc_nlevels; level++) {
  3353. if (cur->bc_ptrs[level] == 0) {
  3354. error = xfs_btree_decrement(cur, level, &i);
  3355. if (error)
  3356. goto error0;
  3357. break;
  3358. }
  3359. }
  3360. }
  3361. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3362. *stat = i;
  3363. return 0;
  3364. error0:
  3365. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3366. return error;
  3367. }
  3368. /*
  3369. * Get the data from the pointed-to record.
  3370. */
  3371. int /* error */
  3372. xfs_btree_get_rec(
  3373. struct xfs_btree_cur *cur, /* btree cursor */
  3374. union xfs_btree_rec **recp, /* output: btree record */
  3375. int *stat) /* output: success/failure */
  3376. {
  3377. struct xfs_btree_block *block; /* btree block */
  3378. struct xfs_buf *bp; /* buffer pointer */
  3379. int ptr; /* record number */
  3380. #ifdef DEBUG
  3381. int error; /* error return value */
  3382. #endif
  3383. ptr = cur->bc_ptrs[0];
  3384. block = xfs_btree_get_block(cur, 0, &bp);
  3385. #ifdef DEBUG
  3386. error = xfs_btree_check_block(cur, block, 0, bp);
  3387. if (error)
  3388. return error;
  3389. #endif
  3390. /*
  3391. * Off the right end or left end, return failure.
  3392. */
  3393. if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
  3394. *stat = 0;
  3395. return 0;
  3396. }
  3397. /*
  3398. * Point to the record and extract its data.
  3399. */
  3400. *recp = xfs_btree_rec_addr(cur, ptr, block);
  3401. *stat = 1;
  3402. return 0;
  3403. }
  3404. /*
  3405. * Change the owner of a btree.
  3406. *
  3407. * The mechanism we use here is ordered buffer logging. Because we don't know
  3408. * how many buffers were are going to need to modify, we don't really want to
  3409. * have to make transaction reservations for the worst case of every buffer in a
  3410. * full size btree as that may be more space that we can fit in the log....
  3411. *
  3412. * We do the btree walk in the most optimal manner possible - we have sibling
  3413. * pointers so we can just walk all the blocks on each level from left to right
  3414. * in a single pass, and then move to the next level and do the same. We can
  3415. * also do readahead on the sibling pointers to get IO moving more quickly,
  3416. * though for slow disks this is unlikely to make much difference to performance
  3417. * as the amount of CPU work we have to do before moving to the next block is
  3418. * relatively small.
  3419. *
  3420. * For each btree block that we load, modify the owner appropriately, set the
  3421. * buffer as an ordered buffer and log it appropriately. We need to ensure that
  3422. * we mark the region we change dirty so that if the buffer is relogged in
  3423. * a subsequent transaction the changes we make here as an ordered buffer are
  3424. * correctly relogged in that transaction. If we are in recovery context, then
  3425. * just queue the modified buffer as delayed write buffer so the transaction
  3426. * recovery completion writes the changes to disk.
  3427. */
  3428. static int
  3429. xfs_btree_block_change_owner(
  3430. struct xfs_btree_cur *cur,
  3431. int level,
  3432. __uint64_t new_owner,
  3433. struct list_head *buffer_list)
  3434. {
  3435. struct xfs_btree_block *block;
  3436. struct xfs_buf *bp;
  3437. union xfs_btree_ptr rptr;
  3438. /* do right sibling readahead */
  3439. xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
  3440. /* modify the owner */
  3441. block = xfs_btree_get_block(cur, level, &bp);
  3442. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  3443. block->bb_u.l.bb_owner = cpu_to_be64(new_owner);
  3444. else
  3445. block->bb_u.s.bb_owner = cpu_to_be32(new_owner);
  3446. /*
  3447. * If the block is a root block hosted in an inode, we might not have a
  3448. * buffer pointer here and we shouldn't attempt to log the change as the
  3449. * information is already held in the inode and discarded when the root
  3450. * block is formatted into the on-disk inode fork. We still change it,
  3451. * though, so everything is consistent in memory.
  3452. */
  3453. if (bp) {
  3454. if (cur->bc_tp) {
  3455. xfs_trans_ordered_buf(cur->bc_tp, bp);
  3456. xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
  3457. } else {
  3458. xfs_buf_delwri_queue(bp, buffer_list);
  3459. }
  3460. } else {
  3461. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  3462. ASSERT(level == cur->bc_nlevels - 1);
  3463. }
  3464. /* now read rh sibling block for next iteration */
  3465. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  3466. if (xfs_btree_ptr_is_null(cur, &rptr))
  3467. return ENOENT;
  3468. return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
  3469. }
  3470. int
  3471. xfs_btree_change_owner(
  3472. struct xfs_btree_cur *cur,
  3473. __uint64_t new_owner,
  3474. struct list_head *buffer_list)
  3475. {
  3476. union xfs_btree_ptr lptr;
  3477. int level;
  3478. struct xfs_btree_block *block = NULL;
  3479. int error = 0;
  3480. cur->bc_ops->init_ptr_from_cur(cur, &lptr);
  3481. /* for each level */
  3482. for (level = cur->bc_nlevels - 1; level >= 0; level--) {
  3483. /* grab the left hand block */
  3484. error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
  3485. if (error)
  3486. return error;
  3487. /* readahead the left most block for the next level down */
  3488. if (level > 0) {
  3489. union xfs_btree_ptr *ptr;
  3490. ptr = xfs_btree_ptr_addr(cur, 1, block);
  3491. xfs_btree_readahead_ptr(cur, ptr, 1);
  3492. /* save for the next iteration of the loop */
  3493. lptr = *ptr;
  3494. }
  3495. /* for each buffer in the level */
  3496. do {
  3497. error = xfs_btree_block_change_owner(cur, level,
  3498. new_owner,
  3499. buffer_list);
  3500. } while (!error);
  3501. if (error != ENOENT)
  3502. return error;
  3503. }
  3504. return 0;
  3505. }