xfs_btree.c 128 KB

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