alloc.c 186 KB

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