alloc.c 183 KB

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