alloc.c 192 KB

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