alloc.c 190 KB

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