alloc.c 191 KB

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