xfs_btree.c 128 KB

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