xfs_btree.c 127 KB

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