alloc.c 180 KB

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