xfs_btree.c 126 KB

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