alloc.c 194 KB

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