alloc.c 168 KB

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