alloc.c 186 KB

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