xfs_btree.c 119 KB

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