alloc.c 190 KB

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