alloc.c 197 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * alloc.c
  5. *
  6. * Extent allocs and frees
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/swap.h>
  30. #include <linux/quotaops.h>
  31. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  32. #include <cluster/masklog.h>
  33. #include "ocfs2.h"
  34. #include "alloc.h"
  35. #include "aops.h"
  36. #include "blockcheck.h"
  37. #include "dlmglue.h"
  38. #include "extent_map.h"
  39. #include "inode.h"
  40. #include "journal.h"
  41. #include "localalloc.h"
  42. #include "suballoc.h"
  43. #include "sysfile.h"
  44. #include "file.h"
  45. #include "super.h"
  46. #include "uptodate.h"
  47. #include "xattr.h"
  48. #include "refcounttree.h"
  49. #include "buffer_head_io.h"
  50. enum ocfs2_contig_type {
  51. CONTIG_NONE = 0,
  52. CONTIG_LEFT,
  53. CONTIG_RIGHT,
  54. CONTIG_LEFTRIGHT,
  55. };
  56. static enum ocfs2_contig_type
  57. ocfs2_extent_rec_contig(struct super_block *sb,
  58. struct ocfs2_extent_rec *ext,
  59. struct ocfs2_extent_rec *insert_rec);
  60. /*
  61. * Operations for a specific extent tree type.
  62. *
  63. * To implement an on-disk btree (extent tree) type in ocfs2, add
  64. * an ocfs2_extent_tree_operations structure and the matching
  65. * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
  66. * for the allocation portion of the extent tree.
  67. */
  68. struct ocfs2_extent_tree_operations {
  69. /*
  70. * last_eb_blk is the block number of the right most leaf extent
  71. * block. Most on-disk structures containing an extent tree store
  72. * this value for fast access. The ->eo_set_last_eb_blk() and
  73. * ->eo_get_last_eb_blk() operations access this value. They are
  74. * both required.
  75. */
  76. void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
  77. u64 blkno);
  78. u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
  79. /*
  80. * The on-disk structure usually keeps track of how many total
  81. * clusters are stored in this extent tree. This function updates
  82. * that value. new_clusters is the delta, and must be
  83. * added to the total. Required.
  84. */
  85. void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
  86. u32 new_clusters);
  87. /*
  88. * If this extent tree is supported by an extent map, insert
  89. * a record into the map.
  90. */
  91. void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
  92. struct ocfs2_extent_rec *rec);
  93. /*
  94. * If this extent tree is supported by an extent map, truncate the
  95. * map to clusters,
  96. */
  97. void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
  98. u32 clusters);
  99. /*
  100. * If ->eo_insert_check() exists, it is called before rec is
  101. * inserted into the extent tree. It is optional.
  102. */
  103. int (*eo_insert_check)(struct ocfs2_extent_tree *et,
  104. struct ocfs2_extent_rec *rec);
  105. int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
  106. /*
  107. * --------------------------------------------------------------
  108. * The remaining are internal to ocfs2_extent_tree and don't have
  109. * accessor functions
  110. */
  111. /*
  112. * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
  113. * It is required.
  114. */
  115. void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
  116. /*
  117. * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
  118. * it exists. If it does not, et->et_max_leaf_clusters is set
  119. * to 0 (unlimited). Optional.
  120. */
  121. void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
  122. /*
  123. * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
  124. * are contiguous or not. Optional. Don't need to set it if use
  125. * ocfs2_extent_rec as the tree leaf.
  126. */
  127. enum ocfs2_contig_type
  128. (*eo_extent_contig)(struct ocfs2_extent_tree *et,
  129. struct ocfs2_extent_rec *ext,
  130. struct ocfs2_extent_rec *insert_rec);
  131. };
  132. /*
  133. * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
  134. * in the methods.
  135. */
  136. static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
  137. static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
  138. u64 blkno);
  139. static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
  140. u32 clusters);
  141. static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
  142. struct ocfs2_extent_rec *rec);
  143. static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
  144. u32 clusters);
  145. static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
  146. struct ocfs2_extent_rec *rec);
  147. static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
  148. static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
  149. static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
  150. .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
  151. .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
  152. .eo_update_clusters = ocfs2_dinode_update_clusters,
  153. .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
  154. .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
  155. .eo_insert_check = ocfs2_dinode_insert_check,
  156. .eo_sanity_check = ocfs2_dinode_sanity_check,
  157. .eo_fill_root_el = ocfs2_dinode_fill_root_el,
  158. };
  159. static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
  160. u64 blkno)
  161. {
  162. struct ocfs2_dinode *di = et->et_object;
  163. BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
  164. di->i_last_eb_blk = cpu_to_le64(blkno);
  165. }
  166. static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
  167. {
  168. struct ocfs2_dinode *di = et->et_object;
  169. BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
  170. return le64_to_cpu(di->i_last_eb_blk);
  171. }
  172. static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
  173. u32 clusters)
  174. {
  175. struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
  176. struct ocfs2_dinode *di = et->et_object;
  177. le32_add_cpu(&di->i_clusters, clusters);
  178. spin_lock(&oi->ip_lock);
  179. oi->ip_clusters = le32_to_cpu(di->i_clusters);
  180. spin_unlock(&oi->ip_lock);
  181. }
  182. static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
  183. struct ocfs2_extent_rec *rec)
  184. {
  185. struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
  186. ocfs2_extent_map_insert_rec(inode, rec);
  187. }
  188. static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
  189. u32 clusters)
  190. {
  191. struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
  192. ocfs2_extent_map_trunc(inode, clusters);
  193. }
  194. static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
  195. struct ocfs2_extent_rec *rec)
  196. {
  197. struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
  198. struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
  199. BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
  200. mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
  201. (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
  202. "Device %s, asking for sparse allocation: inode %llu, "
  203. "cpos %u, clusters %u\n",
  204. osb->dev_str,
  205. (unsigned long long)oi->ip_blkno,
  206. rec->e_cpos, oi->ip_clusters);
  207. return 0;
  208. }
  209. static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
  210. {
  211. struct ocfs2_dinode *di = et->et_object;
  212. BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
  213. BUG_ON(!OCFS2_IS_VALID_DINODE(di));
  214. return 0;
  215. }
  216. static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
  217. {
  218. struct ocfs2_dinode *di = et->et_object;
  219. et->et_root_el = &di->id2.i_list;
  220. }
  221. static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
  222. {
  223. struct ocfs2_xattr_value_buf *vb = et->et_object;
  224. et->et_root_el = &vb->vb_xv->xr_list;
  225. }
  226. static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
  227. u64 blkno)
  228. {
  229. struct ocfs2_xattr_value_buf *vb = et->et_object;
  230. vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
  231. }
  232. static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
  233. {
  234. struct ocfs2_xattr_value_buf *vb = et->et_object;
  235. return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
  236. }
  237. static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
  238. u32 clusters)
  239. {
  240. struct ocfs2_xattr_value_buf *vb = et->et_object;
  241. le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
  242. }
  243. static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
  244. .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
  245. .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
  246. .eo_update_clusters = ocfs2_xattr_value_update_clusters,
  247. .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
  248. };
  249. static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
  250. {
  251. struct ocfs2_xattr_block *xb = et->et_object;
  252. et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
  253. }
  254. static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
  255. {
  256. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  257. et->et_max_leaf_clusters =
  258. ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
  259. }
  260. static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
  261. u64 blkno)
  262. {
  263. struct ocfs2_xattr_block *xb = et->et_object;
  264. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  265. xt->xt_last_eb_blk = cpu_to_le64(blkno);
  266. }
  267. static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
  268. {
  269. struct ocfs2_xattr_block *xb = et->et_object;
  270. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  271. return le64_to_cpu(xt->xt_last_eb_blk);
  272. }
  273. static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
  274. u32 clusters)
  275. {
  276. struct ocfs2_xattr_block *xb = et->et_object;
  277. le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
  278. }
  279. static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
  280. .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
  281. .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
  282. .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
  283. .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
  284. .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
  285. };
  286. static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
  287. u64 blkno)
  288. {
  289. struct ocfs2_dx_root_block *dx_root = et->et_object;
  290. dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
  291. }
  292. static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
  293. {
  294. struct ocfs2_dx_root_block *dx_root = et->et_object;
  295. return le64_to_cpu(dx_root->dr_last_eb_blk);
  296. }
  297. static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
  298. u32 clusters)
  299. {
  300. struct ocfs2_dx_root_block *dx_root = et->et_object;
  301. le32_add_cpu(&dx_root->dr_clusters, clusters);
  302. }
  303. static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
  304. {
  305. struct ocfs2_dx_root_block *dx_root = et->et_object;
  306. BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
  307. return 0;
  308. }
  309. static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
  310. {
  311. struct ocfs2_dx_root_block *dx_root = et->et_object;
  312. et->et_root_el = &dx_root->dr_list;
  313. }
  314. static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
  315. .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
  316. .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
  317. .eo_update_clusters = ocfs2_dx_root_update_clusters,
  318. .eo_sanity_check = ocfs2_dx_root_sanity_check,
  319. .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
  320. };
  321. static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
  322. {
  323. struct ocfs2_refcount_block *rb = et->et_object;
  324. et->et_root_el = &rb->rf_list;
  325. }
  326. static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
  327. u64 blkno)
  328. {
  329. struct ocfs2_refcount_block *rb = et->et_object;
  330. rb->rf_last_eb_blk = cpu_to_le64(blkno);
  331. }
  332. static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
  333. {
  334. struct ocfs2_refcount_block *rb = et->et_object;
  335. return le64_to_cpu(rb->rf_last_eb_blk);
  336. }
  337. static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
  338. u32 clusters)
  339. {
  340. struct ocfs2_refcount_block *rb = et->et_object;
  341. le32_add_cpu(&rb->rf_clusters, clusters);
  342. }
  343. static enum ocfs2_contig_type
  344. ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
  345. struct ocfs2_extent_rec *ext,
  346. struct ocfs2_extent_rec *insert_rec)
  347. {
  348. return CONTIG_NONE;
  349. }
  350. static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
  351. .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk,
  352. .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk,
  353. .eo_update_clusters = ocfs2_refcount_tree_update_clusters,
  354. .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el,
  355. .eo_extent_contig = ocfs2_refcount_tree_extent_contig,
  356. };
  357. static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
  358. struct ocfs2_caching_info *ci,
  359. struct buffer_head *bh,
  360. ocfs2_journal_access_func access,
  361. void *obj,
  362. struct ocfs2_extent_tree_operations *ops)
  363. {
  364. et->et_ops = ops;
  365. et->et_root_bh = bh;
  366. et->et_ci = ci;
  367. et->et_root_journal_access = access;
  368. if (!obj)
  369. obj = (void *)bh->b_data;
  370. et->et_object = obj;
  371. et->et_ops->eo_fill_root_el(et);
  372. if (!et->et_ops->eo_fill_max_leaf_clusters)
  373. et->et_max_leaf_clusters = 0;
  374. else
  375. et->et_ops->eo_fill_max_leaf_clusters(et);
  376. }
  377. void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
  378. struct ocfs2_caching_info *ci,
  379. struct buffer_head *bh)
  380. {
  381. __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
  382. NULL, &ocfs2_dinode_et_ops);
  383. }
  384. void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
  385. struct ocfs2_caching_info *ci,
  386. struct buffer_head *bh)
  387. {
  388. __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
  389. NULL, &ocfs2_xattr_tree_et_ops);
  390. }
  391. void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
  392. struct ocfs2_caching_info *ci,
  393. struct ocfs2_xattr_value_buf *vb)
  394. {
  395. __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
  396. &ocfs2_xattr_value_et_ops);
  397. }
  398. void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
  399. struct ocfs2_caching_info *ci,
  400. struct buffer_head *bh)
  401. {
  402. __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
  403. NULL, &ocfs2_dx_root_et_ops);
  404. }
  405. void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
  406. struct ocfs2_caching_info *ci,
  407. struct buffer_head *bh)
  408. {
  409. __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
  410. NULL, &ocfs2_refcount_tree_et_ops);
  411. }
  412. static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
  413. u64 new_last_eb_blk)
  414. {
  415. et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
  416. }
  417. static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
  418. {
  419. return et->et_ops->eo_get_last_eb_blk(et);
  420. }
  421. static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
  422. u32 clusters)
  423. {
  424. et->et_ops->eo_update_clusters(et, clusters);
  425. }
  426. static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
  427. struct ocfs2_extent_rec *rec)
  428. {
  429. if (et->et_ops->eo_extent_map_insert)
  430. et->et_ops->eo_extent_map_insert(et, rec);
  431. }
  432. static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
  433. u32 clusters)
  434. {
  435. if (et->et_ops->eo_extent_map_truncate)
  436. et->et_ops->eo_extent_map_truncate(et, clusters);
  437. }
  438. static inline int ocfs2_et_root_journal_access(handle_t *handle,
  439. struct ocfs2_extent_tree *et,
  440. int type)
  441. {
  442. return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
  443. type);
  444. }
  445. static inline enum ocfs2_contig_type
  446. ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
  447. struct ocfs2_extent_rec *rec,
  448. struct ocfs2_extent_rec *insert_rec)
  449. {
  450. if (et->et_ops->eo_extent_contig)
  451. return et->et_ops->eo_extent_contig(et, rec, insert_rec);
  452. return ocfs2_extent_rec_contig(
  453. ocfs2_metadata_cache_get_super(et->et_ci),
  454. rec, insert_rec);
  455. }
  456. static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
  457. struct ocfs2_extent_rec *rec)
  458. {
  459. int ret = 0;
  460. if (et->et_ops->eo_insert_check)
  461. ret = et->et_ops->eo_insert_check(et, rec);
  462. return ret;
  463. }
  464. static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
  465. {
  466. int ret = 0;
  467. if (et->et_ops->eo_sanity_check)
  468. ret = et->et_ops->eo_sanity_check(et);
  469. return ret;
  470. }
  471. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
  472. static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
  473. struct ocfs2_extent_block *eb);
  474. static void ocfs2_adjust_rightmost_records(handle_t *handle,
  475. struct ocfs2_extent_tree *et,
  476. struct ocfs2_path *path,
  477. struct ocfs2_extent_rec *insert_rec);
  478. /*
  479. * Reset the actual path elements so that we can re-use the structure
  480. * to build another path. Generally, this involves freeing the buffer
  481. * heads.
  482. */
  483. void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
  484. {
  485. int i, start = 0, depth = 0;
  486. struct ocfs2_path_item *node;
  487. if (keep_root)
  488. start = 1;
  489. for(i = start; i < path_num_items(path); i++) {
  490. node = &path->p_node[i];
  491. brelse(node->bh);
  492. node->bh = NULL;
  493. node->el = NULL;
  494. }
  495. /*
  496. * Tree depth may change during truncate, or insert. If we're
  497. * keeping the root extent list, then make sure that our path
  498. * structure reflects the proper depth.
  499. */
  500. if (keep_root)
  501. depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
  502. else
  503. path_root_access(path) = NULL;
  504. path->p_tree_depth = depth;
  505. }
  506. void ocfs2_free_path(struct ocfs2_path *path)
  507. {
  508. if (path) {
  509. ocfs2_reinit_path(path, 0);
  510. kfree(path);
  511. }
  512. }
  513. /*
  514. * All the elements of src into dest. After this call, src could be freed
  515. * without affecting dest.
  516. *
  517. * Both paths should have the same root. Any non-root elements of dest
  518. * will be freed.
  519. */
  520. static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  521. {
  522. int i;
  523. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  524. BUG_ON(path_root_el(dest) != path_root_el(src));
  525. BUG_ON(path_root_access(dest) != path_root_access(src));
  526. ocfs2_reinit_path(dest, 1);
  527. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  528. dest->p_node[i].bh = src->p_node[i].bh;
  529. dest->p_node[i].el = src->p_node[i].el;
  530. if (dest->p_node[i].bh)
  531. get_bh(dest->p_node[i].bh);
  532. }
  533. }
  534. /*
  535. * Make the *dest path the same as src and re-initialize src path to
  536. * have a root only.
  537. */
  538. static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  539. {
  540. int i;
  541. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  542. BUG_ON(path_root_access(dest) != path_root_access(src));
  543. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  544. brelse(dest->p_node[i].bh);
  545. dest->p_node[i].bh = src->p_node[i].bh;
  546. dest->p_node[i].el = src->p_node[i].el;
  547. src->p_node[i].bh = NULL;
  548. src->p_node[i].el = NULL;
  549. }
  550. }
  551. /*
  552. * Insert an extent block at given index.
  553. *
  554. * This will not take an additional reference on eb_bh.
  555. */
  556. static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
  557. struct buffer_head *eb_bh)
  558. {
  559. struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  560. /*
  561. * Right now, no root bh is an extent block, so this helps
  562. * catch code errors with dinode trees. The assertion can be
  563. * safely removed if we ever need to insert extent block
  564. * structures at the root.
  565. */
  566. BUG_ON(index == 0);
  567. path->p_node[index].bh = eb_bh;
  568. path->p_node[index].el = &eb->h_list;
  569. }
  570. static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
  571. struct ocfs2_extent_list *root_el,
  572. ocfs2_journal_access_func access)
  573. {
  574. struct ocfs2_path *path;
  575. BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
  576. path = kzalloc(sizeof(*path), GFP_NOFS);
  577. if (path) {
  578. path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
  579. get_bh(root_bh);
  580. path_root_bh(path) = root_bh;
  581. path_root_el(path) = root_el;
  582. path_root_access(path) = access;
  583. }
  584. return path;
  585. }
  586. struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
  587. {
  588. return ocfs2_new_path(path_root_bh(path), path_root_el(path),
  589. path_root_access(path));
  590. }
  591. struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
  592. {
  593. return ocfs2_new_path(et->et_root_bh, et->et_root_el,
  594. et->et_root_journal_access);
  595. }
  596. /*
  597. * Journal the buffer at depth idx. All idx>0 are extent_blocks,
  598. * otherwise it's the root_access function.
  599. *
  600. * I don't like the way this function's name looks next to
  601. * ocfs2_journal_access_path(), but I don't have a better one.
  602. */
  603. int ocfs2_path_bh_journal_access(handle_t *handle,
  604. struct ocfs2_caching_info *ci,
  605. struct ocfs2_path *path,
  606. int idx)
  607. {
  608. ocfs2_journal_access_func access = path_root_access(path);
  609. if (!access)
  610. access = ocfs2_journal_access;
  611. if (idx)
  612. access = ocfs2_journal_access_eb;
  613. return access(handle, ci, path->p_node[idx].bh,
  614. OCFS2_JOURNAL_ACCESS_WRITE);
  615. }
  616. /*
  617. * Convenience function to journal all components in a path.
  618. */
  619. int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
  620. handle_t *handle,
  621. struct ocfs2_path *path)
  622. {
  623. int i, ret = 0;
  624. if (!path)
  625. goto out;
  626. for(i = 0; i < path_num_items(path); i++) {
  627. ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
  628. if (ret < 0) {
  629. mlog_errno(ret);
  630. goto out;
  631. }
  632. }
  633. out:
  634. return ret;
  635. }
  636. /*
  637. * Return the index of the extent record which contains cluster #v_cluster.
  638. * -1 is returned if it was not found.
  639. *
  640. * Should work fine on interior and exterior nodes.
  641. */
  642. int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
  643. {
  644. int ret = -1;
  645. int i;
  646. struct ocfs2_extent_rec *rec;
  647. u32 rec_end, rec_start, clusters;
  648. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  649. rec = &el->l_recs[i];
  650. rec_start = le32_to_cpu(rec->e_cpos);
  651. clusters = ocfs2_rec_clusters(el, rec);
  652. rec_end = rec_start + clusters;
  653. if (v_cluster >= rec_start && v_cluster < rec_end) {
  654. ret = i;
  655. break;
  656. }
  657. }
  658. return ret;
  659. }
  660. /*
  661. * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
  662. * ocfs2_extent_rec_contig only work properly against leaf nodes!
  663. */
  664. static int ocfs2_block_extent_contig(struct super_block *sb,
  665. struct ocfs2_extent_rec *ext,
  666. u64 blkno)
  667. {
  668. u64 blk_end = le64_to_cpu(ext->e_blkno);
  669. blk_end += ocfs2_clusters_to_blocks(sb,
  670. le16_to_cpu(ext->e_leaf_clusters));
  671. return blkno == blk_end;
  672. }
  673. static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
  674. struct ocfs2_extent_rec *right)
  675. {
  676. u32 left_range;
  677. left_range = le32_to_cpu(left->e_cpos) +
  678. le16_to_cpu(left->e_leaf_clusters);
  679. return (left_range == le32_to_cpu(right->e_cpos));
  680. }
  681. static enum ocfs2_contig_type
  682. ocfs2_extent_rec_contig(struct super_block *sb,
  683. struct ocfs2_extent_rec *ext,
  684. struct ocfs2_extent_rec *insert_rec)
  685. {
  686. u64 blkno = le64_to_cpu(insert_rec->e_blkno);
  687. /*
  688. * Refuse to coalesce extent records with different flag
  689. * fields - we don't want to mix unwritten extents with user
  690. * data.
  691. */
  692. if (ext->e_flags != insert_rec->e_flags)
  693. return CONTIG_NONE;
  694. if (ocfs2_extents_adjacent(ext, insert_rec) &&
  695. ocfs2_block_extent_contig(sb, ext, blkno))
  696. return CONTIG_RIGHT;
  697. blkno = le64_to_cpu(ext->e_blkno);
  698. if (ocfs2_extents_adjacent(insert_rec, ext) &&
  699. ocfs2_block_extent_contig(sb, insert_rec, blkno))
  700. return CONTIG_LEFT;
  701. return CONTIG_NONE;
  702. }
  703. /*
  704. * NOTE: We can have pretty much any combination of contiguousness and
  705. * appending.
  706. *
  707. * The usefulness of APPEND_TAIL is more in that it lets us know that
  708. * we'll have to update the path to that leaf.
  709. */
  710. enum ocfs2_append_type {
  711. APPEND_NONE = 0,
  712. APPEND_TAIL,
  713. };
  714. enum ocfs2_split_type {
  715. SPLIT_NONE = 0,
  716. SPLIT_LEFT,
  717. SPLIT_RIGHT,
  718. };
  719. struct ocfs2_insert_type {
  720. enum ocfs2_split_type ins_split;
  721. enum ocfs2_append_type ins_appending;
  722. enum ocfs2_contig_type ins_contig;
  723. int ins_contig_index;
  724. int ins_tree_depth;
  725. };
  726. struct ocfs2_merge_ctxt {
  727. enum ocfs2_contig_type c_contig_type;
  728. int c_has_empty_extent;
  729. int c_split_covers_rec;
  730. };
  731. static int ocfs2_validate_extent_block(struct super_block *sb,
  732. struct buffer_head *bh)
  733. {
  734. int rc;
  735. struct ocfs2_extent_block *eb =
  736. (struct ocfs2_extent_block *)bh->b_data;
  737. mlog(0, "Validating extent block %llu\n",
  738. (unsigned long long)bh->b_blocknr);
  739. BUG_ON(!buffer_uptodate(bh));
  740. /*
  741. * If the ecc fails, we return the error but otherwise
  742. * leave the filesystem running. We know any error is
  743. * local to this block.
  744. */
  745. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
  746. if (rc) {
  747. mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
  748. (unsigned long long)bh->b_blocknr);
  749. return rc;
  750. }
  751. /*
  752. * Errors after here are fatal.
  753. */
  754. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  755. ocfs2_error(sb,
  756. "Extent block #%llu has bad signature %.*s",
  757. (unsigned long long)bh->b_blocknr, 7,
  758. eb->h_signature);
  759. return -EINVAL;
  760. }
  761. if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
  762. ocfs2_error(sb,
  763. "Extent block #%llu has an invalid h_blkno "
  764. "of %llu",
  765. (unsigned long long)bh->b_blocknr,
  766. (unsigned long long)le64_to_cpu(eb->h_blkno));
  767. return -EINVAL;
  768. }
  769. if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
  770. ocfs2_error(sb,
  771. "Extent block #%llu has an invalid "
  772. "h_fs_generation of #%u",
  773. (unsigned long long)bh->b_blocknr,
  774. le32_to_cpu(eb->h_fs_generation));
  775. return -EINVAL;
  776. }
  777. return 0;
  778. }
  779. int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
  780. struct buffer_head **bh)
  781. {
  782. int rc;
  783. struct buffer_head *tmp = *bh;
  784. rc = ocfs2_read_block(ci, eb_blkno, &tmp,
  785. ocfs2_validate_extent_block);
  786. /* If ocfs2_read_block() got us a new bh, pass it up. */
  787. if (!rc && !*bh)
  788. *bh = tmp;
  789. return rc;
  790. }
  791. /*
  792. * How many free extents have we got before we need more meta data?
  793. */
  794. int ocfs2_num_free_extents(struct ocfs2_super *osb,
  795. struct ocfs2_extent_tree *et)
  796. {
  797. int retval;
  798. struct ocfs2_extent_list *el = NULL;
  799. struct ocfs2_extent_block *eb;
  800. struct buffer_head *eb_bh = NULL;
  801. u64 last_eb_blk = 0;
  802. mlog_entry_void();
  803. el = et->et_root_el;
  804. last_eb_blk = ocfs2_et_get_last_eb_blk(et);
  805. if (last_eb_blk) {
  806. retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
  807. &eb_bh);
  808. if (retval < 0) {
  809. mlog_errno(retval);
  810. goto bail;
  811. }
  812. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  813. el = &eb->h_list;
  814. }
  815. BUG_ON(el->l_tree_depth != 0);
  816. retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
  817. bail:
  818. brelse(eb_bh);
  819. mlog_exit(retval);
  820. return retval;
  821. }
  822. /* expects array to already be allocated
  823. *
  824. * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
  825. * l_count for you
  826. */
  827. static int ocfs2_create_new_meta_bhs(handle_t *handle,
  828. struct ocfs2_extent_tree *et,
  829. int wanted,
  830. struct ocfs2_alloc_context *meta_ac,
  831. struct buffer_head *bhs[])
  832. {
  833. int count, status, i;
  834. u16 suballoc_bit_start;
  835. u32 num_got;
  836. u64 first_blkno;
  837. struct ocfs2_super *osb =
  838. OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
  839. struct ocfs2_extent_block *eb;
  840. mlog_entry_void();
  841. count = 0;
  842. while (count < wanted) {
  843. status = ocfs2_claim_metadata(osb,
  844. handle,
  845. meta_ac,
  846. wanted - count,
  847. &suballoc_bit_start,
  848. &num_got,
  849. &first_blkno);
  850. if (status < 0) {
  851. mlog_errno(status);
  852. goto bail;
  853. }
  854. for(i = count; i < (num_got + count); i++) {
  855. bhs[i] = sb_getblk(osb->sb, first_blkno);
  856. if (bhs[i] == NULL) {
  857. status = -EIO;
  858. mlog_errno(status);
  859. goto bail;
  860. }
  861. ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
  862. status = ocfs2_journal_access_eb(handle, et->et_ci,
  863. bhs[i],
  864. OCFS2_JOURNAL_ACCESS_CREATE);
  865. if (status < 0) {
  866. mlog_errno(status);
  867. goto bail;
  868. }
  869. memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
  870. eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
  871. /* Ok, setup the minimal stuff here. */
  872. strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
  873. eb->h_blkno = cpu_to_le64(first_blkno);
  874. eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
  875. eb->h_suballoc_slot =
  876. cpu_to_le16(meta_ac->ac_alloc_slot);
  877. eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  878. eb->h_list.l_count =
  879. cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
  880. suballoc_bit_start++;
  881. first_blkno++;
  882. /* We'll also be dirtied by the caller, so
  883. * this isn't absolutely necessary. */
  884. status = ocfs2_journal_dirty(handle, bhs[i]);
  885. if (status < 0) {
  886. mlog_errno(status);
  887. goto bail;
  888. }
  889. }
  890. count += num_got;
  891. }
  892. status = 0;
  893. bail:
  894. if (status < 0) {
  895. for(i = 0; i < wanted; i++) {
  896. brelse(bhs[i]);
  897. bhs[i] = NULL;
  898. }
  899. }
  900. mlog_exit(status);
  901. return status;
  902. }
  903. /*
  904. * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
  905. *
  906. * Returns the sum of the rightmost extent rec logical offset and
  907. * cluster count.
  908. *
  909. * ocfs2_add_branch() uses this to determine what logical cluster
  910. * value should be populated into the leftmost new branch records.
  911. *
  912. * ocfs2_shift_tree_depth() uses this to determine the # clusters
  913. * value for the new topmost tree record.
  914. */
  915. static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
  916. {
  917. int i;
  918. i = le16_to_cpu(el->l_next_free_rec) - 1;
  919. return le32_to_cpu(el->l_recs[i].e_cpos) +
  920. ocfs2_rec_clusters(el, &el->l_recs[i]);
  921. }
  922. /*
  923. * Change range of the branches in the right most path according to the leaf
  924. * extent block's rightmost record.
  925. */
  926. static int ocfs2_adjust_rightmost_branch(handle_t *handle,
  927. struct ocfs2_extent_tree *et)
  928. {
  929. int status;
  930. struct ocfs2_path *path = NULL;
  931. struct ocfs2_extent_list *el;
  932. struct ocfs2_extent_rec *rec;
  933. path = ocfs2_new_path_from_et(et);
  934. if (!path) {
  935. status = -ENOMEM;
  936. return status;
  937. }
  938. status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
  939. if (status < 0) {
  940. mlog_errno(status);
  941. goto out;
  942. }
  943. status = ocfs2_extend_trans(handle, path_num_items(path) +
  944. handle->h_buffer_credits);
  945. if (status < 0) {
  946. mlog_errno(status);
  947. goto out;
  948. }
  949. status = ocfs2_journal_access_path(et->et_ci, handle, path);
  950. if (status < 0) {
  951. mlog_errno(status);
  952. goto out;
  953. }
  954. el = path_leaf_el(path);
  955. rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
  956. ocfs2_adjust_rightmost_records(handle, et, path, rec);
  957. out:
  958. ocfs2_free_path(path);
  959. return status;
  960. }
  961. /*
  962. * Add an entire tree branch to our inode. eb_bh is the extent block
  963. * to start at, if we don't want to start the branch at the root
  964. * structure.
  965. *
  966. * last_eb_bh is required as we have to update it's next_leaf pointer
  967. * for the new last extent block.
  968. *
  969. * the new branch will be 'empty' in the sense that every block will
  970. * contain a single record with cluster count == 0.
  971. */
  972. static int ocfs2_add_branch(handle_t *handle,
  973. struct ocfs2_extent_tree *et,
  974. struct buffer_head *eb_bh,
  975. struct buffer_head **last_eb_bh,
  976. struct ocfs2_alloc_context *meta_ac)
  977. {
  978. int status, new_blocks, i;
  979. u64 next_blkno, new_last_eb_blk;
  980. struct buffer_head *bh;
  981. struct buffer_head **new_eb_bhs = NULL;
  982. struct ocfs2_extent_block *eb;
  983. struct ocfs2_extent_list *eb_el;
  984. struct ocfs2_extent_list *el;
  985. u32 new_cpos, root_end;
  986. mlog_entry_void();
  987. BUG_ON(!last_eb_bh || !*last_eb_bh);
  988. if (eb_bh) {
  989. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  990. el = &eb->h_list;
  991. } else
  992. el = et->et_root_el;
  993. /* we never add a branch to a leaf. */
  994. BUG_ON(!el->l_tree_depth);
  995. new_blocks = le16_to_cpu(el->l_tree_depth);
  996. eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
  997. new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
  998. root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
  999. /*
  1000. * If there is a gap before the root end and the real end
  1001. * of the righmost leaf block, we need to remove the gap
  1002. * between new_cpos and root_end first so that the tree
  1003. * is consistent after we add a new branch(it will start
  1004. * from new_cpos).
  1005. */
  1006. if (root_end > new_cpos) {
  1007. mlog(0, "adjust the cluster end from %u to %u\n",
  1008. root_end, new_cpos);
  1009. status = ocfs2_adjust_rightmost_branch(handle, et);
  1010. if (status) {
  1011. mlog_errno(status);
  1012. goto bail;
  1013. }
  1014. }
  1015. /* allocate the number of new eb blocks we need */
  1016. new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
  1017. GFP_KERNEL);
  1018. if (!new_eb_bhs) {
  1019. status = -ENOMEM;
  1020. mlog_errno(status);
  1021. goto bail;
  1022. }
  1023. status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
  1024. meta_ac, new_eb_bhs);
  1025. if (status < 0) {
  1026. mlog_errno(status);
  1027. goto bail;
  1028. }
  1029. /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
  1030. * linked with the rest of the tree.
  1031. * conversly, new_eb_bhs[0] is the new bottommost leaf.
  1032. *
  1033. * when we leave the loop, new_last_eb_blk will point to the
  1034. * newest leaf, and next_blkno will point to the topmost extent
  1035. * block. */
  1036. next_blkno = new_last_eb_blk = 0;
  1037. for(i = 0; i < new_blocks; i++) {
  1038. bh = new_eb_bhs[i];
  1039. eb = (struct ocfs2_extent_block *) bh->b_data;
  1040. /* ocfs2_create_new_meta_bhs() should create it right! */
  1041. BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
  1042. eb_el = &eb->h_list;
  1043. status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
  1044. OCFS2_JOURNAL_ACCESS_CREATE);
  1045. if (status < 0) {
  1046. mlog_errno(status);
  1047. goto bail;
  1048. }
  1049. eb->h_next_leaf_blk = 0;
  1050. eb_el->l_tree_depth = cpu_to_le16(i);
  1051. eb_el->l_next_free_rec = cpu_to_le16(1);
  1052. /*
  1053. * This actually counts as an empty extent as
  1054. * c_clusters == 0
  1055. */
  1056. eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
  1057. eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
  1058. /*
  1059. * eb_el isn't always an interior node, but even leaf
  1060. * nodes want a zero'd flags and reserved field so
  1061. * this gets the whole 32 bits regardless of use.
  1062. */
  1063. eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
  1064. if (!eb_el->l_tree_depth)
  1065. new_last_eb_blk = le64_to_cpu(eb->h_blkno);
  1066. status = ocfs2_journal_dirty(handle, bh);
  1067. if (status < 0) {
  1068. mlog_errno(status);
  1069. goto bail;
  1070. }
  1071. next_blkno = le64_to_cpu(eb->h_blkno);
  1072. }
  1073. /* This is a bit hairy. We want to update up to three blocks
  1074. * here without leaving any of them in an inconsistent state
  1075. * in case of error. We don't have to worry about
  1076. * journal_dirty erroring as it won't unless we've aborted the
  1077. * handle (in which case we would never be here) so reserving
  1078. * the write with journal_access is all we need to do. */
  1079. status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
  1080. OCFS2_JOURNAL_ACCESS_WRITE);
  1081. if (status < 0) {
  1082. mlog_errno(status);
  1083. goto bail;
  1084. }
  1085. status = ocfs2_et_root_journal_access(handle, et,
  1086. OCFS2_JOURNAL_ACCESS_WRITE);
  1087. if (status < 0) {
  1088. mlog_errno(status);
  1089. goto bail;
  1090. }
  1091. if (eb_bh) {
  1092. status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
  1093. OCFS2_JOURNAL_ACCESS_WRITE);
  1094. if (status < 0) {
  1095. mlog_errno(status);
  1096. goto bail;
  1097. }
  1098. }
  1099. /* Link the new branch into the rest of the tree (el will
  1100. * either be on the root_bh, or the extent block passed in. */
  1101. i = le16_to_cpu(el->l_next_free_rec);
  1102. el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
  1103. el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
  1104. el->l_recs[i].e_int_clusters = 0;
  1105. le16_add_cpu(&el->l_next_free_rec, 1);
  1106. /* fe needs a new last extent block pointer, as does the
  1107. * next_leaf on the previously last-extent-block. */
  1108. ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
  1109. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  1110. eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
  1111. status = ocfs2_journal_dirty(handle, *last_eb_bh);
  1112. if (status < 0)
  1113. mlog_errno(status);
  1114. status = ocfs2_journal_dirty(handle, et->et_root_bh);
  1115. if (status < 0)
  1116. mlog_errno(status);
  1117. if (eb_bh) {
  1118. status = ocfs2_journal_dirty(handle, eb_bh);
  1119. if (status < 0)
  1120. mlog_errno(status);
  1121. }
  1122. /*
  1123. * Some callers want to track the rightmost leaf so pass it
  1124. * back here.
  1125. */
  1126. brelse(*last_eb_bh);
  1127. get_bh(new_eb_bhs[0]);
  1128. *last_eb_bh = new_eb_bhs[0];
  1129. status = 0;
  1130. bail:
  1131. if (new_eb_bhs) {
  1132. for (i = 0; i < new_blocks; i++)
  1133. brelse(new_eb_bhs[i]);
  1134. kfree(new_eb_bhs);
  1135. }
  1136. mlog_exit(status);
  1137. return status;
  1138. }
  1139. /*
  1140. * adds another level to the allocation tree.
  1141. * returns back the new extent block so you can add a branch to it
  1142. * after this call.
  1143. */
  1144. static int ocfs2_shift_tree_depth(handle_t *handle,
  1145. struct ocfs2_extent_tree *et,
  1146. struct ocfs2_alloc_context *meta_ac,
  1147. struct buffer_head **ret_new_eb_bh)
  1148. {
  1149. int status, i;
  1150. u32 new_clusters;
  1151. struct buffer_head *new_eb_bh = NULL;
  1152. struct ocfs2_extent_block *eb;
  1153. struct ocfs2_extent_list *root_el;
  1154. struct ocfs2_extent_list *eb_el;
  1155. mlog_entry_void();
  1156. status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
  1157. &new_eb_bh);
  1158. if (status < 0) {
  1159. mlog_errno(status);
  1160. goto bail;
  1161. }
  1162. eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
  1163. /* ocfs2_create_new_meta_bhs() should create it right! */
  1164. BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
  1165. eb_el = &eb->h_list;
  1166. root_el = et->et_root_el;
  1167. status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
  1168. OCFS2_JOURNAL_ACCESS_CREATE);
  1169. if (status < 0) {
  1170. mlog_errno(status);
  1171. goto bail;
  1172. }
  1173. /* copy the root extent list data into the new extent block */
  1174. eb_el->l_tree_depth = root_el->l_tree_depth;
  1175. eb_el->l_next_free_rec = root_el->l_next_free_rec;
  1176. for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  1177. eb_el->l_recs[i] = root_el->l_recs[i];
  1178. status = ocfs2_journal_dirty(handle, new_eb_bh);
  1179. if (status < 0) {
  1180. mlog_errno(status);
  1181. goto bail;
  1182. }
  1183. status = ocfs2_et_root_journal_access(handle, et,
  1184. OCFS2_JOURNAL_ACCESS_WRITE);
  1185. if (status < 0) {
  1186. mlog_errno(status);
  1187. goto bail;
  1188. }
  1189. new_clusters = ocfs2_sum_rightmost_rec(eb_el);
  1190. /* update root_bh now */
  1191. le16_add_cpu(&root_el->l_tree_depth, 1);
  1192. root_el->l_recs[0].e_cpos = 0;
  1193. root_el->l_recs[0].e_blkno = eb->h_blkno;
  1194. root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
  1195. for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  1196. memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  1197. root_el->l_next_free_rec = cpu_to_le16(1);
  1198. /* If this is our 1st tree depth shift, then last_eb_blk
  1199. * becomes the allocated extent block */
  1200. if (root_el->l_tree_depth == cpu_to_le16(1))
  1201. ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
  1202. status = ocfs2_journal_dirty(handle, et->et_root_bh);
  1203. if (status < 0) {
  1204. mlog_errno(status);
  1205. goto bail;
  1206. }
  1207. *ret_new_eb_bh = new_eb_bh;
  1208. new_eb_bh = NULL;
  1209. status = 0;
  1210. bail:
  1211. brelse(new_eb_bh);
  1212. mlog_exit(status);
  1213. return status;
  1214. }
  1215. /*
  1216. * Should only be called when there is no space left in any of the
  1217. * leaf nodes. What we want to do is find the lowest tree depth
  1218. * non-leaf extent block with room for new records. There are three
  1219. * valid results of this search:
  1220. *
  1221. * 1) a lowest extent block is found, then we pass it back in
  1222. * *lowest_eb_bh and return '0'
  1223. *
  1224. * 2) the search fails to find anything, but the root_el has room. We
  1225. * pass NULL back in *lowest_eb_bh, but still return '0'
  1226. *
  1227. * 3) the search fails to find anything AND the root_el is full, in
  1228. * which case we return > 0
  1229. *
  1230. * return status < 0 indicates an error.
  1231. */
  1232. static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
  1233. struct buffer_head **target_bh)
  1234. {
  1235. int status = 0, i;
  1236. u64 blkno;
  1237. struct ocfs2_extent_block *eb;
  1238. struct ocfs2_extent_list *el;
  1239. struct buffer_head *bh = NULL;
  1240. struct buffer_head *lowest_bh = NULL;
  1241. mlog_entry_void();
  1242. *target_bh = NULL;
  1243. el = et->et_root_el;
  1244. while(le16_to_cpu(el->l_tree_depth) > 1) {
  1245. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  1246. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  1247. "Owner %llu has empty "
  1248. "extent list (next_free_rec == 0)",
  1249. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  1250. status = -EIO;
  1251. goto bail;
  1252. }
  1253. i = le16_to_cpu(el->l_next_free_rec) - 1;
  1254. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  1255. if (!blkno) {
  1256. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  1257. "Owner %llu has extent "
  1258. "list where extent # %d has no physical "
  1259. "block start",
  1260. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
  1261. status = -EIO;
  1262. goto bail;
  1263. }
  1264. brelse(bh);
  1265. bh = NULL;
  1266. status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
  1267. if (status < 0) {
  1268. mlog_errno(status);
  1269. goto bail;
  1270. }
  1271. eb = (struct ocfs2_extent_block *) bh->b_data;
  1272. el = &eb->h_list;
  1273. if (le16_to_cpu(el->l_next_free_rec) <
  1274. le16_to_cpu(el->l_count)) {
  1275. brelse(lowest_bh);
  1276. lowest_bh = bh;
  1277. get_bh(lowest_bh);
  1278. }
  1279. }
  1280. /* If we didn't find one and the fe doesn't have any room,
  1281. * then return '1' */
  1282. el = et->et_root_el;
  1283. if (!lowest_bh && (el->l_next_free_rec == el->l_count))
  1284. status = 1;
  1285. *target_bh = lowest_bh;
  1286. bail:
  1287. brelse(bh);
  1288. mlog_exit(status);
  1289. return status;
  1290. }
  1291. /*
  1292. * Grow a b-tree so that it has more records.
  1293. *
  1294. * We might shift the tree depth in which case existing paths should
  1295. * be considered invalid.
  1296. *
  1297. * Tree depth after the grow is returned via *final_depth.
  1298. *
  1299. * *last_eb_bh will be updated by ocfs2_add_branch().
  1300. */
  1301. static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
  1302. int *final_depth, struct buffer_head **last_eb_bh,
  1303. struct ocfs2_alloc_context *meta_ac)
  1304. {
  1305. int ret, shift;
  1306. struct ocfs2_extent_list *el = et->et_root_el;
  1307. int depth = le16_to_cpu(el->l_tree_depth);
  1308. struct buffer_head *bh = NULL;
  1309. BUG_ON(meta_ac == NULL);
  1310. shift = ocfs2_find_branch_target(et, &bh);
  1311. if (shift < 0) {
  1312. ret = shift;
  1313. mlog_errno(ret);
  1314. goto out;
  1315. }
  1316. /* We traveled all the way to the bottom of the allocation tree
  1317. * and didn't find room for any more extents - we need to add
  1318. * another tree level */
  1319. if (shift) {
  1320. BUG_ON(bh);
  1321. mlog(0, "need to shift tree depth (current = %d)\n", depth);
  1322. /* ocfs2_shift_tree_depth will return us a buffer with
  1323. * the new extent block (so we can pass that to
  1324. * ocfs2_add_branch). */
  1325. ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
  1326. if (ret < 0) {
  1327. mlog_errno(ret);
  1328. goto out;
  1329. }
  1330. depth++;
  1331. if (depth == 1) {
  1332. /*
  1333. * Special case: we have room now if we shifted from
  1334. * tree_depth 0, so no more work needs to be done.
  1335. *
  1336. * We won't be calling add_branch, so pass
  1337. * back *last_eb_bh as the new leaf. At depth
  1338. * zero, it should always be null so there's
  1339. * no reason to brelse.
  1340. */
  1341. BUG_ON(*last_eb_bh);
  1342. get_bh(bh);
  1343. *last_eb_bh = bh;
  1344. goto out;
  1345. }
  1346. }
  1347. /* call ocfs2_add_branch to add the final part of the tree with
  1348. * the new data. */
  1349. mlog(0, "add branch. bh = %p\n", bh);
  1350. ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
  1351. meta_ac);
  1352. if (ret < 0) {
  1353. mlog_errno(ret);
  1354. goto out;
  1355. }
  1356. out:
  1357. if (final_depth)
  1358. *final_depth = depth;
  1359. brelse(bh);
  1360. return ret;
  1361. }
  1362. /*
  1363. * This function will discard the rightmost extent record.
  1364. */
  1365. static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
  1366. {
  1367. int next_free = le16_to_cpu(el->l_next_free_rec);
  1368. int count = le16_to_cpu(el->l_count);
  1369. unsigned int num_bytes;
  1370. BUG_ON(!next_free);
  1371. /* This will cause us to go off the end of our extent list. */
  1372. BUG_ON(next_free >= count);
  1373. num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
  1374. memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
  1375. }
  1376. static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
  1377. struct ocfs2_extent_rec *insert_rec)
  1378. {
  1379. int i, insert_index, next_free, has_empty, num_bytes;
  1380. u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
  1381. struct ocfs2_extent_rec *rec;
  1382. next_free = le16_to_cpu(el->l_next_free_rec);
  1383. has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
  1384. BUG_ON(!next_free);
  1385. /* The tree code before us didn't allow enough room in the leaf. */
  1386. BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
  1387. /*
  1388. * The easiest way to approach this is to just remove the
  1389. * empty extent and temporarily decrement next_free.
  1390. */
  1391. if (has_empty) {
  1392. /*
  1393. * If next_free was 1 (only an empty extent), this
  1394. * loop won't execute, which is fine. We still want
  1395. * the decrement above to happen.
  1396. */
  1397. for(i = 0; i < (next_free - 1); i++)
  1398. el->l_recs[i] = el->l_recs[i+1];
  1399. next_free--;
  1400. }
  1401. /*
  1402. * Figure out what the new record index should be.
  1403. */
  1404. for(i = 0; i < next_free; i++) {
  1405. rec = &el->l_recs[i];
  1406. if (insert_cpos < le32_to_cpu(rec->e_cpos))
  1407. break;
  1408. }
  1409. insert_index = i;
  1410. mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
  1411. insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
  1412. BUG_ON(insert_index < 0);
  1413. BUG_ON(insert_index >= le16_to_cpu(el->l_count));
  1414. BUG_ON(insert_index > next_free);
  1415. /*
  1416. * No need to memmove if we're just adding to the tail.
  1417. */
  1418. if (insert_index != next_free) {
  1419. BUG_ON(next_free >= le16_to_cpu(el->l_count));
  1420. num_bytes = next_free - insert_index;
  1421. num_bytes *= sizeof(struct ocfs2_extent_rec);
  1422. memmove(&el->l_recs[insert_index + 1],
  1423. &el->l_recs[insert_index],
  1424. num_bytes);
  1425. }
  1426. /*
  1427. * Either we had an empty extent, and need to re-increment or
  1428. * there was no empty extent on a non full rightmost leaf node,
  1429. * in which case we still need to increment.
  1430. */
  1431. next_free++;
  1432. el->l_next_free_rec = cpu_to_le16(next_free);
  1433. /*
  1434. * Make sure none of the math above just messed up our tree.
  1435. */
  1436. BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
  1437. el->l_recs[insert_index] = *insert_rec;
  1438. }
  1439. static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
  1440. {
  1441. int size, num_recs = le16_to_cpu(el->l_next_free_rec);
  1442. BUG_ON(num_recs == 0);
  1443. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  1444. num_recs--;
  1445. size = num_recs * sizeof(struct ocfs2_extent_rec);
  1446. memmove(&el->l_recs[0], &el->l_recs[1], size);
  1447. memset(&el->l_recs[num_recs], 0,
  1448. sizeof(struct ocfs2_extent_rec));
  1449. el->l_next_free_rec = cpu_to_le16(num_recs);
  1450. }
  1451. }
  1452. /*
  1453. * Create an empty extent record .
  1454. *
  1455. * l_next_free_rec may be updated.
  1456. *
  1457. * If an empty extent already exists do nothing.
  1458. */
  1459. static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
  1460. {
  1461. int next_free = le16_to_cpu(el->l_next_free_rec);
  1462. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  1463. if (next_free == 0)
  1464. goto set_and_inc;
  1465. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  1466. return;
  1467. mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
  1468. "Asked to create an empty extent in a full list:\n"
  1469. "count = %u, tree depth = %u",
  1470. le16_to_cpu(el->l_count),
  1471. le16_to_cpu(el->l_tree_depth));
  1472. ocfs2_shift_records_right(el);
  1473. set_and_inc:
  1474. le16_add_cpu(&el->l_next_free_rec, 1);
  1475. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1476. }
  1477. /*
  1478. * For a rotation which involves two leaf nodes, the "root node" is
  1479. * the lowest level tree node which contains a path to both leafs. This
  1480. * resulting set of information can be used to form a complete "subtree"
  1481. *
  1482. * This function is passed two full paths from the dinode down to a
  1483. * pair of adjacent leaves. It's task is to figure out which path
  1484. * index contains the subtree root - this can be the root index itself
  1485. * in a worst-case rotation.
  1486. *
  1487. * The array index of the subtree root is passed back.
  1488. */
  1489. int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
  1490. struct ocfs2_path *left,
  1491. struct ocfs2_path *right)
  1492. {
  1493. int i = 0;
  1494. /*
  1495. * Check that the caller passed in two paths from the same tree.
  1496. */
  1497. BUG_ON(path_root_bh(left) != path_root_bh(right));
  1498. do {
  1499. i++;
  1500. /*
  1501. * The caller didn't pass two adjacent paths.
  1502. */
  1503. mlog_bug_on_msg(i > left->p_tree_depth,
  1504. "Owner %llu, left depth %u, right depth %u\n"
  1505. "left leaf blk %llu, right leaf blk %llu\n",
  1506. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  1507. left->p_tree_depth, right->p_tree_depth,
  1508. (unsigned long long)path_leaf_bh(left)->b_blocknr,
  1509. (unsigned long long)path_leaf_bh(right)->b_blocknr);
  1510. } while (left->p_node[i].bh->b_blocknr ==
  1511. right->p_node[i].bh->b_blocknr);
  1512. return i - 1;
  1513. }
  1514. typedef void (path_insert_t)(void *, struct buffer_head *);
  1515. /*
  1516. * Traverse a btree path in search of cpos, starting at root_el.
  1517. *
  1518. * This code can be called with a cpos larger than the tree, in which
  1519. * case it will return the rightmost path.
  1520. */
  1521. static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
  1522. struct ocfs2_extent_list *root_el, u32 cpos,
  1523. path_insert_t *func, void *data)
  1524. {
  1525. int i, ret = 0;
  1526. u32 range;
  1527. u64 blkno;
  1528. struct buffer_head *bh = NULL;
  1529. struct ocfs2_extent_block *eb;
  1530. struct ocfs2_extent_list *el;
  1531. struct ocfs2_extent_rec *rec;
  1532. el = root_el;
  1533. while (el->l_tree_depth) {
  1534. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  1535. ocfs2_error(ocfs2_metadata_cache_get_super(ci),
  1536. "Owner %llu has empty extent list at "
  1537. "depth %u\n",
  1538. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1539. le16_to_cpu(el->l_tree_depth));
  1540. ret = -EROFS;
  1541. goto out;
  1542. }
  1543. for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
  1544. rec = &el->l_recs[i];
  1545. /*
  1546. * In the case that cpos is off the allocation
  1547. * tree, this should just wind up returning the
  1548. * rightmost record.
  1549. */
  1550. range = le32_to_cpu(rec->e_cpos) +
  1551. ocfs2_rec_clusters(el, rec);
  1552. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  1553. break;
  1554. }
  1555. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  1556. if (blkno == 0) {
  1557. ocfs2_error(ocfs2_metadata_cache_get_super(ci),
  1558. "Owner %llu has bad blkno in extent list "
  1559. "at depth %u (index %d)\n",
  1560. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1561. le16_to_cpu(el->l_tree_depth), i);
  1562. ret = -EROFS;
  1563. goto out;
  1564. }
  1565. brelse(bh);
  1566. bh = NULL;
  1567. ret = ocfs2_read_extent_block(ci, blkno, &bh);
  1568. if (ret) {
  1569. mlog_errno(ret);
  1570. goto out;
  1571. }
  1572. eb = (struct ocfs2_extent_block *) bh->b_data;
  1573. el = &eb->h_list;
  1574. if (le16_to_cpu(el->l_next_free_rec) >
  1575. le16_to_cpu(el->l_count)) {
  1576. ocfs2_error(ocfs2_metadata_cache_get_super(ci),
  1577. "Owner %llu has bad count in extent list "
  1578. "at block %llu (next free=%u, count=%u)\n",
  1579. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1580. (unsigned long long)bh->b_blocknr,
  1581. le16_to_cpu(el->l_next_free_rec),
  1582. le16_to_cpu(el->l_count));
  1583. ret = -EROFS;
  1584. goto out;
  1585. }
  1586. if (func)
  1587. func(data, bh);
  1588. }
  1589. out:
  1590. /*
  1591. * Catch any trailing bh that the loop didn't handle.
  1592. */
  1593. brelse(bh);
  1594. return ret;
  1595. }
  1596. /*
  1597. * Given an initialized path (that is, it has a valid root extent
  1598. * list), this function will traverse the btree in search of the path
  1599. * which would contain cpos.
  1600. *
  1601. * The path traveled is recorded in the path structure.
  1602. *
  1603. * Note that this will not do any comparisons on leaf node extent
  1604. * records, so it will work fine in the case that we just added a tree
  1605. * branch.
  1606. */
  1607. struct find_path_data {
  1608. int index;
  1609. struct ocfs2_path *path;
  1610. };
  1611. static void find_path_ins(void *data, struct buffer_head *bh)
  1612. {
  1613. struct find_path_data *fp = data;
  1614. get_bh(bh);
  1615. ocfs2_path_insert_eb(fp->path, fp->index, bh);
  1616. fp->index++;
  1617. }
  1618. int ocfs2_find_path(struct ocfs2_caching_info *ci,
  1619. struct ocfs2_path *path, u32 cpos)
  1620. {
  1621. struct find_path_data data;
  1622. data.index = 1;
  1623. data.path = path;
  1624. return __ocfs2_find_path(ci, path_root_el(path), cpos,
  1625. find_path_ins, &data);
  1626. }
  1627. static void find_leaf_ins(void *data, struct buffer_head *bh)
  1628. {
  1629. struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
  1630. struct ocfs2_extent_list *el = &eb->h_list;
  1631. struct buffer_head **ret = data;
  1632. /* We want to retain only the leaf block. */
  1633. if (le16_to_cpu(el->l_tree_depth) == 0) {
  1634. get_bh(bh);
  1635. *ret = bh;
  1636. }
  1637. }
  1638. /*
  1639. * Find the leaf block in the tree which would contain cpos. No
  1640. * checking of the actual leaf is done.
  1641. *
  1642. * Some paths want to call this instead of allocating a path structure
  1643. * and calling ocfs2_find_path().
  1644. *
  1645. * This function doesn't handle non btree extent lists.
  1646. */
  1647. int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
  1648. struct ocfs2_extent_list *root_el, u32 cpos,
  1649. struct buffer_head **leaf_bh)
  1650. {
  1651. int ret;
  1652. struct buffer_head *bh = NULL;
  1653. ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
  1654. if (ret) {
  1655. mlog_errno(ret);
  1656. goto out;
  1657. }
  1658. *leaf_bh = bh;
  1659. out:
  1660. return ret;
  1661. }
  1662. /*
  1663. * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
  1664. *
  1665. * Basically, we've moved stuff around at the bottom of the tree and
  1666. * we need to fix up the extent records above the changes to reflect
  1667. * the new changes.
  1668. *
  1669. * left_rec: the record on the left.
  1670. * left_child_el: is the child list pointed to by left_rec
  1671. * right_rec: the record to the right of left_rec
  1672. * right_child_el: is the child list pointed to by right_rec
  1673. *
  1674. * By definition, this only works on interior nodes.
  1675. */
  1676. static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
  1677. struct ocfs2_extent_list *left_child_el,
  1678. struct ocfs2_extent_rec *right_rec,
  1679. struct ocfs2_extent_list *right_child_el)
  1680. {
  1681. u32 left_clusters, right_end;
  1682. /*
  1683. * Interior nodes never have holes. Their cpos is the cpos of
  1684. * the leftmost record in their child list. Their cluster
  1685. * count covers the full theoretical range of their child list
  1686. * - the range between their cpos and the cpos of the record
  1687. * immediately to their right.
  1688. */
  1689. left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
  1690. if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
  1691. BUG_ON(right_child_el->l_tree_depth);
  1692. BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
  1693. left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
  1694. }
  1695. left_clusters -= le32_to_cpu(left_rec->e_cpos);
  1696. left_rec->e_int_clusters = cpu_to_le32(left_clusters);
  1697. /*
  1698. * Calculate the rightmost cluster count boundary before
  1699. * moving cpos - we will need to adjust clusters after
  1700. * updating e_cpos to keep the same highest cluster count.
  1701. */
  1702. right_end = le32_to_cpu(right_rec->e_cpos);
  1703. right_end += le32_to_cpu(right_rec->e_int_clusters);
  1704. right_rec->e_cpos = left_rec->e_cpos;
  1705. le32_add_cpu(&right_rec->e_cpos, left_clusters);
  1706. right_end -= le32_to_cpu(right_rec->e_cpos);
  1707. right_rec->e_int_clusters = cpu_to_le32(right_end);
  1708. }
  1709. /*
  1710. * Adjust the adjacent root node records involved in a
  1711. * rotation. left_el_blkno is passed in as a key so that we can easily
  1712. * find it's index in the root list.
  1713. */
  1714. static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
  1715. struct ocfs2_extent_list *left_el,
  1716. struct ocfs2_extent_list *right_el,
  1717. u64 left_el_blkno)
  1718. {
  1719. int i;
  1720. BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
  1721. le16_to_cpu(left_el->l_tree_depth));
  1722. for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
  1723. if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
  1724. break;
  1725. }
  1726. /*
  1727. * The path walking code should have never returned a root and
  1728. * two paths which are not adjacent.
  1729. */
  1730. BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
  1731. ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
  1732. &root_el->l_recs[i + 1], right_el);
  1733. }
  1734. /*
  1735. * We've changed a leaf block (in right_path) and need to reflect that
  1736. * change back up the subtree.
  1737. *
  1738. * This happens in multiple places:
  1739. * - When we've moved an extent record from the left path leaf to the right
  1740. * path leaf to make room for an empty extent in the left path leaf.
  1741. * - When our insert into the right path leaf is at the leftmost edge
  1742. * and requires an update of the path immediately to it's left. This
  1743. * can occur at the end of some types of rotation and appending inserts.
  1744. * - When we've adjusted the last extent record in the left path leaf and the
  1745. * 1st extent record in the right path leaf during cross extent block merge.
  1746. */
  1747. static void ocfs2_complete_edge_insert(handle_t *handle,
  1748. struct ocfs2_path *left_path,
  1749. struct ocfs2_path *right_path,
  1750. int subtree_index)
  1751. {
  1752. int ret, i, idx;
  1753. struct ocfs2_extent_list *el, *left_el, *right_el;
  1754. struct ocfs2_extent_rec *left_rec, *right_rec;
  1755. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  1756. /*
  1757. * Update the counts and position values within all the
  1758. * interior nodes to reflect the leaf rotation we just did.
  1759. *
  1760. * The root node is handled below the loop.
  1761. *
  1762. * We begin the loop with right_el and left_el pointing to the
  1763. * leaf lists and work our way up.
  1764. *
  1765. * NOTE: within this loop, left_el and right_el always refer
  1766. * to the *child* lists.
  1767. */
  1768. left_el = path_leaf_el(left_path);
  1769. right_el = path_leaf_el(right_path);
  1770. for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
  1771. mlog(0, "Adjust records at index %u\n", i);
  1772. /*
  1773. * One nice property of knowing that all of these
  1774. * nodes are below the root is that we only deal with
  1775. * the leftmost right node record and the rightmost
  1776. * left node record.
  1777. */
  1778. el = left_path->p_node[i].el;
  1779. idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1780. left_rec = &el->l_recs[idx];
  1781. el = right_path->p_node[i].el;
  1782. right_rec = &el->l_recs[0];
  1783. ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
  1784. right_el);
  1785. ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
  1786. if (ret)
  1787. mlog_errno(ret);
  1788. ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
  1789. if (ret)
  1790. mlog_errno(ret);
  1791. /*
  1792. * Setup our list pointers now so that the current
  1793. * parents become children in the next iteration.
  1794. */
  1795. left_el = left_path->p_node[i].el;
  1796. right_el = right_path->p_node[i].el;
  1797. }
  1798. /*
  1799. * At the root node, adjust the two adjacent records which
  1800. * begin our path to the leaves.
  1801. */
  1802. el = left_path->p_node[subtree_index].el;
  1803. left_el = left_path->p_node[subtree_index + 1].el;
  1804. right_el = right_path->p_node[subtree_index + 1].el;
  1805. ocfs2_adjust_root_records(el, left_el, right_el,
  1806. left_path->p_node[subtree_index + 1].bh->b_blocknr);
  1807. root_bh = left_path->p_node[subtree_index].bh;
  1808. ret = ocfs2_journal_dirty(handle, root_bh);
  1809. if (ret)
  1810. mlog_errno(ret);
  1811. }
  1812. static int ocfs2_rotate_subtree_right(handle_t *handle,
  1813. struct ocfs2_extent_tree *et,
  1814. struct ocfs2_path *left_path,
  1815. struct ocfs2_path *right_path,
  1816. int subtree_index)
  1817. {
  1818. int ret, i;
  1819. struct buffer_head *right_leaf_bh;
  1820. struct buffer_head *left_leaf_bh = NULL;
  1821. struct buffer_head *root_bh;
  1822. struct ocfs2_extent_list *right_el, *left_el;
  1823. struct ocfs2_extent_rec move_rec;
  1824. left_leaf_bh = path_leaf_bh(left_path);
  1825. left_el = path_leaf_el(left_path);
  1826. if (left_el->l_next_free_rec != left_el->l_count) {
  1827. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  1828. "Inode %llu has non-full interior leaf node %llu"
  1829. "(next free = %u)",
  1830. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  1831. (unsigned long long)left_leaf_bh->b_blocknr,
  1832. le16_to_cpu(left_el->l_next_free_rec));
  1833. return -EROFS;
  1834. }
  1835. /*
  1836. * This extent block may already have an empty record, so we
  1837. * return early if so.
  1838. */
  1839. if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
  1840. return 0;
  1841. root_bh = left_path->p_node[subtree_index].bh;
  1842. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  1843. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  1844. subtree_index);
  1845. if (ret) {
  1846. mlog_errno(ret);
  1847. goto out;
  1848. }
  1849. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  1850. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  1851. right_path, i);
  1852. if (ret) {
  1853. mlog_errno(ret);
  1854. goto out;
  1855. }
  1856. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  1857. left_path, i);
  1858. if (ret) {
  1859. mlog_errno(ret);
  1860. goto out;
  1861. }
  1862. }
  1863. right_leaf_bh = path_leaf_bh(right_path);
  1864. right_el = path_leaf_el(right_path);
  1865. /* This is a code error, not a disk corruption. */
  1866. mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
  1867. "because rightmost leaf block %llu is empty\n",
  1868. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  1869. (unsigned long long)right_leaf_bh->b_blocknr);
  1870. ocfs2_create_empty_extent(right_el);
  1871. ret = ocfs2_journal_dirty(handle, right_leaf_bh);
  1872. if (ret) {
  1873. mlog_errno(ret);
  1874. goto out;
  1875. }
  1876. /* Do the copy now. */
  1877. i = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1878. move_rec = left_el->l_recs[i];
  1879. right_el->l_recs[0] = move_rec;
  1880. /*
  1881. * Clear out the record we just copied and shift everything
  1882. * over, leaving an empty extent in the left leaf.
  1883. *
  1884. * We temporarily subtract from next_free_rec so that the
  1885. * shift will lose the tail record (which is now defunct).
  1886. */
  1887. le16_add_cpu(&left_el->l_next_free_rec, -1);
  1888. ocfs2_shift_records_right(left_el);
  1889. memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1890. le16_add_cpu(&left_el->l_next_free_rec, 1);
  1891. ret = ocfs2_journal_dirty(handle, left_leaf_bh);
  1892. if (ret) {
  1893. mlog_errno(ret);
  1894. goto out;
  1895. }
  1896. ocfs2_complete_edge_insert(handle, left_path, right_path,
  1897. subtree_index);
  1898. out:
  1899. return ret;
  1900. }
  1901. /*
  1902. * Given a full path, determine what cpos value would return us a path
  1903. * containing the leaf immediately to the left of the current one.
  1904. *
  1905. * Will return zero if the path passed in is already the leftmost path.
  1906. */
  1907. static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
  1908. struct ocfs2_path *path, u32 *cpos)
  1909. {
  1910. int i, j, ret = 0;
  1911. u64 blkno;
  1912. struct ocfs2_extent_list *el;
  1913. BUG_ON(path->p_tree_depth == 0);
  1914. *cpos = 0;
  1915. blkno = path_leaf_bh(path)->b_blocknr;
  1916. /* Start at the tree node just above the leaf and work our way up. */
  1917. i = path->p_tree_depth - 1;
  1918. while (i >= 0) {
  1919. el = path->p_node[i].el;
  1920. /*
  1921. * Find the extent record just before the one in our
  1922. * path.
  1923. */
  1924. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  1925. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  1926. if (j == 0) {
  1927. if (i == 0) {
  1928. /*
  1929. * We've determined that the
  1930. * path specified is already
  1931. * the leftmost one - return a
  1932. * cpos of zero.
  1933. */
  1934. goto out;
  1935. }
  1936. /*
  1937. * The leftmost record points to our
  1938. * leaf - we need to travel up the
  1939. * tree one level.
  1940. */
  1941. goto next_node;
  1942. }
  1943. *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
  1944. *cpos = *cpos + ocfs2_rec_clusters(el,
  1945. &el->l_recs[j - 1]);
  1946. *cpos = *cpos - 1;
  1947. goto out;
  1948. }
  1949. }
  1950. /*
  1951. * If we got here, we never found a valid node where
  1952. * the tree indicated one should be.
  1953. */
  1954. ocfs2_error(sb,
  1955. "Invalid extent tree at extent block %llu\n",
  1956. (unsigned long long)blkno);
  1957. ret = -EROFS;
  1958. goto out;
  1959. next_node:
  1960. blkno = path->p_node[i].bh->b_blocknr;
  1961. i--;
  1962. }
  1963. out:
  1964. return ret;
  1965. }
  1966. /*
  1967. * Extend the transaction by enough credits to complete the rotation,
  1968. * and still leave at least the original number of credits allocated
  1969. * to this transaction.
  1970. */
  1971. static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
  1972. int op_credits,
  1973. struct ocfs2_path *path)
  1974. {
  1975. int ret;
  1976. int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
  1977. if (handle->h_buffer_credits < credits) {
  1978. ret = ocfs2_extend_trans(handle,
  1979. credits - handle->h_buffer_credits);
  1980. if (ret)
  1981. return ret;
  1982. if (unlikely(handle->h_buffer_credits < credits))
  1983. return ocfs2_extend_trans(handle, credits);
  1984. }
  1985. return 0;
  1986. }
  1987. /*
  1988. * Trap the case where we're inserting into the theoretical range past
  1989. * the _actual_ left leaf range. Otherwise, we'll rotate a record
  1990. * whose cpos is less than ours into the right leaf.
  1991. *
  1992. * It's only necessary to look at the rightmost record of the left
  1993. * leaf because the logic that calls us should ensure that the
  1994. * theoretical ranges in the path components above the leaves are
  1995. * correct.
  1996. */
  1997. static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
  1998. u32 insert_cpos)
  1999. {
  2000. struct ocfs2_extent_list *left_el;
  2001. struct ocfs2_extent_rec *rec;
  2002. int next_free;
  2003. left_el = path_leaf_el(left_path);
  2004. next_free = le16_to_cpu(left_el->l_next_free_rec);
  2005. rec = &left_el->l_recs[next_free - 1];
  2006. if (insert_cpos > le32_to_cpu(rec->e_cpos))
  2007. return 1;
  2008. return 0;
  2009. }
  2010. static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
  2011. {
  2012. int next_free = le16_to_cpu(el->l_next_free_rec);
  2013. unsigned int range;
  2014. struct ocfs2_extent_rec *rec;
  2015. if (next_free == 0)
  2016. return 0;
  2017. rec = &el->l_recs[0];
  2018. if (ocfs2_is_empty_extent(rec)) {
  2019. /* Empty list. */
  2020. if (next_free == 1)
  2021. return 0;
  2022. rec = &el->l_recs[1];
  2023. }
  2024. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  2025. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  2026. return 1;
  2027. return 0;
  2028. }
  2029. /*
  2030. * Rotate all the records in a btree right one record, starting at insert_cpos.
  2031. *
  2032. * The path to the rightmost leaf should be passed in.
  2033. *
  2034. * The array is assumed to be large enough to hold an entire path (tree depth).
  2035. *
  2036. * Upon successful return from this function:
  2037. *
  2038. * - The 'right_path' array will contain a path to the leaf block
  2039. * whose range contains e_cpos.
  2040. * - That leaf block will have a single empty extent in list index 0.
  2041. * - In the case that the rotation requires a post-insert update,
  2042. * *ret_left_path will contain a valid path which can be passed to
  2043. * ocfs2_insert_path().
  2044. */
  2045. static int ocfs2_rotate_tree_right(handle_t *handle,
  2046. struct ocfs2_extent_tree *et,
  2047. enum ocfs2_split_type split,
  2048. u32 insert_cpos,
  2049. struct ocfs2_path *right_path,
  2050. struct ocfs2_path **ret_left_path)
  2051. {
  2052. int ret, start, orig_credits = handle->h_buffer_credits;
  2053. u32 cpos;
  2054. struct ocfs2_path *left_path = NULL;
  2055. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  2056. *ret_left_path = NULL;
  2057. left_path = ocfs2_new_path_from_path(right_path);
  2058. if (!left_path) {
  2059. ret = -ENOMEM;
  2060. mlog_errno(ret);
  2061. goto out;
  2062. }
  2063. ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
  2064. if (ret) {
  2065. mlog_errno(ret);
  2066. goto out;
  2067. }
  2068. mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
  2069. /*
  2070. * What we want to do here is:
  2071. *
  2072. * 1) Start with the rightmost path.
  2073. *
  2074. * 2) Determine a path to the leaf block directly to the left
  2075. * of that leaf.
  2076. *
  2077. * 3) Determine the 'subtree root' - the lowest level tree node
  2078. * which contains a path to both leaves.
  2079. *
  2080. * 4) Rotate the subtree.
  2081. *
  2082. * 5) Find the next subtree by considering the left path to be
  2083. * the new right path.
  2084. *
  2085. * The check at the top of this while loop also accepts
  2086. * insert_cpos == cpos because cpos is only a _theoretical_
  2087. * value to get us the left path - insert_cpos might very well
  2088. * be filling that hole.
  2089. *
  2090. * Stop at a cpos of '0' because we either started at the
  2091. * leftmost branch (i.e., a tree with one branch and a
  2092. * rotation inside of it), or we've gone as far as we can in
  2093. * rotating subtrees.
  2094. */
  2095. while (cpos && insert_cpos <= cpos) {
  2096. mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
  2097. insert_cpos, cpos);
  2098. ret = ocfs2_find_path(et->et_ci, left_path, cpos);
  2099. if (ret) {
  2100. mlog_errno(ret);
  2101. goto out;
  2102. }
  2103. mlog_bug_on_msg(path_leaf_bh(left_path) ==
  2104. path_leaf_bh(right_path),
  2105. "Owner %llu: error during insert of %u "
  2106. "(left path cpos %u) results in two identical "
  2107. "paths ending at %llu\n",
  2108. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2109. insert_cpos, cpos,
  2110. (unsigned long long)
  2111. path_leaf_bh(left_path)->b_blocknr);
  2112. if (split == SPLIT_NONE &&
  2113. ocfs2_rotate_requires_path_adjustment(left_path,
  2114. insert_cpos)) {
  2115. /*
  2116. * We've rotated the tree as much as we
  2117. * should. The rest is up to
  2118. * ocfs2_insert_path() to complete, after the
  2119. * record insertion. We indicate this
  2120. * situation by returning the left path.
  2121. *
  2122. * The reason we don't adjust the records here
  2123. * before the record insert is that an error
  2124. * later might break the rule where a parent
  2125. * record e_cpos will reflect the actual
  2126. * e_cpos of the 1st nonempty record of the
  2127. * child list.
  2128. */
  2129. *ret_left_path = left_path;
  2130. goto out_ret_path;
  2131. }
  2132. start = ocfs2_find_subtree_root(et, left_path, right_path);
  2133. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  2134. start,
  2135. (unsigned long long) right_path->p_node[start].bh->b_blocknr,
  2136. right_path->p_tree_depth);
  2137. ret = ocfs2_extend_rotate_transaction(handle, start,
  2138. orig_credits, right_path);
  2139. if (ret) {
  2140. mlog_errno(ret);
  2141. goto out;
  2142. }
  2143. ret = ocfs2_rotate_subtree_right(handle, et, left_path,
  2144. right_path, start);
  2145. if (ret) {
  2146. mlog_errno(ret);
  2147. goto out;
  2148. }
  2149. if (split != SPLIT_NONE &&
  2150. ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
  2151. insert_cpos)) {
  2152. /*
  2153. * A rotate moves the rightmost left leaf
  2154. * record over to the leftmost right leaf
  2155. * slot. If we're doing an extent split
  2156. * instead of a real insert, then we have to
  2157. * check that the extent to be split wasn't
  2158. * just moved over. If it was, then we can
  2159. * exit here, passing left_path back -
  2160. * ocfs2_split_extent() is smart enough to
  2161. * search both leaves.
  2162. */
  2163. *ret_left_path = left_path;
  2164. goto out_ret_path;
  2165. }
  2166. /*
  2167. * There is no need to re-read the next right path
  2168. * as we know that it'll be our current left
  2169. * path. Optimize by copying values instead.
  2170. */
  2171. ocfs2_mv_path(right_path, left_path);
  2172. ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
  2173. if (ret) {
  2174. mlog_errno(ret);
  2175. goto out;
  2176. }
  2177. }
  2178. out:
  2179. ocfs2_free_path(left_path);
  2180. out_ret_path:
  2181. return ret;
  2182. }
  2183. static int ocfs2_update_edge_lengths(handle_t *handle,
  2184. struct ocfs2_extent_tree *et,
  2185. int subtree_index, struct ocfs2_path *path)
  2186. {
  2187. int i, idx, ret;
  2188. struct ocfs2_extent_rec *rec;
  2189. struct ocfs2_extent_list *el;
  2190. struct ocfs2_extent_block *eb;
  2191. u32 range;
  2192. /*
  2193. * In normal tree rotation process, we will never touch the
  2194. * tree branch above subtree_index and ocfs2_extend_rotate_transaction
  2195. * doesn't reserve the credits for them either.
  2196. *
  2197. * But we do have a special case here which will update the rightmost
  2198. * records for all the bh in the path.
  2199. * So we have to allocate extra credits and access them.
  2200. */
  2201. ret = ocfs2_extend_trans(handle,
  2202. handle->h_buffer_credits + subtree_index);
  2203. if (ret) {
  2204. mlog_errno(ret);
  2205. goto out;
  2206. }
  2207. ret = ocfs2_journal_access_path(et->et_ci, handle, path);
  2208. if (ret) {
  2209. mlog_errno(ret);
  2210. goto out;
  2211. }
  2212. /* Path should always be rightmost. */
  2213. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  2214. BUG_ON(eb->h_next_leaf_blk != 0ULL);
  2215. el = &eb->h_list;
  2216. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  2217. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  2218. rec = &el->l_recs[idx];
  2219. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  2220. for (i = 0; i < path->p_tree_depth; i++) {
  2221. el = path->p_node[i].el;
  2222. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  2223. rec = &el->l_recs[idx];
  2224. rec->e_int_clusters = cpu_to_le32(range);
  2225. le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
  2226. ocfs2_journal_dirty(handle, path->p_node[i].bh);
  2227. }
  2228. out:
  2229. return ret;
  2230. }
  2231. static void ocfs2_unlink_path(handle_t *handle,
  2232. struct ocfs2_extent_tree *et,
  2233. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2234. struct ocfs2_path *path, int unlink_start)
  2235. {
  2236. int ret, i;
  2237. struct ocfs2_extent_block *eb;
  2238. struct ocfs2_extent_list *el;
  2239. struct buffer_head *bh;
  2240. for(i = unlink_start; i < path_num_items(path); i++) {
  2241. bh = path->p_node[i].bh;
  2242. eb = (struct ocfs2_extent_block *)bh->b_data;
  2243. /*
  2244. * Not all nodes might have had their final count
  2245. * decremented by the caller - handle this here.
  2246. */
  2247. el = &eb->h_list;
  2248. if (le16_to_cpu(el->l_next_free_rec) > 1) {
  2249. mlog(ML_ERROR,
  2250. "Inode %llu, attempted to remove extent block "
  2251. "%llu with %u records\n",
  2252. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2253. (unsigned long long)le64_to_cpu(eb->h_blkno),
  2254. le16_to_cpu(el->l_next_free_rec));
  2255. ocfs2_journal_dirty(handle, bh);
  2256. ocfs2_remove_from_cache(et->et_ci, bh);
  2257. continue;
  2258. }
  2259. el->l_next_free_rec = 0;
  2260. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2261. ocfs2_journal_dirty(handle, bh);
  2262. ret = ocfs2_cache_extent_block_free(dealloc, eb);
  2263. if (ret)
  2264. mlog_errno(ret);
  2265. ocfs2_remove_from_cache(et->et_ci, bh);
  2266. }
  2267. }
  2268. static void ocfs2_unlink_subtree(handle_t *handle,
  2269. struct ocfs2_extent_tree *et,
  2270. struct ocfs2_path *left_path,
  2271. struct ocfs2_path *right_path,
  2272. int subtree_index,
  2273. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2274. {
  2275. int i;
  2276. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  2277. struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
  2278. struct ocfs2_extent_list *el;
  2279. struct ocfs2_extent_block *eb;
  2280. el = path_leaf_el(left_path);
  2281. eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
  2282. for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  2283. if (root_el->l_recs[i].e_blkno == eb->h_blkno)
  2284. break;
  2285. BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
  2286. memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  2287. le16_add_cpu(&root_el->l_next_free_rec, -1);
  2288. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2289. eb->h_next_leaf_blk = 0;
  2290. ocfs2_journal_dirty(handle, root_bh);
  2291. ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  2292. ocfs2_unlink_path(handle, et, dealloc, right_path,
  2293. subtree_index + 1);
  2294. }
  2295. static int ocfs2_rotate_subtree_left(handle_t *handle,
  2296. struct ocfs2_extent_tree *et,
  2297. struct ocfs2_path *left_path,
  2298. struct ocfs2_path *right_path,
  2299. int subtree_index,
  2300. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2301. int *deleted)
  2302. {
  2303. int ret, i, del_right_subtree = 0, right_has_empty = 0;
  2304. struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
  2305. struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
  2306. struct ocfs2_extent_block *eb;
  2307. *deleted = 0;
  2308. right_leaf_el = path_leaf_el(right_path);
  2309. left_leaf_el = path_leaf_el(left_path);
  2310. root_bh = left_path->p_node[subtree_index].bh;
  2311. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  2312. if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
  2313. return 0;
  2314. eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
  2315. if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
  2316. /*
  2317. * It's legal for us to proceed if the right leaf is
  2318. * the rightmost one and it has an empty extent. There
  2319. * are two cases to handle - whether the leaf will be
  2320. * empty after removal or not. If the leaf isn't empty
  2321. * then just remove the empty extent up front. The
  2322. * next block will handle empty leaves by flagging
  2323. * them for unlink.
  2324. *
  2325. * Non rightmost leaves will throw -EAGAIN and the
  2326. * caller can manually move the subtree and retry.
  2327. */
  2328. if (eb->h_next_leaf_blk != 0ULL)
  2329. return -EAGAIN;
  2330. if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
  2331. ret = ocfs2_journal_access_eb(handle, et->et_ci,
  2332. path_leaf_bh(right_path),
  2333. OCFS2_JOURNAL_ACCESS_WRITE);
  2334. if (ret) {
  2335. mlog_errno(ret);
  2336. goto out;
  2337. }
  2338. ocfs2_remove_empty_extent(right_leaf_el);
  2339. } else
  2340. right_has_empty = 1;
  2341. }
  2342. if (eb->h_next_leaf_blk == 0ULL &&
  2343. le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
  2344. /*
  2345. * We have to update i_last_eb_blk during the meta
  2346. * data delete.
  2347. */
  2348. ret = ocfs2_et_root_journal_access(handle, et,
  2349. OCFS2_JOURNAL_ACCESS_WRITE);
  2350. if (ret) {
  2351. mlog_errno(ret);
  2352. goto out;
  2353. }
  2354. del_right_subtree = 1;
  2355. }
  2356. /*
  2357. * Getting here with an empty extent in the right path implies
  2358. * that it's the rightmost path and will be deleted.
  2359. */
  2360. BUG_ON(right_has_empty && !del_right_subtree);
  2361. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  2362. subtree_index);
  2363. if (ret) {
  2364. mlog_errno(ret);
  2365. goto out;
  2366. }
  2367. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  2368. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2369. right_path, i);
  2370. if (ret) {
  2371. mlog_errno(ret);
  2372. goto out;
  2373. }
  2374. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2375. left_path, i);
  2376. if (ret) {
  2377. mlog_errno(ret);
  2378. goto out;
  2379. }
  2380. }
  2381. if (!right_has_empty) {
  2382. /*
  2383. * Only do this if we're moving a real
  2384. * record. Otherwise, the action is delayed until
  2385. * after removal of the right path in which case we
  2386. * can do a simple shift to remove the empty extent.
  2387. */
  2388. ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
  2389. memset(&right_leaf_el->l_recs[0], 0,
  2390. sizeof(struct ocfs2_extent_rec));
  2391. }
  2392. if (eb->h_next_leaf_blk == 0ULL) {
  2393. /*
  2394. * Move recs over to get rid of empty extent, decrease
  2395. * next_free. This is allowed to remove the last
  2396. * extent in our leaf (setting l_next_free_rec to
  2397. * zero) - the delete code below won't care.
  2398. */
  2399. ocfs2_remove_empty_extent(right_leaf_el);
  2400. }
  2401. ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  2402. if (ret)
  2403. mlog_errno(ret);
  2404. ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
  2405. if (ret)
  2406. mlog_errno(ret);
  2407. if (del_right_subtree) {
  2408. ocfs2_unlink_subtree(handle, et, left_path, right_path,
  2409. subtree_index, dealloc);
  2410. ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
  2411. left_path);
  2412. if (ret) {
  2413. mlog_errno(ret);
  2414. goto out;
  2415. }
  2416. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2417. ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
  2418. /*
  2419. * Removal of the extent in the left leaf was skipped
  2420. * above so we could delete the right path
  2421. * 1st.
  2422. */
  2423. if (right_has_empty)
  2424. ocfs2_remove_empty_extent(left_leaf_el);
  2425. ret = ocfs2_journal_dirty(handle, et_root_bh);
  2426. if (ret)
  2427. mlog_errno(ret);
  2428. *deleted = 1;
  2429. } else
  2430. ocfs2_complete_edge_insert(handle, left_path, right_path,
  2431. subtree_index);
  2432. out:
  2433. return ret;
  2434. }
  2435. /*
  2436. * Given a full path, determine what cpos value would return us a path
  2437. * containing the leaf immediately to the right of the current one.
  2438. *
  2439. * Will return zero if the path passed in is already the rightmost path.
  2440. *
  2441. * This looks similar, but is subtly different to
  2442. * ocfs2_find_cpos_for_left_leaf().
  2443. */
  2444. int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
  2445. struct ocfs2_path *path, u32 *cpos)
  2446. {
  2447. int i, j, ret = 0;
  2448. u64 blkno;
  2449. struct ocfs2_extent_list *el;
  2450. *cpos = 0;
  2451. if (path->p_tree_depth == 0)
  2452. return 0;
  2453. blkno = path_leaf_bh(path)->b_blocknr;
  2454. /* Start at the tree node just above the leaf and work our way up. */
  2455. i = path->p_tree_depth - 1;
  2456. while (i >= 0) {
  2457. int next_free;
  2458. el = path->p_node[i].el;
  2459. /*
  2460. * Find the extent record just after the one in our
  2461. * path.
  2462. */
  2463. next_free = le16_to_cpu(el->l_next_free_rec);
  2464. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  2465. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  2466. if (j == (next_free - 1)) {
  2467. if (i == 0) {
  2468. /*
  2469. * We've determined that the
  2470. * path specified is already
  2471. * the rightmost one - return a
  2472. * cpos of zero.
  2473. */
  2474. goto out;
  2475. }
  2476. /*
  2477. * The rightmost record points to our
  2478. * leaf - we need to travel up the
  2479. * tree one level.
  2480. */
  2481. goto next_node;
  2482. }
  2483. *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
  2484. goto out;
  2485. }
  2486. }
  2487. /*
  2488. * If we got here, we never found a valid node where
  2489. * the tree indicated one should be.
  2490. */
  2491. ocfs2_error(sb,
  2492. "Invalid extent tree at extent block %llu\n",
  2493. (unsigned long long)blkno);
  2494. ret = -EROFS;
  2495. goto out;
  2496. next_node:
  2497. blkno = path->p_node[i].bh->b_blocknr;
  2498. i--;
  2499. }
  2500. out:
  2501. return ret;
  2502. }
  2503. static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
  2504. struct ocfs2_extent_tree *et,
  2505. struct ocfs2_path *path)
  2506. {
  2507. int ret;
  2508. struct buffer_head *bh = path_leaf_bh(path);
  2509. struct ocfs2_extent_list *el = path_leaf_el(path);
  2510. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  2511. return 0;
  2512. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
  2513. path_num_items(path) - 1);
  2514. if (ret) {
  2515. mlog_errno(ret);
  2516. goto out;
  2517. }
  2518. ocfs2_remove_empty_extent(el);
  2519. ret = ocfs2_journal_dirty(handle, bh);
  2520. if (ret)
  2521. mlog_errno(ret);
  2522. out:
  2523. return ret;
  2524. }
  2525. static int __ocfs2_rotate_tree_left(handle_t *handle,
  2526. struct ocfs2_extent_tree *et,
  2527. int orig_credits,
  2528. struct ocfs2_path *path,
  2529. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2530. struct ocfs2_path **empty_extent_path)
  2531. {
  2532. int ret, subtree_root, deleted;
  2533. u32 right_cpos;
  2534. struct ocfs2_path *left_path = NULL;
  2535. struct ocfs2_path *right_path = NULL;
  2536. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  2537. BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
  2538. *empty_extent_path = NULL;
  2539. ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
  2540. if (ret) {
  2541. mlog_errno(ret);
  2542. goto out;
  2543. }
  2544. left_path = ocfs2_new_path_from_path(path);
  2545. if (!left_path) {
  2546. ret = -ENOMEM;
  2547. mlog_errno(ret);
  2548. goto out;
  2549. }
  2550. ocfs2_cp_path(left_path, path);
  2551. right_path = ocfs2_new_path_from_path(path);
  2552. if (!right_path) {
  2553. ret = -ENOMEM;
  2554. mlog_errno(ret);
  2555. goto out;
  2556. }
  2557. while (right_cpos) {
  2558. ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
  2559. if (ret) {
  2560. mlog_errno(ret);
  2561. goto out;
  2562. }
  2563. subtree_root = ocfs2_find_subtree_root(et, left_path,
  2564. right_path);
  2565. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  2566. subtree_root,
  2567. (unsigned long long)
  2568. right_path->p_node[subtree_root].bh->b_blocknr,
  2569. right_path->p_tree_depth);
  2570. ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
  2571. orig_credits, left_path);
  2572. if (ret) {
  2573. mlog_errno(ret);
  2574. goto out;
  2575. }
  2576. /*
  2577. * Caller might still want to make changes to the
  2578. * tree root, so re-add it to the journal here.
  2579. */
  2580. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2581. left_path, 0);
  2582. if (ret) {
  2583. mlog_errno(ret);
  2584. goto out;
  2585. }
  2586. ret = ocfs2_rotate_subtree_left(handle, et, left_path,
  2587. right_path, subtree_root,
  2588. dealloc, &deleted);
  2589. if (ret == -EAGAIN) {
  2590. /*
  2591. * The rotation has to temporarily stop due to
  2592. * the right subtree having an empty
  2593. * extent. Pass it back to the caller for a
  2594. * fixup.
  2595. */
  2596. *empty_extent_path = right_path;
  2597. right_path = NULL;
  2598. goto out;
  2599. }
  2600. if (ret) {
  2601. mlog_errno(ret);
  2602. goto out;
  2603. }
  2604. /*
  2605. * The subtree rotate might have removed records on
  2606. * the rightmost edge. If so, then rotation is
  2607. * complete.
  2608. */
  2609. if (deleted)
  2610. break;
  2611. ocfs2_mv_path(left_path, right_path);
  2612. ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
  2613. &right_cpos);
  2614. if (ret) {
  2615. mlog_errno(ret);
  2616. goto out;
  2617. }
  2618. }
  2619. out:
  2620. ocfs2_free_path(right_path);
  2621. ocfs2_free_path(left_path);
  2622. return ret;
  2623. }
  2624. static int ocfs2_remove_rightmost_path(handle_t *handle,
  2625. struct ocfs2_extent_tree *et,
  2626. struct ocfs2_path *path,
  2627. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2628. {
  2629. int ret, subtree_index;
  2630. u32 cpos;
  2631. struct ocfs2_path *left_path = NULL;
  2632. struct ocfs2_extent_block *eb;
  2633. struct ocfs2_extent_list *el;
  2634. ret = ocfs2_et_sanity_check(et);
  2635. if (ret)
  2636. goto out;
  2637. /*
  2638. * There's two ways we handle this depending on
  2639. * whether path is the only existing one.
  2640. */
  2641. ret = ocfs2_extend_rotate_transaction(handle, 0,
  2642. handle->h_buffer_credits,
  2643. path);
  2644. if (ret) {
  2645. mlog_errno(ret);
  2646. goto out;
  2647. }
  2648. ret = ocfs2_journal_access_path(et->et_ci, handle, path);
  2649. if (ret) {
  2650. mlog_errno(ret);
  2651. goto out;
  2652. }
  2653. ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  2654. path, &cpos);
  2655. if (ret) {
  2656. mlog_errno(ret);
  2657. goto out;
  2658. }
  2659. if (cpos) {
  2660. /*
  2661. * We have a path to the left of this one - it needs
  2662. * an update too.
  2663. */
  2664. left_path = ocfs2_new_path_from_path(path);
  2665. if (!left_path) {
  2666. ret = -ENOMEM;
  2667. mlog_errno(ret);
  2668. goto out;
  2669. }
  2670. ret = ocfs2_find_path(et->et_ci, left_path, cpos);
  2671. if (ret) {
  2672. mlog_errno(ret);
  2673. goto out;
  2674. }
  2675. ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
  2676. if (ret) {
  2677. mlog_errno(ret);
  2678. goto out;
  2679. }
  2680. subtree_index = ocfs2_find_subtree_root(et, left_path, path);
  2681. ocfs2_unlink_subtree(handle, et, left_path, path,
  2682. subtree_index, dealloc);
  2683. ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
  2684. left_path);
  2685. if (ret) {
  2686. mlog_errno(ret);
  2687. goto out;
  2688. }
  2689. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2690. ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
  2691. } else {
  2692. /*
  2693. * 'path' is also the leftmost path which
  2694. * means it must be the only one. This gets
  2695. * handled differently because we want to
  2696. * revert the root back to having extents
  2697. * in-line.
  2698. */
  2699. ocfs2_unlink_path(handle, et, dealloc, path, 1);
  2700. el = et->et_root_el;
  2701. el->l_tree_depth = 0;
  2702. el->l_next_free_rec = 0;
  2703. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2704. ocfs2_et_set_last_eb_blk(et, 0);
  2705. }
  2706. ocfs2_journal_dirty(handle, path_root_bh(path));
  2707. out:
  2708. ocfs2_free_path(left_path);
  2709. return ret;
  2710. }
  2711. /*
  2712. * Left rotation of btree records.
  2713. *
  2714. * In many ways, this is (unsurprisingly) the opposite of right
  2715. * rotation. We start at some non-rightmost path containing an empty
  2716. * extent in the leaf block. The code works its way to the rightmost
  2717. * path by rotating records to the left in every subtree.
  2718. *
  2719. * This is used by any code which reduces the number of extent records
  2720. * in a leaf. After removal, an empty record should be placed in the
  2721. * leftmost list position.
  2722. *
  2723. * This won't handle a length update of the rightmost path records if
  2724. * the rightmost tree leaf record is removed so the caller is
  2725. * responsible for detecting and correcting that.
  2726. */
  2727. static int ocfs2_rotate_tree_left(handle_t *handle,
  2728. struct ocfs2_extent_tree *et,
  2729. struct ocfs2_path *path,
  2730. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2731. {
  2732. int ret, orig_credits = handle->h_buffer_credits;
  2733. struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
  2734. struct ocfs2_extent_block *eb;
  2735. struct ocfs2_extent_list *el;
  2736. el = path_leaf_el(path);
  2737. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  2738. return 0;
  2739. if (path->p_tree_depth == 0) {
  2740. rightmost_no_delete:
  2741. /*
  2742. * Inline extents. This is trivially handled, so do
  2743. * it up front.
  2744. */
  2745. ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
  2746. if (ret)
  2747. mlog_errno(ret);
  2748. goto out;
  2749. }
  2750. /*
  2751. * Handle rightmost branch now. There's several cases:
  2752. * 1) simple rotation leaving records in there. That's trivial.
  2753. * 2) rotation requiring a branch delete - there's no more
  2754. * records left. Two cases of this:
  2755. * a) There are branches to the left.
  2756. * b) This is also the leftmost (the only) branch.
  2757. *
  2758. * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
  2759. * 2a) we need the left branch so that we can update it with the unlink
  2760. * 2b) we need to bring the root back to inline extents.
  2761. */
  2762. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  2763. el = &eb->h_list;
  2764. if (eb->h_next_leaf_blk == 0) {
  2765. /*
  2766. * This gets a bit tricky if we're going to delete the
  2767. * rightmost path. Get the other cases out of the way
  2768. * 1st.
  2769. */
  2770. if (le16_to_cpu(el->l_next_free_rec) > 1)
  2771. goto rightmost_no_delete;
  2772. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  2773. ret = -EIO;
  2774. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  2775. "Owner %llu has empty extent block at %llu",
  2776. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2777. (unsigned long long)le64_to_cpu(eb->h_blkno));
  2778. goto out;
  2779. }
  2780. /*
  2781. * XXX: The caller can not trust "path" any more after
  2782. * this as it will have been deleted. What do we do?
  2783. *
  2784. * In theory the rotate-for-merge code will never get
  2785. * here because it'll always ask for a rotate in a
  2786. * nonempty list.
  2787. */
  2788. ret = ocfs2_remove_rightmost_path(handle, et, path,
  2789. dealloc);
  2790. if (ret)
  2791. mlog_errno(ret);
  2792. goto out;
  2793. }
  2794. /*
  2795. * Now we can loop, remembering the path we get from -EAGAIN
  2796. * and restarting from there.
  2797. */
  2798. try_rotate:
  2799. ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
  2800. dealloc, &restart_path);
  2801. if (ret && ret != -EAGAIN) {
  2802. mlog_errno(ret);
  2803. goto out;
  2804. }
  2805. while (ret == -EAGAIN) {
  2806. tmp_path = restart_path;
  2807. restart_path = NULL;
  2808. ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
  2809. tmp_path, dealloc,
  2810. &restart_path);
  2811. if (ret && ret != -EAGAIN) {
  2812. mlog_errno(ret);
  2813. goto out;
  2814. }
  2815. ocfs2_free_path(tmp_path);
  2816. tmp_path = NULL;
  2817. if (ret == 0)
  2818. goto try_rotate;
  2819. }
  2820. out:
  2821. ocfs2_free_path(tmp_path);
  2822. ocfs2_free_path(restart_path);
  2823. return ret;
  2824. }
  2825. static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
  2826. int index)
  2827. {
  2828. struct ocfs2_extent_rec *rec = &el->l_recs[index];
  2829. unsigned int size;
  2830. if (rec->e_leaf_clusters == 0) {
  2831. /*
  2832. * We consumed all of the merged-from record. An empty
  2833. * extent cannot exist anywhere but the 1st array
  2834. * position, so move things over if the merged-from
  2835. * record doesn't occupy that position.
  2836. *
  2837. * This creates a new empty extent so the caller
  2838. * should be smart enough to have removed any existing
  2839. * ones.
  2840. */
  2841. if (index > 0) {
  2842. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  2843. size = index * sizeof(struct ocfs2_extent_rec);
  2844. memmove(&el->l_recs[1], &el->l_recs[0], size);
  2845. }
  2846. /*
  2847. * Always memset - the caller doesn't check whether it
  2848. * created an empty extent, so there could be junk in
  2849. * the other fields.
  2850. */
  2851. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2852. }
  2853. }
  2854. static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
  2855. struct ocfs2_path *left_path,
  2856. struct ocfs2_path **ret_right_path)
  2857. {
  2858. int ret;
  2859. u32 right_cpos;
  2860. struct ocfs2_path *right_path = NULL;
  2861. struct ocfs2_extent_list *left_el;
  2862. *ret_right_path = NULL;
  2863. /* This function shouldn't be called for non-trees. */
  2864. BUG_ON(left_path->p_tree_depth == 0);
  2865. left_el = path_leaf_el(left_path);
  2866. BUG_ON(left_el->l_next_free_rec != left_el->l_count);
  2867. ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  2868. left_path, &right_cpos);
  2869. if (ret) {
  2870. mlog_errno(ret);
  2871. goto out;
  2872. }
  2873. /* This function shouldn't be called for the rightmost leaf. */
  2874. BUG_ON(right_cpos == 0);
  2875. right_path = ocfs2_new_path_from_path(left_path);
  2876. if (!right_path) {
  2877. ret = -ENOMEM;
  2878. mlog_errno(ret);
  2879. goto out;
  2880. }
  2881. ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
  2882. if (ret) {
  2883. mlog_errno(ret);
  2884. goto out;
  2885. }
  2886. *ret_right_path = right_path;
  2887. out:
  2888. if (ret)
  2889. ocfs2_free_path(right_path);
  2890. return ret;
  2891. }
  2892. /*
  2893. * Remove split_rec clusters from the record at index and merge them
  2894. * onto the beginning of the record "next" to it.
  2895. * For index < l_count - 1, the next means the extent rec at index + 1.
  2896. * For index == l_count - 1, the "next" means the 1st extent rec of the
  2897. * next extent block.
  2898. */
  2899. static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
  2900. handle_t *handle,
  2901. struct ocfs2_extent_tree *et,
  2902. struct ocfs2_extent_rec *split_rec,
  2903. int index)
  2904. {
  2905. int ret, next_free, i;
  2906. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  2907. struct ocfs2_extent_rec *left_rec;
  2908. struct ocfs2_extent_rec *right_rec;
  2909. struct ocfs2_extent_list *right_el;
  2910. struct ocfs2_path *right_path = NULL;
  2911. int subtree_index = 0;
  2912. struct ocfs2_extent_list *el = path_leaf_el(left_path);
  2913. struct buffer_head *bh = path_leaf_bh(left_path);
  2914. struct buffer_head *root_bh = NULL;
  2915. BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
  2916. left_rec = &el->l_recs[index];
  2917. if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
  2918. le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
  2919. /* we meet with a cross extent block merge. */
  2920. ret = ocfs2_get_right_path(et, left_path, &right_path);
  2921. if (ret) {
  2922. mlog_errno(ret);
  2923. goto out;
  2924. }
  2925. right_el = path_leaf_el(right_path);
  2926. next_free = le16_to_cpu(right_el->l_next_free_rec);
  2927. BUG_ON(next_free <= 0);
  2928. right_rec = &right_el->l_recs[0];
  2929. if (ocfs2_is_empty_extent(right_rec)) {
  2930. BUG_ON(next_free <= 1);
  2931. right_rec = &right_el->l_recs[1];
  2932. }
  2933. BUG_ON(le32_to_cpu(left_rec->e_cpos) +
  2934. le16_to_cpu(left_rec->e_leaf_clusters) !=
  2935. le32_to_cpu(right_rec->e_cpos));
  2936. subtree_index = ocfs2_find_subtree_root(et, left_path,
  2937. right_path);
  2938. ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
  2939. handle->h_buffer_credits,
  2940. right_path);
  2941. if (ret) {
  2942. mlog_errno(ret);
  2943. goto out;
  2944. }
  2945. root_bh = left_path->p_node[subtree_index].bh;
  2946. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  2947. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  2948. subtree_index);
  2949. if (ret) {
  2950. mlog_errno(ret);
  2951. goto out;
  2952. }
  2953. for (i = subtree_index + 1;
  2954. i < path_num_items(right_path); i++) {
  2955. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2956. right_path, i);
  2957. if (ret) {
  2958. mlog_errno(ret);
  2959. goto out;
  2960. }
  2961. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2962. left_path, i);
  2963. if (ret) {
  2964. mlog_errno(ret);
  2965. goto out;
  2966. }
  2967. }
  2968. } else {
  2969. BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
  2970. right_rec = &el->l_recs[index + 1];
  2971. }
  2972. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
  2973. path_num_items(left_path) - 1);
  2974. if (ret) {
  2975. mlog_errno(ret);
  2976. goto out;
  2977. }
  2978. le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
  2979. le32_add_cpu(&right_rec->e_cpos, -split_clusters);
  2980. le64_add_cpu(&right_rec->e_blkno,
  2981. -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
  2982. split_clusters));
  2983. le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
  2984. ocfs2_cleanup_merge(el, index);
  2985. ret = ocfs2_journal_dirty(handle, bh);
  2986. if (ret)
  2987. mlog_errno(ret);
  2988. if (right_path) {
  2989. ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
  2990. if (ret)
  2991. mlog_errno(ret);
  2992. ocfs2_complete_edge_insert(handle, left_path, right_path,
  2993. subtree_index);
  2994. }
  2995. out:
  2996. if (right_path)
  2997. ocfs2_free_path(right_path);
  2998. return ret;
  2999. }
  3000. static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
  3001. struct ocfs2_path *right_path,
  3002. struct ocfs2_path **ret_left_path)
  3003. {
  3004. int ret;
  3005. u32 left_cpos;
  3006. struct ocfs2_path *left_path = NULL;
  3007. *ret_left_path = NULL;
  3008. /* This function shouldn't be called for non-trees. */
  3009. BUG_ON(right_path->p_tree_depth == 0);
  3010. ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  3011. right_path, &left_cpos);
  3012. if (ret) {
  3013. mlog_errno(ret);
  3014. goto out;
  3015. }
  3016. /* This function shouldn't be called for the leftmost leaf. */
  3017. BUG_ON(left_cpos == 0);
  3018. left_path = ocfs2_new_path_from_path(right_path);
  3019. if (!left_path) {
  3020. ret = -ENOMEM;
  3021. mlog_errno(ret);
  3022. goto out;
  3023. }
  3024. ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
  3025. if (ret) {
  3026. mlog_errno(ret);
  3027. goto out;
  3028. }
  3029. *ret_left_path = left_path;
  3030. out:
  3031. if (ret)
  3032. ocfs2_free_path(left_path);
  3033. return ret;
  3034. }
  3035. /*
  3036. * Remove split_rec clusters from the record at index and merge them
  3037. * onto the tail of the record "before" it.
  3038. * For index > 0, the "before" means the extent rec at index - 1.
  3039. *
  3040. * For index == 0, the "before" means the last record of the previous
  3041. * extent block. And there is also a situation that we may need to
  3042. * remove the rightmost leaf extent block in the right_path and change
  3043. * the right path to indicate the new rightmost path.
  3044. */
  3045. static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
  3046. handle_t *handle,
  3047. struct ocfs2_extent_tree *et,
  3048. struct ocfs2_extent_rec *split_rec,
  3049. struct ocfs2_cached_dealloc_ctxt *dealloc,
  3050. int index)
  3051. {
  3052. int ret, i, subtree_index = 0, has_empty_extent = 0;
  3053. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  3054. struct ocfs2_extent_rec *left_rec;
  3055. struct ocfs2_extent_rec *right_rec;
  3056. struct ocfs2_extent_list *el = path_leaf_el(right_path);
  3057. struct buffer_head *bh = path_leaf_bh(right_path);
  3058. struct buffer_head *root_bh = NULL;
  3059. struct ocfs2_path *left_path = NULL;
  3060. struct ocfs2_extent_list *left_el;
  3061. BUG_ON(index < 0);
  3062. right_rec = &el->l_recs[index];
  3063. if (index == 0) {
  3064. /* we meet with a cross extent block merge. */
  3065. ret = ocfs2_get_left_path(et, right_path, &left_path);
  3066. if (ret) {
  3067. mlog_errno(ret);
  3068. goto out;
  3069. }
  3070. left_el = path_leaf_el(left_path);
  3071. BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
  3072. le16_to_cpu(left_el->l_count));
  3073. left_rec = &left_el->l_recs[
  3074. le16_to_cpu(left_el->l_next_free_rec) - 1];
  3075. BUG_ON(le32_to_cpu(left_rec->e_cpos) +
  3076. le16_to_cpu(left_rec->e_leaf_clusters) !=
  3077. le32_to_cpu(split_rec->e_cpos));
  3078. subtree_index = ocfs2_find_subtree_root(et, left_path,
  3079. right_path);
  3080. ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
  3081. handle->h_buffer_credits,
  3082. left_path);
  3083. if (ret) {
  3084. mlog_errno(ret);
  3085. goto out;
  3086. }
  3087. root_bh = left_path->p_node[subtree_index].bh;
  3088. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  3089. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  3090. subtree_index);
  3091. if (ret) {
  3092. mlog_errno(ret);
  3093. goto out;
  3094. }
  3095. for (i = subtree_index + 1;
  3096. i < path_num_items(right_path); i++) {
  3097. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  3098. right_path, i);
  3099. if (ret) {
  3100. mlog_errno(ret);
  3101. goto out;
  3102. }
  3103. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  3104. left_path, i);
  3105. if (ret) {
  3106. mlog_errno(ret);
  3107. goto out;
  3108. }
  3109. }
  3110. } else {
  3111. left_rec = &el->l_recs[index - 1];
  3112. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  3113. has_empty_extent = 1;
  3114. }
  3115. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  3116. path_num_items(right_path) - 1);
  3117. if (ret) {
  3118. mlog_errno(ret);
  3119. goto out;
  3120. }
  3121. if (has_empty_extent && index == 1) {
  3122. /*
  3123. * The easy case - we can just plop the record right in.
  3124. */
  3125. *left_rec = *split_rec;
  3126. has_empty_extent = 0;
  3127. } else
  3128. le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
  3129. le32_add_cpu(&right_rec->e_cpos, split_clusters);
  3130. le64_add_cpu(&right_rec->e_blkno,
  3131. ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
  3132. split_clusters));
  3133. le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
  3134. ocfs2_cleanup_merge(el, index);
  3135. ret = ocfs2_journal_dirty(handle, bh);
  3136. if (ret)
  3137. mlog_errno(ret);
  3138. if (left_path) {
  3139. ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  3140. if (ret)
  3141. mlog_errno(ret);
  3142. /*
  3143. * In the situation that the right_rec is empty and the extent
  3144. * block is empty also, ocfs2_complete_edge_insert can't handle
  3145. * it and we need to delete the right extent block.
  3146. */
  3147. if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
  3148. le16_to_cpu(el->l_next_free_rec) == 1) {
  3149. ret = ocfs2_remove_rightmost_path(handle, et,
  3150. right_path,
  3151. dealloc);
  3152. if (ret) {
  3153. mlog_errno(ret);
  3154. goto out;
  3155. }
  3156. /* Now the rightmost extent block has been deleted.
  3157. * So we use the new rightmost path.
  3158. */
  3159. ocfs2_mv_path(right_path, left_path);
  3160. left_path = NULL;
  3161. } else
  3162. ocfs2_complete_edge_insert(handle, left_path,
  3163. right_path, subtree_index);
  3164. }
  3165. out:
  3166. if (left_path)
  3167. ocfs2_free_path(left_path);
  3168. return ret;
  3169. }
  3170. static int ocfs2_try_to_merge_extent(handle_t *handle,
  3171. struct ocfs2_extent_tree *et,
  3172. struct ocfs2_path *path,
  3173. int split_index,
  3174. struct ocfs2_extent_rec *split_rec,
  3175. struct ocfs2_cached_dealloc_ctxt *dealloc,
  3176. struct ocfs2_merge_ctxt *ctxt)
  3177. {
  3178. int ret = 0;
  3179. struct ocfs2_extent_list *el = path_leaf_el(path);
  3180. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  3181. BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
  3182. if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
  3183. /*
  3184. * The merge code will need to create an empty
  3185. * extent to take the place of the newly
  3186. * emptied slot. Remove any pre-existing empty
  3187. * extents - having more than one in a leaf is
  3188. * illegal.
  3189. */
  3190. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  3191. if (ret) {
  3192. mlog_errno(ret);
  3193. goto out;
  3194. }
  3195. split_index--;
  3196. rec = &el->l_recs[split_index];
  3197. }
  3198. if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
  3199. /*
  3200. * Left-right contig implies this.
  3201. */
  3202. BUG_ON(!ctxt->c_split_covers_rec);
  3203. /*
  3204. * Since the leftright insert always covers the entire
  3205. * extent, this call will delete the insert record
  3206. * entirely, resulting in an empty extent record added to
  3207. * the extent block.
  3208. *
  3209. * Since the adding of an empty extent shifts
  3210. * everything back to the right, there's no need to
  3211. * update split_index here.
  3212. *
  3213. * When the split_index is zero, we need to merge it to the
  3214. * prevoius extent block. It is more efficient and easier
  3215. * if we do merge_right first and merge_left later.
  3216. */
  3217. ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
  3218. split_index);
  3219. if (ret) {
  3220. mlog_errno(ret);
  3221. goto out;
  3222. }
  3223. /*
  3224. * We can only get this from logic error above.
  3225. */
  3226. BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
  3227. /* The merge left us with an empty extent, remove it. */
  3228. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  3229. if (ret) {
  3230. mlog_errno(ret);
  3231. goto out;
  3232. }
  3233. rec = &el->l_recs[split_index];
  3234. /*
  3235. * Note that we don't pass split_rec here on purpose -
  3236. * we've merged it into the rec already.
  3237. */
  3238. ret = ocfs2_merge_rec_left(path, handle, et, rec,
  3239. dealloc, split_index);
  3240. if (ret) {
  3241. mlog_errno(ret);
  3242. goto out;
  3243. }
  3244. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  3245. /*
  3246. * Error from this last rotate is not critical, so
  3247. * print but don't bubble it up.
  3248. */
  3249. if (ret)
  3250. mlog_errno(ret);
  3251. ret = 0;
  3252. } else {
  3253. /*
  3254. * Merge a record to the left or right.
  3255. *
  3256. * 'contig_type' is relative to the existing record,
  3257. * so for example, if we're "right contig", it's to
  3258. * the record on the left (hence the left merge).
  3259. */
  3260. if (ctxt->c_contig_type == CONTIG_RIGHT) {
  3261. ret = ocfs2_merge_rec_left(path, handle, et,
  3262. split_rec, dealloc,
  3263. split_index);
  3264. if (ret) {
  3265. mlog_errno(ret);
  3266. goto out;
  3267. }
  3268. } else {
  3269. ret = ocfs2_merge_rec_right(path, handle,
  3270. et, split_rec,
  3271. split_index);
  3272. if (ret) {
  3273. mlog_errno(ret);
  3274. goto out;
  3275. }
  3276. }
  3277. if (ctxt->c_split_covers_rec) {
  3278. /*
  3279. * The merge may have left an empty extent in
  3280. * our leaf. Try to rotate it away.
  3281. */
  3282. ret = ocfs2_rotate_tree_left(handle, et, path,
  3283. dealloc);
  3284. if (ret)
  3285. mlog_errno(ret);
  3286. ret = 0;
  3287. }
  3288. }
  3289. out:
  3290. return ret;
  3291. }
  3292. static void ocfs2_subtract_from_rec(struct super_block *sb,
  3293. enum ocfs2_split_type split,
  3294. struct ocfs2_extent_rec *rec,
  3295. struct ocfs2_extent_rec *split_rec)
  3296. {
  3297. u64 len_blocks;
  3298. len_blocks = ocfs2_clusters_to_blocks(sb,
  3299. le16_to_cpu(split_rec->e_leaf_clusters));
  3300. if (split == SPLIT_LEFT) {
  3301. /*
  3302. * Region is on the left edge of the existing
  3303. * record.
  3304. */
  3305. le32_add_cpu(&rec->e_cpos,
  3306. le16_to_cpu(split_rec->e_leaf_clusters));
  3307. le64_add_cpu(&rec->e_blkno, len_blocks);
  3308. le16_add_cpu(&rec->e_leaf_clusters,
  3309. -le16_to_cpu(split_rec->e_leaf_clusters));
  3310. } else {
  3311. /*
  3312. * Region is on the right edge of the existing
  3313. * record.
  3314. */
  3315. le16_add_cpu(&rec->e_leaf_clusters,
  3316. -le16_to_cpu(split_rec->e_leaf_clusters));
  3317. }
  3318. }
  3319. /*
  3320. * Do the final bits of extent record insertion at the target leaf
  3321. * list. If this leaf is part of an allocation tree, it is assumed
  3322. * that the tree above has been prepared.
  3323. */
  3324. static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
  3325. struct ocfs2_extent_rec *insert_rec,
  3326. struct ocfs2_extent_list *el,
  3327. struct ocfs2_insert_type *insert)
  3328. {
  3329. int i = insert->ins_contig_index;
  3330. unsigned int range;
  3331. struct ocfs2_extent_rec *rec;
  3332. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3333. if (insert->ins_split != SPLIT_NONE) {
  3334. i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
  3335. BUG_ON(i == -1);
  3336. rec = &el->l_recs[i];
  3337. ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  3338. insert->ins_split, rec,
  3339. insert_rec);
  3340. goto rotate;
  3341. }
  3342. /*
  3343. * Contiguous insert - either left or right.
  3344. */
  3345. if (insert->ins_contig != CONTIG_NONE) {
  3346. rec = &el->l_recs[i];
  3347. if (insert->ins_contig == CONTIG_LEFT) {
  3348. rec->e_blkno = insert_rec->e_blkno;
  3349. rec->e_cpos = insert_rec->e_cpos;
  3350. }
  3351. le16_add_cpu(&rec->e_leaf_clusters,
  3352. le16_to_cpu(insert_rec->e_leaf_clusters));
  3353. return;
  3354. }
  3355. /*
  3356. * Handle insert into an empty leaf.
  3357. */
  3358. if (le16_to_cpu(el->l_next_free_rec) == 0 ||
  3359. ((le16_to_cpu(el->l_next_free_rec) == 1) &&
  3360. ocfs2_is_empty_extent(&el->l_recs[0]))) {
  3361. el->l_recs[0] = *insert_rec;
  3362. el->l_next_free_rec = cpu_to_le16(1);
  3363. return;
  3364. }
  3365. /*
  3366. * Appending insert.
  3367. */
  3368. if (insert->ins_appending == APPEND_TAIL) {
  3369. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3370. rec = &el->l_recs[i];
  3371. range = le32_to_cpu(rec->e_cpos)
  3372. + le16_to_cpu(rec->e_leaf_clusters);
  3373. BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
  3374. mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
  3375. le16_to_cpu(el->l_count),
  3376. "owner %llu, depth %u, count %u, next free %u, "
  3377. "rec.cpos %u, rec.clusters %u, "
  3378. "insert.cpos %u, insert.clusters %u\n",
  3379. ocfs2_metadata_cache_owner(et->et_ci),
  3380. le16_to_cpu(el->l_tree_depth),
  3381. le16_to_cpu(el->l_count),
  3382. le16_to_cpu(el->l_next_free_rec),
  3383. le32_to_cpu(el->l_recs[i].e_cpos),
  3384. le16_to_cpu(el->l_recs[i].e_leaf_clusters),
  3385. le32_to_cpu(insert_rec->e_cpos),
  3386. le16_to_cpu(insert_rec->e_leaf_clusters));
  3387. i++;
  3388. el->l_recs[i] = *insert_rec;
  3389. le16_add_cpu(&el->l_next_free_rec, 1);
  3390. return;
  3391. }
  3392. rotate:
  3393. /*
  3394. * Ok, we have to rotate.
  3395. *
  3396. * At this point, it is safe to assume that inserting into an
  3397. * empty leaf and appending to a leaf have both been handled
  3398. * above.
  3399. *
  3400. * This leaf needs to have space, either by the empty 1st
  3401. * extent record, or by virtue of an l_next_rec < l_count.
  3402. */
  3403. ocfs2_rotate_leaf(el, insert_rec);
  3404. }
  3405. static void ocfs2_adjust_rightmost_records(handle_t *handle,
  3406. struct ocfs2_extent_tree *et,
  3407. struct ocfs2_path *path,
  3408. struct ocfs2_extent_rec *insert_rec)
  3409. {
  3410. int ret, i, next_free;
  3411. struct buffer_head *bh;
  3412. struct ocfs2_extent_list *el;
  3413. struct ocfs2_extent_rec *rec;
  3414. /*
  3415. * Update everything except the leaf block.
  3416. */
  3417. for (i = 0; i < path->p_tree_depth; i++) {
  3418. bh = path->p_node[i].bh;
  3419. el = path->p_node[i].el;
  3420. next_free = le16_to_cpu(el->l_next_free_rec);
  3421. if (next_free == 0) {
  3422. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  3423. "Owner %llu has a bad extent list",
  3424. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  3425. ret = -EIO;
  3426. return;
  3427. }
  3428. rec = &el->l_recs[next_free - 1];
  3429. rec->e_int_clusters = insert_rec->e_cpos;
  3430. le32_add_cpu(&rec->e_int_clusters,
  3431. le16_to_cpu(insert_rec->e_leaf_clusters));
  3432. le32_add_cpu(&rec->e_int_clusters,
  3433. -le32_to_cpu(rec->e_cpos));
  3434. ret = ocfs2_journal_dirty(handle, bh);
  3435. if (ret)
  3436. mlog_errno(ret);
  3437. }
  3438. }
  3439. static int ocfs2_append_rec_to_path(handle_t *handle,
  3440. struct ocfs2_extent_tree *et,
  3441. struct ocfs2_extent_rec *insert_rec,
  3442. struct ocfs2_path *right_path,
  3443. struct ocfs2_path **ret_left_path)
  3444. {
  3445. int ret, next_free;
  3446. struct ocfs2_extent_list *el;
  3447. struct ocfs2_path *left_path = NULL;
  3448. *ret_left_path = NULL;
  3449. /*
  3450. * This shouldn't happen for non-trees. The extent rec cluster
  3451. * count manipulation below only works for interior nodes.
  3452. */
  3453. BUG_ON(right_path->p_tree_depth == 0);
  3454. /*
  3455. * If our appending insert is at the leftmost edge of a leaf,
  3456. * then we might need to update the rightmost records of the
  3457. * neighboring path.
  3458. */
  3459. el = path_leaf_el(right_path);
  3460. next_free = le16_to_cpu(el->l_next_free_rec);
  3461. if (next_free == 0 ||
  3462. (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
  3463. u32 left_cpos;
  3464. ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  3465. right_path, &left_cpos);
  3466. if (ret) {
  3467. mlog_errno(ret);
  3468. goto out;
  3469. }
  3470. mlog(0, "Append may need a left path update. cpos: %u, "
  3471. "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
  3472. left_cpos);
  3473. /*
  3474. * No need to worry if the append is already in the
  3475. * leftmost leaf.
  3476. */
  3477. if (left_cpos) {
  3478. left_path = ocfs2_new_path_from_path(right_path);
  3479. if (!left_path) {
  3480. ret = -ENOMEM;
  3481. mlog_errno(ret);
  3482. goto out;
  3483. }
  3484. ret = ocfs2_find_path(et->et_ci, left_path,
  3485. left_cpos);
  3486. if (ret) {
  3487. mlog_errno(ret);
  3488. goto out;
  3489. }
  3490. /*
  3491. * ocfs2_insert_path() will pass the left_path to the
  3492. * journal for us.
  3493. */
  3494. }
  3495. }
  3496. ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
  3497. if (ret) {
  3498. mlog_errno(ret);
  3499. goto out;
  3500. }
  3501. ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
  3502. *ret_left_path = left_path;
  3503. ret = 0;
  3504. out:
  3505. if (ret != 0)
  3506. ocfs2_free_path(left_path);
  3507. return ret;
  3508. }
  3509. static void ocfs2_split_record(struct ocfs2_extent_tree *et,
  3510. struct ocfs2_path *left_path,
  3511. struct ocfs2_path *right_path,
  3512. struct ocfs2_extent_rec *split_rec,
  3513. enum ocfs2_split_type split)
  3514. {
  3515. int index;
  3516. u32 cpos = le32_to_cpu(split_rec->e_cpos);
  3517. struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
  3518. struct ocfs2_extent_rec *rec, *tmprec;
  3519. right_el = path_leaf_el(right_path);
  3520. if (left_path)
  3521. left_el = path_leaf_el(left_path);
  3522. el = right_el;
  3523. insert_el = right_el;
  3524. index = ocfs2_search_extent_list(el, cpos);
  3525. if (index != -1) {
  3526. if (index == 0 && left_path) {
  3527. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  3528. /*
  3529. * This typically means that the record
  3530. * started in the left path but moved to the
  3531. * right as a result of rotation. We either
  3532. * move the existing record to the left, or we
  3533. * do the later insert there.
  3534. *
  3535. * In this case, the left path should always
  3536. * exist as the rotate code will have passed
  3537. * it back for a post-insert update.
  3538. */
  3539. if (split == SPLIT_LEFT) {
  3540. /*
  3541. * It's a left split. Since we know
  3542. * that the rotate code gave us an
  3543. * empty extent in the left path, we
  3544. * can just do the insert there.
  3545. */
  3546. insert_el = left_el;
  3547. } else {
  3548. /*
  3549. * Right split - we have to move the
  3550. * existing record over to the left
  3551. * leaf. The insert will be into the
  3552. * newly created empty extent in the
  3553. * right leaf.
  3554. */
  3555. tmprec = &right_el->l_recs[index];
  3556. ocfs2_rotate_leaf(left_el, tmprec);
  3557. el = left_el;
  3558. memset(tmprec, 0, sizeof(*tmprec));
  3559. index = ocfs2_search_extent_list(left_el, cpos);
  3560. BUG_ON(index == -1);
  3561. }
  3562. }
  3563. } else {
  3564. BUG_ON(!left_path);
  3565. BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
  3566. /*
  3567. * Left path is easy - we can just allow the insert to
  3568. * happen.
  3569. */
  3570. el = left_el;
  3571. insert_el = left_el;
  3572. index = ocfs2_search_extent_list(el, cpos);
  3573. BUG_ON(index == -1);
  3574. }
  3575. rec = &el->l_recs[index];
  3576. ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  3577. split, rec, split_rec);
  3578. ocfs2_rotate_leaf(insert_el, split_rec);
  3579. }
  3580. /*
  3581. * This function only does inserts on an allocation b-tree. For tree
  3582. * depth = 0, ocfs2_insert_at_leaf() is called directly.
  3583. *
  3584. * right_path is the path we want to do the actual insert
  3585. * in. left_path should only be passed in if we need to update that
  3586. * portion of the tree after an edge insert.
  3587. */
  3588. static int ocfs2_insert_path(handle_t *handle,
  3589. struct ocfs2_extent_tree *et,
  3590. struct ocfs2_path *left_path,
  3591. struct ocfs2_path *right_path,
  3592. struct ocfs2_extent_rec *insert_rec,
  3593. struct ocfs2_insert_type *insert)
  3594. {
  3595. int ret, subtree_index;
  3596. struct buffer_head *leaf_bh = path_leaf_bh(right_path);
  3597. if (left_path) {
  3598. int credits = handle->h_buffer_credits;
  3599. /*
  3600. * There's a chance that left_path got passed back to
  3601. * us without being accounted for in the
  3602. * journal. Extend our transaction here to be sure we
  3603. * can change those blocks.
  3604. */
  3605. credits += left_path->p_tree_depth;
  3606. ret = ocfs2_extend_trans(handle, credits);
  3607. if (ret < 0) {
  3608. mlog_errno(ret);
  3609. goto out;
  3610. }
  3611. ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
  3612. if (ret < 0) {
  3613. mlog_errno(ret);
  3614. goto out;
  3615. }
  3616. }
  3617. /*
  3618. * Pass both paths to the journal. The majority of inserts
  3619. * will be touching all components anyway.
  3620. */
  3621. ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
  3622. if (ret < 0) {
  3623. mlog_errno(ret);
  3624. goto out;
  3625. }
  3626. if (insert->ins_split != SPLIT_NONE) {
  3627. /*
  3628. * We could call ocfs2_insert_at_leaf() for some types
  3629. * of splits, but it's easier to just let one separate
  3630. * function sort it all out.
  3631. */
  3632. ocfs2_split_record(et, left_path, right_path,
  3633. insert_rec, insert->ins_split);
  3634. /*
  3635. * Split might have modified either leaf and we don't
  3636. * have a guarantee that the later edge insert will
  3637. * dirty this for us.
  3638. */
  3639. if (left_path)
  3640. ret = ocfs2_journal_dirty(handle,
  3641. path_leaf_bh(left_path));
  3642. if (ret)
  3643. mlog_errno(ret);
  3644. } else
  3645. ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
  3646. insert);
  3647. ret = ocfs2_journal_dirty(handle, leaf_bh);
  3648. if (ret)
  3649. mlog_errno(ret);
  3650. if (left_path) {
  3651. /*
  3652. * The rotate code has indicated that we need to fix
  3653. * up portions of the tree after the insert.
  3654. *
  3655. * XXX: Should we extend the transaction here?
  3656. */
  3657. subtree_index = ocfs2_find_subtree_root(et, left_path,
  3658. right_path);
  3659. ocfs2_complete_edge_insert(handle, left_path, right_path,
  3660. subtree_index);
  3661. }
  3662. ret = 0;
  3663. out:
  3664. return ret;
  3665. }
  3666. static int ocfs2_do_insert_extent(handle_t *handle,
  3667. struct ocfs2_extent_tree *et,
  3668. struct ocfs2_extent_rec *insert_rec,
  3669. struct ocfs2_insert_type *type)
  3670. {
  3671. int ret, rotate = 0;
  3672. u32 cpos;
  3673. struct ocfs2_path *right_path = NULL;
  3674. struct ocfs2_path *left_path = NULL;
  3675. struct ocfs2_extent_list *el;
  3676. el = et->et_root_el;
  3677. ret = ocfs2_et_root_journal_access(handle, et,
  3678. OCFS2_JOURNAL_ACCESS_WRITE);
  3679. if (ret) {
  3680. mlog_errno(ret);
  3681. goto out;
  3682. }
  3683. if (le16_to_cpu(el->l_tree_depth) == 0) {
  3684. ocfs2_insert_at_leaf(et, insert_rec, el, type);
  3685. goto out_update_clusters;
  3686. }
  3687. right_path = ocfs2_new_path_from_et(et);
  3688. if (!right_path) {
  3689. ret = -ENOMEM;
  3690. mlog_errno(ret);
  3691. goto out;
  3692. }
  3693. /*
  3694. * Determine the path to start with. Rotations need the
  3695. * rightmost path, everything else can go directly to the
  3696. * target leaf.
  3697. */
  3698. cpos = le32_to_cpu(insert_rec->e_cpos);
  3699. if (type->ins_appending == APPEND_NONE &&
  3700. type->ins_contig == CONTIG_NONE) {
  3701. rotate = 1;
  3702. cpos = UINT_MAX;
  3703. }
  3704. ret = ocfs2_find_path(et->et_ci, right_path, cpos);
  3705. if (ret) {
  3706. mlog_errno(ret);
  3707. goto out;
  3708. }
  3709. /*
  3710. * Rotations and appends need special treatment - they modify
  3711. * parts of the tree's above them.
  3712. *
  3713. * Both might pass back a path immediate to the left of the
  3714. * one being inserted to. This will be cause
  3715. * ocfs2_insert_path() to modify the rightmost records of
  3716. * left_path to account for an edge insert.
  3717. *
  3718. * XXX: When modifying this code, keep in mind that an insert
  3719. * can wind up skipping both of these two special cases...
  3720. */
  3721. if (rotate) {
  3722. ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
  3723. le32_to_cpu(insert_rec->e_cpos),
  3724. right_path, &left_path);
  3725. if (ret) {
  3726. mlog_errno(ret);
  3727. goto out;
  3728. }
  3729. /*
  3730. * ocfs2_rotate_tree_right() might have extended the
  3731. * transaction without re-journaling our tree root.
  3732. */
  3733. ret = ocfs2_et_root_journal_access(handle, et,
  3734. OCFS2_JOURNAL_ACCESS_WRITE);
  3735. if (ret) {
  3736. mlog_errno(ret);
  3737. goto out;
  3738. }
  3739. } else if (type->ins_appending == APPEND_TAIL
  3740. && type->ins_contig != CONTIG_LEFT) {
  3741. ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
  3742. right_path, &left_path);
  3743. if (ret) {
  3744. mlog_errno(ret);
  3745. goto out;
  3746. }
  3747. }
  3748. ret = ocfs2_insert_path(handle, et, left_path, right_path,
  3749. insert_rec, type);
  3750. if (ret) {
  3751. mlog_errno(ret);
  3752. goto out;
  3753. }
  3754. out_update_clusters:
  3755. if (type->ins_split == SPLIT_NONE)
  3756. ocfs2_et_update_clusters(et,
  3757. le16_to_cpu(insert_rec->e_leaf_clusters));
  3758. ret = ocfs2_journal_dirty(handle, et->et_root_bh);
  3759. if (ret)
  3760. mlog_errno(ret);
  3761. out:
  3762. ocfs2_free_path(left_path);
  3763. ocfs2_free_path(right_path);
  3764. return ret;
  3765. }
  3766. static enum ocfs2_contig_type
  3767. ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
  3768. struct ocfs2_path *path,
  3769. struct ocfs2_extent_list *el, int index,
  3770. struct ocfs2_extent_rec *split_rec)
  3771. {
  3772. int status;
  3773. enum ocfs2_contig_type ret = CONTIG_NONE;
  3774. u32 left_cpos, right_cpos;
  3775. struct ocfs2_extent_rec *rec = NULL;
  3776. struct ocfs2_extent_list *new_el;
  3777. struct ocfs2_path *left_path = NULL, *right_path = NULL;
  3778. struct buffer_head *bh;
  3779. struct ocfs2_extent_block *eb;
  3780. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  3781. if (index > 0) {
  3782. rec = &el->l_recs[index - 1];
  3783. } else if (path->p_tree_depth > 0) {
  3784. status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
  3785. if (status)
  3786. goto out;
  3787. if (left_cpos != 0) {
  3788. left_path = ocfs2_new_path_from_path(path);
  3789. if (!left_path)
  3790. goto out;
  3791. status = ocfs2_find_path(et->et_ci, left_path,
  3792. left_cpos);
  3793. if (status)
  3794. goto out;
  3795. new_el = path_leaf_el(left_path);
  3796. if (le16_to_cpu(new_el->l_next_free_rec) !=
  3797. le16_to_cpu(new_el->l_count)) {
  3798. bh = path_leaf_bh(left_path);
  3799. eb = (struct ocfs2_extent_block *)bh->b_data;
  3800. ocfs2_error(sb,
  3801. "Extent block #%llu has an "
  3802. "invalid l_next_free_rec of "
  3803. "%d. It should have "
  3804. "matched the l_count of %d",
  3805. (unsigned long long)le64_to_cpu(eb->h_blkno),
  3806. le16_to_cpu(new_el->l_next_free_rec),
  3807. le16_to_cpu(new_el->l_count));
  3808. status = -EINVAL;
  3809. goto out;
  3810. }
  3811. rec = &new_el->l_recs[
  3812. le16_to_cpu(new_el->l_next_free_rec) - 1];
  3813. }
  3814. }
  3815. /*
  3816. * We're careful to check for an empty extent record here -
  3817. * the merge code will know what to do if it sees one.
  3818. */
  3819. if (rec) {
  3820. if (index == 1 && ocfs2_is_empty_extent(rec)) {
  3821. if (split_rec->e_cpos == el->l_recs[index].e_cpos)
  3822. ret = CONTIG_RIGHT;
  3823. } else {
  3824. ret = ocfs2_et_extent_contig(et, rec, split_rec);
  3825. }
  3826. }
  3827. rec = NULL;
  3828. if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
  3829. rec = &el->l_recs[index + 1];
  3830. else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
  3831. path->p_tree_depth > 0) {
  3832. status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
  3833. if (status)
  3834. goto out;
  3835. if (right_cpos == 0)
  3836. goto out;
  3837. right_path = ocfs2_new_path_from_path(path);
  3838. if (!right_path)
  3839. goto out;
  3840. status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
  3841. if (status)
  3842. goto out;
  3843. new_el = path_leaf_el(right_path);
  3844. rec = &new_el->l_recs[0];
  3845. if (ocfs2_is_empty_extent(rec)) {
  3846. if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
  3847. bh = path_leaf_bh(right_path);
  3848. eb = (struct ocfs2_extent_block *)bh->b_data;
  3849. ocfs2_error(sb,
  3850. "Extent block #%llu has an "
  3851. "invalid l_next_free_rec of %d",
  3852. (unsigned long long)le64_to_cpu(eb->h_blkno),
  3853. le16_to_cpu(new_el->l_next_free_rec));
  3854. status = -EINVAL;
  3855. goto out;
  3856. }
  3857. rec = &new_el->l_recs[1];
  3858. }
  3859. }
  3860. if (rec) {
  3861. enum ocfs2_contig_type contig_type;
  3862. contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
  3863. if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
  3864. ret = CONTIG_LEFTRIGHT;
  3865. else if (ret == CONTIG_NONE)
  3866. ret = contig_type;
  3867. }
  3868. out:
  3869. if (left_path)
  3870. ocfs2_free_path(left_path);
  3871. if (right_path)
  3872. ocfs2_free_path(right_path);
  3873. return ret;
  3874. }
  3875. static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
  3876. struct ocfs2_insert_type *insert,
  3877. struct ocfs2_extent_list *el,
  3878. struct ocfs2_extent_rec *insert_rec)
  3879. {
  3880. int i;
  3881. enum ocfs2_contig_type contig_type = CONTIG_NONE;
  3882. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3883. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  3884. contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
  3885. insert_rec);
  3886. if (contig_type != CONTIG_NONE) {
  3887. insert->ins_contig_index = i;
  3888. break;
  3889. }
  3890. }
  3891. insert->ins_contig = contig_type;
  3892. if (insert->ins_contig != CONTIG_NONE) {
  3893. struct ocfs2_extent_rec *rec =
  3894. &el->l_recs[insert->ins_contig_index];
  3895. unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
  3896. le16_to_cpu(insert_rec->e_leaf_clusters);
  3897. /*
  3898. * Caller might want us to limit the size of extents, don't
  3899. * calculate contiguousness if we might exceed that limit.
  3900. */
  3901. if (et->et_max_leaf_clusters &&
  3902. (len > et->et_max_leaf_clusters))
  3903. insert->ins_contig = CONTIG_NONE;
  3904. }
  3905. }
  3906. /*
  3907. * This should only be called against the righmost leaf extent list.
  3908. *
  3909. * ocfs2_figure_appending_type() will figure out whether we'll have to
  3910. * insert at the tail of the rightmost leaf.
  3911. *
  3912. * This should also work against the root extent list for tree's with 0
  3913. * depth. If we consider the root extent list to be the rightmost leaf node
  3914. * then the logic here makes sense.
  3915. */
  3916. static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
  3917. struct ocfs2_extent_list *el,
  3918. struct ocfs2_extent_rec *insert_rec)
  3919. {
  3920. int i;
  3921. u32 cpos = le32_to_cpu(insert_rec->e_cpos);
  3922. struct ocfs2_extent_rec *rec;
  3923. insert->ins_appending = APPEND_NONE;
  3924. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3925. if (!el->l_next_free_rec)
  3926. goto set_tail_append;
  3927. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  3928. /* Were all records empty? */
  3929. if (le16_to_cpu(el->l_next_free_rec) == 1)
  3930. goto set_tail_append;
  3931. }
  3932. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3933. rec = &el->l_recs[i];
  3934. if (cpos >=
  3935. (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
  3936. goto set_tail_append;
  3937. return;
  3938. set_tail_append:
  3939. insert->ins_appending = APPEND_TAIL;
  3940. }
  3941. /*
  3942. * Helper function called at the begining of an insert.
  3943. *
  3944. * This computes a few things that are commonly used in the process of
  3945. * inserting into the btree:
  3946. * - Whether the new extent is contiguous with an existing one.
  3947. * - The current tree depth.
  3948. * - Whether the insert is an appending one.
  3949. * - The total # of free records in the tree.
  3950. *
  3951. * All of the information is stored on the ocfs2_insert_type
  3952. * structure.
  3953. */
  3954. static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
  3955. struct buffer_head **last_eb_bh,
  3956. struct ocfs2_extent_rec *insert_rec,
  3957. int *free_records,
  3958. struct ocfs2_insert_type *insert)
  3959. {
  3960. int ret;
  3961. struct ocfs2_extent_block *eb;
  3962. struct ocfs2_extent_list *el;
  3963. struct ocfs2_path *path = NULL;
  3964. struct buffer_head *bh = NULL;
  3965. insert->ins_split = SPLIT_NONE;
  3966. el = et->et_root_el;
  3967. insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
  3968. if (el->l_tree_depth) {
  3969. /*
  3970. * If we have tree depth, we read in the
  3971. * rightmost extent block ahead of time as
  3972. * ocfs2_figure_insert_type() and ocfs2_add_branch()
  3973. * may want it later.
  3974. */
  3975. ret = ocfs2_read_extent_block(et->et_ci,
  3976. ocfs2_et_get_last_eb_blk(et),
  3977. &bh);
  3978. if (ret) {
  3979. mlog_exit(ret);
  3980. goto out;
  3981. }
  3982. eb = (struct ocfs2_extent_block *) bh->b_data;
  3983. el = &eb->h_list;
  3984. }
  3985. /*
  3986. * Unless we have a contiguous insert, we'll need to know if
  3987. * there is room left in our allocation tree for another
  3988. * extent record.
  3989. *
  3990. * XXX: This test is simplistic, we can search for empty
  3991. * extent records too.
  3992. */
  3993. *free_records = le16_to_cpu(el->l_count) -
  3994. le16_to_cpu(el->l_next_free_rec);
  3995. if (!insert->ins_tree_depth) {
  3996. ocfs2_figure_contig_type(et, insert, el, insert_rec);
  3997. ocfs2_figure_appending_type(insert, el, insert_rec);
  3998. return 0;
  3999. }
  4000. path = ocfs2_new_path_from_et(et);
  4001. if (!path) {
  4002. ret = -ENOMEM;
  4003. mlog_errno(ret);
  4004. goto out;
  4005. }
  4006. /*
  4007. * In the case that we're inserting past what the tree
  4008. * currently accounts for, ocfs2_find_path() will return for
  4009. * us the rightmost tree path. This is accounted for below in
  4010. * the appending code.
  4011. */
  4012. ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
  4013. if (ret) {
  4014. mlog_errno(ret);
  4015. goto out;
  4016. }
  4017. el = path_leaf_el(path);
  4018. /*
  4019. * Now that we have the path, there's two things we want to determine:
  4020. * 1) Contiguousness (also set contig_index if this is so)
  4021. *
  4022. * 2) Are we doing an append? We can trivially break this up
  4023. * into two types of appends: simple record append, or a
  4024. * rotate inside the tail leaf.
  4025. */
  4026. ocfs2_figure_contig_type(et, insert, el, insert_rec);
  4027. /*
  4028. * The insert code isn't quite ready to deal with all cases of
  4029. * left contiguousness. Specifically, if it's an insert into
  4030. * the 1st record in a leaf, it will require the adjustment of
  4031. * cluster count on the last record of the path directly to it's
  4032. * left. For now, just catch that case and fool the layers
  4033. * above us. This works just fine for tree_depth == 0, which
  4034. * is why we allow that above.
  4035. */
  4036. if (insert->ins_contig == CONTIG_LEFT &&
  4037. insert->ins_contig_index == 0)
  4038. insert->ins_contig = CONTIG_NONE;
  4039. /*
  4040. * Ok, so we can simply compare against last_eb to figure out
  4041. * whether the path doesn't exist. This will only happen in
  4042. * the case that we're doing a tail append, so maybe we can
  4043. * take advantage of that information somehow.
  4044. */
  4045. if (ocfs2_et_get_last_eb_blk(et) ==
  4046. path_leaf_bh(path)->b_blocknr) {
  4047. /*
  4048. * Ok, ocfs2_find_path() returned us the rightmost
  4049. * tree path. This might be an appending insert. There are
  4050. * two cases:
  4051. * 1) We're doing a true append at the tail:
  4052. * -This might even be off the end of the leaf
  4053. * 2) We're "appending" by rotating in the tail
  4054. */
  4055. ocfs2_figure_appending_type(insert, el, insert_rec);
  4056. }
  4057. out:
  4058. ocfs2_free_path(path);
  4059. if (ret == 0)
  4060. *last_eb_bh = bh;
  4061. else
  4062. brelse(bh);
  4063. return ret;
  4064. }
  4065. /*
  4066. * Insert an extent into a btree.
  4067. *
  4068. * The caller needs to update the owning btree's cluster count.
  4069. */
  4070. int ocfs2_insert_extent(handle_t *handle,
  4071. struct ocfs2_extent_tree *et,
  4072. u32 cpos,
  4073. u64 start_blk,
  4074. u32 new_clusters,
  4075. u8 flags,
  4076. struct ocfs2_alloc_context *meta_ac)
  4077. {
  4078. int status;
  4079. int uninitialized_var(free_records);
  4080. struct buffer_head *last_eb_bh = NULL;
  4081. struct ocfs2_insert_type insert = {0, };
  4082. struct ocfs2_extent_rec rec;
  4083. mlog(0, "add %u clusters at position %u to owner %llu\n",
  4084. new_clusters, cpos,
  4085. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  4086. memset(&rec, 0, sizeof(rec));
  4087. rec.e_cpos = cpu_to_le32(cpos);
  4088. rec.e_blkno = cpu_to_le64(start_blk);
  4089. rec.e_leaf_clusters = cpu_to_le16(new_clusters);
  4090. rec.e_flags = flags;
  4091. status = ocfs2_et_insert_check(et, &rec);
  4092. if (status) {
  4093. mlog_errno(status);
  4094. goto bail;
  4095. }
  4096. status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
  4097. &free_records, &insert);
  4098. if (status < 0) {
  4099. mlog_errno(status);
  4100. goto bail;
  4101. }
  4102. mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
  4103. "Insert.contig_index: %d, Insert.free_records: %d, "
  4104. "Insert.tree_depth: %d\n",
  4105. insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
  4106. free_records, insert.ins_tree_depth);
  4107. if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
  4108. status = ocfs2_grow_tree(handle, et,
  4109. &insert.ins_tree_depth, &last_eb_bh,
  4110. meta_ac);
  4111. if (status) {
  4112. mlog_errno(status);
  4113. goto bail;
  4114. }
  4115. }
  4116. /* Finally, we can add clusters. This might rotate the tree for us. */
  4117. status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
  4118. if (status < 0)
  4119. mlog_errno(status);
  4120. else
  4121. ocfs2_et_extent_map_insert(et, &rec);
  4122. bail:
  4123. brelse(last_eb_bh);
  4124. mlog_exit(status);
  4125. return status;
  4126. }
  4127. /*
  4128. * Allcate and add clusters into the extent b-tree.
  4129. * The new clusters(clusters_to_add) will be inserted at logical_offset.
  4130. * The extent b-tree's root is specified by et, and
  4131. * it is not limited to the file storage. Any extent tree can use this
  4132. * function if it implements the proper ocfs2_extent_tree.
  4133. */
  4134. int ocfs2_add_clusters_in_btree(handle_t *handle,
  4135. struct ocfs2_extent_tree *et,
  4136. u32 *logical_offset,
  4137. u32 clusters_to_add,
  4138. int mark_unwritten,
  4139. struct ocfs2_alloc_context *data_ac,
  4140. struct ocfs2_alloc_context *meta_ac,
  4141. enum ocfs2_alloc_restarted *reason_ret)
  4142. {
  4143. int status = 0;
  4144. int free_extents;
  4145. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  4146. u32 bit_off, num_bits;
  4147. u64 block;
  4148. u8 flags = 0;
  4149. struct ocfs2_super *osb =
  4150. OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
  4151. BUG_ON(!clusters_to_add);
  4152. if (mark_unwritten)
  4153. flags = OCFS2_EXT_UNWRITTEN;
  4154. free_extents = ocfs2_num_free_extents(osb, et);
  4155. if (free_extents < 0) {
  4156. status = free_extents;
  4157. mlog_errno(status);
  4158. goto leave;
  4159. }
  4160. /* there are two cases which could cause us to EAGAIN in the
  4161. * we-need-more-metadata case:
  4162. * 1) we haven't reserved *any*
  4163. * 2) we are so fragmented, we've needed to add metadata too
  4164. * many times. */
  4165. if (!free_extents && !meta_ac) {
  4166. mlog(0, "we haven't reserved any metadata!\n");
  4167. status = -EAGAIN;
  4168. reason = RESTART_META;
  4169. goto leave;
  4170. } else if ((!free_extents)
  4171. && (ocfs2_alloc_context_bits_left(meta_ac)
  4172. < ocfs2_extend_meta_needed(et->et_root_el))) {
  4173. mlog(0, "filesystem is really fragmented...\n");
  4174. status = -EAGAIN;
  4175. reason = RESTART_META;
  4176. goto leave;
  4177. }
  4178. status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
  4179. clusters_to_add, &bit_off, &num_bits);
  4180. if (status < 0) {
  4181. if (status != -ENOSPC)
  4182. mlog_errno(status);
  4183. goto leave;
  4184. }
  4185. BUG_ON(num_bits > clusters_to_add);
  4186. /* reserve our write early -- insert_extent may update the tree root */
  4187. status = ocfs2_et_root_journal_access(handle, et,
  4188. OCFS2_JOURNAL_ACCESS_WRITE);
  4189. if (status < 0) {
  4190. mlog_errno(status);
  4191. goto leave;
  4192. }
  4193. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  4194. mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
  4195. num_bits, bit_off,
  4196. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  4197. status = ocfs2_insert_extent(handle, et, *logical_offset, block,
  4198. num_bits, flags, meta_ac);
  4199. if (status < 0) {
  4200. mlog_errno(status);
  4201. goto leave;
  4202. }
  4203. status = ocfs2_journal_dirty(handle, et->et_root_bh);
  4204. if (status < 0) {
  4205. mlog_errno(status);
  4206. goto leave;
  4207. }
  4208. clusters_to_add -= num_bits;
  4209. *logical_offset += num_bits;
  4210. if (clusters_to_add) {
  4211. mlog(0, "need to alloc once more, wanted = %u\n",
  4212. clusters_to_add);
  4213. status = -EAGAIN;
  4214. reason = RESTART_TRANS;
  4215. }
  4216. leave:
  4217. mlog_exit(status);
  4218. if (reason_ret)
  4219. *reason_ret = reason;
  4220. return status;
  4221. }
  4222. static void ocfs2_make_right_split_rec(struct super_block *sb,
  4223. struct ocfs2_extent_rec *split_rec,
  4224. u32 cpos,
  4225. struct ocfs2_extent_rec *rec)
  4226. {
  4227. u32 rec_cpos = le32_to_cpu(rec->e_cpos);
  4228. u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
  4229. memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
  4230. split_rec->e_cpos = cpu_to_le32(cpos);
  4231. split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
  4232. split_rec->e_blkno = rec->e_blkno;
  4233. le64_add_cpu(&split_rec->e_blkno,
  4234. ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
  4235. split_rec->e_flags = rec->e_flags;
  4236. }
  4237. static int ocfs2_split_and_insert(handle_t *handle,
  4238. struct ocfs2_extent_tree *et,
  4239. struct ocfs2_path *path,
  4240. struct buffer_head **last_eb_bh,
  4241. int split_index,
  4242. struct ocfs2_extent_rec *orig_split_rec,
  4243. struct ocfs2_alloc_context *meta_ac)
  4244. {
  4245. int ret = 0, depth;
  4246. unsigned int insert_range, rec_range, do_leftright = 0;
  4247. struct ocfs2_extent_rec tmprec;
  4248. struct ocfs2_extent_list *rightmost_el;
  4249. struct ocfs2_extent_rec rec;
  4250. struct ocfs2_extent_rec split_rec = *orig_split_rec;
  4251. struct ocfs2_insert_type insert;
  4252. struct ocfs2_extent_block *eb;
  4253. leftright:
  4254. /*
  4255. * Store a copy of the record on the stack - it might move
  4256. * around as the tree is manipulated below.
  4257. */
  4258. rec = path_leaf_el(path)->l_recs[split_index];
  4259. rightmost_el = et->et_root_el;
  4260. depth = le16_to_cpu(rightmost_el->l_tree_depth);
  4261. if (depth) {
  4262. BUG_ON(!(*last_eb_bh));
  4263. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  4264. rightmost_el = &eb->h_list;
  4265. }
  4266. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  4267. le16_to_cpu(rightmost_el->l_count)) {
  4268. ret = ocfs2_grow_tree(handle, et,
  4269. &depth, last_eb_bh, meta_ac);
  4270. if (ret) {
  4271. mlog_errno(ret);
  4272. goto out;
  4273. }
  4274. }
  4275. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  4276. insert.ins_appending = APPEND_NONE;
  4277. insert.ins_contig = CONTIG_NONE;
  4278. insert.ins_tree_depth = depth;
  4279. insert_range = le32_to_cpu(split_rec.e_cpos) +
  4280. le16_to_cpu(split_rec.e_leaf_clusters);
  4281. rec_range = le32_to_cpu(rec.e_cpos) +
  4282. le16_to_cpu(rec.e_leaf_clusters);
  4283. if (split_rec.e_cpos == rec.e_cpos) {
  4284. insert.ins_split = SPLIT_LEFT;
  4285. } else if (insert_range == rec_range) {
  4286. insert.ins_split = SPLIT_RIGHT;
  4287. } else {
  4288. /*
  4289. * Left/right split. We fake this as a right split
  4290. * first and then make a second pass as a left split.
  4291. */
  4292. insert.ins_split = SPLIT_RIGHT;
  4293. ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  4294. &tmprec, insert_range, &rec);
  4295. split_rec = tmprec;
  4296. BUG_ON(do_leftright);
  4297. do_leftright = 1;
  4298. }
  4299. ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
  4300. if (ret) {
  4301. mlog_errno(ret);
  4302. goto out;
  4303. }
  4304. if (do_leftright == 1) {
  4305. u32 cpos;
  4306. struct ocfs2_extent_list *el;
  4307. do_leftright++;
  4308. split_rec = *orig_split_rec;
  4309. ocfs2_reinit_path(path, 1);
  4310. cpos = le32_to_cpu(split_rec.e_cpos);
  4311. ret = ocfs2_find_path(et->et_ci, path, cpos);
  4312. if (ret) {
  4313. mlog_errno(ret);
  4314. goto out;
  4315. }
  4316. el = path_leaf_el(path);
  4317. split_index = ocfs2_search_extent_list(el, cpos);
  4318. goto leftright;
  4319. }
  4320. out:
  4321. return ret;
  4322. }
  4323. static int ocfs2_replace_extent_rec(handle_t *handle,
  4324. struct ocfs2_extent_tree *et,
  4325. struct ocfs2_path *path,
  4326. struct ocfs2_extent_list *el,
  4327. int split_index,
  4328. struct ocfs2_extent_rec *split_rec)
  4329. {
  4330. int ret;
  4331. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
  4332. path_num_items(path) - 1);
  4333. if (ret) {
  4334. mlog_errno(ret);
  4335. goto out;
  4336. }
  4337. el->l_recs[split_index] = *split_rec;
  4338. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  4339. out:
  4340. return ret;
  4341. }
  4342. /*
  4343. * Split part or all of the extent record at split_index in the leaf
  4344. * pointed to by path. Merge with the contiguous extent record if needed.
  4345. *
  4346. * Care is taken to handle contiguousness so as to not grow the tree.
  4347. *
  4348. * meta_ac is not strictly necessary - we only truly need it if growth
  4349. * of the tree is required. All other cases will degrade into a less
  4350. * optimal tree layout.
  4351. *
  4352. * last_eb_bh should be the rightmost leaf block for any extent
  4353. * btree. Since a split may grow the tree or a merge might shrink it,
  4354. * the caller cannot trust the contents of that buffer after this call.
  4355. *
  4356. * This code is optimized for readability - several passes might be
  4357. * made over certain portions of the tree. All of those blocks will
  4358. * have been brought into cache (and pinned via the journal), so the
  4359. * extra overhead is not expressed in terms of disk reads.
  4360. */
  4361. int ocfs2_split_extent(handle_t *handle,
  4362. struct ocfs2_extent_tree *et,
  4363. struct ocfs2_path *path,
  4364. int split_index,
  4365. struct ocfs2_extent_rec *split_rec,
  4366. struct ocfs2_alloc_context *meta_ac,
  4367. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4368. {
  4369. int ret = 0;
  4370. struct ocfs2_extent_list *el = path_leaf_el(path);
  4371. struct buffer_head *last_eb_bh = NULL;
  4372. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  4373. struct ocfs2_merge_ctxt ctxt;
  4374. struct ocfs2_extent_list *rightmost_el;
  4375. if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
  4376. ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
  4377. (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
  4378. ret = -EIO;
  4379. mlog_errno(ret);
  4380. goto out;
  4381. }
  4382. ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
  4383. split_index,
  4384. split_rec);
  4385. /*
  4386. * The core merge / split code wants to know how much room is
  4387. * left in this allocation tree, so we pass the
  4388. * rightmost extent list.
  4389. */
  4390. if (path->p_tree_depth) {
  4391. struct ocfs2_extent_block *eb;
  4392. ret = ocfs2_read_extent_block(et->et_ci,
  4393. ocfs2_et_get_last_eb_blk(et),
  4394. &last_eb_bh);
  4395. if (ret) {
  4396. mlog_exit(ret);
  4397. goto out;
  4398. }
  4399. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  4400. rightmost_el = &eb->h_list;
  4401. } else
  4402. rightmost_el = path_root_el(path);
  4403. if (rec->e_cpos == split_rec->e_cpos &&
  4404. rec->e_leaf_clusters == split_rec->e_leaf_clusters)
  4405. ctxt.c_split_covers_rec = 1;
  4406. else
  4407. ctxt.c_split_covers_rec = 0;
  4408. ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
  4409. mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
  4410. split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
  4411. ctxt.c_split_covers_rec);
  4412. if (ctxt.c_contig_type == CONTIG_NONE) {
  4413. if (ctxt.c_split_covers_rec)
  4414. ret = ocfs2_replace_extent_rec(handle, et, path, el,
  4415. split_index, split_rec);
  4416. else
  4417. ret = ocfs2_split_and_insert(handle, et, path,
  4418. &last_eb_bh, split_index,
  4419. split_rec, meta_ac);
  4420. if (ret)
  4421. mlog_errno(ret);
  4422. } else {
  4423. ret = ocfs2_try_to_merge_extent(handle, et, path,
  4424. split_index, split_rec,
  4425. dealloc, &ctxt);
  4426. if (ret)
  4427. mlog_errno(ret);
  4428. }
  4429. out:
  4430. brelse(last_eb_bh);
  4431. return ret;
  4432. }
  4433. /*
  4434. * Change the flags of the already-existing extent at cpos for len clusters.
  4435. *
  4436. * new_flags: the flags we want to set.
  4437. * clear_flags: the flags we want to clear.
  4438. * phys: the new physical offset we want this new extent starts from.
  4439. *
  4440. * If the existing extent is larger than the request, initiate a
  4441. * split. An attempt will be made at merging with adjacent extents.
  4442. *
  4443. * The caller is responsible for passing down meta_ac if we'll need it.
  4444. */
  4445. int ocfs2_change_extent_flag(handle_t *handle,
  4446. struct ocfs2_extent_tree *et,
  4447. u32 cpos, u32 len, u32 phys,
  4448. struct ocfs2_alloc_context *meta_ac,
  4449. struct ocfs2_cached_dealloc_ctxt *dealloc,
  4450. int new_flags, int clear_flags)
  4451. {
  4452. int ret, index;
  4453. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  4454. u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
  4455. struct ocfs2_extent_rec split_rec;
  4456. struct ocfs2_path *left_path = NULL;
  4457. struct ocfs2_extent_list *el;
  4458. struct ocfs2_extent_rec *rec;
  4459. left_path = ocfs2_new_path_from_et(et);
  4460. if (!left_path) {
  4461. ret = -ENOMEM;
  4462. mlog_errno(ret);
  4463. goto out;
  4464. }
  4465. ret = ocfs2_find_path(et->et_ci, left_path, cpos);
  4466. if (ret) {
  4467. mlog_errno(ret);
  4468. goto out;
  4469. }
  4470. el = path_leaf_el(left_path);
  4471. index = ocfs2_search_extent_list(el, cpos);
  4472. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  4473. ocfs2_error(sb,
  4474. "Owner %llu has an extent at cpos %u which can no "
  4475. "longer be found.\n",
  4476. (unsigned long long)
  4477. ocfs2_metadata_cache_owner(et->et_ci), cpos);
  4478. ret = -EROFS;
  4479. goto out;
  4480. }
  4481. ret = -EIO;
  4482. rec = &el->l_recs[index];
  4483. if (new_flags && (rec->e_flags & new_flags)) {
  4484. mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
  4485. "extent that already had them",
  4486. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4487. new_flags);
  4488. goto out;
  4489. }
  4490. if (clear_flags && !(rec->e_flags & clear_flags)) {
  4491. mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
  4492. "extent that didn't have them",
  4493. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4494. clear_flags);
  4495. goto out;
  4496. }
  4497. memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
  4498. split_rec.e_cpos = cpu_to_le32(cpos);
  4499. split_rec.e_leaf_clusters = cpu_to_le16(len);
  4500. split_rec.e_blkno = cpu_to_le64(start_blkno);
  4501. split_rec.e_flags = rec->e_flags;
  4502. if (new_flags)
  4503. split_rec.e_flags |= new_flags;
  4504. if (clear_flags)
  4505. split_rec.e_flags &= ~clear_flags;
  4506. ret = ocfs2_split_extent(handle, et, left_path,
  4507. index, &split_rec, meta_ac,
  4508. dealloc);
  4509. if (ret)
  4510. mlog_errno(ret);
  4511. out:
  4512. ocfs2_free_path(left_path);
  4513. return ret;
  4514. }
  4515. /*
  4516. * Mark the already-existing extent at cpos as written for len clusters.
  4517. * This removes the unwritten extent flag.
  4518. *
  4519. * If the existing extent is larger than the request, initiate a
  4520. * split. An attempt will be made at merging with adjacent extents.
  4521. *
  4522. * The caller is responsible for passing down meta_ac if we'll need it.
  4523. */
  4524. int ocfs2_mark_extent_written(struct inode *inode,
  4525. struct ocfs2_extent_tree *et,
  4526. handle_t *handle, u32 cpos, u32 len, u32 phys,
  4527. struct ocfs2_alloc_context *meta_ac,
  4528. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4529. {
  4530. int ret;
  4531. mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
  4532. inode->i_ino, cpos, len, phys);
  4533. if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
  4534. ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
  4535. "that are being written to, but the feature bit "
  4536. "is not set in the super block.",
  4537. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  4538. ret = -EROFS;
  4539. goto out;
  4540. }
  4541. /*
  4542. * XXX: This should be fixed up so that we just re-insert the
  4543. * next extent records.
  4544. */
  4545. ocfs2_et_extent_map_truncate(et, 0);
  4546. ret = ocfs2_change_extent_flag(handle, et, cpos,
  4547. len, phys, meta_ac, dealloc,
  4548. 0, OCFS2_EXT_UNWRITTEN);
  4549. if (ret)
  4550. mlog_errno(ret);
  4551. out:
  4552. return ret;
  4553. }
  4554. static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
  4555. struct ocfs2_path *path,
  4556. int index, u32 new_range,
  4557. struct ocfs2_alloc_context *meta_ac)
  4558. {
  4559. int ret, depth, credits = handle->h_buffer_credits;
  4560. struct buffer_head *last_eb_bh = NULL;
  4561. struct ocfs2_extent_block *eb;
  4562. struct ocfs2_extent_list *rightmost_el, *el;
  4563. struct ocfs2_extent_rec split_rec;
  4564. struct ocfs2_extent_rec *rec;
  4565. struct ocfs2_insert_type insert;
  4566. /*
  4567. * Setup the record to split before we grow the tree.
  4568. */
  4569. el = path_leaf_el(path);
  4570. rec = &el->l_recs[index];
  4571. ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  4572. &split_rec, new_range, rec);
  4573. depth = path->p_tree_depth;
  4574. if (depth > 0) {
  4575. ret = ocfs2_read_extent_block(et->et_ci,
  4576. ocfs2_et_get_last_eb_blk(et),
  4577. &last_eb_bh);
  4578. if (ret < 0) {
  4579. mlog_errno(ret);
  4580. goto out;
  4581. }
  4582. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  4583. rightmost_el = &eb->h_list;
  4584. } else
  4585. rightmost_el = path_leaf_el(path);
  4586. credits += path->p_tree_depth +
  4587. ocfs2_extend_meta_needed(et->et_root_el);
  4588. ret = ocfs2_extend_trans(handle, credits);
  4589. if (ret) {
  4590. mlog_errno(ret);
  4591. goto out;
  4592. }
  4593. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  4594. le16_to_cpu(rightmost_el->l_count)) {
  4595. ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
  4596. meta_ac);
  4597. if (ret) {
  4598. mlog_errno(ret);
  4599. goto out;
  4600. }
  4601. }
  4602. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  4603. insert.ins_appending = APPEND_NONE;
  4604. insert.ins_contig = CONTIG_NONE;
  4605. insert.ins_split = SPLIT_RIGHT;
  4606. insert.ins_tree_depth = depth;
  4607. ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
  4608. if (ret)
  4609. mlog_errno(ret);
  4610. out:
  4611. brelse(last_eb_bh);
  4612. return ret;
  4613. }
  4614. static int ocfs2_truncate_rec(handle_t *handle,
  4615. struct ocfs2_extent_tree *et,
  4616. struct ocfs2_path *path, int index,
  4617. struct ocfs2_cached_dealloc_ctxt *dealloc,
  4618. u32 cpos, u32 len)
  4619. {
  4620. int ret;
  4621. u32 left_cpos, rec_range, trunc_range;
  4622. int wants_rotate = 0, is_rightmost_tree_rec = 0;
  4623. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  4624. struct ocfs2_path *left_path = NULL;
  4625. struct ocfs2_extent_list *el = path_leaf_el(path);
  4626. struct ocfs2_extent_rec *rec;
  4627. struct ocfs2_extent_block *eb;
  4628. if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
  4629. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  4630. if (ret) {
  4631. mlog_errno(ret);
  4632. goto out;
  4633. }
  4634. index--;
  4635. }
  4636. if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
  4637. path->p_tree_depth) {
  4638. /*
  4639. * Check whether this is the rightmost tree record. If
  4640. * we remove all of this record or part of its right
  4641. * edge then an update of the record lengths above it
  4642. * will be required.
  4643. */
  4644. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  4645. if (eb->h_next_leaf_blk == 0)
  4646. is_rightmost_tree_rec = 1;
  4647. }
  4648. rec = &el->l_recs[index];
  4649. if (index == 0 && path->p_tree_depth &&
  4650. le32_to_cpu(rec->e_cpos) == cpos) {
  4651. /*
  4652. * Changing the leftmost offset (via partial or whole
  4653. * record truncate) of an interior (or rightmost) path
  4654. * means we have to update the subtree that is formed
  4655. * by this leaf and the one to it's left.
  4656. *
  4657. * There are two cases we can skip:
  4658. * 1) Path is the leftmost one in our btree.
  4659. * 2) The leaf is rightmost and will be empty after
  4660. * we remove the extent record - the rotate code
  4661. * knows how to update the newly formed edge.
  4662. */
  4663. ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
  4664. if (ret) {
  4665. mlog_errno(ret);
  4666. goto out;
  4667. }
  4668. if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
  4669. left_path = ocfs2_new_path_from_path(path);
  4670. if (!left_path) {
  4671. ret = -ENOMEM;
  4672. mlog_errno(ret);
  4673. goto out;
  4674. }
  4675. ret = ocfs2_find_path(et->et_ci, left_path,
  4676. left_cpos);
  4677. if (ret) {
  4678. mlog_errno(ret);
  4679. goto out;
  4680. }
  4681. }
  4682. }
  4683. ret = ocfs2_extend_rotate_transaction(handle, 0,
  4684. handle->h_buffer_credits,
  4685. path);
  4686. if (ret) {
  4687. mlog_errno(ret);
  4688. goto out;
  4689. }
  4690. ret = ocfs2_journal_access_path(et->et_ci, handle, path);
  4691. if (ret) {
  4692. mlog_errno(ret);
  4693. goto out;
  4694. }
  4695. ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
  4696. if (ret) {
  4697. mlog_errno(ret);
  4698. goto out;
  4699. }
  4700. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  4701. trunc_range = cpos + len;
  4702. if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
  4703. int next_free;
  4704. memset(rec, 0, sizeof(*rec));
  4705. ocfs2_cleanup_merge(el, index);
  4706. wants_rotate = 1;
  4707. next_free = le16_to_cpu(el->l_next_free_rec);
  4708. if (is_rightmost_tree_rec && next_free > 1) {
  4709. /*
  4710. * We skip the edge update if this path will
  4711. * be deleted by the rotate code.
  4712. */
  4713. rec = &el->l_recs[next_free - 1];
  4714. ocfs2_adjust_rightmost_records(handle, et, path,
  4715. rec);
  4716. }
  4717. } else if (le32_to_cpu(rec->e_cpos) == cpos) {
  4718. /* Remove leftmost portion of the record. */
  4719. le32_add_cpu(&rec->e_cpos, len);
  4720. le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
  4721. le16_add_cpu(&rec->e_leaf_clusters, -len);
  4722. } else if (rec_range == trunc_range) {
  4723. /* Remove rightmost portion of the record */
  4724. le16_add_cpu(&rec->e_leaf_clusters, -len);
  4725. if (is_rightmost_tree_rec)
  4726. ocfs2_adjust_rightmost_records(handle, et, path, rec);
  4727. } else {
  4728. /* Caller should have trapped this. */
  4729. mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
  4730. "(%u, %u)\n",
  4731. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4732. le32_to_cpu(rec->e_cpos),
  4733. le16_to_cpu(rec->e_leaf_clusters), cpos, len);
  4734. BUG();
  4735. }
  4736. if (left_path) {
  4737. int subtree_index;
  4738. subtree_index = ocfs2_find_subtree_root(et, left_path, path);
  4739. ocfs2_complete_edge_insert(handle, left_path, path,
  4740. subtree_index);
  4741. }
  4742. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  4743. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  4744. if (ret) {
  4745. mlog_errno(ret);
  4746. goto out;
  4747. }
  4748. out:
  4749. ocfs2_free_path(left_path);
  4750. return ret;
  4751. }
  4752. int ocfs2_remove_extent(handle_t *handle,
  4753. struct ocfs2_extent_tree *et,
  4754. u32 cpos, u32 len,
  4755. struct ocfs2_alloc_context *meta_ac,
  4756. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4757. {
  4758. int ret, index;
  4759. u32 rec_range, trunc_range;
  4760. struct ocfs2_extent_rec *rec;
  4761. struct ocfs2_extent_list *el;
  4762. struct ocfs2_path *path = NULL;
  4763. /*
  4764. * XXX: Why are we truncating to 0 instead of wherever this
  4765. * affects us?
  4766. */
  4767. ocfs2_et_extent_map_truncate(et, 0);
  4768. path = ocfs2_new_path_from_et(et);
  4769. if (!path) {
  4770. ret = -ENOMEM;
  4771. mlog_errno(ret);
  4772. goto out;
  4773. }
  4774. ret = ocfs2_find_path(et->et_ci, path, cpos);
  4775. if (ret) {
  4776. mlog_errno(ret);
  4777. goto out;
  4778. }
  4779. el = path_leaf_el(path);
  4780. index = ocfs2_search_extent_list(el, cpos);
  4781. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  4782. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4783. "Owner %llu has an extent at cpos %u which can no "
  4784. "longer be found.\n",
  4785. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4786. cpos);
  4787. ret = -EROFS;
  4788. goto out;
  4789. }
  4790. /*
  4791. * We have 3 cases of extent removal:
  4792. * 1) Range covers the entire extent rec
  4793. * 2) Range begins or ends on one edge of the extent rec
  4794. * 3) Range is in the middle of the extent rec (no shared edges)
  4795. *
  4796. * For case 1 we remove the extent rec and left rotate to
  4797. * fill the hole.
  4798. *
  4799. * For case 2 we just shrink the existing extent rec, with a
  4800. * tree update if the shrinking edge is also the edge of an
  4801. * extent block.
  4802. *
  4803. * For case 3 we do a right split to turn the extent rec into
  4804. * something case 2 can handle.
  4805. */
  4806. rec = &el->l_recs[index];
  4807. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  4808. trunc_range = cpos + len;
  4809. BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
  4810. mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
  4811. "(cpos %u, len %u)\n",
  4812. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4813. cpos, len, index,
  4814. le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
  4815. if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
  4816. ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
  4817. cpos, len);
  4818. if (ret) {
  4819. mlog_errno(ret);
  4820. goto out;
  4821. }
  4822. } else {
  4823. ret = ocfs2_split_tree(handle, et, path, index,
  4824. trunc_range, meta_ac);
  4825. if (ret) {
  4826. mlog_errno(ret);
  4827. goto out;
  4828. }
  4829. /*
  4830. * The split could have manipulated the tree enough to
  4831. * move the record location, so we have to look for it again.
  4832. */
  4833. ocfs2_reinit_path(path, 1);
  4834. ret = ocfs2_find_path(et->et_ci, path, cpos);
  4835. if (ret) {
  4836. mlog_errno(ret);
  4837. goto out;
  4838. }
  4839. el = path_leaf_el(path);
  4840. index = ocfs2_search_extent_list(el, cpos);
  4841. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  4842. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4843. "Owner %llu: split at cpos %u lost record.",
  4844. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4845. cpos);
  4846. ret = -EROFS;
  4847. goto out;
  4848. }
  4849. /*
  4850. * Double check our values here. If anything is fishy,
  4851. * it's easier to catch it at the top level.
  4852. */
  4853. rec = &el->l_recs[index];
  4854. rec_range = le32_to_cpu(rec->e_cpos) +
  4855. ocfs2_rec_clusters(el, rec);
  4856. if (rec_range != trunc_range) {
  4857. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4858. "Owner %llu: error after split at cpos %u"
  4859. "trunc len %u, existing record is (%u,%u)",
  4860. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4861. cpos, len, le32_to_cpu(rec->e_cpos),
  4862. ocfs2_rec_clusters(el, rec));
  4863. ret = -EROFS;
  4864. goto out;
  4865. }
  4866. ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
  4867. cpos, len);
  4868. if (ret) {
  4869. mlog_errno(ret);
  4870. goto out;
  4871. }
  4872. }
  4873. out:
  4874. ocfs2_free_path(path);
  4875. return ret;
  4876. }
  4877. int ocfs2_remove_btree_range(struct inode *inode,
  4878. struct ocfs2_extent_tree *et,
  4879. u32 cpos, u32 phys_cpos, u32 len,
  4880. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4881. {
  4882. int ret;
  4883. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  4884. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4885. struct inode *tl_inode = osb->osb_tl_inode;
  4886. handle_t *handle;
  4887. struct ocfs2_alloc_context *meta_ac = NULL;
  4888. ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
  4889. if (ret) {
  4890. mlog_errno(ret);
  4891. return ret;
  4892. }
  4893. mutex_lock(&tl_inode->i_mutex);
  4894. if (ocfs2_truncate_log_needs_flush(osb)) {
  4895. ret = __ocfs2_flush_truncate_log(osb);
  4896. if (ret < 0) {
  4897. mlog_errno(ret);
  4898. goto out;
  4899. }
  4900. }
  4901. handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
  4902. if (IS_ERR(handle)) {
  4903. ret = PTR_ERR(handle);
  4904. mlog_errno(ret);
  4905. goto out;
  4906. }
  4907. ret = ocfs2_et_root_journal_access(handle, et,
  4908. OCFS2_JOURNAL_ACCESS_WRITE);
  4909. if (ret) {
  4910. mlog_errno(ret);
  4911. goto out;
  4912. }
  4913. vfs_dq_free_space_nodirty(inode,
  4914. ocfs2_clusters_to_bytes(inode->i_sb, len));
  4915. ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
  4916. if (ret) {
  4917. mlog_errno(ret);
  4918. goto out_commit;
  4919. }
  4920. ocfs2_et_update_clusters(et, -len);
  4921. ret = ocfs2_journal_dirty(handle, et->et_root_bh);
  4922. if (ret) {
  4923. mlog_errno(ret);
  4924. goto out_commit;
  4925. }
  4926. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  4927. if (ret)
  4928. mlog_errno(ret);
  4929. out_commit:
  4930. ocfs2_commit_trans(osb, handle);
  4931. out:
  4932. mutex_unlock(&tl_inode->i_mutex);
  4933. if (meta_ac)
  4934. ocfs2_free_alloc_context(meta_ac);
  4935. return ret;
  4936. }
  4937. int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
  4938. {
  4939. struct buffer_head *tl_bh = osb->osb_tl_bh;
  4940. struct ocfs2_dinode *di;
  4941. struct ocfs2_truncate_log *tl;
  4942. di = (struct ocfs2_dinode *) tl_bh->b_data;
  4943. tl = &di->id2.i_dealloc;
  4944. mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
  4945. "slot %d, invalid truncate log parameters: used = "
  4946. "%u, count = %u\n", osb->slot_num,
  4947. le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
  4948. return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
  4949. }
  4950. static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
  4951. unsigned int new_start)
  4952. {
  4953. unsigned int tail_index;
  4954. unsigned int current_tail;
  4955. /* No records, nothing to coalesce */
  4956. if (!le16_to_cpu(tl->tl_used))
  4957. return 0;
  4958. tail_index = le16_to_cpu(tl->tl_used) - 1;
  4959. current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
  4960. current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
  4961. return current_tail == new_start;
  4962. }
  4963. int ocfs2_truncate_log_append(struct ocfs2_super *osb,
  4964. handle_t *handle,
  4965. u64 start_blk,
  4966. unsigned int num_clusters)
  4967. {
  4968. int status, index;
  4969. unsigned int start_cluster, tl_count;
  4970. struct inode *tl_inode = osb->osb_tl_inode;
  4971. struct buffer_head *tl_bh = osb->osb_tl_bh;
  4972. struct ocfs2_dinode *di;
  4973. struct ocfs2_truncate_log *tl;
  4974. mlog_entry("start_blk = %llu, num_clusters = %u\n",
  4975. (unsigned long long)start_blk, num_clusters);
  4976. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  4977. start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
  4978. di = (struct ocfs2_dinode *) tl_bh->b_data;
  4979. /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
  4980. * by the underlying call to ocfs2_read_inode_block(), so any
  4981. * corruption is a code bug */
  4982. BUG_ON(!OCFS2_IS_VALID_DINODE(di));
  4983. tl = &di->id2.i_dealloc;
  4984. tl_count = le16_to_cpu(tl->tl_count);
  4985. mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
  4986. tl_count == 0,
  4987. "Truncate record count on #%llu invalid "
  4988. "wanted %u, actual %u\n",
  4989. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
  4990. ocfs2_truncate_recs_per_inode(osb->sb),
  4991. le16_to_cpu(tl->tl_count));
  4992. /* Caller should have known to flush before calling us. */
  4993. index = le16_to_cpu(tl->tl_used);
  4994. if (index >= tl_count) {
  4995. status = -ENOSPC;
  4996. mlog_errno(status);
  4997. goto bail;
  4998. }
  4999. status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
  5000. OCFS2_JOURNAL_ACCESS_WRITE);
  5001. if (status < 0) {
  5002. mlog_errno(status);
  5003. goto bail;
  5004. }
  5005. mlog(0, "Log truncate of %u clusters starting at cluster %u to "
  5006. "%llu (index = %d)\n", num_clusters, start_cluster,
  5007. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
  5008. if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
  5009. /*
  5010. * Move index back to the record we are coalescing with.
  5011. * ocfs2_truncate_log_can_coalesce() guarantees nonzero
  5012. */
  5013. index--;
  5014. num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
  5015. mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
  5016. index, le32_to_cpu(tl->tl_recs[index].t_start),
  5017. num_clusters);
  5018. } else {
  5019. tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
  5020. tl->tl_used = cpu_to_le16(index + 1);
  5021. }
  5022. tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
  5023. status = ocfs2_journal_dirty(handle, tl_bh);
  5024. if (status < 0) {
  5025. mlog_errno(status);
  5026. goto bail;
  5027. }
  5028. bail:
  5029. mlog_exit(status);
  5030. return status;
  5031. }
  5032. static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
  5033. handle_t *handle,
  5034. struct inode *data_alloc_inode,
  5035. struct buffer_head *data_alloc_bh)
  5036. {
  5037. int status = 0;
  5038. int i;
  5039. unsigned int num_clusters;
  5040. u64 start_blk;
  5041. struct ocfs2_truncate_rec rec;
  5042. struct ocfs2_dinode *di;
  5043. struct ocfs2_truncate_log *tl;
  5044. struct inode *tl_inode = osb->osb_tl_inode;
  5045. struct buffer_head *tl_bh = osb->osb_tl_bh;
  5046. mlog_entry_void();
  5047. di = (struct ocfs2_dinode *) tl_bh->b_data;
  5048. tl = &di->id2.i_dealloc;
  5049. i = le16_to_cpu(tl->tl_used) - 1;
  5050. while (i >= 0) {
  5051. /* Caller has given us at least enough credits to
  5052. * update the truncate log dinode */
  5053. status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
  5054. OCFS2_JOURNAL_ACCESS_WRITE);
  5055. if (status < 0) {
  5056. mlog_errno(status);
  5057. goto bail;
  5058. }
  5059. tl->tl_used = cpu_to_le16(i);
  5060. status = ocfs2_journal_dirty(handle, tl_bh);
  5061. if (status < 0) {
  5062. mlog_errno(status);
  5063. goto bail;
  5064. }
  5065. /* TODO: Perhaps we can calculate the bulk of the
  5066. * credits up front rather than extending like
  5067. * this. */
  5068. status = ocfs2_extend_trans(handle,
  5069. OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
  5070. if (status < 0) {
  5071. mlog_errno(status);
  5072. goto bail;
  5073. }
  5074. rec = tl->tl_recs[i];
  5075. start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
  5076. le32_to_cpu(rec.t_start));
  5077. num_clusters = le32_to_cpu(rec.t_clusters);
  5078. /* if start_blk is not set, we ignore the record as
  5079. * invalid. */
  5080. if (start_blk) {
  5081. mlog(0, "free record %d, start = %u, clusters = %u\n",
  5082. i, le32_to_cpu(rec.t_start), num_clusters);
  5083. status = ocfs2_free_clusters(handle, data_alloc_inode,
  5084. data_alloc_bh, start_blk,
  5085. num_clusters);
  5086. if (status < 0) {
  5087. mlog_errno(status);
  5088. goto bail;
  5089. }
  5090. }
  5091. i--;
  5092. }
  5093. bail:
  5094. mlog_exit(status);
  5095. return status;
  5096. }
  5097. /* Expects you to already be holding tl_inode->i_mutex */
  5098. int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  5099. {
  5100. int status;
  5101. unsigned int num_to_flush;
  5102. handle_t *handle;
  5103. struct inode *tl_inode = osb->osb_tl_inode;
  5104. struct inode *data_alloc_inode = NULL;
  5105. struct buffer_head *tl_bh = osb->osb_tl_bh;
  5106. struct buffer_head *data_alloc_bh = NULL;
  5107. struct ocfs2_dinode *di;
  5108. struct ocfs2_truncate_log *tl;
  5109. mlog_entry_void();
  5110. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  5111. di = (struct ocfs2_dinode *) tl_bh->b_data;
  5112. /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
  5113. * by the underlying call to ocfs2_read_inode_block(), so any
  5114. * corruption is a code bug */
  5115. BUG_ON(!OCFS2_IS_VALID_DINODE(di));
  5116. tl = &di->id2.i_dealloc;
  5117. num_to_flush = le16_to_cpu(tl->tl_used);
  5118. mlog(0, "Flush %u records from truncate log #%llu\n",
  5119. num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
  5120. if (!num_to_flush) {
  5121. status = 0;
  5122. goto out;
  5123. }
  5124. data_alloc_inode = ocfs2_get_system_file_inode(osb,
  5125. GLOBAL_BITMAP_SYSTEM_INODE,
  5126. OCFS2_INVALID_SLOT);
  5127. if (!data_alloc_inode) {
  5128. status = -EINVAL;
  5129. mlog(ML_ERROR, "Could not get bitmap inode!\n");
  5130. goto out;
  5131. }
  5132. mutex_lock(&data_alloc_inode->i_mutex);
  5133. status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
  5134. if (status < 0) {
  5135. mlog_errno(status);
  5136. goto out_mutex;
  5137. }
  5138. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  5139. if (IS_ERR(handle)) {
  5140. status = PTR_ERR(handle);
  5141. mlog_errno(status);
  5142. goto out_unlock;
  5143. }
  5144. status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
  5145. data_alloc_bh);
  5146. if (status < 0)
  5147. mlog_errno(status);
  5148. ocfs2_commit_trans(osb, handle);
  5149. out_unlock:
  5150. brelse(data_alloc_bh);
  5151. ocfs2_inode_unlock(data_alloc_inode, 1);
  5152. out_mutex:
  5153. mutex_unlock(&data_alloc_inode->i_mutex);
  5154. iput(data_alloc_inode);
  5155. out:
  5156. mlog_exit(status);
  5157. return status;
  5158. }
  5159. int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  5160. {
  5161. int status;
  5162. struct inode *tl_inode = osb->osb_tl_inode;
  5163. mutex_lock(&tl_inode->i_mutex);
  5164. status = __ocfs2_flush_truncate_log(osb);
  5165. mutex_unlock(&tl_inode->i_mutex);
  5166. return status;
  5167. }
  5168. static void ocfs2_truncate_log_worker(struct work_struct *work)
  5169. {
  5170. int status;
  5171. struct ocfs2_super *osb =
  5172. container_of(work, struct ocfs2_super,
  5173. osb_truncate_log_wq.work);
  5174. mlog_entry_void();
  5175. status = ocfs2_flush_truncate_log(osb);
  5176. if (status < 0)
  5177. mlog_errno(status);
  5178. else
  5179. ocfs2_init_steal_slots(osb);
  5180. mlog_exit(status);
  5181. }
  5182. #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
  5183. void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
  5184. int cancel)
  5185. {
  5186. if (osb->osb_tl_inode) {
  5187. /* We want to push off log flushes while truncates are
  5188. * still running. */
  5189. if (cancel)
  5190. cancel_delayed_work(&osb->osb_truncate_log_wq);
  5191. queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
  5192. OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
  5193. }
  5194. }
  5195. static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
  5196. int slot_num,
  5197. struct inode **tl_inode,
  5198. struct buffer_head **tl_bh)
  5199. {
  5200. int status;
  5201. struct inode *inode = NULL;
  5202. struct buffer_head *bh = NULL;
  5203. inode = ocfs2_get_system_file_inode(osb,
  5204. TRUNCATE_LOG_SYSTEM_INODE,
  5205. slot_num);
  5206. if (!inode) {
  5207. status = -EINVAL;
  5208. mlog(ML_ERROR, "Could not get load truncate log inode!\n");
  5209. goto bail;
  5210. }
  5211. status = ocfs2_read_inode_block(inode, &bh);
  5212. if (status < 0) {
  5213. iput(inode);
  5214. mlog_errno(status);
  5215. goto bail;
  5216. }
  5217. *tl_inode = inode;
  5218. *tl_bh = bh;
  5219. bail:
  5220. mlog_exit(status);
  5221. return status;
  5222. }
  5223. /* called during the 1st stage of node recovery. we stamp a clean
  5224. * truncate log and pass back a copy for processing later. if the
  5225. * truncate log does not require processing, a *tl_copy is set to
  5226. * NULL. */
  5227. int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
  5228. int slot_num,
  5229. struct ocfs2_dinode **tl_copy)
  5230. {
  5231. int status;
  5232. struct inode *tl_inode = NULL;
  5233. struct buffer_head *tl_bh = NULL;
  5234. struct ocfs2_dinode *di;
  5235. struct ocfs2_truncate_log *tl;
  5236. *tl_copy = NULL;
  5237. mlog(0, "recover truncate log from slot %d\n", slot_num);
  5238. status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
  5239. if (status < 0) {
  5240. mlog_errno(status);
  5241. goto bail;
  5242. }
  5243. di = (struct ocfs2_dinode *) tl_bh->b_data;
  5244. /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
  5245. * validated by the underlying call to ocfs2_read_inode_block(),
  5246. * so any corruption is a code bug */
  5247. BUG_ON(!OCFS2_IS_VALID_DINODE(di));
  5248. tl = &di->id2.i_dealloc;
  5249. if (le16_to_cpu(tl->tl_used)) {
  5250. mlog(0, "We'll have %u logs to recover\n",
  5251. le16_to_cpu(tl->tl_used));
  5252. *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
  5253. if (!(*tl_copy)) {
  5254. status = -ENOMEM;
  5255. mlog_errno(status);
  5256. goto bail;
  5257. }
  5258. /* Assuming the write-out below goes well, this copy
  5259. * will be passed back to recovery for processing. */
  5260. memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
  5261. /* All we need to do to clear the truncate log is set
  5262. * tl_used. */
  5263. tl->tl_used = 0;
  5264. ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
  5265. status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
  5266. if (status < 0) {
  5267. mlog_errno(status);
  5268. goto bail;
  5269. }
  5270. }
  5271. bail:
  5272. if (tl_inode)
  5273. iput(tl_inode);
  5274. brelse(tl_bh);
  5275. if (status < 0 && (*tl_copy)) {
  5276. kfree(*tl_copy);
  5277. *tl_copy = NULL;
  5278. }
  5279. mlog_exit(status);
  5280. return status;
  5281. }
  5282. int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
  5283. struct ocfs2_dinode *tl_copy)
  5284. {
  5285. int status = 0;
  5286. int i;
  5287. unsigned int clusters, num_recs, start_cluster;
  5288. u64 start_blk;
  5289. handle_t *handle;
  5290. struct inode *tl_inode = osb->osb_tl_inode;
  5291. struct ocfs2_truncate_log *tl;
  5292. mlog_entry_void();
  5293. if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
  5294. mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
  5295. return -EINVAL;
  5296. }
  5297. tl = &tl_copy->id2.i_dealloc;
  5298. num_recs = le16_to_cpu(tl->tl_used);
  5299. mlog(0, "cleanup %u records from %llu\n", num_recs,
  5300. (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
  5301. mutex_lock(&tl_inode->i_mutex);
  5302. for(i = 0; i < num_recs; i++) {
  5303. if (ocfs2_truncate_log_needs_flush(osb)) {
  5304. status = __ocfs2_flush_truncate_log(osb);
  5305. if (status < 0) {
  5306. mlog_errno(status);
  5307. goto bail_up;
  5308. }
  5309. }
  5310. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  5311. if (IS_ERR(handle)) {
  5312. status = PTR_ERR(handle);
  5313. mlog_errno(status);
  5314. goto bail_up;
  5315. }
  5316. clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
  5317. start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
  5318. start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
  5319. status = ocfs2_truncate_log_append(osb, handle,
  5320. start_blk, clusters);
  5321. ocfs2_commit_trans(osb, handle);
  5322. if (status < 0) {
  5323. mlog_errno(status);
  5324. goto bail_up;
  5325. }
  5326. }
  5327. bail_up:
  5328. mutex_unlock(&tl_inode->i_mutex);
  5329. mlog_exit(status);
  5330. return status;
  5331. }
  5332. void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
  5333. {
  5334. int status;
  5335. struct inode *tl_inode = osb->osb_tl_inode;
  5336. mlog_entry_void();
  5337. if (tl_inode) {
  5338. cancel_delayed_work(&osb->osb_truncate_log_wq);
  5339. flush_workqueue(ocfs2_wq);
  5340. status = ocfs2_flush_truncate_log(osb);
  5341. if (status < 0)
  5342. mlog_errno(status);
  5343. brelse(osb->osb_tl_bh);
  5344. iput(osb->osb_tl_inode);
  5345. }
  5346. mlog_exit_void();
  5347. }
  5348. int ocfs2_truncate_log_init(struct ocfs2_super *osb)
  5349. {
  5350. int status;
  5351. struct inode *tl_inode = NULL;
  5352. struct buffer_head *tl_bh = NULL;
  5353. mlog_entry_void();
  5354. status = ocfs2_get_truncate_log_info(osb,
  5355. osb->slot_num,
  5356. &tl_inode,
  5357. &tl_bh);
  5358. if (status < 0)
  5359. mlog_errno(status);
  5360. /* ocfs2_truncate_log_shutdown keys on the existence of
  5361. * osb->osb_tl_inode so we don't set any of the osb variables
  5362. * until we're sure all is well. */
  5363. INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
  5364. ocfs2_truncate_log_worker);
  5365. osb->osb_tl_bh = tl_bh;
  5366. osb->osb_tl_inode = tl_inode;
  5367. mlog_exit(status);
  5368. return status;
  5369. }
  5370. /*
  5371. * Delayed de-allocation of suballocator blocks.
  5372. *
  5373. * Some sets of block de-allocations might involve multiple suballocator inodes.
  5374. *
  5375. * The locking for this can get extremely complicated, especially when
  5376. * the suballocator inodes to delete from aren't known until deep
  5377. * within an unrelated codepath.
  5378. *
  5379. * ocfs2_extent_block structures are a good example of this - an inode
  5380. * btree could have been grown by any number of nodes each allocating
  5381. * out of their own suballoc inode.
  5382. *
  5383. * These structures allow the delay of block de-allocation until a
  5384. * later time, when locking of multiple cluster inodes won't cause
  5385. * deadlock.
  5386. */
  5387. /*
  5388. * Describe a single bit freed from a suballocator. For the block
  5389. * suballocators, it represents one block. For the global cluster
  5390. * allocator, it represents some clusters and free_bit indicates
  5391. * clusters number.
  5392. */
  5393. struct ocfs2_cached_block_free {
  5394. struct ocfs2_cached_block_free *free_next;
  5395. u64 free_blk;
  5396. unsigned int free_bit;
  5397. };
  5398. struct ocfs2_per_slot_free_list {
  5399. struct ocfs2_per_slot_free_list *f_next_suballocator;
  5400. int f_inode_type;
  5401. int f_slot;
  5402. struct ocfs2_cached_block_free *f_first;
  5403. };
  5404. static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
  5405. int sysfile_type,
  5406. int slot,
  5407. struct ocfs2_cached_block_free *head)
  5408. {
  5409. int ret;
  5410. u64 bg_blkno;
  5411. handle_t *handle;
  5412. struct inode *inode;
  5413. struct buffer_head *di_bh = NULL;
  5414. struct ocfs2_cached_block_free *tmp;
  5415. inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
  5416. if (!inode) {
  5417. ret = -EINVAL;
  5418. mlog_errno(ret);
  5419. goto out;
  5420. }
  5421. mutex_lock(&inode->i_mutex);
  5422. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  5423. if (ret) {
  5424. mlog_errno(ret);
  5425. goto out_mutex;
  5426. }
  5427. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  5428. if (IS_ERR(handle)) {
  5429. ret = PTR_ERR(handle);
  5430. mlog_errno(ret);
  5431. goto out_unlock;
  5432. }
  5433. while (head) {
  5434. bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
  5435. head->free_bit);
  5436. mlog(0, "Free bit: (bit %u, blkno %llu)\n",
  5437. head->free_bit, (unsigned long long)head->free_blk);
  5438. ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
  5439. head->free_bit, bg_blkno, 1);
  5440. if (ret) {
  5441. mlog_errno(ret);
  5442. goto out_journal;
  5443. }
  5444. ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
  5445. if (ret) {
  5446. mlog_errno(ret);
  5447. goto out_journal;
  5448. }
  5449. tmp = head;
  5450. head = head->free_next;
  5451. kfree(tmp);
  5452. }
  5453. out_journal:
  5454. ocfs2_commit_trans(osb, handle);
  5455. out_unlock:
  5456. ocfs2_inode_unlock(inode, 1);
  5457. brelse(di_bh);
  5458. out_mutex:
  5459. mutex_unlock(&inode->i_mutex);
  5460. iput(inode);
  5461. out:
  5462. while(head) {
  5463. /* Premature exit may have left some dangling items. */
  5464. tmp = head;
  5465. head = head->free_next;
  5466. kfree(tmp);
  5467. }
  5468. return ret;
  5469. }
  5470. int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
  5471. u64 blkno, unsigned int bit)
  5472. {
  5473. int ret = 0;
  5474. struct ocfs2_cached_block_free *item;
  5475. item = kmalloc(sizeof(*item), GFP_NOFS);
  5476. if (item == NULL) {
  5477. ret = -ENOMEM;
  5478. mlog_errno(ret);
  5479. return ret;
  5480. }
  5481. mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
  5482. bit, (unsigned long long)blkno);
  5483. item->free_blk = blkno;
  5484. item->free_bit = bit;
  5485. item->free_next = ctxt->c_global_allocator;
  5486. ctxt->c_global_allocator = item;
  5487. return ret;
  5488. }
  5489. static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
  5490. struct ocfs2_cached_block_free *head)
  5491. {
  5492. struct ocfs2_cached_block_free *tmp;
  5493. struct inode *tl_inode = osb->osb_tl_inode;
  5494. handle_t *handle;
  5495. int ret = 0;
  5496. mutex_lock(&tl_inode->i_mutex);
  5497. while (head) {
  5498. if (ocfs2_truncate_log_needs_flush(osb)) {
  5499. ret = __ocfs2_flush_truncate_log(osb);
  5500. if (ret < 0) {
  5501. mlog_errno(ret);
  5502. break;
  5503. }
  5504. }
  5505. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  5506. if (IS_ERR(handle)) {
  5507. ret = PTR_ERR(handle);
  5508. mlog_errno(ret);
  5509. break;
  5510. }
  5511. ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
  5512. head->free_bit);
  5513. ocfs2_commit_trans(osb, handle);
  5514. tmp = head;
  5515. head = head->free_next;
  5516. kfree(tmp);
  5517. if (ret < 0) {
  5518. mlog_errno(ret);
  5519. break;
  5520. }
  5521. }
  5522. mutex_unlock(&tl_inode->i_mutex);
  5523. while (head) {
  5524. /* Premature exit may have left some dangling items. */
  5525. tmp = head;
  5526. head = head->free_next;
  5527. kfree(tmp);
  5528. }
  5529. return ret;
  5530. }
  5531. int ocfs2_run_deallocs(struct ocfs2_super *osb,
  5532. struct ocfs2_cached_dealloc_ctxt *ctxt)
  5533. {
  5534. int ret = 0, ret2;
  5535. struct ocfs2_per_slot_free_list *fl;
  5536. if (!ctxt)
  5537. return 0;
  5538. while (ctxt->c_first_suballocator) {
  5539. fl = ctxt->c_first_suballocator;
  5540. if (fl->f_first) {
  5541. mlog(0, "Free items: (type %u, slot %d)\n",
  5542. fl->f_inode_type, fl->f_slot);
  5543. ret2 = ocfs2_free_cached_blocks(osb,
  5544. fl->f_inode_type,
  5545. fl->f_slot,
  5546. fl->f_first);
  5547. if (ret2)
  5548. mlog_errno(ret2);
  5549. if (!ret)
  5550. ret = ret2;
  5551. }
  5552. ctxt->c_first_suballocator = fl->f_next_suballocator;
  5553. kfree(fl);
  5554. }
  5555. if (ctxt->c_global_allocator) {
  5556. ret2 = ocfs2_free_cached_clusters(osb,
  5557. ctxt->c_global_allocator);
  5558. if (ret2)
  5559. mlog_errno(ret2);
  5560. if (!ret)
  5561. ret = ret2;
  5562. ctxt->c_global_allocator = NULL;
  5563. }
  5564. return ret;
  5565. }
  5566. static struct ocfs2_per_slot_free_list *
  5567. ocfs2_find_per_slot_free_list(int type,
  5568. int slot,
  5569. struct ocfs2_cached_dealloc_ctxt *ctxt)
  5570. {
  5571. struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
  5572. while (fl) {
  5573. if (fl->f_inode_type == type && fl->f_slot == slot)
  5574. return fl;
  5575. fl = fl->f_next_suballocator;
  5576. }
  5577. fl = kmalloc(sizeof(*fl), GFP_NOFS);
  5578. if (fl) {
  5579. fl->f_inode_type = type;
  5580. fl->f_slot = slot;
  5581. fl->f_first = NULL;
  5582. fl->f_next_suballocator = ctxt->c_first_suballocator;
  5583. ctxt->c_first_suballocator = fl;
  5584. }
  5585. return fl;
  5586. }
  5587. int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
  5588. int type, int slot, u64 blkno,
  5589. unsigned int bit)
  5590. {
  5591. int ret;
  5592. struct ocfs2_per_slot_free_list *fl;
  5593. struct ocfs2_cached_block_free *item;
  5594. fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
  5595. if (fl == NULL) {
  5596. ret = -ENOMEM;
  5597. mlog_errno(ret);
  5598. goto out;
  5599. }
  5600. item = kmalloc(sizeof(*item), GFP_NOFS);
  5601. if (item == NULL) {
  5602. ret = -ENOMEM;
  5603. mlog_errno(ret);
  5604. goto out;
  5605. }
  5606. mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
  5607. type, slot, bit, (unsigned long long)blkno);
  5608. item->free_blk = blkno;
  5609. item->free_bit = bit;
  5610. item->free_next = fl->f_first;
  5611. fl->f_first = item;
  5612. ret = 0;
  5613. out:
  5614. return ret;
  5615. }
  5616. static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
  5617. struct ocfs2_extent_block *eb)
  5618. {
  5619. return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
  5620. le16_to_cpu(eb->h_suballoc_slot),
  5621. le64_to_cpu(eb->h_blkno),
  5622. le16_to_cpu(eb->h_suballoc_bit));
  5623. }
  5624. /* This function will figure out whether the currently last extent
  5625. * block will be deleted, and if it will, what the new last extent
  5626. * block will be so we can update his h_next_leaf_blk field, as well
  5627. * as the dinodes i_last_eb_blk */
  5628. static int ocfs2_find_new_last_ext_blk(struct inode *inode,
  5629. unsigned int clusters_to_del,
  5630. struct ocfs2_path *path,
  5631. struct buffer_head **new_last_eb)
  5632. {
  5633. int next_free, ret = 0;
  5634. u32 cpos;
  5635. struct ocfs2_extent_rec *rec;
  5636. struct ocfs2_extent_block *eb;
  5637. struct ocfs2_extent_list *el;
  5638. struct buffer_head *bh = NULL;
  5639. *new_last_eb = NULL;
  5640. /* we have no tree, so of course, no last_eb. */
  5641. if (!path->p_tree_depth)
  5642. goto out;
  5643. /* trunc to zero special case - this makes tree_depth = 0
  5644. * regardless of what it is. */
  5645. if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
  5646. goto out;
  5647. el = path_leaf_el(path);
  5648. BUG_ON(!el->l_next_free_rec);
  5649. /*
  5650. * Make sure that this extent list will actually be empty
  5651. * after we clear away the data. We can shortcut out if
  5652. * there's more than one non-empty extent in the
  5653. * list. Otherwise, a check of the remaining extent is
  5654. * necessary.
  5655. */
  5656. next_free = le16_to_cpu(el->l_next_free_rec);
  5657. rec = NULL;
  5658. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  5659. if (next_free > 2)
  5660. goto out;
  5661. /* We may have a valid extent in index 1, check it. */
  5662. if (next_free == 2)
  5663. rec = &el->l_recs[1];
  5664. /*
  5665. * Fall through - no more nonempty extents, so we want
  5666. * to delete this leaf.
  5667. */
  5668. } else {
  5669. if (next_free > 1)
  5670. goto out;
  5671. rec = &el->l_recs[0];
  5672. }
  5673. if (rec) {
  5674. /*
  5675. * Check it we'll only be trimming off the end of this
  5676. * cluster.
  5677. */
  5678. if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
  5679. goto out;
  5680. }
  5681. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
  5682. if (ret) {
  5683. mlog_errno(ret);
  5684. goto out;
  5685. }
  5686. ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
  5687. if (ret) {
  5688. mlog_errno(ret);
  5689. goto out;
  5690. }
  5691. eb = (struct ocfs2_extent_block *) bh->b_data;
  5692. el = &eb->h_list;
  5693. /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
  5694. * Any corruption is a code bug. */
  5695. BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
  5696. *new_last_eb = bh;
  5697. get_bh(*new_last_eb);
  5698. mlog(0, "returning block %llu, (cpos: %u)\n",
  5699. (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
  5700. out:
  5701. brelse(bh);
  5702. return ret;
  5703. }
  5704. /*
  5705. * Trim some clusters off the rightmost edge of a tree. Only called
  5706. * during truncate.
  5707. *
  5708. * The caller needs to:
  5709. * - start journaling of each path component.
  5710. * - compute and fully set up any new last ext block
  5711. */
  5712. static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
  5713. handle_t *handle, struct ocfs2_truncate_context *tc,
  5714. u32 clusters_to_del, u64 *delete_start, u8 *flags)
  5715. {
  5716. int ret, i, index = path->p_tree_depth;
  5717. u32 new_edge = 0;
  5718. u64 deleted_eb = 0;
  5719. struct buffer_head *bh;
  5720. struct ocfs2_extent_list *el;
  5721. struct ocfs2_extent_rec *rec;
  5722. *delete_start = 0;
  5723. *flags = 0;
  5724. while (index >= 0) {
  5725. bh = path->p_node[index].bh;
  5726. el = path->p_node[index].el;
  5727. mlog(0, "traveling tree (index = %d, block = %llu)\n",
  5728. index, (unsigned long long)bh->b_blocknr);
  5729. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  5730. if (index !=
  5731. (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
  5732. ocfs2_error(inode->i_sb,
  5733. "Inode %lu has invalid ext. block %llu",
  5734. inode->i_ino,
  5735. (unsigned long long)bh->b_blocknr);
  5736. ret = -EROFS;
  5737. goto out;
  5738. }
  5739. find_tail_record:
  5740. i = le16_to_cpu(el->l_next_free_rec) - 1;
  5741. rec = &el->l_recs[i];
  5742. mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
  5743. "next = %u\n", i, le32_to_cpu(rec->e_cpos),
  5744. ocfs2_rec_clusters(el, rec),
  5745. (unsigned long long)le64_to_cpu(rec->e_blkno),
  5746. le16_to_cpu(el->l_next_free_rec));
  5747. BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
  5748. if (le16_to_cpu(el->l_tree_depth) == 0) {
  5749. /*
  5750. * If the leaf block contains a single empty
  5751. * extent and no records, we can just remove
  5752. * the block.
  5753. */
  5754. if (i == 0 && ocfs2_is_empty_extent(rec)) {
  5755. memset(rec, 0,
  5756. sizeof(struct ocfs2_extent_rec));
  5757. el->l_next_free_rec = cpu_to_le16(0);
  5758. goto delete;
  5759. }
  5760. /*
  5761. * Remove any empty extents by shifting things
  5762. * left. That should make life much easier on
  5763. * the code below. This condition is rare
  5764. * enough that we shouldn't see a performance
  5765. * hit.
  5766. */
  5767. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  5768. le16_add_cpu(&el->l_next_free_rec, -1);
  5769. for(i = 0;
  5770. i < le16_to_cpu(el->l_next_free_rec); i++)
  5771. el->l_recs[i] = el->l_recs[i + 1];
  5772. memset(&el->l_recs[i], 0,
  5773. sizeof(struct ocfs2_extent_rec));
  5774. /*
  5775. * We've modified our extent list. The
  5776. * simplest way to handle this change
  5777. * is to being the search from the
  5778. * start again.
  5779. */
  5780. goto find_tail_record;
  5781. }
  5782. le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
  5783. /*
  5784. * We'll use "new_edge" on our way back up the
  5785. * tree to know what our rightmost cpos is.
  5786. */
  5787. new_edge = le16_to_cpu(rec->e_leaf_clusters);
  5788. new_edge += le32_to_cpu(rec->e_cpos);
  5789. /*
  5790. * The caller will use this to delete data blocks.
  5791. */
  5792. *delete_start = le64_to_cpu(rec->e_blkno)
  5793. + ocfs2_clusters_to_blocks(inode->i_sb,
  5794. le16_to_cpu(rec->e_leaf_clusters));
  5795. *flags = rec->e_flags;
  5796. /*
  5797. * If it's now empty, remove this record.
  5798. */
  5799. if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
  5800. memset(rec, 0,
  5801. sizeof(struct ocfs2_extent_rec));
  5802. le16_add_cpu(&el->l_next_free_rec, -1);
  5803. }
  5804. } else {
  5805. if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
  5806. memset(rec, 0,
  5807. sizeof(struct ocfs2_extent_rec));
  5808. le16_add_cpu(&el->l_next_free_rec, -1);
  5809. goto delete;
  5810. }
  5811. /* Can this actually happen? */
  5812. if (le16_to_cpu(el->l_next_free_rec) == 0)
  5813. goto delete;
  5814. /*
  5815. * We never actually deleted any clusters
  5816. * because our leaf was empty. There's no
  5817. * reason to adjust the rightmost edge then.
  5818. */
  5819. if (new_edge == 0)
  5820. goto delete;
  5821. rec->e_int_clusters = cpu_to_le32(new_edge);
  5822. le32_add_cpu(&rec->e_int_clusters,
  5823. -le32_to_cpu(rec->e_cpos));
  5824. /*
  5825. * A deleted child record should have been
  5826. * caught above.
  5827. */
  5828. BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
  5829. }
  5830. delete:
  5831. ret = ocfs2_journal_dirty(handle, bh);
  5832. if (ret) {
  5833. mlog_errno(ret);
  5834. goto out;
  5835. }
  5836. mlog(0, "extent list container %llu, after: record %d: "
  5837. "(%u, %u, %llu), next = %u.\n",
  5838. (unsigned long long)bh->b_blocknr, i,
  5839. le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
  5840. (unsigned long long)le64_to_cpu(rec->e_blkno),
  5841. le16_to_cpu(el->l_next_free_rec));
  5842. /*
  5843. * We must be careful to only attempt delete of an
  5844. * extent block (and not the root inode block).
  5845. */
  5846. if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
  5847. struct ocfs2_extent_block *eb =
  5848. (struct ocfs2_extent_block *)bh->b_data;
  5849. /*
  5850. * Save this for use when processing the
  5851. * parent block.
  5852. */
  5853. deleted_eb = le64_to_cpu(eb->h_blkno);
  5854. mlog(0, "deleting this extent block.\n");
  5855. ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
  5856. BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
  5857. BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
  5858. BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
  5859. ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
  5860. /* An error here is not fatal. */
  5861. if (ret < 0)
  5862. mlog_errno(ret);
  5863. } else {
  5864. deleted_eb = 0;
  5865. }
  5866. index--;
  5867. }
  5868. ret = 0;
  5869. out:
  5870. return ret;
  5871. }
  5872. static int ocfs2_do_truncate(struct ocfs2_super *osb,
  5873. unsigned int clusters_to_del,
  5874. struct inode *inode,
  5875. struct buffer_head *fe_bh,
  5876. handle_t *handle,
  5877. struct ocfs2_truncate_context *tc,
  5878. struct ocfs2_path *path,
  5879. struct ocfs2_alloc_context *meta_ac)
  5880. {
  5881. int status;
  5882. struct ocfs2_dinode *fe;
  5883. struct ocfs2_extent_block *last_eb = NULL;
  5884. struct ocfs2_extent_list *el;
  5885. struct buffer_head *last_eb_bh = NULL;
  5886. u64 delete_blk = 0;
  5887. u8 rec_flags;
  5888. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  5889. status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
  5890. path, &last_eb_bh);
  5891. if (status < 0) {
  5892. mlog_errno(status);
  5893. goto bail;
  5894. }
  5895. /*
  5896. * Each component will be touched, so we might as well journal
  5897. * here to avoid having to handle errors later.
  5898. */
  5899. status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
  5900. if (status < 0) {
  5901. mlog_errno(status);
  5902. goto bail;
  5903. }
  5904. if (last_eb_bh) {
  5905. status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
  5906. OCFS2_JOURNAL_ACCESS_WRITE);
  5907. if (status < 0) {
  5908. mlog_errno(status);
  5909. goto bail;
  5910. }
  5911. last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  5912. }
  5913. el = &(fe->id2.i_list);
  5914. /*
  5915. * Lower levels depend on this never happening, but it's best
  5916. * to check it up here before changing the tree.
  5917. */
  5918. if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
  5919. ocfs2_error(inode->i_sb,
  5920. "Inode %lu has an empty extent record, depth %u\n",
  5921. inode->i_ino, le16_to_cpu(el->l_tree_depth));
  5922. status = -EROFS;
  5923. goto bail;
  5924. }
  5925. vfs_dq_free_space_nodirty(inode,
  5926. ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
  5927. spin_lock(&OCFS2_I(inode)->ip_lock);
  5928. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
  5929. clusters_to_del;
  5930. spin_unlock(&OCFS2_I(inode)->ip_lock);
  5931. le32_add_cpu(&fe->i_clusters, -clusters_to_del);
  5932. inode->i_blocks = ocfs2_inode_sector_count(inode);
  5933. status = ocfs2_trim_tree(inode, path, handle, tc,
  5934. clusters_to_del, &delete_blk, &rec_flags);
  5935. if (status) {
  5936. mlog_errno(status);
  5937. goto bail;
  5938. }
  5939. if (le32_to_cpu(fe->i_clusters) == 0) {
  5940. /* trunc to zero is a special case. */
  5941. el->l_tree_depth = 0;
  5942. fe->i_last_eb_blk = 0;
  5943. } else if (last_eb)
  5944. fe->i_last_eb_blk = last_eb->h_blkno;
  5945. status = ocfs2_journal_dirty(handle, fe_bh);
  5946. if (status < 0) {
  5947. mlog_errno(status);
  5948. goto bail;
  5949. }
  5950. if (last_eb) {
  5951. /* If there will be a new last extent block, then by
  5952. * definition, there cannot be any leaves to the right of
  5953. * him. */
  5954. last_eb->h_next_leaf_blk = 0;
  5955. status = ocfs2_journal_dirty(handle, last_eb_bh);
  5956. if (status < 0) {
  5957. mlog_errno(status);
  5958. goto bail;
  5959. }
  5960. }
  5961. if (delete_blk) {
  5962. if (rec_flags & OCFS2_EXT_REFCOUNTED)
  5963. status = ocfs2_decrease_refcount(inode, handle,
  5964. ocfs2_blocks_to_clusters(osb->sb,
  5965. delete_blk),
  5966. clusters_to_del, meta_ac,
  5967. &tc->tc_dealloc, 1);
  5968. else
  5969. status = ocfs2_truncate_log_append(osb, handle,
  5970. delete_blk,
  5971. clusters_to_del);
  5972. if (status < 0) {
  5973. mlog_errno(status);
  5974. goto bail;
  5975. }
  5976. }
  5977. status = 0;
  5978. bail:
  5979. brelse(last_eb_bh);
  5980. mlog_exit(status);
  5981. return status;
  5982. }
  5983. static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
  5984. {
  5985. set_buffer_uptodate(bh);
  5986. mark_buffer_dirty(bh);
  5987. return 0;
  5988. }
  5989. void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
  5990. unsigned int from, unsigned int to,
  5991. struct page *page, int zero, u64 *phys)
  5992. {
  5993. int ret, partial = 0;
  5994. ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
  5995. if (ret)
  5996. mlog_errno(ret);
  5997. if (zero)
  5998. zero_user_segment(page, from, to);
  5999. /*
  6000. * Need to set the buffers we zero'd into uptodate
  6001. * here if they aren't - ocfs2_map_page_blocks()
  6002. * might've skipped some
  6003. */
  6004. ret = walk_page_buffers(handle, page_buffers(page),
  6005. from, to, &partial,
  6006. ocfs2_zero_func);
  6007. if (ret < 0)
  6008. mlog_errno(ret);
  6009. else if (ocfs2_should_order_data(inode)) {
  6010. ret = ocfs2_jbd2_file_inode(handle, inode);
  6011. if (ret < 0)
  6012. mlog_errno(ret);
  6013. }
  6014. if (!partial)
  6015. SetPageUptodate(page);
  6016. flush_dcache_page(page);
  6017. }
  6018. static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
  6019. loff_t end, struct page **pages,
  6020. int numpages, u64 phys, handle_t *handle)
  6021. {
  6022. int i;
  6023. struct page *page;
  6024. unsigned int from, to = PAGE_CACHE_SIZE;
  6025. struct super_block *sb = inode->i_sb;
  6026. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
  6027. if (numpages == 0)
  6028. goto out;
  6029. to = PAGE_CACHE_SIZE;
  6030. for(i = 0; i < numpages; i++) {
  6031. page = pages[i];
  6032. from = start & (PAGE_CACHE_SIZE - 1);
  6033. if ((end >> PAGE_CACHE_SHIFT) == page->index)
  6034. to = end & (PAGE_CACHE_SIZE - 1);
  6035. BUG_ON(from > PAGE_CACHE_SIZE);
  6036. BUG_ON(to > PAGE_CACHE_SIZE);
  6037. ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
  6038. &phys);
  6039. start = (page->index + 1) << PAGE_CACHE_SHIFT;
  6040. }
  6041. out:
  6042. if (pages)
  6043. ocfs2_unlock_and_free_pages(pages, numpages);
  6044. }
  6045. int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
  6046. struct page **pages, int *num)
  6047. {
  6048. int numpages, ret = 0;
  6049. struct address_space *mapping = inode->i_mapping;
  6050. unsigned long index;
  6051. loff_t last_page_bytes;
  6052. BUG_ON(start > end);
  6053. numpages = 0;
  6054. last_page_bytes = PAGE_ALIGN(end);
  6055. index = start >> PAGE_CACHE_SHIFT;
  6056. do {
  6057. pages[numpages] = grab_cache_page(mapping, index);
  6058. if (!pages[numpages]) {
  6059. ret = -ENOMEM;
  6060. mlog_errno(ret);
  6061. goto out;
  6062. }
  6063. numpages++;
  6064. index++;
  6065. } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
  6066. out:
  6067. if (ret != 0) {
  6068. if (pages)
  6069. ocfs2_unlock_and_free_pages(pages, numpages);
  6070. numpages = 0;
  6071. }
  6072. *num = numpages;
  6073. return ret;
  6074. }
  6075. static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
  6076. struct page **pages, int *num)
  6077. {
  6078. struct super_block *sb = inode->i_sb;
  6079. BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
  6080. (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
  6081. return ocfs2_grab_pages(inode, start, end, pages, num);
  6082. }
  6083. /*
  6084. * Zero the area past i_size but still within an allocated
  6085. * cluster. This avoids exposing nonzero data on subsequent file
  6086. * extends.
  6087. *
  6088. * We need to call this before i_size is updated on the inode because
  6089. * otherwise block_write_full_page() will skip writeout of pages past
  6090. * i_size. The new_i_size parameter is passed for this reason.
  6091. */
  6092. int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
  6093. u64 range_start, u64 range_end)
  6094. {
  6095. int ret = 0, numpages;
  6096. struct page **pages = NULL;
  6097. u64 phys;
  6098. unsigned int ext_flags;
  6099. struct super_block *sb = inode->i_sb;
  6100. /*
  6101. * File systems which don't support sparse files zero on every
  6102. * extend.
  6103. */
  6104. if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
  6105. return 0;
  6106. pages = kcalloc(ocfs2_pages_per_cluster(sb),
  6107. sizeof(struct page *), GFP_NOFS);
  6108. if (pages == NULL) {
  6109. ret = -ENOMEM;
  6110. mlog_errno(ret);
  6111. goto out;
  6112. }
  6113. if (range_start == range_end)
  6114. goto out;
  6115. ret = ocfs2_extent_map_get_blocks(inode,
  6116. range_start >> sb->s_blocksize_bits,
  6117. &phys, NULL, &ext_flags);
  6118. if (ret) {
  6119. mlog_errno(ret);
  6120. goto out;
  6121. }
  6122. /*
  6123. * Tail is a hole, or is marked unwritten. In either case, we
  6124. * can count on read and write to return/push zero's.
  6125. */
  6126. if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
  6127. goto out;
  6128. ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
  6129. &numpages);
  6130. if (ret) {
  6131. mlog_errno(ret);
  6132. goto out;
  6133. }
  6134. ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
  6135. numpages, phys, handle);
  6136. /*
  6137. * Initiate writeout of the pages we zero'd here. We don't
  6138. * wait on them - the truncate_inode_pages() call later will
  6139. * do that for us.
  6140. */
  6141. ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
  6142. range_end - 1);
  6143. if (ret)
  6144. mlog_errno(ret);
  6145. out:
  6146. if (pages)
  6147. kfree(pages);
  6148. return ret;
  6149. }
  6150. static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
  6151. struct ocfs2_dinode *di)
  6152. {
  6153. unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
  6154. unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
  6155. if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
  6156. memset(&di->id2, 0, blocksize -
  6157. offsetof(struct ocfs2_dinode, id2) -
  6158. xattrsize);
  6159. else
  6160. memset(&di->id2, 0, blocksize -
  6161. offsetof(struct ocfs2_dinode, id2));
  6162. }
  6163. void ocfs2_dinode_new_extent_list(struct inode *inode,
  6164. struct ocfs2_dinode *di)
  6165. {
  6166. ocfs2_zero_dinode_id2_with_xattr(inode, di);
  6167. di->id2.i_list.l_tree_depth = 0;
  6168. di->id2.i_list.l_next_free_rec = 0;
  6169. di->id2.i_list.l_count = cpu_to_le16(
  6170. ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
  6171. }
  6172. void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
  6173. {
  6174. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  6175. struct ocfs2_inline_data *idata = &di->id2.i_data;
  6176. spin_lock(&oi->ip_lock);
  6177. oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
  6178. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  6179. spin_unlock(&oi->ip_lock);
  6180. /*
  6181. * We clear the entire i_data structure here so that all
  6182. * fields can be properly initialized.
  6183. */
  6184. ocfs2_zero_dinode_id2_with_xattr(inode, di);
  6185. idata->id_count = cpu_to_le16(
  6186. ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
  6187. }
  6188. int ocfs2_convert_inline_data_to_extents(struct inode *inode,
  6189. struct buffer_head *di_bh)
  6190. {
  6191. int ret, i, has_data, num_pages = 0;
  6192. handle_t *handle;
  6193. u64 uninitialized_var(block);
  6194. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  6195. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  6196. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  6197. struct ocfs2_alloc_context *data_ac = NULL;
  6198. struct page **pages = NULL;
  6199. loff_t end = osb->s_clustersize;
  6200. struct ocfs2_extent_tree et;
  6201. int did_quota = 0;
  6202. has_data = i_size_read(inode) ? 1 : 0;
  6203. if (has_data) {
  6204. pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
  6205. sizeof(struct page *), GFP_NOFS);
  6206. if (pages == NULL) {
  6207. ret = -ENOMEM;
  6208. mlog_errno(ret);
  6209. goto out;
  6210. }
  6211. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  6212. if (ret) {
  6213. mlog_errno(ret);
  6214. goto out;
  6215. }
  6216. }
  6217. handle = ocfs2_start_trans(osb,
  6218. ocfs2_inline_to_extents_credits(osb->sb));
  6219. if (IS_ERR(handle)) {
  6220. ret = PTR_ERR(handle);
  6221. mlog_errno(ret);
  6222. goto out_unlock;
  6223. }
  6224. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  6225. OCFS2_JOURNAL_ACCESS_WRITE);
  6226. if (ret) {
  6227. mlog_errno(ret);
  6228. goto out_commit;
  6229. }
  6230. if (has_data) {
  6231. u32 bit_off, num;
  6232. unsigned int page_end;
  6233. u64 phys;
  6234. if (vfs_dq_alloc_space_nodirty(inode,
  6235. ocfs2_clusters_to_bytes(osb->sb, 1))) {
  6236. ret = -EDQUOT;
  6237. goto out_commit;
  6238. }
  6239. did_quota = 1;
  6240. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
  6241. &num);
  6242. if (ret) {
  6243. mlog_errno(ret);
  6244. goto out_commit;
  6245. }
  6246. /*
  6247. * Save two copies, one for insert, and one that can
  6248. * be changed by ocfs2_map_and_dirty_page() below.
  6249. */
  6250. block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  6251. /*
  6252. * Non sparse file systems zero on extend, so no need
  6253. * to do that now.
  6254. */
  6255. if (!ocfs2_sparse_alloc(osb) &&
  6256. PAGE_CACHE_SIZE < osb->s_clustersize)
  6257. end = PAGE_CACHE_SIZE;
  6258. ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
  6259. if (ret) {
  6260. mlog_errno(ret);
  6261. goto out_commit;
  6262. }
  6263. /*
  6264. * This should populate the 1st page for us and mark
  6265. * it up to date.
  6266. */
  6267. ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
  6268. if (ret) {
  6269. mlog_errno(ret);
  6270. goto out_commit;
  6271. }
  6272. page_end = PAGE_CACHE_SIZE;
  6273. if (PAGE_CACHE_SIZE > osb->s_clustersize)
  6274. page_end = osb->s_clustersize;
  6275. for (i = 0; i < num_pages; i++)
  6276. ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
  6277. pages[i], i > 0, &phys);
  6278. }
  6279. spin_lock(&oi->ip_lock);
  6280. oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
  6281. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  6282. spin_unlock(&oi->ip_lock);
  6283. ocfs2_dinode_new_extent_list(inode, di);
  6284. ocfs2_journal_dirty(handle, di_bh);
  6285. if (has_data) {
  6286. /*
  6287. * An error at this point should be extremely rare. If
  6288. * this proves to be false, we could always re-build
  6289. * the in-inode data from our pages.
  6290. */
  6291. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  6292. ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
  6293. if (ret) {
  6294. mlog_errno(ret);
  6295. goto out_commit;
  6296. }
  6297. inode->i_blocks = ocfs2_inode_sector_count(inode);
  6298. }
  6299. out_commit:
  6300. if (ret < 0 && did_quota)
  6301. vfs_dq_free_space_nodirty(inode,
  6302. ocfs2_clusters_to_bytes(osb->sb, 1));
  6303. ocfs2_commit_trans(osb, handle);
  6304. out_unlock:
  6305. if (data_ac)
  6306. ocfs2_free_alloc_context(data_ac);
  6307. out:
  6308. if (pages) {
  6309. ocfs2_unlock_and_free_pages(pages, num_pages);
  6310. kfree(pages);
  6311. }
  6312. return ret;
  6313. }
  6314. /*
  6315. * It is expected, that by the time you call this function,
  6316. * inode->i_size and fe->i_size have been adjusted.
  6317. *
  6318. * WARNING: This will kfree the truncate context
  6319. */
  6320. int ocfs2_commit_truncate(struct ocfs2_super *osb,
  6321. struct inode *inode,
  6322. struct buffer_head *fe_bh,
  6323. struct ocfs2_truncate_context *tc)
  6324. {
  6325. int status, i, credits, tl_sem = 0;
  6326. u32 clusters_to_del, new_highest_cpos, range;
  6327. u64 blkno = 0;
  6328. struct ocfs2_extent_list *el;
  6329. handle_t *handle = NULL;
  6330. struct inode *tl_inode = osb->osb_tl_inode;
  6331. struct ocfs2_path *path = NULL;
  6332. struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
  6333. struct ocfs2_alloc_context *meta_ac = NULL;
  6334. struct ocfs2_refcount_tree *ref_tree = NULL;
  6335. mlog_entry_void();
  6336. new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
  6337. i_size_read(inode));
  6338. path = ocfs2_new_path(fe_bh, &di->id2.i_list,
  6339. ocfs2_journal_access_di);
  6340. if (!path) {
  6341. status = -ENOMEM;
  6342. mlog_errno(status);
  6343. goto bail;
  6344. }
  6345. ocfs2_extent_map_trunc(inode, new_highest_cpos);
  6346. start:
  6347. /*
  6348. * Check that we still have allocation to delete.
  6349. */
  6350. if (OCFS2_I(inode)->ip_clusters == 0) {
  6351. status = 0;
  6352. goto bail;
  6353. }
  6354. credits = 0;
  6355. /*
  6356. * Truncate always works against the rightmost tree branch.
  6357. */
  6358. status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
  6359. if (status) {
  6360. mlog_errno(status);
  6361. goto bail;
  6362. }
  6363. mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
  6364. OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
  6365. /*
  6366. * By now, el will point to the extent list on the bottom most
  6367. * portion of this tree. Only the tail record is considered in
  6368. * each pass.
  6369. *
  6370. * We handle the following cases, in order:
  6371. * - empty extent: delete the remaining branch
  6372. * - remove the entire record
  6373. * - remove a partial record
  6374. * - no record needs to be removed (truncate has completed)
  6375. */
  6376. el = path_leaf_el(path);
  6377. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  6378. ocfs2_error(inode->i_sb,
  6379. "Inode %llu has empty extent block at %llu\n",
  6380. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  6381. (unsigned long long)path_leaf_bh(path)->b_blocknr);
  6382. status = -EROFS;
  6383. goto bail;
  6384. }
  6385. i = le16_to_cpu(el->l_next_free_rec) - 1;
  6386. range = le32_to_cpu(el->l_recs[i].e_cpos) +
  6387. ocfs2_rec_clusters(el, &el->l_recs[i]);
  6388. if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
  6389. clusters_to_del = 0;
  6390. } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
  6391. clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
  6392. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  6393. } else if (range > new_highest_cpos) {
  6394. clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
  6395. le32_to_cpu(el->l_recs[i].e_cpos)) -
  6396. new_highest_cpos;
  6397. blkno = le64_to_cpu(el->l_recs[i].e_blkno) +
  6398. ocfs2_clusters_to_blocks(inode->i_sb,
  6399. ocfs2_rec_clusters(el, &el->l_recs[i]) -
  6400. clusters_to_del);
  6401. } else {
  6402. status = 0;
  6403. goto bail;
  6404. }
  6405. mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
  6406. clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
  6407. if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) {
  6408. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
  6409. OCFS2_HAS_REFCOUNT_FL));
  6410. status = ocfs2_lock_refcount_tree(osb,
  6411. le64_to_cpu(di->i_refcount_loc),
  6412. 1, &ref_tree, NULL);
  6413. if (status) {
  6414. mlog_errno(status);
  6415. goto bail;
  6416. }
  6417. status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh,
  6418. blkno,
  6419. clusters_to_del,
  6420. &credits,
  6421. &meta_ac);
  6422. if (status < 0) {
  6423. mlog_errno(status);
  6424. goto bail;
  6425. }
  6426. }
  6427. mutex_lock(&tl_inode->i_mutex);
  6428. tl_sem = 1;
  6429. /* ocfs2_truncate_log_needs_flush guarantees us at least one
  6430. * record is free for use. If there isn't any, we flush to get
  6431. * an empty truncate log. */
  6432. if (ocfs2_truncate_log_needs_flush(osb)) {
  6433. status = __ocfs2_flush_truncate_log(osb);
  6434. if (status < 0) {
  6435. mlog_errno(status);
  6436. goto bail;
  6437. }
  6438. }
  6439. credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
  6440. (struct ocfs2_dinode *)fe_bh->b_data,
  6441. el);
  6442. handle = ocfs2_start_trans(osb, credits);
  6443. if (IS_ERR(handle)) {
  6444. status = PTR_ERR(handle);
  6445. handle = NULL;
  6446. mlog_errno(status);
  6447. goto bail;
  6448. }
  6449. status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
  6450. tc, path, meta_ac);
  6451. if (status < 0) {
  6452. mlog_errno(status);
  6453. goto bail;
  6454. }
  6455. mutex_unlock(&tl_inode->i_mutex);
  6456. tl_sem = 0;
  6457. ocfs2_commit_trans(osb, handle);
  6458. handle = NULL;
  6459. ocfs2_reinit_path(path, 1);
  6460. if (meta_ac) {
  6461. ocfs2_free_alloc_context(meta_ac);
  6462. meta_ac = NULL;
  6463. }
  6464. if (ref_tree) {
  6465. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  6466. ref_tree = NULL;
  6467. }
  6468. /*
  6469. * The check above will catch the case where we've truncated
  6470. * away all allocation.
  6471. */
  6472. goto start;
  6473. bail:
  6474. ocfs2_schedule_truncate_log_flush(osb, 1);
  6475. if (tl_sem)
  6476. mutex_unlock(&tl_inode->i_mutex);
  6477. if (handle)
  6478. ocfs2_commit_trans(osb, handle);
  6479. if (meta_ac)
  6480. ocfs2_free_alloc_context(meta_ac);
  6481. if (ref_tree)
  6482. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  6483. ocfs2_run_deallocs(osb, &tc->tc_dealloc);
  6484. ocfs2_free_path(path);
  6485. /* This will drop the ext_alloc cluster lock for us */
  6486. ocfs2_free_truncate_context(tc);
  6487. mlog_exit(status);
  6488. return status;
  6489. }
  6490. /*
  6491. * Expects the inode to already be locked.
  6492. */
  6493. int ocfs2_prepare_truncate(struct ocfs2_super *osb,
  6494. struct inode *inode,
  6495. struct buffer_head *fe_bh,
  6496. struct ocfs2_truncate_context **tc)
  6497. {
  6498. int status;
  6499. unsigned int new_i_clusters;
  6500. struct ocfs2_dinode *fe;
  6501. struct ocfs2_extent_block *eb;
  6502. struct buffer_head *last_eb_bh = NULL;
  6503. mlog_entry_void();
  6504. *tc = NULL;
  6505. new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
  6506. i_size_read(inode));
  6507. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  6508. mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
  6509. "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
  6510. (unsigned long long)le64_to_cpu(fe->i_size));
  6511. *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
  6512. if (!(*tc)) {
  6513. status = -ENOMEM;
  6514. mlog_errno(status);
  6515. goto bail;
  6516. }
  6517. ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
  6518. if (fe->id2.i_list.l_tree_depth) {
  6519. status = ocfs2_read_extent_block(INODE_CACHE(inode),
  6520. le64_to_cpu(fe->i_last_eb_blk),
  6521. &last_eb_bh);
  6522. if (status < 0) {
  6523. mlog_errno(status);
  6524. goto bail;
  6525. }
  6526. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  6527. }
  6528. (*tc)->tc_last_eb_bh = last_eb_bh;
  6529. status = 0;
  6530. bail:
  6531. if (status < 0) {
  6532. if (*tc)
  6533. ocfs2_free_truncate_context(*tc);
  6534. *tc = NULL;
  6535. }
  6536. mlog_exit_void();
  6537. return status;
  6538. }
  6539. /*
  6540. * 'start' is inclusive, 'end' is not.
  6541. */
  6542. int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
  6543. unsigned int start, unsigned int end, int trunc)
  6544. {
  6545. int ret;
  6546. unsigned int numbytes;
  6547. handle_t *handle;
  6548. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  6549. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  6550. struct ocfs2_inline_data *idata = &di->id2.i_data;
  6551. if (end > i_size_read(inode))
  6552. end = i_size_read(inode);
  6553. BUG_ON(start >= end);
  6554. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
  6555. !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
  6556. !ocfs2_supports_inline_data(osb)) {
  6557. ocfs2_error(inode->i_sb,
  6558. "Inline data flags for inode %llu don't agree! "
  6559. "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
  6560. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  6561. le16_to_cpu(di->i_dyn_features),
  6562. OCFS2_I(inode)->ip_dyn_features,
  6563. osb->s_feature_incompat);
  6564. ret = -EROFS;
  6565. goto out;
  6566. }
  6567. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  6568. if (IS_ERR(handle)) {
  6569. ret = PTR_ERR(handle);
  6570. mlog_errno(ret);
  6571. goto out;
  6572. }
  6573. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  6574. OCFS2_JOURNAL_ACCESS_WRITE);
  6575. if (ret) {
  6576. mlog_errno(ret);
  6577. goto out_commit;
  6578. }
  6579. numbytes = end - start;
  6580. memset(idata->id_data + start, 0, numbytes);
  6581. /*
  6582. * No need to worry about the data page here - it's been
  6583. * truncated already and inline data doesn't need it for
  6584. * pushing zero's to disk, so we'll let readpage pick it up
  6585. * later.
  6586. */
  6587. if (trunc) {
  6588. i_size_write(inode, start);
  6589. di->i_size = cpu_to_le64(start);
  6590. }
  6591. inode->i_blocks = ocfs2_inode_sector_count(inode);
  6592. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  6593. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  6594. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  6595. ocfs2_journal_dirty(handle, di_bh);
  6596. out_commit:
  6597. ocfs2_commit_trans(osb, handle);
  6598. out:
  6599. return ret;
  6600. }
  6601. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
  6602. {
  6603. /*
  6604. * The caller is responsible for completing deallocation
  6605. * before freeing the context.
  6606. */
  6607. if (tc->tc_dealloc.c_first_suballocator != NULL)
  6608. mlog(ML_NOTICE,
  6609. "Truncate completion has non-empty dealloc context\n");
  6610. brelse(tc->tc_last_eb_bh);
  6611. kfree(tc);
  6612. }