alloc.c 159 KB

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