alloc.c 188 KB

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