alloc.c 191 KB

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