xfs_btree.c 127 KB

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