alloc.c 168 KB

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