alloc.c 181 KB

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