alloc.c 180 KB

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