alloc.c 197 KB

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