xfs_btree.c 127 KB

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