xfs_btree.c 128 KB

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