alloc.c 179 KB

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