alloc.c 195 KB

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