alloc.c 187 KB

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