alloc.c 184 KB

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