alloc.c 190 KB

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