alloc.c 181 KB

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