alloc.c 174 KB

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