xfs_btree.c 131 KB

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