xfs_btree.c 128 KB

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