alloc.c 184 KB

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