xfs_bmap.c 196 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_alloc_btree.h"
  31. #include "xfs_ialloc_btree.h"
  32. #include "xfs_dir2_sf.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_dinode.h"
  35. #include "xfs_inode.h"
  36. #include "xfs_btree.h"
  37. #include "xfs_dmapi.h"
  38. #include "xfs_mount.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_itable.h"
  41. #include "xfs_dir2_data.h"
  42. #include "xfs_dir2_leaf.h"
  43. #include "xfs_dir2_block.h"
  44. #include "xfs_inode_item.h"
  45. #include "xfs_extfree_item.h"
  46. #include "xfs_alloc.h"
  47. #include "xfs_bmap.h"
  48. #include "xfs_rtalloc.h"
  49. #include "xfs_error.h"
  50. #include "xfs_attr_leaf.h"
  51. #include "xfs_rw.h"
  52. #include "xfs_quota.h"
  53. #include "xfs_trans_space.h"
  54. #include "xfs_buf_item.h"
  55. #ifdef DEBUG
  56. STATIC void
  57. xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
  58. #endif
  59. kmem_zone_t *xfs_bmap_free_item_zone;
  60. /*
  61. * Prototypes for internal bmap routines.
  62. */
  63. /*
  64. * Called from xfs_bmap_add_attrfork to handle extents format files.
  65. */
  66. STATIC int /* error */
  67. xfs_bmap_add_attrfork_extents(
  68. xfs_trans_t *tp, /* transaction pointer */
  69. xfs_inode_t *ip, /* incore inode pointer */
  70. xfs_fsblock_t *firstblock, /* first block allocated */
  71. xfs_bmap_free_t *flist, /* blocks to free at commit */
  72. int *flags); /* inode logging flags */
  73. /*
  74. * Called from xfs_bmap_add_attrfork to handle local format files.
  75. */
  76. STATIC int /* error */
  77. xfs_bmap_add_attrfork_local(
  78. xfs_trans_t *tp, /* transaction pointer */
  79. xfs_inode_t *ip, /* incore inode pointer */
  80. xfs_fsblock_t *firstblock, /* first block allocated */
  81. xfs_bmap_free_t *flist, /* blocks to free at commit */
  82. int *flags); /* inode logging flags */
  83. /*
  84. * Called by xfs_bmapi to update file extent records and the btree
  85. * after allocating space (or doing a delayed allocation).
  86. */
  87. STATIC int /* error */
  88. xfs_bmap_add_extent(
  89. xfs_inode_t *ip, /* incore inode pointer */
  90. xfs_extnum_t idx, /* extent number to update/insert */
  91. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  92. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  93. xfs_fsblock_t *first, /* pointer to firstblock variable */
  94. xfs_bmap_free_t *flist, /* list of extents to be freed */
  95. int *logflagsp, /* inode logging flags */
  96. xfs_extdelta_t *delta, /* Change made to incore extents */
  97. int whichfork, /* data or attr fork */
  98. int rsvd); /* OK to allocate reserved blocks */
  99. /*
  100. * Called by xfs_bmap_add_extent to handle cases converting a delayed
  101. * allocation to a real allocation.
  102. */
  103. STATIC int /* error */
  104. xfs_bmap_add_extent_delay_real(
  105. xfs_inode_t *ip, /* incore inode pointer */
  106. xfs_extnum_t idx, /* extent number to update/insert */
  107. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  108. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  109. xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
  110. xfs_fsblock_t *first, /* pointer to firstblock variable */
  111. xfs_bmap_free_t *flist, /* list of extents to be freed */
  112. int *logflagsp, /* inode logging flags */
  113. xfs_extdelta_t *delta, /* Change made to incore extents */
  114. int rsvd); /* OK to allocate reserved blocks */
  115. /*
  116. * Called by xfs_bmap_add_extent to handle cases converting a hole
  117. * to a delayed allocation.
  118. */
  119. STATIC int /* error */
  120. xfs_bmap_add_extent_hole_delay(
  121. xfs_inode_t *ip, /* incore inode pointer */
  122. xfs_extnum_t idx, /* extent number to update/insert */
  123. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  124. int *logflagsp,/* inode logging flags */
  125. xfs_extdelta_t *delta, /* Change made to incore extents */
  126. int rsvd); /* OK to allocate reserved blocks */
  127. /*
  128. * Called by xfs_bmap_add_extent to handle cases converting a hole
  129. * to a real allocation.
  130. */
  131. STATIC int /* error */
  132. xfs_bmap_add_extent_hole_real(
  133. xfs_inode_t *ip, /* incore inode pointer */
  134. xfs_extnum_t idx, /* extent number to update/insert */
  135. xfs_btree_cur_t *cur, /* if null, not a btree */
  136. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  137. int *logflagsp, /* inode logging flags */
  138. xfs_extdelta_t *delta, /* Change made to incore extents */
  139. int whichfork); /* data or attr fork */
  140. /*
  141. * Called by xfs_bmap_add_extent to handle cases converting an unwritten
  142. * allocation to a real allocation or vice versa.
  143. */
  144. STATIC int /* error */
  145. xfs_bmap_add_extent_unwritten_real(
  146. xfs_inode_t *ip, /* incore inode pointer */
  147. xfs_extnum_t idx, /* extent number to update/insert */
  148. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  149. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  150. int *logflagsp, /* inode logging flags */
  151. xfs_extdelta_t *delta); /* Change made to incore extents */
  152. /*
  153. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  154. * It figures out where to ask the underlying allocator to put the new extent.
  155. */
  156. STATIC int /* error */
  157. xfs_bmap_alloc(
  158. xfs_bmalloca_t *ap); /* bmap alloc argument struct */
  159. /*
  160. * Transform a btree format file with only one leaf node, where the
  161. * extents list will fit in the inode, into an extents format file.
  162. * Since the file extents are already in-core, all we have to do is
  163. * give up the space for the btree root and pitch the leaf block.
  164. */
  165. STATIC int /* error */
  166. xfs_bmap_btree_to_extents(
  167. xfs_trans_t *tp, /* transaction pointer */
  168. xfs_inode_t *ip, /* incore inode pointer */
  169. xfs_btree_cur_t *cur, /* btree cursor */
  170. int *logflagsp, /* inode logging flags */
  171. int whichfork); /* data or attr fork */
  172. /*
  173. * Called by xfs_bmapi to update file extent records and the btree
  174. * after removing space (or undoing a delayed allocation).
  175. */
  176. STATIC int /* error */
  177. xfs_bmap_del_extent(
  178. xfs_inode_t *ip, /* incore inode pointer */
  179. xfs_trans_t *tp, /* current trans pointer */
  180. xfs_extnum_t idx, /* extent number to update/insert */
  181. xfs_bmap_free_t *flist, /* list of extents to be freed */
  182. xfs_btree_cur_t *cur, /* if null, not a btree */
  183. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  184. int *logflagsp,/* inode logging flags */
  185. xfs_extdelta_t *delta, /* Change made to incore extents */
  186. int whichfork, /* data or attr fork */
  187. int rsvd); /* OK to allocate reserved blocks */
  188. /*
  189. * Remove the entry "free" from the free item list. Prev points to the
  190. * previous entry, unless "free" is the head of the list.
  191. */
  192. STATIC void
  193. xfs_bmap_del_free(
  194. xfs_bmap_free_t *flist, /* free item list header */
  195. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  196. xfs_bmap_free_item_t *free); /* list item to be freed */
  197. /*
  198. * Convert an extents-format file into a btree-format file.
  199. * The new file will have a root block (in the inode) and a single child block.
  200. */
  201. STATIC int /* error */
  202. xfs_bmap_extents_to_btree(
  203. xfs_trans_t *tp, /* transaction pointer */
  204. xfs_inode_t *ip, /* incore inode pointer */
  205. xfs_fsblock_t *firstblock, /* first-block-allocated */
  206. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  207. xfs_btree_cur_t **curp, /* cursor returned to caller */
  208. int wasdel, /* converting a delayed alloc */
  209. int *logflagsp, /* inode logging flags */
  210. int whichfork); /* data or attr fork */
  211. /*
  212. * Convert a local file to an extents file.
  213. * This code is sort of bogus, since the file data needs to get
  214. * logged so it won't be lost. The bmap-level manipulations are ok, though.
  215. */
  216. STATIC int /* error */
  217. xfs_bmap_local_to_extents(
  218. xfs_trans_t *tp, /* transaction pointer */
  219. xfs_inode_t *ip, /* incore inode pointer */
  220. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  221. xfs_extlen_t total, /* total blocks needed by transaction */
  222. int *logflagsp, /* inode logging flags */
  223. int whichfork); /* data or attr fork */
  224. /*
  225. * Search the extents list for the inode, for the extent containing bno.
  226. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  227. * *eofp will be set, and *prevp will contain the last entry (null if none).
  228. * Else, *lastxp will be set to the index of the found
  229. * entry; *gotp will contain the entry.
  230. */
  231. STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
  232. xfs_bmap_search_extents(
  233. xfs_inode_t *ip, /* incore inode pointer */
  234. xfs_fileoff_t bno, /* block number searched for */
  235. int whichfork, /* data or attr fork */
  236. int *eofp, /* out: end of file found */
  237. xfs_extnum_t *lastxp, /* out: last extent index */
  238. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  239. xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
  240. /*
  241. * Check the last inode extent to determine whether this allocation will result
  242. * in blocks being allocated at the end of the file. When we allocate new data
  243. * blocks at the end of the file which do not start at the previous data block,
  244. * we will try to align the new blocks at stripe unit boundaries.
  245. */
  246. STATIC int /* error */
  247. xfs_bmap_isaeof(
  248. xfs_inode_t *ip, /* incore inode pointer */
  249. xfs_fileoff_t off, /* file offset in fsblocks */
  250. int whichfork, /* data or attribute fork */
  251. char *aeof); /* return value */
  252. #ifdef XFS_BMAP_TRACE
  253. /*
  254. * Add a bmap trace buffer entry. Base routine for the others.
  255. */
  256. STATIC void
  257. xfs_bmap_trace_addentry(
  258. int opcode, /* operation */
  259. char *fname, /* function name */
  260. char *desc, /* operation description */
  261. xfs_inode_t *ip, /* incore inode pointer */
  262. xfs_extnum_t idx, /* index of entry(ies) */
  263. xfs_extnum_t cnt, /* count of entries, 1 or 2 */
  264. xfs_bmbt_rec_t *r1, /* first record */
  265. xfs_bmbt_rec_t *r2, /* second record or null */
  266. int whichfork); /* data or attr fork */
  267. /*
  268. * Add bmap trace entry prior to a call to xfs_iext_remove.
  269. */
  270. STATIC void
  271. xfs_bmap_trace_delete(
  272. char *fname, /* function name */
  273. char *desc, /* operation description */
  274. xfs_inode_t *ip, /* incore inode pointer */
  275. xfs_extnum_t idx, /* index of entry(entries) deleted */
  276. xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
  277. int whichfork); /* data or attr fork */
  278. /*
  279. * Add bmap trace entry prior to a call to xfs_iext_insert, or
  280. * reading in the extents list from the disk (in the btree).
  281. */
  282. STATIC void
  283. xfs_bmap_trace_insert(
  284. char *fname, /* function name */
  285. char *desc, /* operation description */
  286. xfs_inode_t *ip, /* incore inode pointer */
  287. xfs_extnum_t idx, /* index of entry(entries) inserted */
  288. xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
  289. xfs_bmbt_irec_t *r1, /* inserted record 1 */
  290. xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
  291. int whichfork); /* data or attr fork */
  292. /*
  293. * Add bmap trace entry after updating an extent record in place.
  294. */
  295. STATIC void
  296. xfs_bmap_trace_post_update(
  297. char *fname, /* function name */
  298. char *desc, /* operation description */
  299. xfs_inode_t *ip, /* incore inode pointer */
  300. xfs_extnum_t idx, /* index of entry updated */
  301. int whichfork); /* data or attr fork */
  302. /*
  303. * Add bmap trace entry prior to updating an extent record in place.
  304. */
  305. STATIC void
  306. xfs_bmap_trace_pre_update(
  307. char *fname, /* function name */
  308. char *desc, /* operation description */
  309. xfs_inode_t *ip, /* incore inode pointer */
  310. xfs_extnum_t idx, /* index of entry to be updated */
  311. int whichfork); /* data or attr fork */
  312. #else
  313. #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
  314. #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
  315. #define xfs_bmap_trace_post_update(f,d,ip,i,w)
  316. #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
  317. #endif /* XFS_BMAP_TRACE */
  318. /*
  319. * Compute the worst-case number of indirect blocks that will be used
  320. * for ip's delayed extent of length "len".
  321. */
  322. STATIC xfs_filblks_t
  323. xfs_bmap_worst_indlen(
  324. xfs_inode_t *ip, /* incore inode pointer */
  325. xfs_filblks_t len); /* delayed extent length */
  326. #ifdef DEBUG
  327. /*
  328. * Perform various validation checks on the values being returned
  329. * from xfs_bmapi().
  330. */
  331. STATIC void
  332. xfs_bmap_validate_ret(
  333. xfs_fileoff_t bno,
  334. xfs_filblks_t len,
  335. int flags,
  336. xfs_bmbt_irec_t *mval,
  337. int nmap,
  338. int ret_nmap);
  339. #else
  340. #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
  341. #endif /* DEBUG */
  342. #if defined(XFS_RW_TRACE)
  343. STATIC void
  344. xfs_bunmap_trace(
  345. xfs_inode_t *ip,
  346. xfs_fileoff_t bno,
  347. xfs_filblks_t len,
  348. int flags,
  349. inst_t *ra);
  350. #else
  351. #define xfs_bunmap_trace(ip, bno, len, flags, ra)
  352. #endif /* XFS_RW_TRACE */
  353. STATIC int
  354. xfs_bmap_count_tree(
  355. xfs_mount_t *mp,
  356. xfs_trans_t *tp,
  357. xfs_ifork_t *ifp,
  358. xfs_fsblock_t blockno,
  359. int levelin,
  360. int *count);
  361. STATIC int
  362. xfs_bmap_count_leaves(
  363. xfs_ifork_t *ifp,
  364. xfs_extnum_t idx,
  365. int numrecs,
  366. int *count);
  367. STATIC int
  368. xfs_bmap_disk_count_leaves(
  369. xfs_extnum_t idx,
  370. xfs_bmbt_block_t *block,
  371. int numrecs,
  372. int *count);
  373. /*
  374. * Bmap internal routines.
  375. */
  376. /*
  377. * Called from xfs_bmap_add_attrfork to handle btree format files.
  378. */
  379. STATIC int /* error */
  380. xfs_bmap_add_attrfork_btree(
  381. xfs_trans_t *tp, /* transaction pointer */
  382. xfs_inode_t *ip, /* incore inode pointer */
  383. xfs_fsblock_t *firstblock, /* first block allocated */
  384. xfs_bmap_free_t *flist, /* blocks to free at commit */
  385. int *flags) /* inode logging flags */
  386. {
  387. xfs_btree_cur_t *cur; /* btree cursor */
  388. int error; /* error return value */
  389. xfs_mount_t *mp; /* file system mount struct */
  390. int stat; /* newroot status */
  391. mp = ip->i_mount;
  392. if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
  393. *flags |= XFS_ILOG_DBROOT;
  394. else {
  395. cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
  396. XFS_DATA_FORK);
  397. cur->bc_private.b.flist = flist;
  398. cur->bc_private.b.firstblock = *firstblock;
  399. if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
  400. goto error0;
  401. ASSERT(stat == 1); /* must be at least one entry */
  402. if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
  403. goto error0;
  404. if (stat == 0) {
  405. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  406. return XFS_ERROR(ENOSPC);
  407. }
  408. *firstblock = cur->bc_private.b.firstblock;
  409. cur->bc_private.b.allocated = 0;
  410. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  411. }
  412. return 0;
  413. error0:
  414. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  415. return error;
  416. }
  417. /*
  418. * Called from xfs_bmap_add_attrfork to handle extents format files.
  419. */
  420. STATIC int /* error */
  421. xfs_bmap_add_attrfork_extents(
  422. xfs_trans_t *tp, /* transaction pointer */
  423. xfs_inode_t *ip, /* incore inode pointer */
  424. xfs_fsblock_t *firstblock, /* first block allocated */
  425. xfs_bmap_free_t *flist, /* blocks to free at commit */
  426. int *flags) /* inode logging flags */
  427. {
  428. xfs_btree_cur_t *cur; /* bmap btree cursor */
  429. int error; /* error return value */
  430. if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
  431. return 0;
  432. cur = NULL;
  433. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
  434. flags, XFS_DATA_FORK);
  435. if (cur) {
  436. cur->bc_private.b.allocated = 0;
  437. xfs_btree_del_cursor(cur,
  438. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  439. }
  440. return error;
  441. }
  442. /*
  443. * Called from xfs_bmap_add_attrfork to handle local format files.
  444. */
  445. STATIC int /* error */
  446. xfs_bmap_add_attrfork_local(
  447. xfs_trans_t *tp, /* transaction pointer */
  448. xfs_inode_t *ip, /* incore inode pointer */
  449. xfs_fsblock_t *firstblock, /* first block allocated */
  450. xfs_bmap_free_t *flist, /* blocks to free at commit */
  451. int *flags) /* inode logging flags */
  452. {
  453. xfs_da_args_t dargs; /* args for dir/attr code */
  454. int error; /* error return value */
  455. xfs_mount_t *mp; /* mount structure pointer */
  456. if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
  457. return 0;
  458. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  459. mp = ip->i_mount;
  460. memset(&dargs, 0, sizeof(dargs));
  461. dargs.dp = ip;
  462. dargs.firstblock = firstblock;
  463. dargs.flist = flist;
  464. dargs.total = mp->m_dirblkfsbs;
  465. dargs.whichfork = XFS_DATA_FORK;
  466. dargs.trans = tp;
  467. error = xfs_dir2_sf_to_block(&dargs);
  468. } else
  469. error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
  470. XFS_DATA_FORK);
  471. return error;
  472. }
  473. /*
  474. * Called by xfs_bmapi to update file extent records and the btree
  475. * after allocating space (or doing a delayed allocation).
  476. */
  477. STATIC int /* error */
  478. xfs_bmap_add_extent(
  479. xfs_inode_t *ip, /* incore inode pointer */
  480. xfs_extnum_t idx, /* extent number to update/insert */
  481. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  482. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  483. xfs_fsblock_t *first, /* pointer to firstblock variable */
  484. xfs_bmap_free_t *flist, /* list of extents to be freed */
  485. int *logflagsp, /* inode logging flags */
  486. xfs_extdelta_t *delta, /* Change made to incore extents */
  487. int whichfork, /* data or attr fork */
  488. int rsvd) /* OK to use reserved data blocks */
  489. {
  490. xfs_btree_cur_t *cur; /* btree cursor or null */
  491. xfs_filblks_t da_new; /* new count del alloc blocks used */
  492. xfs_filblks_t da_old; /* old count del alloc blocks used */
  493. int error; /* error return value */
  494. #ifdef XFS_BMAP_TRACE
  495. static char fname[] = "xfs_bmap_add_extent";
  496. #endif
  497. xfs_ifork_t *ifp; /* inode fork ptr */
  498. int logflags; /* returned value */
  499. xfs_extnum_t nextents; /* number of extents in file now */
  500. XFS_STATS_INC(xs_add_exlist);
  501. cur = *curp;
  502. ifp = XFS_IFORK_PTR(ip, whichfork);
  503. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  504. ASSERT(idx <= nextents);
  505. da_old = da_new = 0;
  506. error = 0;
  507. /*
  508. * This is the first extent added to a new/empty file.
  509. * Special case this one, so other routines get to assume there are
  510. * already extents in the list.
  511. */
  512. if (nextents == 0) {
  513. xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
  514. NULL, whichfork);
  515. xfs_iext_insert(ifp, 0, 1, new);
  516. ASSERT(cur == NULL);
  517. ifp->if_lastex = 0;
  518. if (!ISNULLSTARTBLOCK(new->br_startblock)) {
  519. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  520. logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
  521. } else
  522. logflags = 0;
  523. /* DELTA: single new extent */
  524. if (delta) {
  525. if (delta->xed_startoff > new->br_startoff)
  526. delta->xed_startoff = new->br_startoff;
  527. if (delta->xed_blockcount <
  528. new->br_startoff + new->br_blockcount)
  529. delta->xed_blockcount = new->br_startoff +
  530. new->br_blockcount;
  531. }
  532. }
  533. /*
  534. * Any kind of new delayed allocation goes here.
  535. */
  536. else if (ISNULLSTARTBLOCK(new->br_startblock)) {
  537. if (cur)
  538. ASSERT((cur->bc_private.b.flags &
  539. XFS_BTCUR_BPRV_WASDEL) == 0);
  540. if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
  541. &logflags, delta, rsvd)))
  542. goto done;
  543. }
  544. /*
  545. * Real allocation off the end of the file.
  546. */
  547. else if (idx == nextents) {
  548. if (cur)
  549. ASSERT((cur->bc_private.b.flags &
  550. XFS_BTCUR_BPRV_WASDEL) == 0);
  551. if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
  552. &logflags, delta, whichfork)))
  553. goto done;
  554. } else {
  555. xfs_bmbt_irec_t prev; /* old extent at offset idx */
  556. /*
  557. * Get the record referred to by idx.
  558. */
  559. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
  560. /*
  561. * If it's a real allocation record, and the new allocation ends
  562. * after the start of the referred to record, then we're filling
  563. * in a delayed or unwritten allocation with a real one, or
  564. * converting real back to unwritten.
  565. */
  566. if (!ISNULLSTARTBLOCK(new->br_startblock) &&
  567. new->br_startoff + new->br_blockcount > prev.br_startoff) {
  568. if (prev.br_state != XFS_EXT_UNWRITTEN &&
  569. ISNULLSTARTBLOCK(prev.br_startblock)) {
  570. da_old = STARTBLOCKVAL(prev.br_startblock);
  571. if (cur)
  572. ASSERT(cur->bc_private.b.flags &
  573. XFS_BTCUR_BPRV_WASDEL);
  574. if ((error = xfs_bmap_add_extent_delay_real(ip,
  575. idx, &cur, new, &da_new, first, flist,
  576. &logflags, delta, rsvd)))
  577. goto done;
  578. } else if (new->br_state == XFS_EXT_NORM) {
  579. ASSERT(new->br_state == XFS_EXT_NORM);
  580. if ((error = xfs_bmap_add_extent_unwritten_real(
  581. ip, idx, &cur, new, &logflags, delta)))
  582. goto done;
  583. } else {
  584. ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
  585. if ((error = xfs_bmap_add_extent_unwritten_real(
  586. ip, idx, &cur, new, &logflags, delta)))
  587. goto done;
  588. }
  589. ASSERT(*curp == cur || *curp == NULL);
  590. }
  591. /*
  592. * Otherwise we're filling in a hole with an allocation.
  593. */
  594. else {
  595. if (cur)
  596. ASSERT((cur->bc_private.b.flags &
  597. XFS_BTCUR_BPRV_WASDEL) == 0);
  598. if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
  599. new, &logflags, delta, whichfork)))
  600. goto done;
  601. }
  602. }
  603. ASSERT(*curp == cur || *curp == NULL);
  604. /*
  605. * Convert to a btree if necessary.
  606. */
  607. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  608. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
  609. int tmp_logflags; /* partial log flag return val */
  610. ASSERT(cur == NULL);
  611. error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
  612. flist, &cur, da_old > 0, &tmp_logflags, whichfork);
  613. logflags |= tmp_logflags;
  614. if (error)
  615. goto done;
  616. }
  617. /*
  618. * Adjust for changes in reserved delayed indirect blocks.
  619. * Nothing to do for disk quotas here.
  620. */
  621. if (da_old || da_new) {
  622. xfs_filblks_t nblks;
  623. nblks = da_new;
  624. if (cur)
  625. nblks += cur->bc_private.b.allocated;
  626. ASSERT(nblks <= da_old);
  627. if (nblks < da_old)
  628. xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
  629. (int64_t)(da_old - nblks), rsvd);
  630. }
  631. /*
  632. * Clear out the allocated field, done with it now in any case.
  633. */
  634. if (cur) {
  635. cur->bc_private.b.allocated = 0;
  636. *curp = cur;
  637. }
  638. done:
  639. #ifdef DEBUG
  640. if (!error)
  641. xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
  642. #endif
  643. *logflagsp = logflags;
  644. return error;
  645. }
  646. /*
  647. * Called by xfs_bmap_add_extent to handle cases converting a delayed
  648. * allocation to a real allocation.
  649. */
  650. STATIC int /* error */
  651. xfs_bmap_add_extent_delay_real(
  652. xfs_inode_t *ip, /* incore inode pointer */
  653. xfs_extnum_t idx, /* extent number to update/insert */
  654. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  655. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  656. xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
  657. xfs_fsblock_t *first, /* pointer to firstblock variable */
  658. xfs_bmap_free_t *flist, /* list of extents to be freed */
  659. int *logflagsp, /* inode logging flags */
  660. xfs_extdelta_t *delta, /* Change made to incore extents */
  661. int rsvd) /* OK to use reserved data block allocation */
  662. {
  663. xfs_btree_cur_t *cur; /* btree cursor */
  664. int diff; /* temp value */
  665. xfs_bmbt_rec_t *ep; /* extent entry for idx */
  666. int error; /* error return value */
  667. #ifdef XFS_BMAP_TRACE
  668. static char fname[] = "xfs_bmap_add_extent_delay_real";
  669. #endif
  670. int i; /* temp state */
  671. xfs_ifork_t *ifp; /* inode fork pointer */
  672. xfs_fileoff_t new_endoff; /* end offset of new entry */
  673. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  674. /* left is 0, right is 1, prev is 2 */
  675. int rval=0; /* return value (logging flags) */
  676. int state = 0;/* state bits, accessed thru macros */
  677. xfs_filblks_t temp=0; /* value for dnew calculations */
  678. xfs_filblks_t temp2=0;/* value for dnew calculations */
  679. int tmp_rval; /* partial logging flags */
  680. enum { /* bit number definitions for state */
  681. LEFT_CONTIG, RIGHT_CONTIG,
  682. LEFT_FILLING, RIGHT_FILLING,
  683. LEFT_DELAY, RIGHT_DELAY,
  684. LEFT_VALID, RIGHT_VALID
  685. };
  686. #define LEFT r[0]
  687. #define RIGHT r[1]
  688. #define PREV r[2]
  689. #define MASK(b) (1 << (b))
  690. #define MASK2(a,b) (MASK(a) | MASK(b))
  691. #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
  692. #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
  693. #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
  694. #define STATE_TEST(b) (state & MASK(b))
  695. #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
  696. ((state &= ~MASK(b)), 0))
  697. #define SWITCH_STATE \
  698. (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
  699. /*
  700. * Set up a bunch of variables to make the tests simpler.
  701. */
  702. cur = *curp;
  703. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  704. ep = xfs_iext_get_ext(ifp, idx);
  705. xfs_bmbt_get_all(ep, &PREV);
  706. new_endoff = new->br_startoff + new->br_blockcount;
  707. ASSERT(PREV.br_startoff <= new->br_startoff);
  708. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  709. /*
  710. * Set flags determining what part of the previous delayed allocation
  711. * extent is being replaced by a real allocation.
  712. */
  713. STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
  714. STATE_SET(RIGHT_FILLING,
  715. PREV.br_startoff + PREV.br_blockcount == new_endoff);
  716. /*
  717. * Check and set flags if this segment has a left neighbor.
  718. * Don't set contiguous if the combined extent would be too large.
  719. */
  720. if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
  721. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
  722. STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
  723. }
  724. STATE_SET(LEFT_CONTIG,
  725. STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
  726. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  727. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  728. LEFT.br_state == new->br_state &&
  729. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
  730. /*
  731. * Check and set flags if this segment has a right neighbor.
  732. * Don't set contiguous if the combined extent would be too large.
  733. * Also check for all-three-contiguous being too large.
  734. */
  735. if (STATE_SET_TEST(RIGHT_VALID,
  736. idx <
  737. ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
  738. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
  739. STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
  740. }
  741. STATE_SET(RIGHT_CONTIG,
  742. STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
  743. new_endoff == RIGHT.br_startoff &&
  744. new->br_startblock + new->br_blockcount ==
  745. RIGHT.br_startblock &&
  746. new->br_state == RIGHT.br_state &&
  747. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  748. ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
  749. MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
  750. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  751. <= MAXEXTLEN));
  752. error = 0;
  753. /*
  754. * Switch out based on the FILLING and CONTIG state bits.
  755. */
  756. switch (SWITCH_STATE) {
  757. case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  758. /*
  759. * Filling in all of a previously delayed allocation extent.
  760. * The left and right neighbors are both contiguous with new.
  761. */
  762. xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
  763. XFS_DATA_FORK);
  764. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  765. LEFT.br_blockcount + PREV.br_blockcount +
  766. RIGHT.br_blockcount);
  767. xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
  768. XFS_DATA_FORK);
  769. xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
  770. XFS_DATA_FORK);
  771. xfs_iext_remove(ifp, idx, 2);
  772. ip->i_df.if_lastex = idx - 1;
  773. ip->i_d.di_nextents--;
  774. if (cur == NULL)
  775. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  776. else {
  777. rval = XFS_ILOG_CORE;
  778. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  779. RIGHT.br_startblock,
  780. RIGHT.br_blockcount, &i)))
  781. goto done;
  782. ASSERT(i == 1);
  783. if ((error = xfs_bmbt_delete(cur, &i)))
  784. goto done;
  785. ASSERT(i == 1);
  786. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  787. goto done;
  788. ASSERT(i == 1);
  789. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  790. LEFT.br_startblock,
  791. LEFT.br_blockcount +
  792. PREV.br_blockcount +
  793. RIGHT.br_blockcount, LEFT.br_state)))
  794. goto done;
  795. }
  796. *dnew = 0;
  797. /* DELTA: Three in-core extents are replaced by one. */
  798. temp = LEFT.br_startoff;
  799. temp2 = LEFT.br_blockcount +
  800. PREV.br_blockcount +
  801. RIGHT.br_blockcount;
  802. break;
  803. case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
  804. /*
  805. * Filling in all of a previously delayed allocation extent.
  806. * The left neighbor is contiguous, the right is not.
  807. */
  808. xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
  809. XFS_DATA_FORK);
  810. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  811. LEFT.br_blockcount + PREV.br_blockcount);
  812. xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
  813. XFS_DATA_FORK);
  814. ip->i_df.if_lastex = idx - 1;
  815. xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
  816. XFS_DATA_FORK);
  817. xfs_iext_remove(ifp, idx, 1);
  818. if (cur == NULL)
  819. rval = XFS_ILOG_DEXT;
  820. else {
  821. rval = 0;
  822. if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
  823. LEFT.br_startblock, LEFT.br_blockcount,
  824. &i)))
  825. goto done;
  826. ASSERT(i == 1);
  827. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  828. LEFT.br_startblock,
  829. LEFT.br_blockcount +
  830. PREV.br_blockcount, LEFT.br_state)))
  831. goto done;
  832. }
  833. *dnew = 0;
  834. /* DELTA: Two in-core extents are replaced by one. */
  835. temp = LEFT.br_startoff;
  836. temp2 = LEFT.br_blockcount +
  837. PREV.br_blockcount;
  838. break;
  839. case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
  840. /*
  841. * Filling in all of a previously delayed allocation extent.
  842. * The right neighbor is contiguous, the left is not.
  843. */
  844. xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
  845. XFS_DATA_FORK);
  846. xfs_bmbt_set_startblock(ep, new->br_startblock);
  847. xfs_bmbt_set_blockcount(ep,
  848. PREV.br_blockcount + RIGHT.br_blockcount);
  849. xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
  850. XFS_DATA_FORK);
  851. ip->i_df.if_lastex = idx;
  852. xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
  853. XFS_DATA_FORK);
  854. xfs_iext_remove(ifp, idx + 1, 1);
  855. if (cur == NULL)
  856. rval = XFS_ILOG_DEXT;
  857. else {
  858. rval = 0;
  859. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  860. RIGHT.br_startblock,
  861. RIGHT.br_blockcount, &i)))
  862. goto done;
  863. ASSERT(i == 1);
  864. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  865. new->br_startblock,
  866. PREV.br_blockcount +
  867. RIGHT.br_blockcount, PREV.br_state)))
  868. goto done;
  869. }
  870. *dnew = 0;
  871. /* DELTA: Two in-core extents are replaced by one. */
  872. temp = PREV.br_startoff;
  873. temp2 = PREV.br_blockcount +
  874. RIGHT.br_blockcount;
  875. break;
  876. case MASK2(LEFT_FILLING, RIGHT_FILLING):
  877. /*
  878. * Filling in all of a previously delayed allocation extent.
  879. * Neither the left nor right neighbors are contiguous with
  880. * the new one.
  881. */
  882. xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
  883. XFS_DATA_FORK);
  884. xfs_bmbt_set_startblock(ep, new->br_startblock);
  885. xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
  886. XFS_DATA_FORK);
  887. ip->i_df.if_lastex = idx;
  888. ip->i_d.di_nextents++;
  889. if (cur == NULL)
  890. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  891. else {
  892. rval = XFS_ILOG_CORE;
  893. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  894. new->br_startblock, new->br_blockcount,
  895. &i)))
  896. goto done;
  897. ASSERT(i == 0);
  898. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  899. if ((error = xfs_bmbt_insert(cur, &i)))
  900. goto done;
  901. ASSERT(i == 1);
  902. }
  903. *dnew = 0;
  904. /* DELTA: The in-core extent described by new changed type. */
  905. temp = new->br_startoff;
  906. temp2 = new->br_blockcount;
  907. break;
  908. case MASK2(LEFT_FILLING, LEFT_CONTIG):
  909. /*
  910. * Filling in the first part of a previous delayed allocation.
  911. * The left neighbor is contiguous.
  912. */
  913. xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
  914. XFS_DATA_FORK);
  915. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  916. LEFT.br_blockcount + new->br_blockcount);
  917. xfs_bmbt_set_startoff(ep,
  918. PREV.br_startoff + new->br_blockcount);
  919. xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
  920. XFS_DATA_FORK);
  921. temp = PREV.br_blockcount - new->br_blockcount;
  922. xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
  923. XFS_DATA_FORK);
  924. xfs_bmbt_set_blockcount(ep, temp);
  925. ip->i_df.if_lastex = idx - 1;
  926. if (cur == NULL)
  927. rval = XFS_ILOG_DEXT;
  928. else {
  929. rval = 0;
  930. if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
  931. LEFT.br_startblock, LEFT.br_blockcount,
  932. &i)))
  933. goto done;
  934. ASSERT(i == 1);
  935. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  936. LEFT.br_startblock,
  937. LEFT.br_blockcount +
  938. new->br_blockcount,
  939. LEFT.br_state)))
  940. goto done;
  941. }
  942. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  943. STARTBLOCKVAL(PREV.br_startblock));
  944. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  945. xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
  946. XFS_DATA_FORK);
  947. *dnew = temp;
  948. /* DELTA: The boundary between two in-core extents moved. */
  949. temp = LEFT.br_startoff;
  950. temp2 = LEFT.br_blockcount +
  951. PREV.br_blockcount;
  952. break;
  953. case MASK(LEFT_FILLING):
  954. /*
  955. * Filling in the first part of a previous delayed allocation.
  956. * The left neighbor is not contiguous.
  957. */
  958. xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
  959. xfs_bmbt_set_startoff(ep, new_endoff);
  960. temp = PREV.br_blockcount - new->br_blockcount;
  961. xfs_bmbt_set_blockcount(ep, temp);
  962. xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
  963. XFS_DATA_FORK);
  964. xfs_iext_insert(ifp, idx, 1, new);
  965. ip->i_df.if_lastex = idx;
  966. ip->i_d.di_nextents++;
  967. if (cur == NULL)
  968. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  969. else {
  970. rval = XFS_ILOG_CORE;
  971. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  972. new->br_startblock, new->br_blockcount,
  973. &i)))
  974. goto done;
  975. ASSERT(i == 0);
  976. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  977. if ((error = xfs_bmbt_insert(cur, &i)))
  978. goto done;
  979. ASSERT(i == 1);
  980. }
  981. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  982. ip->i_d.di_nextents > ip->i_df.if_ext_max) {
  983. error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
  984. first, flist, &cur, 1, &tmp_rval,
  985. XFS_DATA_FORK);
  986. rval |= tmp_rval;
  987. if (error)
  988. goto done;
  989. }
  990. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  991. STARTBLOCKVAL(PREV.br_startblock) -
  992. (cur ? cur->bc_private.b.allocated : 0));
  993. ep = xfs_iext_get_ext(ifp, idx + 1);
  994. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  995. xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
  996. XFS_DATA_FORK);
  997. *dnew = temp;
  998. /* DELTA: One in-core extent is split in two. */
  999. temp = PREV.br_startoff;
  1000. temp2 = PREV.br_blockcount;
  1001. break;
  1002. case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
  1003. /*
  1004. * Filling in the last part of a previous delayed allocation.
  1005. * The right neighbor is contiguous with the new allocation.
  1006. */
  1007. temp = PREV.br_blockcount - new->br_blockcount;
  1008. xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
  1009. XFS_DATA_FORK);
  1010. xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
  1011. XFS_DATA_FORK);
  1012. xfs_bmbt_set_blockcount(ep, temp);
  1013. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
  1014. new->br_startoff, new->br_startblock,
  1015. new->br_blockcount + RIGHT.br_blockcount,
  1016. RIGHT.br_state);
  1017. xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
  1018. XFS_DATA_FORK);
  1019. ip->i_df.if_lastex = idx + 1;
  1020. if (cur == NULL)
  1021. rval = XFS_ILOG_DEXT;
  1022. else {
  1023. rval = 0;
  1024. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1025. RIGHT.br_startblock,
  1026. RIGHT.br_blockcount, &i)))
  1027. goto done;
  1028. ASSERT(i == 1);
  1029. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1030. new->br_startblock,
  1031. new->br_blockcount +
  1032. RIGHT.br_blockcount,
  1033. RIGHT.br_state)))
  1034. goto done;
  1035. }
  1036. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  1037. STARTBLOCKVAL(PREV.br_startblock));
  1038. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  1039. xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
  1040. XFS_DATA_FORK);
  1041. *dnew = temp;
  1042. /* DELTA: The boundary between two in-core extents moved. */
  1043. temp = PREV.br_startoff;
  1044. temp2 = PREV.br_blockcount +
  1045. RIGHT.br_blockcount;
  1046. break;
  1047. case MASK(RIGHT_FILLING):
  1048. /*
  1049. * Filling in the last part of a previous delayed allocation.
  1050. * The right neighbor is not contiguous.
  1051. */
  1052. temp = PREV.br_blockcount - new->br_blockcount;
  1053. xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
  1054. xfs_bmbt_set_blockcount(ep, temp);
  1055. xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
  1056. new, NULL, XFS_DATA_FORK);
  1057. xfs_iext_insert(ifp, idx + 1, 1, new);
  1058. ip->i_df.if_lastex = idx + 1;
  1059. ip->i_d.di_nextents++;
  1060. if (cur == NULL)
  1061. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1062. else {
  1063. rval = XFS_ILOG_CORE;
  1064. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1065. new->br_startblock, new->br_blockcount,
  1066. &i)))
  1067. goto done;
  1068. ASSERT(i == 0);
  1069. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1070. if ((error = xfs_bmbt_insert(cur, &i)))
  1071. goto done;
  1072. ASSERT(i == 1);
  1073. }
  1074. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1075. ip->i_d.di_nextents > ip->i_df.if_ext_max) {
  1076. error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
  1077. first, flist, &cur, 1, &tmp_rval,
  1078. XFS_DATA_FORK);
  1079. rval |= tmp_rval;
  1080. if (error)
  1081. goto done;
  1082. }
  1083. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  1084. STARTBLOCKVAL(PREV.br_startblock) -
  1085. (cur ? cur->bc_private.b.allocated : 0));
  1086. ep = xfs_iext_get_ext(ifp, idx);
  1087. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  1088. xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
  1089. *dnew = temp;
  1090. /* DELTA: One in-core extent is split in two. */
  1091. temp = PREV.br_startoff;
  1092. temp2 = PREV.br_blockcount;
  1093. break;
  1094. case 0:
  1095. /*
  1096. * Filling in the middle part of a previous delayed allocation.
  1097. * Contiguity is impossible here.
  1098. * This case is avoided almost all the time.
  1099. */
  1100. temp = new->br_startoff - PREV.br_startoff;
  1101. xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
  1102. xfs_bmbt_set_blockcount(ep, temp);
  1103. r[0] = *new;
  1104. r[1].br_state = PREV.br_state;
  1105. r[1].br_startblock = 0;
  1106. r[1].br_startoff = new_endoff;
  1107. temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
  1108. r[1].br_blockcount = temp2;
  1109. xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
  1110. XFS_DATA_FORK);
  1111. xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
  1112. ip->i_df.if_lastex = idx + 1;
  1113. ip->i_d.di_nextents++;
  1114. if (cur == NULL)
  1115. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1116. else {
  1117. rval = XFS_ILOG_CORE;
  1118. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1119. new->br_startblock, new->br_blockcount,
  1120. &i)))
  1121. goto done;
  1122. ASSERT(i == 0);
  1123. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1124. if ((error = xfs_bmbt_insert(cur, &i)))
  1125. goto done;
  1126. ASSERT(i == 1);
  1127. }
  1128. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1129. ip->i_d.di_nextents > ip->i_df.if_ext_max) {
  1130. error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
  1131. first, flist, &cur, 1, &tmp_rval,
  1132. XFS_DATA_FORK);
  1133. rval |= tmp_rval;
  1134. if (error)
  1135. goto done;
  1136. }
  1137. temp = xfs_bmap_worst_indlen(ip, temp);
  1138. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  1139. diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
  1140. (cur ? cur->bc_private.b.allocated : 0));
  1141. if (diff > 0 &&
  1142. xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
  1143. /*
  1144. * Ick gross gag me with a spoon.
  1145. */
  1146. ASSERT(0); /* want to see if this ever happens! */
  1147. while (diff > 0) {
  1148. if (temp) {
  1149. temp--;
  1150. diff--;
  1151. if (!diff ||
  1152. !xfs_mod_incore_sb(ip->i_mount,
  1153. XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
  1154. break;
  1155. }
  1156. if (temp2) {
  1157. temp2--;
  1158. diff--;
  1159. if (!diff ||
  1160. !xfs_mod_incore_sb(ip->i_mount,
  1161. XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
  1162. break;
  1163. }
  1164. }
  1165. }
  1166. ep = xfs_iext_get_ext(ifp, idx);
  1167. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  1168. xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
  1169. xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
  1170. XFS_DATA_FORK);
  1171. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
  1172. NULLSTARTBLOCK((int)temp2));
  1173. xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
  1174. XFS_DATA_FORK);
  1175. *dnew = temp + temp2;
  1176. /* DELTA: One in-core extent is split in three. */
  1177. temp = PREV.br_startoff;
  1178. temp2 = PREV.br_blockcount;
  1179. break;
  1180. case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1181. case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1182. case MASK2(LEFT_FILLING, RIGHT_CONTIG):
  1183. case MASK2(RIGHT_FILLING, LEFT_CONTIG):
  1184. case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
  1185. case MASK(LEFT_CONTIG):
  1186. case MASK(RIGHT_CONTIG):
  1187. /*
  1188. * These cases are all impossible.
  1189. */
  1190. ASSERT(0);
  1191. }
  1192. *curp = cur;
  1193. if (delta) {
  1194. temp2 += temp;
  1195. if (delta->xed_startoff > temp)
  1196. delta->xed_startoff = temp;
  1197. if (delta->xed_blockcount < temp2)
  1198. delta->xed_blockcount = temp2;
  1199. }
  1200. done:
  1201. *logflagsp = rval;
  1202. return error;
  1203. #undef LEFT
  1204. #undef RIGHT
  1205. #undef PREV
  1206. #undef MASK
  1207. #undef MASK2
  1208. #undef MASK3
  1209. #undef MASK4
  1210. #undef STATE_SET
  1211. #undef STATE_TEST
  1212. #undef STATE_SET_TEST
  1213. #undef SWITCH_STATE
  1214. }
  1215. /*
  1216. * Called by xfs_bmap_add_extent to handle cases converting an unwritten
  1217. * allocation to a real allocation or vice versa.
  1218. */
  1219. STATIC int /* error */
  1220. xfs_bmap_add_extent_unwritten_real(
  1221. xfs_inode_t *ip, /* incore inode pointer */
  1222. xfs_extnum_t idx, /* extent number to update/insert */
  1223. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  1224. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  1225. int *logflagsp, /* inode logging flags */
  1226. xfs_extdelta_t *delta) /* Change made to incore extents */
  1227. {
  1228. xfs_btree_cur_t *cur; /* btree cursor */
  1229. xfs_bmbt_rec_t *ep; /* extent entry for idx */
  1230. int error; /* error return value */
  1231. #ifdef XFS_BMAP_TRACE
  1232. static char fname[] = "xfs_bmap_add_extent_unwritten_real";
  1233. #endif
  1234. int i; /* temp state */
  1235. xfs_ifork_t *ifp; /* inode fork pointer */
  1236. xfs_fileoff_t new_endoff; /* end offset of new entry */
  1237. xfs_exntst_t newext; /* new extent state */
  1238. xfs_exntst_t oldext; /* old extent state */
  1239. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  1240. /* left is 0, right is 1, prev is 2 */
  1241. int rval=0; /* return value (logging flags) */
  1242. int state = 0;/* state bits, accessed thru macros */
  1243. xfs_filblks_t temp=0;
  1244. xfs_filblks_t temp2=0;
  1245. enum { /* bit number definitions for state */
  1246. LEFT_CONTIG, RIGHT_CONTIG,
  1247. LEFT_FILLING, RIGHT_FILLING,
  1248. LEFT_DELAY, RIGHT_DELAY,
  1249. LEFT_VALID, RIGHT_VALID
  1250. };
  1251. #define LEFT r[0]
  1252. #define RIGHT r[1]
  1253. #define PREV r[2]
  1254. #define MASK(b) (1 << (b))
  1255. #define MASK2(a,b) (MASK(a) | MASK(b))
  1256. #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
  1257. #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
  1258. #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
  1259. #define STATE_TEST(b) (state & MASK(b))
  1260. #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
  1261. ((state &= ~MASK(b)), 0))
  1262. #define SWITCH_STATE \
  1263. (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
  1264. /*
  1265. * Set up a bunch of variables to make the tests simpler.
  1266. */
  1267. error = 0;
  1268. cur = *curp;
  1269. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1270. ep = xfs_iext_get_ext(ifp, idx);
  1271. xfs_bmbt_get_all(ep, &PREV);
  1272. newext = new->br_state;
  1273. oldext = (newext == XFS_EXT_UNWRITTEN) ?
  1274. XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  1275. ASSERT(PREV.br_state == oldext);
  1276. new_endoff = new->br_startoff + new->br_blockcount;
  1277. ASSERT(PREV.br_startoff <= new->br_startoff);
  1278. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  1279. /*
  1280. * Set flags determining what part of the previous oldext allocation
  1281. * extent is being replaced by a newext allocation.
  1282. */
  1283. STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
  1284. STATE_SET(RIGHT_FILLING,
  1285. PREV.br_startoff + PREV.br_blockcount == new_endoff);
  1286. /*
  1287. * Check and set flags if this segment has a left neighbor.
  1288. * Don't set contiguous if the combined extent would be too large.
  1289. */
  1290. if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
  1291. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
  1292. STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
  1293. }
  1294. STATE_SET(LEFT_CONTIG,
  1295. STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
  1296. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  1297. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  1298. LEFT.br_state == newext &&
  1299. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
  1300. /*
  1301. * Check and set flags if this segment has a right neighbor.
  1302. * Don't set contiguous if the combined extent would be too large.
  1303. * Also check for all-three-contiguous being too large.
  1304. */
  1305. if (STATE_SET_TEST(RIGHT_VALID,
  1306. idx <
  1307. ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
  1308. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
  1309. STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
  1310. }
  1311. STATE_SET(RIGHT_CONTIG,
  1312. STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
  1313. new_endoff == RIGHT.br_startoff &&
  1314. new->br_startblock + new->br_blockcount ==
  1315. RIGHT.br_startblock &&
  1316. newext == RIGHT.br_state &&
  1317. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  1318. ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
  1319. MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
  1320. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  1321. <= MAXEXTLEN));
  1322. /*
  1323. * Switch out based on the FILLING and CONTIG state bits.
  1324. */
  1325. switch (SWITCH_STATE) {
  1326. case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1327. /*
  1328. * Setting all of a previous oldext extent to newext.
  1329. * The left and right neighbors are both contiguous with new.
  1330. */
  1331. xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
  1332. XFS_DATA_FORK);
  1333. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  1334. LEFT.br_blockcount + PREV.br_blockcount +
  1335. RIGHT.br_blockcount);
  1336. xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
  1337. XFS_DATA_FORK);
  1338. xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
  1339. XFS_DATA_FORK);
  1340. xfs_iext_remove(ifp, idx, 2);
  1341. ip->i_df.if_lastex = idx - 1;
  1342. ip->i_d.di_nextents -= 2;
  1343. if (cur == NULL)
  1344. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1345. else {
  1346. rval = XFS_ILOG_CORE;
  1347. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1348. RIGHT.br_startblock,
  1349. RIGHT.br_blockcount, &i)))
  1350. goto done;
  1351. ASSERT(i == 1);
  1352. if ((error = xfs_bmbt_delete(cur, &i)))
  1353. goto done;
  1354. ASSERT(i == 1);
  1355. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1356. goto done;
  1357. ASSERT(i == 1);
  1358. if ((error = xfs_bmbt_delete(cur, &i)))
  1359. goto done;
  1360. ASSERT(i == 1);
  1361. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1362. goto done;
  1363. ASSERT(i == 1);
  1364. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  1365. LEFT.br_startblock,
  1366. LEFT.br_blockcount + PREV.br_blockcount +
  1367. RIGHT.br_blockcount, LEFT.br_state)))
  1368. goto done;
  1369. }
  1370. /* DELTA: Three in-core extents are replaced by one. */
  1371. temp = LEFT.br_startoff;
  1372. temp2 = LEFT.br_blockcount +
  1373. PREV.br_blockcount +
  1374. RIGHT.br_blockcount;
  1375. break;
  1376. case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
  1377. /*
  1378. * Setting all of a previous oldext extent to newext.
  1379. * The left neighbor is contiguous, the right is not.
  1380. */
  1381. xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
  1382. XFS_DATA_FORK);
  1383. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  1384. LEFT.br_blockcount + PREV.br_blockcount);
  1385. xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
  1386. XFS_DATA_FORK);
  1387. ip->i_df.if_lastex = idx - 1;
  1388. xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
  1389. XFS_DATA_FORK);
  1390. xfs_iext_remove(ifp, idx, 1);
  1391. ip->i_d.di_nextents--;
  1392. if (cur == NULL)
  1393. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1394. else {
  1395. rval = XFS_ILOG_CORE;
  1396. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1397. PREV.br_startblock, PREV.br_blockcount,
  1398. &i)))
  1399. goto done;
  1400. ASSERT(i == 1);
  1401. if ((error = xfs_bmbt_delete(cur, &i)))
  1402. goto done;
  1403. ASSERT(i == 1);
  1404. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1405. goto done;
  1406. ASSERT(i == 1);
  1407. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  1408. LEFT.br_startblock,
  1409. LEFT.br_blockcount + PREV.br_blockcount,
  1410. LEFT.br_state)))
  1411. goto done;
  1412. }
  1413. /* DELTA: Two in-core extents are replaced by one. */
  1414. temp = LEFT.br_startoff;
  1415. temp2 = LEFT.br_blockcount +
  1416. PREV.br_blockcount;
  1417. break;
  1418. case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
  1419. /*
  1420. * Setting all of a previous oldext extent to newext.
  1421. * The right neighbor is contiguous, the left is not.
  1422. */
  1423. xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
  1424. XFS_DATA_FORK);
  1425. xfs_bmbt_set_blockcount(ep,
  1426. PREV.br_blockcount + RIGHT.br_blockcount);
  1427. xfs_bmbt_set_state(ep, newext);
  1428. xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
  1429. XFS_DATA_FORK);
  1430. ip->i_df.if_lastex = idx;
  1431. xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
  1432. XFS_DATA_FORK);
  1433. xfs_iext_remove(ifp, idx + 1, 1);
  1434. ip->i_d.di_nextents--;
  1435. if (cur == NULL)
  1436. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1437. else {
  1438. rval = XFS_ILOG_CORE;
  1439. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1440. RIGHT.br_startblock,
  1441. RIGHT.br_blockcount, &i)))
  1442. goto done;
  1443. ASSERT(i == 1);
  1444. if ((error = xfs_bmbt_delete(cur, &i)))
  1445. goto done;
  1446. ASSERT(i == 1);
  1447. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1448. goto done;
  1449. ASSERT(i == 1);
  1450. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1451. new->br_startblock,
  1452. new->br_blockcount + RIGHT.br_blockcount,
  1453. newext)))
  1454. goto done;
  1455. }
  1456. /* DELTA: Two in-core extents are replaced by one. */
  1457. temp = PREV.br_startoff;
  1458. temp2 = PREV.br_blockcount +
  1459. RIGHT.br_blockcount;
  1460. break;
  1461. case MASK2(LEFT_FILLING, RIGHT_FILLING):
  1462. /*
  1463. * Setting all of a previous oldext extent to newext.
  1464. * Neither the left nor right neighbors are contiguous with
  1465. * the new one.
  1466. */
  1467. xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
  1468. XFS_DATA_FORK);
  1469. xfs_bmbt_set_state(ep, newext);
  1470. xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
  1471. XFS_DATA_FORK);
  1472. ip->i_df.if_lastex = idx;
  1473. if (cur == NULL)
  1474. rval = XFS_ILOG_DEXT;
  1475. else {
  1476. rval = 0;
  1477. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1478. new->br_startblock, new->br_blockcount,
  1479. &i)))
  1480. goto done;
  1481. ASSERT(i == 1);
  1482. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1483. new->br_startblock, new->br_blockcount,
  1484. newext)))
  1485. goto done;
  1486. }
  1487. /* DELTA: The in-core extent described by new changed type. */
  1488. temp = new->br_startoff;
  1489. temp2 = new->br_blockcount;
  1490. break;
  1491. case MASK2(LEFT_FILLING, LEFT_CONTIG):
  1492. /*
  1493. * Setting the first part of a previous oldext extent to newext.
  1494. * The left neighbor is contiguous.
  1495. */
  1496. xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
  1497. XFS_DATA_FORK);
  1498. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  1499. LEFT.br_blockcount + new->br_blockcount);
  1500. xfs_bmbt_set_startoff(ep,
  1501. PREV.br_startoff + new->br_blockcount);
  1502. xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
  1503. XFS_DATA_FORK);
  1504. xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
  1505. XFS_DATA_FORK);
  1506. xfs_bmbt_set_startblock(ep,
  1507. new->br_startblock + new->br_blockcount);
  1508. xfs_bmbt_set_blockcount(ep,
  1509. PREV.br_blockcount - new->br_blockcount);
  1510. xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
  1511. XFS_DATA_FORK);
  1512. ip->i_df.if_lastex = idx - 1;
  1513. if (cur == NULL)
  1514. rval = XFS_ILOG_DEXT;
  1515. else {
  1516. rval = 0;
  1517. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1518. PREV.br_startblock, PREV.br_blockcount,
  1519. &i)))
  1520. goto done;
  1521. ASSERT(i == 1);
  1522. if ((error = xfs_bmbt_update(cur,
  1523. PREV.br_startoff + new->br_blockcount,
  1524. PREV.br_startblock + new->br_blockcount,
  1525. PREV.br_blockcount - new->br_blockcount,
  1526. oldext)))
  1527. goto done;
  1528. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  1529. goto done;
  1530. if (xfs_bmbt_update(cur, LEFT.br_startoff,
  1531. LEFT.br_startblock,
  1532. LEFT.br_blockcount + new->br_blockcount,
  1533. LEFT.br_state))
  1534. goto done;
  1535. }
  1536. /* DELTA: The boundary between two in-core extents moved. */
  1537. temp = LEFT.br_startoff;
  1538. temp2 = LEFT.br_blockcount +
  1539. PREV.br_blockcount;
  1540. break;
  1541. case MASK(LEFT_FILLING):
  1542. /*
  1543. * Setting the first part of a previous oldext extent to newext.
  1544. * The left neighbor is not contiguous.
  1545. */
  1546. xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
  1547. ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
  1548. xfs_bmbt_set_startoff(ep, new_endoff);
  1549. xfs_bmbt_set_blockcount(ep,
  1550. PREV.br_blockcount - new->br_blockcount);
  1551. xfs_bmbt_set_startblock(ep,
  1552. new->br_startblock + new->br_blockcount);
  1553. xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
  1554. xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
  1555. XFS_DATA_FORK);
  1556. xfs_iext_insert(ifp, idx, 1, new);
  1557. ip->i_df.if_lastex = idx;
  1558. ip->i_d.di_nextents++;
  1559. if (cur == NULL)
  1560. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1561. else {
  1562. rval = XFS_ILOG_CORE;
  1563. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1564. PREV.br_startblock, PREV.br_blockcount,
  1565. &i)))
  1566. goto done;
  1567. ASSERT(i == 1);
  1568. if ((error = xfs_bmbt_update(cur,
  1569. PREV.br_startoff + new->br_blockcount,
  1570. PREV.br_startblock + new->br_blockcount,
  1571. PREV.br_blockcount - new->br_blockcount,
  1572. oldext)))
  1573. goto done;
  1574. cur->bc_rec.b = *new;
  1575. if ((error = xfs_bmbt_insert(cur, &i)))
  1576. goto done;
  1577. ASSERT(i == 1);
  1578. }
  1579. /* DELTA: One in-core extent is split in two. */
  1580. temp = PREV.br_startoff;
  1581. temp2 = PREV.br_blockcount;
  1582. break;
  1583. case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
  1584. /*
  1585. * Setting the last part of a previous oldext extent to newext.
  1586. * The right neighbor is contiguous with the new allocation.
  1587. */
  1588. xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
  1589. XFS_DATA_FORK);
  1590. xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
  1591. XFS_DATA_FORK);
  1592. xfs_bmbt_set_blockcount(ep,
  1593. PREV.br_blockcount - new->br_blockcount);
  1594. xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
  1595. XFS_DATA_FORK);
  1596. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
  1597. new->br_startoff, new->br_startblock,
  1598. new->br_blockcount + RIGHT.br_blockcount, newext);
  1599. xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
  1600. XFS_DATA_FORK);
  1601. ip->i_df.if_lastex = idx + 1;
  1602. if (cur == NULL)
  1603. rval = XFS_ILOG_DEXT;
  1604. else {
  1605. rval = 0;
  1606. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1607. PREV.br_startblock,
  1608. PREV.br_blockcount, &i)))
  1609. goto done;
  1610. ASSERT(i == 1);
  1611. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  1612. PREV.br_startblock,
  1613. PREV.br_blockcount - new->br_blockcount,
  1614. oldext)))
  1615. goto done;
  1616. if ((error = xfs_bmbt_increment(cur, 0, &i)))
  1617. goto done;
  1618. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1619. new->br_startblock,
  1620. new->br_blockcount + RIGHT.br_blockcount,
  1621. newext)))
  1622. goto done;
  1623. }
  1624. /* DELTA: The boundary between two in-core extents moved. */
  1625. temp = PREV.br_startoff;
  1626. temp2 = PREV.br_blockcount +
  1627. RIGHT.br_blockcount;
  1628. break;
  1629. case MASK(RIGHT_FILLING):
  1630. /*
  1631. * Setting the last part of a previous oldext extent to newext.
  1632. * The right neighbor is not contiguous.
  1633. */
  1634. xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
  1635. xfs_bmbt_set_blockcount(ep,
  1636. PREV.br_blockcount - new->br_blockcount);
  1637. xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
  1638. xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
  1639. new, NULL, XFS_DATA_FORK);
  1640. xfs_iext_insert(ifp, idx + 1, 1, new);
  1641. ip->i_df.if_lastex = idx + 1;
  1642. ip->i_d.di_nextents++;
  1643. if (cur == NULL)
  1644. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1645. else {
  1646. rval = XFS_ILOG_CORE;
  1647. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1648. PREV.br_startblock, PREV.br_blockcount,
  1649. &i)))
  1650. goto done;
  1651. ASSERT(i == 1);
  1652. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  1653. PREV.br_startblock,
  1654. PREV.br_blockcount - new->br_blockcount,
  1655. oldext)))
  1656. goto done;
  1657. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1658. new->br_startblock, new->br_blockcount,
  1659. &i)))
  1660. goto done;
  1661. ASSERT(i == 0);
  1662. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1663. if ((error = xfs_bmbt_insert(cur, &i)))
  1664. goto done;
  1665. ASSERT(i == 1);
  1666. }
  1667. /* DELTA: One in-core extent is split in two. */
  1668. temp = PREV.br_startoff;
  1669. temp2 = PREV.br_blockcount;
  1670. break;
  1671. case 0:
  1672. /*
  1673. * Setting the middle part of a previous oldext extent to
  1674. * newext. Contiguity is impossible here.
  1675. * One extent becomes three extents.
  1676. */
  1677. xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
  1678. xfs_bmbt_set_blockcount(ep,
  1679. new->br_startoff - PREV.br_startoff);
  1680. xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
  1681. r[0] = *new;
  1682. r[1].br_startoff = new_endoff;
  1683. r[1].br_blockcount =
  1684. PREV.br_startoff + PREV.br_blockcount - new_endoff;
  1685. r[1].br_startblock = new->br_startblock + new->br_blockcount;
  1686. r[1].br_state = oldext;
  1687. xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
  1688. XFS_DATA_FORK);
  1689. xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
  1690. ip->i_df.if_lastex = idx + 1;
  1691. ip->i_d.di_nextents += 2;
  1692. if (cur == NULL)
  1693. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1694. else {
  1695. rval = XFS_ILOG_CORE;
  1696. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1697. PREV.br_startblock, PREV.br_blockcount,
  1698. &i)))
  1699. goto done;
  1700. ASSERT(i == 1);
  1701. /* new right extent - oldext */
  1702. if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
  1703. r[1].br_startblock, r[1].br_blockcount,
  1704. r[1].br_state)))
  1705. goto done;
  1706. /* new left extent - oldext */
  1707. PREV.br_blockcount =
  1708. new->br_startoff - PREV.br_startoff;
  1709. cur->bc_rec.b = PREV;
  1710. if ((error = xfs_bmbt_insert(cur, &i)))
  1711. goto done;
  1712. ASSERT(i == 1);
  1713. if ((error = xfs_bmbt_increment(cur, 0, &i)))
  1714. goto done;
  1715. ASSERT(i == 1);
  1716. /* new middle extent - newext */
  1717. cur->bc_rec.b = *new;
  1718. if ((error = xfs_bmbt_insert(cur, &i)))
  1719. goto done;
  1720. ASSERT(i == 1);
  1721. }
  1722. /* DELTA: One in-core extent is split in three. */
  1723. temp = PREV.br_startoff;
  1724. temp2 = PREV.br_blockcount;
  1725. break;
  1726. case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1727. case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
  1728. case MASK2(LEFT_FILLING, RIGHT_CONTIG):
  1729. case MASK2(RIGHT_FILLING, LEFT_CONTIG):
  1730. case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
  1731. case MASK(LEFT_CONTIG):
  1732. case MASK(RIGHT_CONTIG):
  1733. /*
  1734. * These cases are all impossible.
  1735. */
  1736. ASSERT(0);
  1737. }
  1738. *curp = cur;
  1739. if (delta) {
  1740. temp2 += temp;
  1741. if (delta->xed_startoff > temp)
  1742. delta->xed_startoff = temp;
  1743. if (delta->xed_blockcount < temp2)
  1744. delta->xed_blockcount = temp2;
  1745. }
  1746. done:
  1747. *logflagsp = rval;
  1748. return error;
  1749. #undef LEFT
  1750. #undef RIGHT
  1751. #undef PREV
  1752. #undef MASK
  1753. #undef MASK2
  1754. #undef MASK3
  1755. #undef MASK4
  1756. #undef STATE_SET
  1757. #undef STATE_TEST
  1758. #undef STATE_SET_TEST
  1759. #undef SWITCH_STATE
  1760. }
  1761. /*
  1762. * Called by xfs_bmap_add_extent to handle cases converting a hole
  1763. * to a delayed allocation.
  1764. */
  1765. /*ARGSUSED*/
  1766. STATIC int /* error */
  1767. xfs_bmap_add_extent_hole_delay(
  1768. xfs_inode_t *ip, /* incore inode pointer */
  1769. xfs_extnum_t idx, /* extent number to update/insert */
  1770. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  1771. int *logflagsp, /* inode logging flags */
  1772. xfs_extdelta_t *delta, /* Change made to incore extents */
  1773. int rsvd) /* OK to allocate reserved blocks */
  1774. {
  1775. xfs_bmbt_rec_t *ep; /* extent record for idx */
  1776. #ifdef XFS_BMAP_TRACE
  1777. static char fname[] = "xfs_bmap_add_extent_hole_delay";
  1778. #endif
  1779. xfs_ifork_t *ifp; /* inode fork pointer */
  1780. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  1781. xfs_filblks_t newlen=0; /* new indirect size */
  1782. xfs_filblks_t oldlen=0; /* old indirect size */
  1783. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  1784. int state; /* state bits, accessed thru macros */
  1785. xfs_filblks_t temp=0; /* temp for indirect calculations */
  1786. xfs_filblks_t temp2=0;
  1787. enum { /* bit number definitions for state */
  1788. LEFT_CONTIG, RIGHT_CONTIG,
  1789. LEFT_DELAY, RIGHT_DELAY,
  1790. LEFT_VALID, RIGHT_VALID
  1791. };
  1792. #define MASK(b) (1 << (b))
  1793. #define MASK2(a,b) (MASK(a) | MASK(b))
  1794. #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
  1795. #define STATE_TEST(b) (state & MASK(b))
  1796. #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
  1797. ((state &= ~MASK(b)), 0))
  1798. #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
  1799. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1800. ep = xfs_iext_get_ext(ifp, idx);
  1801. state = 0;
  1802. ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
  1803. /*
  1804. * Check and set flags if this segment has a left neighbor
  1805. */
  1806. if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
  1807. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
  1808. STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
  1809. }
  1810. /*
  1811. * Check and set flags if the current (right) segment exists.
  1812. * If it doesn't exist, we're converting the hole at end-of-file.
  1813. */
  1814. if (STATE_SET_TEST(RIGHT_VALID,
  1815. idx <
  1816. ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
  1817. xfs_bmbt_get_all(ep, &right);
  1818. STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
  1819. }
  1820. /*
  1821. * Set contiguity flags on the left and right neighbors.
  1822. * Don't let extents get too large, even if the pieces are contiguous.
  1823. */
  1824. STATE_SET(LEFT_CONTIG,
  1825. STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
  1826. left.br_startoff + left.br_blockcount == new->br_startoff &&
  1827. left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
  1828. STATE_SET(RIGHT_CONTIG,
  1829. STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
  1830. new->br_startoff + new->br_blockcount == right.br_startoff &&
  1831. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  1832. (!STATE_TEST(LEFT_CONTIG) ||
  1833. (left.br_blockcount + new->br_blockcount +
  1834. right.br_blockcount <= MAXEXTLEN)));
  1835. /*
  1836. * Switch out based on the contiguity flags.
  1837. */
  1838. switch (SWITCH_STATE) {
  1839. case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
  1840. /*
  1841. * New allocation is contiguous with delayed allocations
  1842. * on the left and on the right.
  1843. * Merge all three into a single extent record.
  1844. */
  1845. temp = left.br_blockcount + new->br_blockcount +
  1846. right.br_blockcount;
  1847. xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
  1848. XFS_DATA_FORK);
  1849. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
  1850. oldlen = STARTBLOCKVAL(left.br_startblock) +
  1851. STARTBLOCKVAL(new->br_startblock) +
  1852. STARTBLOCKVAL(right.br_startblock);
  1853. newlen = xfs_bmap_worst_indlen(ip, temp);
  1854. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
  1855. NULLSTARTBLOCK((int)newlen));
  1856. xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
  1857. XFS_DATA_FORK);
  1858. xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
  1859. XFS_DATA_FORK);
  1860. xfs_iext_remove(ifp, idx, 1);
  1861. ip->i_df.if_lastex = idx - 1;
  1862. /* DELTA: Two in-core extents were replaced by one. */
  1863. temp2 = temp;
  1864. temp = left.br_startoff;
  1865. break;
  1866. case MASK(LEFT_CONTIG):
  1867. /*
  1868. * New allocation is contiguous with a delayed allocation
  1869. * on the left.
  1870. * Merge the new allocation with the left neighbor.
  1871. */
  1872. temp = left.br_blockcount + new->br_blockcount;
  1873. xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
  1874. XFS_DATA_FORK);
  1875. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
  1876. oldlen = STARTBLOCKVAL(left.br_startblock) +
  1877. STARTBLOCKVAL(new->br_startblock);
  1878. newlen = xfs_bmap_worst_indlen(ip, temp);
  1879. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
  1880. NULLSTARTBLOCK((int)newlen));
  1881. xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
  1882. XFS_DATA_FORK);
  1883. ip->i_df.if_lastex = idx - 1;
  1884. /* DELTA: One in-core extent grew into a hole. */
  1885. temp2 = temp;
  1886. temp = left.br_startoff;
  1887. break;
  1888. case MASK(RIGHT_CONTIG):
  1889. /*
  1890. * New allocation is contiguous with a delayed allocation
  1891. * on the right.
  1892. * Merge the new allocation with the right neighbor.
  1893. */
  1894. xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
  1895. temp = new->br_blockcount + right.br_blockcount;
  1896. oldlen = STARTBLOCKVAL(new->br_startblock) +
  1897. STARTBLOCKVAL(right.br_startblock);
  1898. newlen = xfs_bmap_worst_indlen(ip, temp);
  1899. xfs_bmbt_set_allf(ep, new->br_startoff,
  1900. NULLSTARTBLOCK((int)newlen), temp, right.br_state);
  1901. xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
  1902. ip->i_df.if_lastex = idx;
  1903. /* DELTA: One in-core extent grew into a hole. */
  1904. temp2 = temp;
  1905. temp = new->br_startoff;
  1906. break;
  1907. case 0:
  1908. /*
  1909. * New allocation is not contiguous with another
  1910. * delayed allocation.
  1911. * Insert a new entry.
  1912. */
  1913. oldlen = newlen = 0;
  1914. xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
  1915. XFS_DATA_FORK);
  1916. xfs_iext_insert(ifp, idx, 1, new);
  1917. ip->i_df.if_lastex = idx;
  1918. /* DELTA: A new in-core extent was added in a hole. */
  1919. temp2 = new->br_blockcount;
  1920. temp = new->br_startoff;
  1921. break;
  1922. }
  1923. if (oldlen != newlen) {
  1924. ASSERT(oldlen > newlen);
  1925. xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
  1926. (int64_t)(oldlen - newlen), rsvd);
  1927. /*
  1928. * Nothing to do for disk quota accounting here.
  1929. */
  1930. }
  1931. if (delta) {
  1932. temp2 += temp;
  1933. if (delta->xed_startoff > temp)
  1934. delta->xed_startoff = temp;
  1935. if (delta->xed_blockcount < temp2)
  1936. delta->xed_blockcount = temp2;
  1937. }
  1938. *logflagsp = 0;
  1939. return 0;
  1940. #undef MASK
  1941. #undef MASK2
  1942. #undef STATE_SET
  1943. #undef STATE_TEST
  1944. #undef STATE_SET_TEST
  1945. #undef SWITCH_STATE
  1946. }
  1947. /*
  1948. * Called by xfs_bmap_add_extent to handle cases converting a hole
  1949. * to a real allocation.
  1950. */
  1951. STATIC int /* error */
  1952. xfs_bmap_add_extent_hole_real(
  1953. xfs_inode_t *ip, /* incore inode pointer */
  1954. xfs_extnum_t idx, /* extent number to update/insert */
  1955. xfs_btree_cur_t *cur, /* if null, not a btree */
  1956. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  1957. int *logflagsp, /* inode logging flags */
  1958. xfs_extdelta_t *delta, /* Change made to incore extents */
  1959. int whichfork) /* data or attr fork */
  1960. {
  1961. xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
  1962. int error; /* error return value */
  1963. #ifdef XFS_BMAP_TRACE
  1964. static char fname[] = "xfs_bmap_add_extent_hole_real";
  1965. #endif
  1966. int i; /* temp state */
  1967. xfs_ifork_t *ifp; /* inode fork pointer */
  1968. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  1969. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  1970. int rval=0; /* return value (logging flags) */
  1971. int state; /* state bits, accessed thru macros */
  1972. xfs_filblks_t temp=0;
  1973. xfs_filblks_t temp2=0;
  1974. enum { /* bit number definitions for state */
  1975. LEFT_CONTIG, RIGHT_CONTIG,
  1976. LEFT_DELAY, RIGHT_DELAY,
  1977. LEFT_VALID, RIGHT_VALID
  1978. };
  1979. #define MASK(b) (1 << (b))
  1980. #define MASK2(a,b) (MASK(a) | MASK(b))
  1981. #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
  1982. #define STATE_TEST(b) (state & MASK(b))
  1983. #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
  1984. ((state &= ~MASK(b)), 0))
  1985. #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
  1986. ifp = XFS_IFORK_PTR(ip, whichfork);
  1987. ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
  1988. ep = xfs_iext_get_ext(ifp, idx);
  1989. state = 0;
  1990. /*
  1991. * Check and set flags if this segment has a left neighbor.
  1992. */
  1993. if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
  1994. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
  1995. STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
  1996. }
  1997. /*
  1998. * Check and set flags if this segment has a current value.
  1999. * Not true if we're inserting into the "hole" at eof.
  2000. */
  2001. if (STATE_SET_TEST(RIGHT_VALID,
  2002. idx <
  2003. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
  2004. xfs_bmbt_get_all(ep, &right);
  2005. STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
  2006. }
  2007. /*
  2008. * We're inserting a real allocation between "left" and "right".
  2009. * Set the contiguity flags. Don't let extents get too large.
  2010. */
  2011. STATE_SET(LEFT_CONTIG,
  2012. STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
  2013. left.br_startoff + left.br_blockcount == new->br_startoff &&
  2014. left.br_startblock + left.br_blockcount == new->br_startblock &&
  2015. left.br_state == new->br_state &&
  2016. left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
  2017. STATE_SET(RIGHT_CONTIG,
  2018. STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
  2019. new->br_startoff + new->br_blockcount == right.br_startoff &&
  2020. new->br_startblock + new->br_blockcount ==
  2021. right.br_startblock &&
  2022. new->br_state == right.br_state &&
  2023. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  2024. (!STATE_TEST(LEFT_CONTIG) ||
  2025. left.br_blockcount + new->br_blockcount +
  2026. right.br_blockcount <= MAXEXTLEN));
  2027. error = 0;
  2028. /*
  2029. * Select which case we're in here, and implement it.
  2030. */
  2031. switch (SWITCH_STATE) {
  2032. case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
  2033. /*
  2034. * New allocation is contiguous with real allocations on the
  2035. * left and on the right.
  2036. * Merge all three into a single extent record.
  2037. */
  2038. xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
  2039. whichfork);
  2040. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  2041. left.br_blockcount + new->br_blockcount +
  2042. right.br_blockcount);
  2043. xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
  2044. whichfork);
  2045. xfs_bmap_trace_delete(fname, "LC|RC", ip,
  2046. idx, 1, whichfork);
  2047. xfs_iext_remove(ifp, idx, 1);
  2048. ifp->if_lastex = idx - 1;
  2049. XFS_IFORK_NEXT_SET(ip, whichfork,
  2050. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2051. if (cur == NULL) {
  2052. rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
  2053. } else {
  2054. rval = XFS_ILOG_CORE;
  2055. if ((error = xfs_bmbt_lookup_eq(cur,
  2056. right.br_startoff,
  2057. right.br_startblock,
  2058. right.br_blockcount, &i)))
  2059. goto done;
  2060. ASSERT(i == 1);
  2061. if ((error = xfs_bmbt_delete(cur, &i)))
  2062. goto done;
  2063. ASSERT(i == 1);
  2064. if ((error = xfs_bmbt_decrement(cur, 0, &i)))
  2065. goto done;
  2066. ASSERT(i == 1);
  2067. if ((error = xfs_bmbt_update(cur, left.br_startoff,
  2068. left.br_startblock,
  2069. left.br_blockcount +
  2070. new->br_blockcount +
  2071. right.br_blockcount,
  2072. left.br_state)))
  2073. goto done;
  2074. }
  2075. /* DELTA: Two in-core extents were replaced by one. */
  2076. temp = left.br_startoff;
  2077. temp2 = left.br_blockcount +
  2078. new->br_blockcount +
  2079. right.br_blockcount;
  2080. break;
  2081. case MASK(LEFT_CONTIG):
  2082. /*
  2083. * New allocation is contiguous with a real allocation
  2084. * on the left.
  2085. * Merge the new allocation with the left neighbor.
  2086. */
  2087. xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
  2088. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  2089. left.br_blockcount + new->br_blockcount);
  2090. xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
  2091. ifp->if_lastex = idx - 1;
  2092. if (cur == NULL) {
  2093. rval = XFS_ILOG_FEXT(whichfork);
  2094. } else {
  2095. rval = 0;
  2096. if ((error = xfs_bmbt_lookup_eq(cur,
  2097. left.br_startoff,
  2098. left.br_startblock,
  2099. left.br_blockcount, &i)))
  2100. goto done;
  2101. ASSERT(i == 1);
  2102. if ((error = xfs_bmbt_update(cur, left.br_startoff,
  2103. left.br_startblock,
  2104. left.br_blockcount +
  2105. new->br_blockcount,
  2106. left.br_state)))
  2107. goto done;
  2108. }
  2109. /* DELTA: One in-core extent grew. */
  2110. temp = left.br_startoff;
  2111. temp2 = left.br_blockcount +
  2112. new->br_blockcount;
  2113. break;
  2114. case MASK(RIGHT_CONTIG):
  2115. /*
  2116. * New allocation is contiguous with a real allocation
  2117. * on the right.
  2118. * Merge the new allocation with the right neighbor.
  2119. */
  2120. xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
  2121. xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
  2122. new->br_blockcount + right.br_blockcount,
  2123. right.br_state);
  2124. xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
  2125. ifp->if_lastex = idx;
  2126. if (cur == NULL) {
  2127. rval = XFS_ILOG_FEXT(whichfork);
  2128. } else {
  2129. rval = 0;
  2130. if ((error = xfs_bmbt_lookup_eq(cur,
  2131. right.br_startoff,
  2132. right.br_startblock,
  2133. right.br_blockcount, &i)))
  2134. goto done;
  2135. ASSERT(i == 1);
  2136. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  2137. new->br_startblock,
  2138. new->br_blockcount +
  2139. right.br_blockcount,
  2140. right.br_state)))
  2141. goto done;
  2142. }
  2143. /* DELTA: One in-core extent grew. */
  2144. temp = new->br_startoff;
  2145. temp2 = new->br_blockcount +
  2146. right.br_blockcount;
  2147. break;
  2148. case 0:
  2149. /*
  2150. * New allocation is not contiguous with another
  2151. * real allocation.
  2152. * Insert a new entry.
  2153. */
  2154. xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
  2155. whichfork);
  2156. xfs_iext_insert(ifp, idx, 1, new);
  2157. ifp->if_lastex = idx;
  2158. XFS_IFORK_NEXT_SET(ip, whichfork,
  2159. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2160. if (cur == NULL) {
  2161. rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
  2162. } else {
  2163. rval = XFS_ILOG_CORE;
  2164. if ((error = xfs_bmbt_lookup_eq(cur,
  2165. new->br_startoff,
  2166. new->br_startblock,
  2167. new->br_blockcount, &i)))
  2168. goto done;
  2169. ASSERT(i == 0);
  2170. cur->bc_rec.b.br_state = new->br_state;
  2171. if ((error = xfs_bmbt_insert(cur, &i)))
  2172. goto done;
  2173. ASSERT(i == 1);
  2174. }
  2175. /* DELTA: A new extent was added in a hole. */
  2176. temp = new->br_startoff;
  2177. temp2 = new->br_blockcount;
  2178. break;
  2179. }
  2180. if (delta) {
  2181. temp2 += temp;
  2182. if (delta->xed_startoff > temp)
  2183. delta->xed_startoff = temp;
  2184. if (delta->xed_blockcount < temp2)
  2185. delta->xed_blockcount = temp2;
  2186. }
  2187. done:
  2188. *logflagsp = rval;
  2189. return error;
  2190. #undef MASK
  2191. #undef MASK2
  2192. #undef STATE_SET
  2193. #undef STATE_TEST
  2194. #undef STATE_SET_TEST
  2195. #undef SWITCH_STATE
  2196. }
  2197. /*
  2198. * Adjust the size of the new extent based on di_extsize and rt extsize.
  2199. */
  2200. STATIC int
  2201. xfs_bmap_extsize_align(
  2202. xfs_mount_t *mp,
  2203. xfs_bmbt_irec_t *gotp, /* next extent pointer */
  2204. xfs_bmbt_irec_t *prevp, /* previous extent pointer */
  2205. xfs_extlen_t extsz, /* align to this extent size */
  2206. int rt, /* is this a realtime inode? */
  2207. int eof, /* is extent at end-of-file? */
  2208. int delay, /* creating delalloc extent? */
  2209. int convert, /* overwriting unwritten extent? */
  2210. xfs_fileoff_t *offp, /* in/out: aligned offset */
  2211. xfs_extlen_t *lenp) /* in/out: aligned length */
  2212. {
  2213. xfs_fileoff_t orig_off; /* original offset */
  2214. xfs_extlen_t orig_alen; /* original length */
  2215. xfs_fileoff_t orig_end; /* original off+len */
  2216. xfs_fileoff_t nexto; /* next file offset */
  2217. xfs_fileoff_t prevo; /* previous file offset */
  2218. xfs_fileoff_t align_off; /* temp for offset */
  2219. xfs_extlen_t align_alen; /* temp for length */
  2220. xfs_extlen_t temp; /* temp for calculations */
  2221. if (convert)
  2222. return 0;
  2223. orig_off = align_off = *offp;
  2224. orig_alen = align_alen = *lenp;
  2225. orig_end = orig_off + orig_alen;
  2226. /*
  2227. * If this request overlaps an existing extent, then don't
  2228. * attempt to perform any additional alignment.
  2229. */
  2230. if (!delay && !eof &&
  2231. (orig_off >= gotp->br_startoff) &&
  2232. (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
  2233. return 0;
  2234. }
  2235. /*
  2236. * If the file offset is unaligned vs. the extent size
  2237. * we need to align it. This will be possible unless
  2238. * the file was previously written with a kernel that didn't
  2239. * perform this alignment, or if a truncate shot us in the
  2240. * foot.
  2241. */
  2242. temp = do_mod(orig_off, extsz);
  2243. if (temp) {
  2244. align_alen += temp;
  2245. align_off -= temp;
  2246. }
  2247. /*
  2248. * Same adjustment for the end of the requested area.
  2249. */
  2250. if ((temp = (align_alen % extsz))) {
  2251. align_alen += extsz - temp;
  2252. }
  2253. /*
  2254. * If the previous block overlaps with this proposed allocation
  2255. * then move the start forward without adjusting the length.
  2256. */
  2257. if (prevp->br_startoff != NULLFILEOFF) {
  2258. if (prevp->br_startblock == HOLESTARTBLOCK)
  2259. prevo = prevp->br_startoff;
  2260. else
  2261. prevo = prevp->br_startoff + prevp->br_blockcount;
  2262. } else
  2263. prevo = 0;
  2264. if (align_off != orig_off && align_off < prevo)
  2265. align_off = prevo;
  2266. /*
  2267. * If the next block overlaps with this proposed allocation
  2268. * then move the start back without adjusting the length,
  2269. * but not before offset 0.
  2270. * This may of course make the start overlap previous block,
  2271. * and if we hit the offset 0 limit then the next block
  2272. * can still overlap too.
  2273. */
  2274. if (!eof && gotp->br_startoff != NULLFILEOFF) {
  2275. if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
  2276. (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
  2277. nexto = gotp->br_startoff + gotp->br_blockcount;
  2278. else
  2279. nexto = gotp->br_startoff;
  2280. } else
  2281. nexto = NULLFILEOFF;
  2282. if (!eof &&
  2283. align_off + align_alen != orig_end &&
  2284. align_off + align_alen > nexto)
  2285. align_off = nexto > align_alen ? nexto - align_alen : 0;
  2286. /*
  2287. * If we're now overlapping the next or previous extent that
  2288. * means we can't fit an extsz piece in this hole. Just move
  2289. * the start forward to the first valid spot and set
  2290. * the length so we hit the end.
  2291. */
  2292. if (align_off != orig_off && align_off < prevo)
  2293. align_off = prevo;
  2294. if (align_off + align_alen != orig_end &&
  2295. align_off + align_alen > nexto &&
  2296. nexto != NULLFILEOFF) {
  2297. ASSERT(nexto > prevo);
  2298. align_alen = nexto - align_off;
  2299. }
  2300. /*
  2301. * If realtime, and the result isn't a multiple of the realtime
  2302. * extent size we need to remove blocks until it is.
  2303. */
  2304. if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
  2305. /*
  2306. * We're not covering the original request, or
  2307. * we won't be able to once we fix the length.
  2308. */
  2309. if (orig_off < align_off ||
  2310. orig_end > align_off + align_alen ||
  2311. align_alen - temp < orig_alen)
  2312. return XFS_ERROR(EINVAL);
  2313. /*
  2314. * Try to fix it by moving the start up.
  2315. */
  2316. if (align_off + temp <= orig_off) {
  2317. align_alen -= temp;
  2318. align_off += temp;
  2319. }
  2320. /*
  2321. * Try to fix it by moving the end in.
  2322. */
  2323. else if (align_off + align_alen - temp >= orig_end)
  2324. align_alen -= temp;
  2325. /*
  2326. * Set the start to the minimum then trim the length.
  2327. */
  2328. else {
  2329. align_alen -= orig_off - align_off;
  2330. align_off = orig_off;
  2331. align_alen -= align_alen % mp->m_sb.sb_rextsize;
  2332. }
  2333. /*
  2334. * Result doesn't cover the request, fail it.
  2335. */
  2336. if (orig_off < align_off || orig_end > align_off + align_alen)
  2337. return XFS_ERROR(EINVAL);
  2338. } else {
  2339. ASSERT(orig_off >= align_off);
  2340. ASSERT(orig_end <= align_off + align_alen);
  2341. }
  2342. #ifdef DEBUG
  2343. if (!eof && gotp->br_startoff != NULLFILEOFF)
  2344. ASSERT(align_off + align_alen <= gotp->br_startoff);
  2345. if (prevp->br_startoff != NULLFILEOFF)
  2346. ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
  2347. #endif
  2348. *lenp = align_alen;
  2349. *offp = align_off;
  2350. return 0;
  2351. }
  2352. #define XFS_ALLOC_GAP_UNITS 4
  2353. STATIC int
  2354. xfs_bmap_adjacent(
  2355. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2356. {
  2357. xfs_fsblock_t adjust; /* adjustment to block numbers */
  2358. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  2359. xfs_mount_t *mp; /* mount point structure */
  2360. int nullfb; /* true if ap->firstblock isn't set */
  2361. int rt; /* true if inode is realtime */
  2362. #define ISVALID(x,y) \
  2363. (rt ? \
  2364. (x) < mp->m_sb.sb_rblocks : \
  2365. XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
  2366. XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
  2367. XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
  2368. mp = ap->ip->i_mount;
  2369. nullfb = ap->firstblock == NULLFSBLOCK;
  2370. rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
  2371. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
  2372. /*
  2373. * If allocating at eof, and there's a previous real block,
  2374. * try to use it's last block as our starting point.
  2375. */
  2376. if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
  2377. !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
  2378. ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
  2379. ap->prevp->br_startblock)) {
  2380. ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
  2381. /*
  2382. * Adjust for the gap between prevp and us.
  2383. */
  2384. adjust = ap->off -
  2385. (ap->prevp->br_startoff + ap->prevp->br_blockcount);
  2386. if (adjust &&
  2387. ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
  2388. ap->rval += adjust;
  2389. }
  2390. /*
  2391. * If not at eof, then compare the two neighbor blocks.
  2392. * Figure out whether either one gives us a good starting point,
  2393. * and pick the better one.
  2394. */
  2395. else if (!ap->eof) {
  2396. xfs_fsblock_t gotbno; /* right side block number */
  2397. xfs_fsblock_t gotdiff=0; /* right side difference */
  2398. xfs_fsblock_t prevbno; /* left side block number */
  2399. xfs_fsblock_t prevdiff=0; /* left side difference */
  2400. /*
  2401. * If there's a previous (left) block, select a requested
  2402. * start block based on it.
  2403. */
  2404. if (ap->prevp->br_startoff != NULLFILEOFF &&
  2405. !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
  2406. (prevbno = ap->prevp->br_startblock +
  2407. ap->prevp->br_blockcount) &&
  2408. ISVALID(prevbno, ap->prevp->br_startblock)) {
  2409. /*
  2410. * Calculate gap to end of previous block.
  2411. */
  2412. adjust = prevdiff = ap->off -
  2413. (ap->prevp->br_startoff +
  2414. ap->prevp->br_blockcount);
  2415. /*
  2416. * Figure the startblock based on the previous block's
  2417. * end and the gap size.
  2418. * Heuristic!
  2419. * If the gap is large relative to the piece we're
  2420. * allocating, or using it gives us an invalid block
  2421. * number, then just use the end of the previous block.
  2422. */
  2423. if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
  2424. ISVALID(prevbno + prevdiff,
  2425. ap->prevp->br_startblock))
  2426. prevbno += adjust;
  2427. else
  2428. prevdiff += adjust;
  2429. /*
  2430. * If the firstblock forbids it, can't use it,
  2431. * must use default.
  2432. */
  2433. if (!rt && !nullfb &&
  2434. XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
  2435. prevbno = NULLFSBLOCK;
  2436. }
  2437. /*
  2438. * No previous block or can't follow it, just default.
  2439. */
  2440. else
  2441. prevbno = NULLFSBLOCK;
  2442. /*
  2443. * If there's a following (right) block, select a requested
  2444. * start block based on it.
  2445. */
  2446. if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
  2447. /*
  2448. * Calculate gap to start of next block.
  2449. */
  2450. adjust = gotdiff = ap->gotp->br_startoff - ap->off;
  2451. /*
  2452. * Figure the startblock based on the next block's
  2453. * start and the gap size.
  2454. */
  2455. gotbno = ap->gotp->br_startblock;
  2456. /*
  2457. * Heuristic!
  2458. * If the gap is large relative to the piece we're
  2459. * allocating, or using it gives us an invalid block
  2460. * number, then just use the start of the next block
  2461. * offset by our length.
  2462. */
  2463. if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
  2464. ISVALID(gotbno - gotdiff, gotbno))
  2465. gotbno -= adjust;
  2466. else if (ISVALID(gotbno - ap->alen, gotbno)) {
  2467. gotbno -= ap->alen;
  2468. gotdiff += adjust - ap->alen;
  2469. } else
  2470. gotdiff += adjust;
  2471. /*
  2472. * If the firstblock forbids it, can't use it,
  2473. * must use default.
  2474. */
  2475. if (!rt && !nullfb &&
  2476. XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
  2477. gotbno = NULLFSBLOCK;
  2478. }
  2479. /*
  2480. * No next block, just default.
  2481. */
  2482. else
  2483. gotbno = NULLFSBLOCK;
  2484. /*
  2485. * If both valid, pick the better one, else the only good
  2486. * one, else ap->rval is already set (to 0 or the inode block).
  2487. */
  2488. if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
  2489. ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
  2490. else if (prevbno != NULLFSBLOCK)
  2491. ap->rval = prevbno;
  2492. else if (gotbno != NULLFSBLOCK)
  2493. ap->rval = gotbno;
  2494. }
  2495. #undef ISVALID
  2496. return 0;
  2497. }
  2498. STATIC int
  2499. xfs_bmap_rtalloc(
  2500. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2501. {
  2502. xfs_alloctype_t atype = 0; /* type for allocation routines */
  2503. int error; /* error return value */
  2504. xfs_mount_t *mp; /* mount point structure */
  2505. xfs_extlen_t prod = 0; /* product factor for allocators */
  2506. xfs_extlen_t ralen = 0; /* realtime allocation length */
  2507. xfs_extlen_t align; /* minimum allocation alignment */
  2508. xfs_rtblock_t rtx; /* realtime extent number */
  2509. xfs_rtblock_t rtb;
  2510. mp = ap->ip->i_mount;
  2511. align = ap->ip->i_d.di_extsize ?
  2512. ap->ip->i_d.di_extsize : mp->m_sb.sb_rextsize;
  2513. prod = align / mp->m_sb.sb_rextsize;
  2514. error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
  2515. align, 1, ap->eof, 0,
  2516. ap->conv, &ap->off, &ap->alen);
  2517. if (error)
  2518. return error;
  2519. ASSERT(ap->alen);
  2520. ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
  2521. /*
  2522. * If the offset & length are not perfectly aligned
  2523. * then kill prod, it will just get us in trouble.
  2524. */
  2525. if (do_mod(ap->off, align) || ap->alen % align)
  2526. prod = 1;
  2527. /*
  2528. * Set ralen to be the actual requested length in rtextents.
  2529. */
  2530. ralen = ap->alen / mp->m_sb.sb_rextsize;
  2531. /*
  2532. * If the old value was close enough to MAXEXTLEN that
  2533. * we rounded up to it, cut it back so it's valid again.
  2534. * Note that if it's a really large request (bigger than
  2535. * MAXEXTLEN), we don't hear about that number, and can't
  2536. * adjust the starting point to match it.
  2537. */
  2538. if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
  2539. ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
  2540. /*
  2541. * If it's an allocation to an empty file at offset 0,
  2542. * pick an extent that will space things out in the rt area.
  2543. */
  2544. if (ap->eof && ap->off == 0) {
  2545. error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
  2546. if (error)
  2547. return error;
  2548. ap->rval = rtx * mp->m_sb.sb_rextsize;
  2549. } else {
  2550. ap->rval = 0;
  2551. }
  2552. xfs_bmap_adjacent(ap);
  2553. /*
  2554. * Realtime allocation, done through xfs_rtallocate_extent.
  2555. */
  2556. atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
  2557. do_div(ap->rval, mp->m_sb.sb_rextsize);
  2558. rtb = ap->rval;
  2559. ap->alen = ralen;
  2560. if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
  2561. &ralen, atype, ap->wasdel, prod, &rtb)))
  2562. return error;
  2563. if (rtb == NULLFSBLOCK && prod > 1 &&
  2564. (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
  2565. ap->alen, &ralen, atype,
  2566. ap->wasdel, 1, &rtb)))
  2567. return error;
  2568. ap->rval = rtb;
  2569. if (ap->rval != NULLFSBLOCK) {
  2570. ap->rval *= mp->m_sb.sb_rextsize;
  2571. ralen *= mp->m_sb.sb_rextsize;
  2572. ap->alen = ralen;
  2573. ap->ip->i_d.di_nblocks += ralen;
  2574. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2575. if (ap->wasdel)
  2576. ap->ip->i_delayed_blks -= ralen;
  2577. /*
  2578. * Adjust the disk quota also. This was reserved
  2579. * earlier.
  2580. */
  2581. XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
  2582. ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
  2583. XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
  2584. } else {
  2585. ap->alen = 0;
  2586. }
  2587. return 0;
  2588. }
  2589. STATIC int
  2590. xfs_bmap_btalloc(
  2591. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2592. {
  2593. xfs_mount_t *mp; /* mount point structure */
  2594. xfs_alloctype_t atype = 0; /* type for allocation routines */
  2595. xfs_extlen_t align; /* minimum allocation alignment */
  2596. xfs_agnumber_t ag;
  2597. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  2598. xfs_agnumber_t startag;
  2599. xfs_alloc_arg_t args;
  2600. xfs_extlen_t blen;
  2601. xfs_extlen_t delta;
  2602. xfs_extlen_t longest;
  2603. xfs_extlen_t need;
  2604. xfs_extlen_t nextminlen = 0;
  2605. xfs_perag_t *pag;
  2606. int nullfb; /* true if ap->firstblock isn't set */
  2607. int isaligned;
  2608. int notinit;
  2609. int tryagain;
  2610. int error;
  2611. mp = ap->ip->i_mount;
  2612. align = (ap->userdata && ap->ip->i_d.di_extsize &&
  2613. (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)) ?
  2614. ap->ip->i_d.di_extsize : 0;
  2615. if (unlikely(align)) {
  2616. error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
  2617. align, 0, ap->eof, 0, ap->conv,
  2618. &ap->off, &ap->alen);
  2619. ASSERT(!error);
  2620. ASSERT(ap->alen);
  2621. }
  2622. nullfb = ap->firstblock == NULLFSBLOCK;
  2623. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
  2624. if (nullfb)
  2625. ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
  2626. else
  2627. ap->rval = ap->firstblock;
  2628. xfs_bmap_adjacent(ap);
  2629. /*
  2630. * If allowed, use ap->rval; otherwise must use firstblock since
  2631. * it's in the right allocation group.
  2632. */
  2633. if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
  2634. ;
  2635. else
  2636. ap->rval = ap->firstblock;
  2637. /*
  2638. * Normal allocation, done through xfs_alloc_vextent.
  2639. */
  2640. tryagain = isaligned = 0;
  2641. args.tp = ap->tp;
  2642. args.mp = mp;
  2643. args.fsbno = ap->rval;
  2644. args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
  2645. args.firstblock = ap->firstblock;
  2646. blen = 0;
  2647. if (nullfb) {
  2648. args.type = XFS_ALLOCTYPE_START_BNO;
  2649. args.total = ap->total;
  2650. /*
  2651. * Find the longest available space.
  2652. * We're going to try for the whole allocation at once.
  2653. */
  2654. startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
  2655. notinit = 0;
  2656. down_read(&mp->m_peraglock);
  2657. while (blen < ap->alen) {
  2658. pag = &mp->m_perag[ag];
  2659. if (!pag->pagf_init &&
  2660. (error = xfs_alloc_pagf_init(mp, args.tp,
  2661. ag, XFS_ALLOC_FLAG_TRYLOCK))) {
  2662. up_read(&mp->m_peraglock);
  2663. return error;
  2664. }
  2665. /*
  2666. * See xfs_alloc_fix_freelist...
  2667. */
  2668. if (pag->pagf_init) {
  2669. need = XFS_MIN_FREELIST_PAG(pag, mp);
  2670. delta = need > pag->pagf_flcount ?
  2671. need - pag->pagf_flcount : 0;
  2672. longest = (pag->pagf_longest > delta) ?
  2673. (pag->pagf_longest - delta) :
  2674. (pag->pagf_flcount > 0 ||
  2675. pag->pagf_longest > 0);
  2676. if (blen < longest)
  2677. blen = longest;
  2678. } else
  2679. notinit = 1;
  2680. if (++ag == mp->m_sb.sb_agcount)
  2681. ag = 0;
  2682. if (ag == startag)
  2683. break;
  2684. }
  2685. up_read(&mp->m_peraglock);
  2686. /*
  2687. * Since the above loop did a BUF_TRYLOCK, it is
  2688. * possible that there is space for this request.
  2689. */
  2690. if (notinit || blen < ap->minlen)
  2691. args.minlen = ap->minlen;
  2692. /*
  2693. * If the best seen length is less than the request
  2694. * length, use the best as the minimum.
  2695. */
  2696. else if (blen < ap->alen)
  2697. args.minlen = blen;
  2698. /*
  2699. * Otherwise we've seen an extent as big as alen,
  2700. * use that as the minimum.
  2701. */
  2702. else
  2703. args.minlen = ap->alen;
  2704. } else if (ap->low) {
  2705. args.type = XFS_ALLOCTYPE_START_BNO;
  2706. args.total = args.minlen = ap->minlen;
  2707. } else {
  2708. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  2709. args.total = ap->total;
  2710. args.minlen = ap->minlen;
  2711. }
  2712. if (unlikely(ap->userdata && ap->ip->i_d.di_extsize &&
  2713. (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE))) {
  2714. args.prod = ap->ip->i_d.di_extsize;
  2715. if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
  2716. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2717. } else if (mp->m_sb.sb_blocksize >= NBPP) {
  2718. args.prod = 1;
  2719. args.mod = 0;
  2720. } else {
  2721. args.prod = NBPP >> mp->m_sb.sb_blocklog;
  2722. if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
  2723. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2724. }
  2725. /*
  2726. * If we are not low on available data blocks, and the
  2727. * underlying logical volume manager is a stripe, and
  2728. * the file offset is zero then try to allocate data
  2729. * blocks on stripe unit boundary.
  2730. * NOTE: ap->aeof is only set if the allocation length
  2731. * is >= the stripe unit and the allocation offset is
  2732. * at the end of file.
  2733. */
  2734. if (!ap->low && ap->aeof) {
  2735. if (!ap->off) {
  2736. args.alignment = mp->m_dalign;
  2737. atype = args.type;
  2738. isaligned = 1;
  2739. /*
  2740. * Adjust for alignment
  2741. */
  2742. if (blen > args.alignment && blen <= ap->alen)
  2743. args.minlen = blen - args.alignment;
  2744. args.minalignslop = 0;
  2745. } else {
  2746. /*
  2747. * First try an exact bno allocation.
  2748. * If it fails then do a near or start bno
  2749. * allocation with alignment turned on.
  2750. */
  2751. atype = args.type;
  2752. tryagain = 1;
  2753. args.type = XFS_ALLOCTYPE_THIS_BNO;
  2754. args.alignment = 1;
  2755. /*
  2756. * Compute the minlen+alignment for the
  2757. * next case. Set slop so that the value
  2758. * of minlen+alignment+slop doesn't go up
  2759. * between the calls.
  2760. */
  2761. if (blen > mp->m_dalign && blen <= ap->alen)
  2762. nextminlen = blen - mp->m_dalign;
  2763. else
  2764. nextminlen = args.minlen;
  2765. if (nextminlen + mp->m_dalign > args.minlen + 1)
  2766. args.minalignslop =
  2767. nextminlen + mp->m_dalign -
  2768. args.minlen - 1;
  2769. else
  2770. args.minalignslop = 0;
  2771. }
  2772. } else {
  2773. args.alignment = 1;
  2774. args.minalignslop = 0;
  2775. }
  2776. args.minleft = ap->minleft;
  2777. args.wasdel = ap->wasdel;
  2778. args.isfl = 0;
  2779. args.userdata = ap->userdata;
  2780. if ((error = xfs_alloc_vextent(&args)))
  2781. return error;
  2782. if (tryagain && args.fsbno == NULLFSBLOCK) {
  2783. /*
  2784. * Exact allocation failed. Now try with alignment
  2785. * turned on.
  2786. */
  2787. args.type = atype;
  2788. args.fsbno = ap->rval;
  2789. args.alignment = mp->m_dalign;
  2790. args.minlen = nextminlen;
  2791. args.minalignslop = 0;
  2792. isaligned = 1;
  2793. if ((error = xfs_alloc_vextent(&args)))
  2794. return error;
  2795. }
  2796. if (isaligned && args.fsbno == NULLFSBLOCK) {
  2797. /*
  2798. * allocation failed, so turn off alignment and
  2799. * try again.
  2800. */
  2801. args.type = atype;
  2802. args.fsbno = ap->rval;
  2803. args.alignment = 0;
  2804. if ((error = xfs_alloc_vextent(&args)))
  2805. return error;
  2806. }
  2807. if (args.fsbno == NULLFSBLOCK && nullfb &&
  2808. args.minlen > ap->minlen) {
  2809. args.minlen = ap->minlen;
  2810. args.type = XFS_ALLOCTYPE_START_BNO;
  2811. args.fsbno = ap->rval;
  2812. if ((error = xfs_alloc_vextent(&args)))
  2813. return error;
  2814. }
  2815. if (args.fsbno == NULLFSBLOCK && nullfb) {
  2816. args.fsbno = 0;
  2817. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2818. args.total = ap->minlen;
  2819. args.minleft = 0;
  2820. if ((error = xfs_alloc_vextent(&args)))
  2821. return error;
  2822. ap->low = 1;
  2823. }
  2824. if (args.fsbno != NULLFSBLOCK) {
  2825. ap->firstblock = ap->rval = args.fsbno;
  2826. ASSERT(nullfb || fb_agno == args.agno ||
  2827. (ap->low && fb_agno < args.agno));
  2828. ap->alen = args.len;
  2829. ap->ip->i_d.di_nblocks += args.len;
  2830. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2831. if (ap->wasdel)
  2832. ap->ip->i_delayed_blks -= args.len;
  2833. /*
  2834. * Adjust the disk quota also. This was reserved
  2835. * earlier.
  2836. */
  2837. XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
  2838. ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
  2839. XFS_TRANS_DQ_BCOUNT,
  2840. (long) args.len);
  2841. } else {
  2842. ap->rval = NULLFSBLOCK;
  2843. ap->alen = 0;
  2844. }
  2845. return 0;
  2846. }
  2847. /*
  2848. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  2849. * It figures out where to ask the underlying allocator to put the new extent.
  2850. */
  2851. STATIC int
  2852. xfs_bmap_alloc(
  2853. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2854. {
  2855. if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
  2856. return xfs_bmap_rtalloc(ap);
  2857. return xfs_bmap_btalloc(ap);
  2858. }
  2859. /*
  2860. * Transform a btree format file with only one leaf node, where the
  2861. * extents list will fit in the inode, into an extents format file.
  2862. * Since the file extents are already in-core, all we have to do is
  2863. * give up the space for the btree root and pitch the leaf block.
  2864. */
  2865. STATIC int /* error */
  2866. xfs_bmap_btree_to_extents(
  2867. xfs_trans_t *tp, /* transaction pointer */
  2868. xfs_inode_t *ip, /* incore inode pointer */
  2869. xfs_btree_cur_t *cur, /* btree cursor */
  2870. int *logflagsp, /* inode logging flags */
  2871. int whichfork) /* data or attr fork */
  2872. {
  2873. /* REFERENCED */
  2874. xfs_bmbt_block_t *cblock;/* child btree block */
  2875. xfs_fsblock_t cbno; /* child block number */
  2876. xfs_buf_t *cbp; /* child block's buffer */
  2877. int error; /* error return value */
  2878. xfs_ifork_t *ifp; /* inode fork data */
  2879. xfs_mount_t *mp; /* mount point structure */
  2880. __be64 *pp; /* ptr to block address */
  2881. xfs_bmbt_block_t *rblock;/* root btree block */
  2882. ifp = XFS_IFORK_PTR(ip, whichfork);
  2883. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2884. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  2885. rblock = ifp->if_broot;
  2886. ASSERT(be16_to_cpu(rblock->bb_level) == 1);
  2887. ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
  2888. ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
  2889. mp = ip->i_mount;
  2890. pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
  2891. cbno = be64_to_cpu(*pp);
  2892. *logflagsp = 0;
  2893. #ifdef DEBUG
  2894. if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
  2895. return error;
  2896. #endif
  2897. if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
  2898. XFS_BMAP_BTREE_REF)))
  2899. return error;
  2900. cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
  2901. if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
  2902. return error;
  2903. xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
  2904. ip->i_d.di_nblocks--;
  2905. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  2906. xfs_trans_binval(tp, cbp);
  2907. if (cur->bc_bufs[0] == cbp)
  2908. cur->bc_bufs[0] = NULL;
  2909. xfs_iroot_realloc(ip, -1, whichfork);
  2910. ASSERT(ifp->if_broot == NULL);
  2911. ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
  2912. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  2913. *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
  2914. return 0;
  2915. }
  2916. /*
  2917. * Called by xfs_bmapi to update file extent records and the btree
  2918. * after removing space (or undoing a delayed allocation).
  2919. */
  2920. STATIC int /* error */
  2921. xfs_bmap_del_extent(
  2922. xfs_inode_t *ip, /* incore inode pointer */
  2923. xfs_trans_t *tp, /* current transaction pointer */
  2924. xfs_extnum_t idx, /* extent number to update/delete */
  2925. xfs_bmap_free_t *flist, /* list of extents to be freed */
  2926. xfs_btree_cur_t *cur, /* if null, not a btree */
  2927. xfs_bmbt_irec_t *del, /* data to remove from extents */
  2928. int *logflagsp, /* inode logging flags */
  2929. xfs_extdelta_t *delta, /* Change made to incore extents */
  2930. int whichfork, /* data or attr fork */
  2931. int rsvd) /* OK to allocate reserved blocks */
  2932. {
  2933. xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
  2934. xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
  2935. xfs_fsblock_t del_endblock=0; /* first block past del */
  2936. xfs_fileoff_t del_endoff; /* first offset past del */
  2937. int delay; /* current block is delayed allocated */
  2938. int do_fx; /* free extent at end of routine */
  2939. xfs_bmbt_rec_t *ep; /* current extent entry pointer */
  2940. int error; /* error return value */
  2941. int flags; /* inode logging flags */
  2942. #ifdef XFS_BMAP_TRACE
  2943. static char fname[] = "xfs_bmap_del_extent";
  2944. #endif
  2945. xfs_bmbt_irec_t got; /* current extent entry */
  2946. xfs_fileoff_t got_endoff; /* first offset past got */
  2947. int i; /* temp state */
  2948. xfs_ifork_t *ifp; /* inode fork pointer */
  2949. xfs_mount_t *mp; /* mount structure */
  2950. xfs_filblks_t nblks; /* quota/sb block count */
  2951. xfs_bmbt_irec_t new; /* new record to be inserted */
  2952. /* REFERENCED */
  2953. uint qfield; /* quota field to update */
  2954. xfs_filblks_t temp; /* for indirect length calculations */
  2955. xfs_filblks_t temp2; /* for indirect length calculations */
  2956. XFS_STATS_INC(xs_del_exlist);
  2957. mp = ip->i_mount;
  2958. ifp = XFS_IFORK_PTR(ip, whichfork);
  2959. ASSERT((idx >= 0) && (idx < ifp->if_bytes /
  2960. (uint)sizeof(xfs_bmbt_rec_t)));
  2961. ASSERT(del->br_blockcount > 0);
  2962. ep = xfs_iext_get_ext(ifp, idx);
  2963. xfs_bmbt_get_all(ep, &got);
  2964. ASSERT(got.br_startoff <= del->br_startoff);
  2965. del_endoff = del->br_startoff + del->br_blockcount;
  2966. got_endoff = got.br_startoff + got.br_blockcount;
  2967. ASSERT(got_endoff >= del_endoff);
  2968. delay = ISNULLSTARTBLOCK(got.br_startblock);
  2969. ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
  2970. flags = 0;
  2971. qfield = 0;
  2972. error = 0;
  2973. /*
  2974. * If deleting a real allocation, must free up the disk space.
  2975. */
  2976. if (!delay) {
  2977. flags = XFS_ILOG_CORE;
  2978. /*
  2979. * Realtime allocation. Free it and record di_nblocks update.
  2980. */
  2981. if (whichfork == XFS_DATA_FORK &&
  2982. (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
  2983. xfs_fsblock_t bno;
  2984. xfs_filblks_t len;
  2985. ASSERT(do_mod(del->br_blockcount,
  2986. mp->m_sb.sb_rextsize) == 0);
  2987. ASSERT(do_mod(del->br_startblock,
  2988. mp->m_sb.sb_rextsize) == 0);
  2989. bno = del->br_startblock;
  2990. len = del->br_blockcount;
  2991. do_div(bno, mp->m_sb.sb_rextsize);
  2992. do_div(len, mp->m_sb.sb_rextsize);
  2993. if ((error = xfs_rtfree_extent(ip->i_transp, bno,
  2994. (xfs_extlen_t)len)))
  2995. goto done;
  2996. do_fx = 0;
  2997. nblks = len * mp->m_sb.sb_rextsize;
  2998. qfield = XFS_TRANS_DQ_RTBCOUNT;
  2999. }
  3000. /*
  3001. * Ordinary allocation.
  3002. */
  3003. else {
  3004. do_fx = 1;
  3005. nblks = del->br_blockcount;
  3006. qfield = XFS_TRANS_DQ_BCOUNT;
  3007. }
  3008. /*
  3009. * Set up del_endblock and cur for later.
  3010. */
  3011. del_endblock = del->br_startblock + del->br_blockcount;
  3012. if (cur) {
  3013. if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  3014. got.br_startblock, got.br_blockcount,
  3015. &i)))
  3016. goto done;
  3017. ASSERT(i == 1);
  3018. }
  3019. da_old = da_new = 0;
  3020. } else {
  3021. da_old = STARTBLOCKVAL(got.br_startblock);
  3022. da_new = 0;
  3023. nblks = 0;
  3024. do_fx = 0;
  3025. }
  3026. /*
  3027. * Set flag value to use in switch statement.
  3028. * Left-contig is 2, right-contig is 1.
  3029. */
  3030. switch (((got.br_startoff == del->br_startoff) << 1) |
  3031. (got_endoff == del_endoff)) {
  3032. case 3:
  3033. /*
  3034. * Matches the whole extent. Delete the entry.
  3035. */
  3036. xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
  3037. xfs_iext_remove(ifp, idx, 1);
  3038. ifp->if_lastex = idx;
  3039. if (delay)
  3040. break;
  3041. XFS_IFORK_NEXT_SET(ip, whichfork,
  3042. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  3043. flags |= XFS_ILOG_CORE;
  3044. if (!cur) {
  3045. flags |= XFS_ILOG_FEXT(whichfork);
  3046. break;
  3047. }
  3048. if ((error = xfs_bmbt_delete(cur, &i)))
  3049. goto done;
  3050. ASSERT(i == 1);
  3051. break;
  3052. case 2:
  3053. /*
  3054. * Deleting the first part of the extent.
  3055. */
  3056. xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
  3057. xfs_bmbt_set_startoff(ep, del_endoff);
  3058. temp = got.br_blockcount - del->br_blockcount;
  3059. xfs_bmbt_set_blockcount(ep, temp);
  3060. ifp->if_lastex = idx;
  3061. if (delay) {
  3062. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  3063. da_old);
  3064. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  3065. xfs_bmap_trace_post_update(fname, "2", ip, idx,
  3066. whichfork);
  3067. da_new = temp;
  3068. break;
  3069. }
  3070. xfs_bmbt_set_startblock(ep, del_endblock);
  3071. xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
  3072. if (!cur) {
  3073. flags |= XFS_ILOG_FEXT(whichfork);
  3074. break;
  3075. }
  3076. if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
  3077. got.br_blockcount - del->br_blockcount,
  3078. got.br_state)))
  3079. goto done;
  3080. break;
  3081. case 1:
  3082. /*
  3083. * Deleting the last part of the extent.
  3084. */
  3085. temp = got.br_blockcount - del->br_blockcount;
  3086. xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
  3087. xfs_bmbt_set_blockcount(ep, temp);
  3088. ifp->if_lastex = idx;
  3089. if (delay) {
  3090. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  3091. da_old);
  3092. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  3093. xfs_bmap_trace_post_update(fname, "1", ip, idx,
  3094. whichfork);
  3095. da_new = temp;
  3096. break;
  3097. }
  3098. xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
  3099. if (!cur) {
  3100. flags |= XFS_ILOG_FEXT(whichfork);
  3101. break;
  3102. }
  3103. if ((error = xfs_bmbt_update(cur, got.br_startoff,
  3104. got.br_startblock,
  3105. got.br_blockcount - del->br_blockcount,
  3106. got.br_state)))
  3107. goto done;
  3108. break;
  3109. case 0:
  3110. /*
  3111. * Deleting the middle of the extent.
  3112. */
  3113. temp = del->br_startoff - got.br_startoff;
  3114. xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
  3115. xfs_bmbt_set_blockcount(ep, temp);
  3116. new.br_startoff = del_endoff;
  3117. temp2 = got_endoff - del_endoff;
  3118. new.br_blockcount = temp2;
  3119. new.br_state = got.br_state;
  3120. if (!delay) {
  3121. new.br_startblock = del_endblock;
  3122. flags |= XFS_ILOG_CORE;
  3123. if (cur) {
  3124. if ((error = xfs_bmbt_update(cur,
  3125. got.br_startoff,
  3126. got.br_startblock, temp,
  3127. got.br_state)))
  3128. goto done;
  3129. if ((error = xfs_bmbt_increment(cur, 0, &i)))
  3130. goto done;
  3131. cur->bc_rec.b = new;
  3132. error = xfs_bmbt_insert(cur, &i);
  3133. if (error && error != ENOSPC)
  3134. goto done;
  3135. /*
  3136. * If get no-space back from btree insert,
  3137. * it tried a split, and we have a zero
  3138. * block reservation.
  3139. * Fix up our state and return the error.
  3140. */
  3141. if (error == ENOSPC) {
  3142. /*
  3143. * Reset the cursor, don't trust
  3144. * it after any insert operation.
  3145. */
  3146. if ((error = xfs_bmbt_lookup_eq(cur,
  3147. got.br_startoff,
  3148. got.br_startblock,
  3149. temp, &i)))
  3150. goto done;
  3151. ASSERT(i == 1);
  3152. /*
  3153. * Update the btree record back
  3154. * to the original value.
  3155. */
  3156. if ((error = xfs_bmbt_update(cur,
  3157. got.br_startoff,
  3158. got.br_startblock,
  3159. got.br_blockcount,
  3160. got.br_state)))
  3161. goto done;
  3162. /*
  3163. * Reset the extent record back
  3164. * to the original value.
  3165. */
  3166. xfs_bmbt_set_blockcount(ep,
  3167. got.br_blockcount);
  3168. flags = 0;
  3169. error = XFS_ERROR(ENOSPC);
  3170. goto done;
  3171. }
  3172. ASSERT(i == 1);
  3173. } else
  3174. flags |= XFS_ILOG_FEXT(whichfork);
  3175. XFS_IFORK_NEXT_SET(ip, whichfork,
  3176. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  3177. } else {
  3178. ASSERT(whichfork == XFS_DATA_FORK);
  3179. temp = xfs_bmap_worst_indlen(ip, temp);
  3180. xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
  3181. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  3182. new.br_startblock = NULLSTARTBLOCK((int)temp2);
  3183. da_new = temp + temp2;
  3184. while (da_new > da_old) {
  3185. if (temp) {
  3186. temp--;
  3187. da_new--;
  3188. xfs_bmbt_set_startblock(ep,
  3189. NULLSTARTBLOCK((int)temp));
  3190. }
  3191. if (da_new == da_old)
  3192. break;
  3193. if (temp2) {
  3194. temp2--;
  3195. da_new--;
  3196. new.br_startblock =
  3197. NULLSTARTBLOCK((int)temp2);
  3198. }
  3199. }
  3200. }
  3201. xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
  3202. xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
  3203. whichfork);
  3204. xfs_iext_insert(ifp, idx + 1, 1, &new);
  3205. ifp->if_lastex = idx + 1;
  3206. break;
  3207. }
  3208. /*
  3209. * If we need to, add to list of extents to delete.
  3210. */
  3211. if (do_fx)
  3212. xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
  3213. mp);
  3214. /*
  3215. * Adjust inode # blocks in the file.
  3216. */
  3217. if (nblks)
  3218. ip->i_d.di_nblocks -= nblks;
  3219. /*
  3220. * Adjust quota data.
  3221. */
  3222. if (qfield)
  3223. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
  3224. /*
  3225. * Account for change in delayed indirect blocks.
  3226. * Nothing to do for disk quota accounting here.
  3227. */
  3228. ASSERT(da_old >= da_new);
  3229. if (da_old > da_new)
  3230. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
  3231. rsvd);
  3232. if (delta) {
  3233. /* DELTA: report the original extent. */
  3234. if (delta->xed_startoff > got.br_startoff)
  3235. delta->xed_startoff = got.br_startoff;
  3236. if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
  3237. delta->xed_blockcount = got.br_startoff +
  3238. got.br_blockcount;
  3239. }
  3240. done:
  3241. *logflagsp = flags;
  3242. return error;
  3243. }
  3244. /*
  3245. * Remove the entry "free" from the free item list. Prev points to the
  3246. * previous entry, unless "free" is the head of the list.
  3247. */
  3248. STATIC void
  3249. xfs_bmap_del_free(
  3250. xfs_bmap_free_t *flist, /* free item list header */
  3251. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  3252. xfs_bmap_free_item_t *free) /* list item to be freed */
  3253. {
  3254. if (prev)
  3255. prev->xbfi_next = free->xbfi_next;
  3256. else
  3257. flist->xbf_first = free->xbfi_next;
  3258. flist->xbf_count--;
  3259. kmem_zone_free(xfs_bmap_free_item_zone, free);
  3260. }
  3261. /*
  3262. * Convert an extents-format file into a btree-format file.
  3263. * The new file will have a root block (in the inode) and a single child block.
  3264. */
  3265. STATIC int /* error */
  3266. xfs_bmap_extents_to_btree(
  3267. xfs_trans_t *tp, /* transaction pointer */
  3268. xfs_inode_t *ip, /* incore inode pointer */
  3269. xfs_fsblock_t *firstblock, /* first-block-allocated */
  3270. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  3271. xfs_btree_cur_t **curp, /* cursor returned to caller */
  3272. int wasdel, /* converting a delayed alloc */
  3273. int *logflagsp, /* inode logging flags */
  3274. int whichfork) /* data or attr fork */
  3275. {
  3276. xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
  3277. xfs_buf_t *abp; /* buffer for ablock */
  3278. xfs_alloc_arg_t args; /* allocation arguments */
  3279. xfs_bmbt_rec_t *arp; /* child record pointer */
  3280. xfs_bmbt_block_t *block; /* btree root block */
  3281. xfs_btree_cur_t *cur; /* bmap btree cursor */
  3282. xfs_bmbt_rec_t *ep; /* extent record pointer */
  3283. int error; /* error return value */
  3284. xfs_extnum_t i, cnt; /* extent record index */
  3285. xfs_ifork_t *ifp; /* inode fork pointer */
  3286. xfs_bmbt_key_t *kp; /* root block key pointer */
  3287. xfs_mount_t *mp; /* mount structure */
  3288. xfs_extnum_t nextents; /* number of file extents */
  3289. xfs_bmbt_ptr_t *pp; /* root block address pointer */
  3290. ifp = XFS_IFORK_PTR(ip, whichfork);
  3291. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
  3292. ASSERT(ifp->if_ext_max ==
  3293. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  3294. /*
  3295. * Make space in the inode incore.
  3296. */
  3297. xfs_iroot_realloc(ip, 1, whichfork);
  3298. ifp->if_flags |= XFS_IFBROOT;
  3299. /*
  3300. * Fill in the root.
  3301. */
  3302. block = ifp->if_broot;
  3303. block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3304. block->bb_level = cpu_to_be16(1);
  3305. block->bb_numrecs = cpu_to_be16(1);
  3306. block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3307. block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3308. /*
  3309. * Need a cursor. Can't allocate until bb_level is filled in.
  3310. */
  3311. mp = ip->i_mount;
  3312. cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
  3313. whichfork);
  3314. cur->bc_private.b.firstblock = *firstblock;
  3315. cur->bc_private.b.flist = flist;
  3316. cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  3317. /*
  3318. * Convert to a btree with two levels, one record in root.
  3319. */
  3320. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
  3321. args.tp = tp;
  3322. args.mp = mp;
  3323. args.firstblock = *firstblock;
  3324. if (*firstblock == NULLFSBLOCK) {
  3325. args.type = XFS_ALLOCTYPE_START_BNO;
  3326. args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
  3327. } else if (flist->xbf_low) {
  3328. args.type = XFS_ALLOCTYPE_START_BNO;
  3329. args.fsbno = *firstblock;
  3330. } else {
  3331. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3332. args.fsbno = *firstblock;
  3333. }
  3334. args.minlen = args.maxlen = args.prod = 1;
  3335. args.total = args.minleft = args.alignment = args.mod = args.isfl =
  3336. args.minalignslop = 0;
  3337. args.wasdel = wasdel;
  3338. *logflagsp = 0;
  3339. if ((error = xfs_alloc_vextent(&args))) {
  3340. xfs_iroot_realloc(ip, -1, whichfork);
  3341. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  3342. return error;
  3343. }
  3344. /*
  3345. * Allocation can't fail, the space was reserved.
  3346. */
  3347. ASSERT(args.fsbno != NULLFSBLOCK);
  3348. ASSERT(*firstblock == NULLFSBLOCK ||
  3349. args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
  3350. (flist->xbf_low &&
  3351. args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
  3352. *firstblock = cur->bc_private.b.firstblock = args.fsbno;
  3353. cur->bc_private.b.allocated++;
  3354. ip->i_d.di_nblocks++;
  3355. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
  3356. abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
  3357. /*
  3358. * Fill in the child block.
  3359. */
  3360. ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
  3361. ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3362. ablock->bb_level = 0;
  3363. ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3364. ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3365. arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
  3366. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3367. for (cnt = i = 0; i < nextents; i++) {
  3368. ep = xfs_iext_get_ext(ifp, i);
  3369. if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
  3370. arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
  3371. arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
  3372. arp++; cnt++;
  3373. }
  3374. }
  3375. ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
  3376. ablock->bb_numrecs = cpu_to_be16(cnt);
  3377. /*
  3378. * Fill in the root key and pointer.
  3379. */
  3380. kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
  3381. arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
  3382. kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
  3383. pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
  3384. *pp = cpu_to_be64(args.fsbno);
  3385. /*
  3386. * Do all this logging at the end so that
  3387. * the root is at the right level.
  3388. */
  3389. xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
  3390. xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
  3391. ASSERT(*curp == NULL);
  3392. *curp = cur;
  3393. *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
  3394. return 0;
  3395. }
  3396. /*
  3397. * Helper routine to reset inode di_forkoff field when switching
  3398. * attribute fork from local to extent format - we reset it where
  3399. * possible to make space available for inline data fork extents.
  3400. */
  3401. STATIC void
  3402. xfs_bmap_forkoff_reset(
  3403. xfs_mount_t *mp,
  3404. xfs_inode_t *ip,
  3405. int whichfork)
  3406. {
  3407. if (whichfork == XFS_ATTR_FORK &&
  3408. (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
  3409. (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
  3410. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  3411. ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
  3412. ip->i_d.di_forkoff = mp->m_attroffset >> 3;
  3413. ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
  3414. (uint)sizeof(xfs_bmbt_rec_t);
  3415. ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
  3416. (uint)sizeof(xfs_bmbt_rec_t);
  3417. }
  3418. }
  3419. /*
  3420. * Convert a local file to an extents file.
  3421. * This code is out of bounds for data forks of regular files,
  3422. * since the file data needs to get logged so things will stay consistent.
  3423. * (The bmap-level manipulations are ok, though).
  3424. */
  3425. STATIC int /* error */
  3426. xfs_bmap_local_to_extents(
  3427. xfs_trans_t *tp, /* transaction pointer */
  3428. xfs_inode_t *ip, /* incore inode pointer */
  3429. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  3430. xfs_extlen_t total, /* total blocks needed by transaction */
  3431. int *logflagsp, /* inode logging flags */
  3432. int whichfork) /* data or attr fork */
  3433. {
  3434. int error; /* error return value */
  3435. int flags; /* logging flags returned */
  3436. #ifdef XFS_BMAP_TRACE
  3437. static char fname[] = "xfs_bmap_local_to_extents";
  3438. #endif
  3439. xfs_ifork_t *ifp; /* inode fork pointer */
  3440. /*
  3441. * We don't want to deal with the case of keeping inode data inline yet.
  3442. * So sending the data fork of a regular inode is invalid.
  3443. */
  3444. ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
  3445. whichfork == XFS_DATA_FORK));
  3446. ifp = XFS_IFORK_PTR(ip, whichfork);
  3447. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  3448. flags = 0;
  3449. error = 0;
  3450. if (ifp->if_bytes) {
  3451. xfs_alloc_arg_t args; /* allocation arguments */
  3452. xfs_buf_t *bp; /* buffer for extent block */
  3453. xfs_bmbt_rec_t *ep; /* extent record pointer */
  3454. args.tp = tp;
  3455. args.mp = ip->i_mount;
  3456. args.firstblock = *firstblock;
  3457. ASSERT((ifp->if_flags &
  3458. (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
  3459. /*
  3460. * Allocate a block. We know we need only one, since the
  3461. * file currently fits in an inode.
  3462. */
  3463. if (*firstblock == NULLFSBLOCK) {
  3464. args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
  3465. args.type = XFS_ALLOCTYPE_START_BNO;
  3466. } else {
  3467. args.fsbno = *firstblock;
  3468. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3469. }
  3470. args.total = total;
  3471. args.mod = args.minleft = args.alignment = args.wasdel =
  3472. args.isfl = args.minalignslop = 0;
  3473. args.minlen = args.maxlen = args.prod = 1;
  3474. if ((error = xfs_alloc_vextent(&args)))
  3475. goto done;
  3476. /*
  3477. * Can't fail, the space was reserved.
  3478. */
  3479. ASSERT(args.fsbno != NULLFSBLOCK);
  3480. ASSERT(args.len == 1);
  3481. *firstblock = args.fsbno;
  3482. bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
  3483. memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
  3484. ifp->if_bytes);
  3485. xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
  3486. xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
  3487. xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
  3488. xfs_iext_add(ifp, 0, 1);
  3489. ep = xfs_iext_get_ext(ifp, 0);
  3490. xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
  3491. xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
  3492. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  3493. ip->i_d.di_nblocks = 1;
  3494. XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
  3495. XFS_TRANS_DQ_BCOUNT, 1L);
  3496. flags |= XFS_ILOG_FEXT(whichfork);
  3497. } else {
  3498. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
  3499. xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
  3500. }
  3501. ifp->if_flags &= ~XFS_IFINLINE;
  3502. ifp->if_flags |= XFS_IFEXTENTS;
  3503. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  3504. flags |= XFS_ILOG_CORE;
  3505. done:
  3506. *logflagsp = flags;
  3507. return error;
  3508. }
  3509. /*
  3510. * Search the extent records for the entry containing block bno.
  3511. * If bno lies in a hole, point to the next entry. If bno lies
  3512. * past eof, *eofp will be set, and *prevp will contain the last
  3513. * entry (null if none). Else, *lastxp will be set to the index
  3514. * of the found entry; *gotp will contain the entry.
  3515. */
  3516. xfs_bmbt_rec_t * /* pointer to found extent entry */
  3517. xfs_bmap_search_multi_extents(
  3518. xfs_ifork_t *ifp, /* inode fork pointer */
  3519. xfs_fileoff_t bno, /* block number searched for */
  3520. int *eofp, /* out: end of file found */
  3521. xfs_extnum_t *lastxp, /* out: last extent index */
  3522. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3523. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3524. {
  3525. xfs_bmbt_rec_t *ep; /* extent record pointer */
  3526. xfs_extnum_t lastx; /* last extent index */
  3527. /*
  3528. * Initialize the extent entry structure to catch access to
  3529. * uninitialized br_startblock field.
  3530. */
  3531. gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
  3532. gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
  3533. gotp->br_state = XFS_EXT_INVALID;
  3534. #if XFS_BIG_BLKNOS
  3535. gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
  3536. #else
  3537. gotp->br_startblock = 0xffffa5a5;
  3538. #endif
  3539. prevp->br_startoff = NULLFILEOFF;
  3540. ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
  3541. if (lastx > 0) {
  3542. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
  3543. }
  3544. if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
  3545. xfs_bmbt_get_all(ep, gotp);
  3546. *eofp = 0;
  3547. } else {
  3548. if (lastx > 0) {
  3549. *gotp = *prevp;
  3550. }
  3551. *eofp = 1;
  3552. ep = NULL;
  3553. }
  3554. *lastxp = lastx;
  3555. return ep;
  3556. }
  3557. /*
  3558. * Search the extents list for the inode, for the extent containing bno.
  3559. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  3560. * *eofp will be set, and *prevp will contain the last entry (null if none).
  3561. * Else, *lastxp will be set to the index of the found
  3562. * entry; *gotp will contain the entry.
  3563. */
  3564. STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
  3565. xfs_bmap_search_extents(
  3566. xfs_inode_t *ip, /* incore inode pointer */
  3567. xfs_fileoff_t bno, /* block number searched for */
  3568. int fork, /* data or attr fork */
  3569. int *eofp, /* out: end of file found */
  3570. xfs_extnum_t *lastxp, /* out: last extent index */
  3571. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3572. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3573. {
  3574. xfs_ifork_t *ifp; /* inode fork pointer */
  3575. xfs_bmbt_rec_t *ep; /* extent record pointer */
  3576. XFS_STATS_INC(xs_look_exlist);
  3577. ifp = XFS_IFORK_PTR(ip, fork);
  3578. ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
  3579. if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
  3580. !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
  3581. xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
  3582. "Access to block zero in inode %llu "
  3583. "start_block: %llx start_off: %llx "
  3584. "blkcnt: %llx extent-state: %x lastx: %x\n",
  3585. (unsigned long long)ip->i_ino,
  3586. (unsigned long long)gotp->br_startblock,
  3587. (unsigned long long)gotp->br_startoff,
  3588. (unsigned long long)gotp->br_blockcount,
  3589. gotp->br_state, *lastxp);
  3590. *lastxp = NULLEXTNUM;
  3591. *eofp = 1;
  3592. return NULL;
  3593. }
  3594. return ep;
  3595. }
  3596. #ifdef XFS_BMAP_TRACE
  3597. ktrace_t *xfs_bmap_trace_buf;
  3598. /*
  3599. * Add a bmap trace buffer entry. Base routine for the others.
  3600. */
  3601. STATIC void
  3602. xfs_bmap_trace_addentry(
  3603. int opcode, /* operation */
  3604. char *fname, /* function name */
  3605. char *desc, /* operation description */
  3606. xfs_inode_t *ip, /* incore inode pointer */
  3607. xfs_extnum_t idx, /* index of entry(ies) */
  3608. xfs_extnum_t cnt, /* count of entries, 1 or 2 */
  3609. xfs_bmbt_rec_t *r1, /* first record */
  3610. xfs_bmbt_rec_t *r2, /* second record or null */
  3611. int whichfork) /* data or attr fork */
  3612. {
  3613. xfs_bmbt_rec_t tr2;
  3614. ASSERT(cnt == 1 || cnt == 2);
  3615. ASSERT(r1 != NULL);
  3616. if (cnt == 1) {
  3617. ASSERT(r2 == NULL);
  3618. r2 = &tr2;
  3619. memset(&tr2, 0, sizeof(tr2));
  3620. } else
  3621. ASSERT(r2 != NULL);
  3622. ktrace_enter(xfs_bmap_trace_buf,
  3623. (void *)(__psint_t)(opcode | (whichfork << 16)),
  3624. (void *)fname, (void *)desc, (void *)ip,
  3625. (void *)(__psint_t)idx,
  3626. (void *)(__psint_t)cnt,
  3627. (void *)(__psunsigned_t)(ip->i_ino >> 32),
  3628. (void *)(__psunsigned_t)(unsigned)ip->i_ino,
  3629. (void *)(__psunsigned_t)(r1->l0 >> 32),
  3630. (void *)(__psunsigned_t)(unsigned)(r1->l0),
  3631. (void *)(__psunsigned_t)(r1->l1 >> 32),
  3632. (void *)(__psunsigned_t)(unsigned)(r1->l1),
  3633. (void *)(__psunsigned_t)(r2->l0 >> 32),
  3634. (void *)(__psunsigned_t)(unsigned)(r2->l0),
  3635. (void *)(__psunsigned_t)(r2->l1 >> 32),
  3636. (void *)(__psunsigned_t)(unsigned)(r2->l1)
  3637. );
  3638. ASSERT(ip->i_xtrace);
  3639. ktrace_enter(ip->i_xtrace,
  3640. (void *)(__psint_t)(opcode | (whichfork << 16)),
  3641. (void *)fname, (void *)desc, (void *)ip,
  3642. (void *)(__psint_t)idx,
  3643. (void *)(__psint_t)cnt,
  3644. (void *)(__psunsigned_t)(ip->i_ino >> 32),
  3645. (void *)(__psunsigned_t)(unsigned)ip->i_ino,
  3646. (void *)(__psunsigned_t)(r1->l0 >> 32),
  3647. (void *)(__psunsigned_t)(unsigned)(r1->l0),
  3648. (void *)(__psunsigned_t)(r1->l1 >> 32),
  3649. (void *)(__psunsigned_t)(unsigned)(r1->l1),
  3650. (void *)(__psunsigned_t)(r2->l0 >> 32),
  3651. (void *)(__psunsigned_t)(unsigned)(r2->l0),
  3652. (void *)(__psunsigned_t)(r2->l1 >> 32),
  3653. (void *)(__psunsigned_t)(unsigned)(r2->l1)
  3654. );
  3655. }
  3656. /*
  3657. * Add bmap trace entry prior to a call to xfs_iext_remove.
  3658. */
  3659. STATIC void
  3660. xfs_bmap_trace_delete(
  3661. char *fname, /* function name */
  3662. char *desc, /* operation description */
  3663. xfs_inode_t *ip, /* incore inode pointer */
  3664. xfs_extnum_t idx, /* index of entry(entries) deleted */
  3665. xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
  3666. int whichfork) /* data or attr fork */
  3667. {
  3668. xfs_ifork_t *ifp; /* inode fork pointer */
  3669. ifp = XFS_IFORK_PTR(ip, whichfork);
  3670. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
  3671. cnt, xfs_iext_get_ext(ifp, idx),
  3672. cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
  3673. whichfork);
  3674. }
  3675. /*
  3676. * Add bmap trace entry prior to a call to xfs_iext_insert, or
  3677. * reading in the extents list from the disk (in the btree).
  3678. */
  3679. STATIC void
  3680. xfs_bmap_trace_insert(
  3681. char *fname, /* function name */
  3682. char *desc, /* operation description */
  3683. xfs_inode_t *ip, /* incore inode pointer */
  3684. xfs_extnum_t idx, /* index of entry(entries) inserted */
  3685. xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
  3686. xfs_bmbt_irec_t *r1, /* inserted record 1 */
  3687. xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
  3688. int whichfork) /* data or attr fork */
  3689. {
  3690. xfs_bmbt_rec_t tr1; /* compressed record 1 */
  3691. xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
  3692. xfs_bmbt_set_all(&tr1, r1);
  3693. if (cnt == 2) {
  3694. ASSERT(r2 != NULL);
  3695. xfs_bmbt_set_all(&tr2, r2);
  3696. } else {
  3697. ASSERT(cnt == 1);
  3698. ASSERT(r2 == NULL);
  3699. }
  3700. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
  3701. cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
  3702. }
  3703. /*
  3704. * Add bmap trace entry after updating an extent record in place.
  3705. */
  3706. STATIC void
  3707. xfs_bmap_trace_post_update(
  3708. char *fname, /* function name */
  3709. char *desc, /* operation description */
  3710. xfs_inode_t *ip, /* incore inode pointer */
  3711. xfs_extnum_t idx, /* index of entry updated */
  3712. int whichfork) /* data or attr fork */
  3713. {
  3714. xfs_ifork_t *ifp; /* inode fork pointer */
  3715. ifp = XFS_IFORK_PTR(ip, whichfork);
  3716. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
  3717. 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
  3718. }
  3719. /*
  3720. * Add bmap trace entry prior to updating an extent record in place.
  3721. */
  3722. STATIC void
  3723. xfs_bmap_trace_pre_update(
  3724. char *fname, /* function name */
  3725. char *desc, /* operation description */
  3726. xfs_inode_t *ip, /* incore inode pointer */
  3727. xfs_extnum_t idx, /* index of entry to be updated */
  3728. int whichfork) /* data or attr fork */
  3729. {
  3730. xfs_ifork_t *ifp; /* inode fork pointer */
  3731. ifp = XFS_IFORK_PTR(ip, whichfork);
  3732. xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
  3733. xfs_iext_get_ext(ifp, idx), NULL, whichfork);
  3734. }
  3735. #endif /* XFS_BMAP_TRACE */
  3736. /*
  3737. * Compute the worst-case number of indirect blocks that will be used
  3738. * for ip's delayed extent of length "len".
  3739. */
  3740. STATIC xfs_filblks_t
  3741. xfs_bmap_worst_indlen(
  3742. xfs_inode_t *ip, /* incore inode pointer */
  3743. xfs_filblks_t len) /* delayed extent length */
  3744. {
  3745. int level; /* btree level number */
  3746. int maxrecs; /* maximum record count at this level */
  3747. xfs_mount_t *mp; /* mount structure */
  3748. xfs_filblks_t rval; /* return value */
  3749. mp = ip->i_mount;
  3750. maxrecs = mp->m_bmap_dmxr[0];
  3751. for (level = 0, rval = 0;
  3752. level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
  3753. level++) {
  3754. len += maxrecs - 1;
  3755. do_div(len, maxrecs);
  3756. rval += len;
  3757. if (len == 1)
  3758. return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
  3759. level - 1;
  3760. if (level == 0)
  3761. maxrecs = mp->m_bmap_dmxr[1];
  3762. }
  3763. return rval;
  3764. }
  3765. #if defined(XFS_RW_TRACE)
  3766. STATIC void
  3767. xfs_bunmap_trace(
  3768. xfs_inode_t *ip,
  3769. xfs_fileoff_t bno,
  3770. xfs_filblks_t len,
  3771. int flags,
  3772. inst_t *ra)
  3773. {
  3774. if (ip->i_rwtrace == NULL)
  3775. return;
  3776. ktrace_enter(ip->i_rwtrace,
  3777. (void *)(__psint_t)XFS_BUNMAP,
  3778. (void *)ip,
  3779. (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
  3780. (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
  3781. (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
  3782. (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
  3783. (void *)(__psint_t)len,
  3784. (void *)(__psint_t)flags,
  3785. (void *)(unsigned long)current_cpu(),
  3786. (void *)ra,
  3787. (void *)0,
  3788. (void *)0,
  3789. (void *)0,
  3790. (void *)0,
  3791. (void *)0,
  3792. (void *)0);
  3793. }
  3794. #endif
  3795. /*
  3796. * Convert inode from non-attributed to attributed.
  3797. * Must not be in a transaction, ip must not be locked.
  3798. */
  3799. int /* error code */
  3800. xfs_bmap_add_attrfork(
  3801. xfs_inode_t *ip, /* incore inode pointer */
  3802. int size, /* space new attribute needs */
  3803. int rsvd) /* xact may use reserved blks */
  3804. {
  3805. xfs_fsblock_t firstblock; /* 1st block/ag allocated */
  3806. xfs_bmap_free_t flist; /* freed extent records */
  3807. xfs_mount_t *mp; /* mount structure */
  3808. xfs_trans_t *tp; /* transaction pointer */
  3809. unsigned long s; /* spinlock spl value */
  3810. int blks; /* space reservation */
  3811. int version = 1; /* superblock attr version */
  3812. int committed; /* xaction was committed */
  3813. int logflags; /* logging flags */
  3814. int error; /* error return value */
  3815. ASSERT(XFS_IFORK_Q(ip) == 0);
  3816. ASSERT(ip->i_df.if_ext_max ==
  3817. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3818. mp = ip->i_mount;
  3819. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  3820. tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
  3821. blks = XFS_ADDAFORK_SPACE_RES(mp);
  3822. if (rsvd)
  3823. tp->t_flags |= XFS_TRANS_RESERVE;
  3824. if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
  3825. XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
  3826. goto error0;
  3827. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3828. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
  3829. XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  3830. XFS_QMOPT_RES_REGBLKS);
  3831. if (error) {
  3832. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3833. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
  3834. return error;
  3835. }
  3836. if (XFS_IFORK_Q(ip))
  3837. goto error1;
  3838. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
  3839. /*
  3840. * For inodes coming from pre-6.2 filesystems.
  3841. */
  3842. ASSERT(ip->i_d.di_aformat == 0);
  3843. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  3844. }
  3845. ASSERT(ip->i_d.di_anextents == 0);
  3846. VN_HOLD(XFS_ITOV(ip));
  3847. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3848. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3849. switch (ip->i_d.di_format) {
  3850. case XFS_DINODE_FMT_DEV:
  3851. ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
  3852. break;
  3853. case XFS_DINODE_FMT_UUID:
  3854. ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
  3855. break;
  3856. case XFS_DINODE_FMT_LOCAL:
  3857. case XFS_DINODE_FMT_EXTENTS:
  3858. case XFS_DINODE_FMT_BTREE:
  3859. ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
  3860. if (!ip->i_d.di_forkoff)
  3861. ip->i_d.di_forkoff = mp->m_attroffset >> 3;
  3862. else if (mp->m_flags & XFS_MOUNT_ATTR2)
  3863. version = 2;
  3864. break;
  3865. default:
  3866. ASSERT(0);
  3867. error = XFS_ERROR(EINVAL);
  3868. goto error1;
  3869. }
  3870. ip->i_df.if_ext_max =
  3871. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3872. ASSERT(ip->i_afp == NULL);
  3873. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  3874. ip->i_afp->if_ext_max =
  3875. XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3876. ip->i_afp->if_flags = XFS_IFEXTENTS;
  3877. logflags = 0;
  3878. XFS_BMAP_INIT(&flist, &firstblock);
  3879. switch (ip->i_d.di_format) {
  3880. case XFS_DINODE_FMT_LOCAL:
  3881. error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
  3882. &logflags);
  3883. break;
  3884. case XFS_DINODE_FMT_EXTENTS:
  3885. error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
  3886. &flist, &logflags);
  3887. break;
  3888. case XFS_DINODE_FMT_BTREE:
  3889. error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
  3890. &logflags);
  3891. break;
  3892. default:
  3893. error = 0;
  3894. break;
  3895. }
  3896. if (logflags)
  3897. xfs_trans_log_inode(tp, ip, logflags);
  3898. if (error)
  3899. goto error2;
  3900. if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
  3901. (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
  3902. __int64_t sbfields = 0;
  3903. s = XFS_SB_LOCK(mp);
  3904. if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
  3905. XFS_SB_VERSION_ADDATTR(&mp->m_sb);
  3906. sbfields |= XFS_SB_VERSIONNUM;
  3907. }
  3908. if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
  3909. XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
  3910. sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
  3911. }
  3912. if (sbfields) {
  3913. XFS_SB_UNLOCK(mp, s);
  3914. xfs_mod_sb(tp, sbfields);
  3915. } else
  3916. XFS_SB_UNLOCK(mp, s);
  3917. }
  3918. if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
  3919. goto error2;
  3920. error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
  3921. ASSERT(ip->i_df.if_ext_max ==
  3922. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3923. return error;
  3924. error2:
  3925. xfs_bmap_cancel(&flist);
  3926. error1:
  3927. ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
  3928. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3929. error0:
  3930. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  3931. ASSERT(ip->i_df.if_ext_max ==
  3932. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3933. return error;
  3934. }
  3935. /*
  3936. * Add the extent to the list of extents to be free at transaction end.
  3937. * The list is maintained sorted (by block number).
  3938. */
  3939. /* ARGSUSED */
  3940. void
  3941. xfs_bmap_add_free(
  3942. xfs_fsblock_t bno, /* fs block number of extent */
  3943. xfs_filblks_t len, /* length of extent */
  3944. xfs_bmap_free_t *flist, /* list of extents */
  3945. xfs_mount_t *mp) /* mount point structure */
  3946. {
  3947. xfs_bmap_free_item_t *cur; /* current (next) element */
  3948. xfs_bmap_free_item_t *new; /* new element */
  3949. xfs_bmap_free_item_t *prev; /* previous element */
  3950. #ifdef DEBUG
  3951. xfs_agnumber_t agno;
  3952. xfs_agblock_t agbno;
  3953. ASSERT(bno != NULLFSBLOCK);
  3954. ASSERT(len > 0);
  3955. ASSERT(len <= MAXEXTLEN);
  3956. ASSERT(!ISNULLSTARTBLOCK(bno));
  3957. agno = XFS_FSB_TO_AGNO(mp, bno);
  3958. agbno = XFS_FSB_TO_AGBNO(mp, bno);
  3959. ASSERT(agno < mp->m_sb.sb_agcount);
  3960. ASSERT(agbno < mp->m_sb.sb_agblocks);
  3961. ASSERT(len < mp->m_sb.sb_agblocks);
  3962. ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
  3963. #endif
  3964. ASSERT(xfs_bmap_free_item_zone != NULL);
  3965. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  3966. new->xbfi_startblock = bno;
  3967. new->xbfi_blockcount = (xfs_extlen_t)len;
  3968. for (prev = NULL, cur = flist->xbf_first;
  3969. cur != NULL;
  3970. prev = cur, cur = cur->xbfi_next) {
  3971. if (cur->xbfi_startblock >= bno)
  3972. break;
  3973. }
  3974. if (prev)
  3975. prev->xbfi_next = new;
  3976. else
  3977. flist->xbf_first = new;
  3978. new->xbfi_next = cur;
  3979. flist->xbf_count++;
  3980. }
  3981. /*
  3982. * Compute and fill in the value of the maximum depth of a bmap btree
  3983. * in this filesystem. Done once, during mount.
  3984. */
  3985. void
  3986. xfs_bmap_compute_maxlevels(
  3987. xfs_mount_t *mp, /* file system mount structure */
  3988. int whichfork) /* data or attr fork */
  3989. {
  3990. int level; /* btree level */
  3991. uint maxblocks; /* max blocks at this level */
  3992. uint maxleafents; /* max leaf entries possible */
  3993. int maxrootrecs; /* max records in root block */
  3994. int minleafrecs; /* min records in leaf block */
  3995. int minnoderecs; /* min records in node block */
  3996. int sz; /* root block size */
  3997. /*
  3998. * The maximum number of extents in a file, hence the maximum
  3999. * number of leaf entries, is controlled by the type of di_nextents
  4000. * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
  4001. * (a signed 16-bit number, xfs_aextnum_t).
  4002. */
  4003. if (whichfork == XFS_DATA_FORK) {
  4004. maxleafents = MAXEXTNUM;
  4005. sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
  4006. XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
  4007. } else {
  4008. maxleafents = MAXAEXTNUM;
  4009. sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
  4010. XFS_BMDR_SPACE_CALC(MINABTPTRS) :
  4011. mp->m_sb.sb_inodesize - mp->m_attroffset;
  4012. }
  4013. maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
  4014. minleafrecs = mp->m_bmap_dmnr[0];
  4015. minnoderecs = mp->m_bmap_dmnr[1];
  4016. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  4017. for (level = 1; maxblocks > 1; level++) {
  4018. if (maxblocks <= maxrootrecs)
  4019. maxblocks = 1;
  4020. else
  4021. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  4022. }
  4023. mp->m_bm_maxlevels[whichfork] = level;
  4024. }
  4025. /*
  4026. * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
  4027. * caller. Frees all the extents that need freeing, which must be done
  4028. * last due to locking considerations. We never free any extents in
  4029. * the first transaction. This is to allow the caller to make the first
  4030. * transaction a synchronous one so that the pointers to the data being
  4031. * broken in this transaction will be permanent before the data is actually
  4032. * freed. This is necessary to prevent blocks from being reallocated
  4033. * and written to before the free and reallocation are actually permanent.
  4034. * We do not just make the first transaction synchronous here, because
  4035. * there are more efficient ways to gain the same protection in some cases
  4036. * (see the file truncation code).
  4037. *
  4038. * Return 1 if the given transaction was committed and a new one
  4039. * started, and 0 otherwise in the committed parameter.
  4040. */
  4041. /*ARGSUSED*/
  4042. int /* error */
  4043. xfs_bmap_finish(
  4044. xfs_trans_t **tp, /* transaction pointer addr */
  4045. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4046. int *committed) /* xact committed or not */
  4047. {
  4048. xfs_efd_log_item_t *efd; /* extent free data */
  4049. xfs_efi_log_item_t *efi; /* extent free intention */
  4050. int error; /* error return value */
  4051. xfs_bmap_free_item_t *free; /* free extent item */
  4052. unsigned int logres; /* new log reservation */
  4053. unsigned int logcount; /* new log count */
  4054. xfs_mount_t *mp; /* filesystem mount structure */
  4055. xfs_bmap_free_item_t *next; /* next item on free list */
  4056. xfs_trans_t *ntp; /* new transaction pointer */
  4057. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  4058. if (flist->xbf_count == 0) {
  4059. *committed = 0;
  4060. return 0;
  4061. }
  4062. ntp = *tp;
  4063. efi = xfs_trans_get_efi(ntp, flist->xbf_count);
  4064. for (free = flist->xbf_first; free; free = free->xbfi_next)
  4065. xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
  4066. free->xbfi_blockcount);
  4067. logres = ntp->t_log_res;
  4068. logcount = ntp->t_log_count;
  4069. ntp = xfs_trans_dup(*tp);
  4070. error = xfs_trans_commit(*tp, 0);
  4071. *tp = ntp;
  4072. *committed = 1;
  4073. /*
  4074. * We have a new transaction, so we should return committed=1,
  4075. * even though we're returning an error.
  4076. */
  4077. if (error) {
  4078. return error;
  4079. }
  4080. if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
  4081. logcount)))
  4082. return error;
  4083. efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
  4084. for (free = flist->xbf_first; free != NULL; free = next) {
  4085. next = free->xbfi_next;
  4086. if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
  4087. free->xbfi_blockcount))) {
  4088. /*
  4089. * The bmap free list will be cleaned up at a
  4090. * higher level. The EFI will be canceled when
  4091. * this transaction is aborted.
  4092. * Need to force shutdown here to make sure it
  4093. * happens, since this transaction may not be
  4094. * dirty yet.
  4095. */
  4096. mp = ntp->t_mountp;
  4097. if (!XFS_FORCED_SHUTDOWN(mp))
  4098. xfs_force_shutdown(mp,
  4099. (error == EFSCORRUPTED) ?
  4100. SHUTDOWN_CORRUPT_INCORE :
  4101. SHUTDOWN_META_IO_ERROR);
  4102. return error;
  4103. }
  4104. xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
  4105. free->xbfi_blockcount);
  4106. xfs_bmap_del_free(flist, NULL, free);
  4107. }
  4108. return 0;
  4109. }
  4110. /*
  4111. * Free up any items left in the list.
  4112. */
  4113. void
  4114. xfs_bmap_cancel(
  4115. xfs_bmap_free_t *flist) /* list of bmap_free_items */
  4116. {
  4117. xfs_bmap_free_item_t *free; /* free list item */
  4118. xfs_bmap_free_item_t *next;
  4119. if (flist->xbf_count == 0)
  4120. return;
  4121. ASSERT(flist->xbf_first != NULL);
  4122. for (free = flist->xbf_first; free; free = next) {
  4123. next = free->xbfi_next;
  4124. xfs_bmap_del_free(flist, NULL, free);
  4125. }
  4126. ASSERT(flist->xbf_count == 0);
  4127. }
  4128. /*
  4129. * Returns the file-relative block number of the first unused block(s)
  4130. * in the file with at least "len" logically contiguous blocks free.
  4131. * This is the lowest-address hole if the file has holes, else the first block
  4132. * past the end of file.
  4133. * Return 0 if the file is currently local (in-inode).
  4134. */
  4135. int /* error */
  4136. xfs_bmap_first_unused(
  4137. xfs_trans_t *tp, /* transaction pointer */
  4138. xfs_inode_t *ip, /* incore inode */
  4139. xfs_extlen_t len, /* size of hole to find */
  4140. xfs_fileoff_t *first_unused, /* unused block */
  4141. int whichfork) /* data or attr fork */
  4142. {
  4143. xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
  4144. int error; /* error return value */
  4145. int idx; /* extent record index */
  4146. xfs_ifork_t *ifp; /* inode fork pointer */
  4147. xfs_fileoff_t lastaddr; /* last block number seen */
  4148. xfs_fileoff_t lowest; /* lowest useful block */
  4149. xfs_fileoff_t max; /* starting useful block */
  4150. xfs_fileoff_t off; /* offset for this block */
  4151. xfs_extnum_t nextents; /* number of extent entries */
  4152. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
  4153. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
  4154. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  4155. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4156. *first_unused = 0;
  4157. return 0;
  4158. }
  4159. ifp = XFS_IFORK_PTR(ip, whichfork);
  4160. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4161. (error = xfs_iread_extents(tp, ip, whichfork)))
  4162. return error;
  4163. lowest = *first_unused;
  4164. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4165. for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
  4166. ep = xfs_iext_get_ext(ifp, idx);
  4167. off = xfs_bmbt_get_startoff(ep);
  4168. /*
  4169. * See if the hole before this extent will work.
  4170. */
  4171. if (off >= lowest + len && off - max >= len) {
  4172. *first_unused = max;
  4173. return 0;
  4174. }
  4175. lastaddr = off + xfs_bmbt_get_blockcount(ep);
  4176. max = XFS_FILEOFF_MAX(lastaddr, lowest);
  4177. }
  4178. *first_unused = max;
  4179. return 0;
  4180. }
  4181. /*
  4182. * Returns the file-relative block number of the last block + 1 before
  4183. * last_block (input value) in the file.
  4184. * This is not based on i_size, it is based on the extent records.
  4185. * Returns 0 for local files, as they do not have extent records.
  4186. */
  4187. int /* error */
  4188. xfs_bmap_last_before(
  4189. xfs_trans_t *tp, /* transaction pointer */
  4190. xfs_inode_t *ip, /* incore inode */
  4191. xfs_fileoff_t *last_block, /* last block */
  4192. int whichfork) /* data or attr fork */
  4193. {
  4194. xfs_fileoff_t bno; /* input file offset */
  4195. int eof; /* hit end of file */
  4196. xfs_bmbt_rec_t *ep; /* pointer to last extent */
  4197. int error; /* error return value */
  4198. xfs_bmbt_irec_t got; /* current extent value */
  4199. xfs_ifork_t *ifp; /* inode fork pointer */
  4200. xfs_extnum_t lastx; /* last extent used */
  4201. xfs_bmbt_irec_t prev; /* previous extent value */
  4202. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4203. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4204. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  4205. return XFS_ERROR(EIO);
  4206. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4207. *last_block = 0;
  4208. return 0;
  4209. }
  4210. ifp = XFS_IFORK_PTR(ip, whichfork);
  4211. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4212. (error = xfs_iread_extents(tp, ip, whichfork)))
  4213. return error;
  4214. bno = *last_block - 1;
  4215. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4216. &prev);
  4217. if (eof || xfs_bmbt_get_startoff(ep) > bno) {
  4218. if (prev.br_startoff == NULLFILEOFF)
  4219. *last_block = 0;
  4220. else
  4221. *last_block = prev.br_startoff + prev.br_blockcount;
  4222. }
  4223. /*
  4224. * Otherwise *last_block is already the right answer.
  4225. */
  4226. return 0;
  4227. }
  4228. /*
  4229. * Returns the file-relative block number of the first block past eof in
  4230. * the file. This is not based on i_size, it is based on the extent records.
  4231. * Returns 0 for local files, as they do not have extent records.
  4232. */
  4233. int /* error */
  4234. xfs_bmap_last_offset(
  4235. xfs_trans_t *tp, /* transaction pointer */
  4236. xfs_inode_t *ip, /* incore inode */
  4237. xfs_fileoff_t *last_block, /* last block */
  4238. int whichfork) /* data or attr fork */
  4239. {
  4240. xfs_bmbt_rec_t *ep; /* pointer to last extent */
  4241. int error; /* error return value */
  4242. xfs_ifork_t *ifp; /* inode fork pointer */
  4243. xfs_extnum_t nextents; /* number of extent entries */
  4244. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4245. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4246. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  4247. return XFS_ERROR(EIO);
  4248. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4249. *last_block = 0;
  4250. return 0;
  4251. }
  4252. ifp = XFS_IFORK_PTR(ip, whichfork);
  4253. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4254. (error = xfs_iread_extents(tp, ip, whichfork)))
  4255. return error;
  4256. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4257. if (!nextents) {
  4258. *last_block = 0;
  4259. return 0;
  4260. }
  4261. ep = xfs_iext_get_ext(ifp, nextents - 1);
  4262. *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
  4263. return 0;
  4264. }
  4265. /*
  4266. * Returns whether the selected fork of the inode has exactly one
  4267. * block or not. For the data fork we check this matches di_size,
  4268. * implying the file's range is 0..bsize-1.
  4269. */
  4270. int /* 1=>1 block, 0=>otherwise */
  4271. xfs_bmap_one_block(
  4272. xfs_inode_t *ip, /* incore inode */
  4273. int whichfork) /* data or attr fork */
  4274. {
  4275. xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
  4276. xfs_ifork_t *ifp; /* inode fork pointer */
  4277. int rval; /* return value */
  4278. xfs_bmbt_irec_t s; /* internal version of extent */
  4279. #ifndef DEBUG
  4280. if (whichfork == XFS_DATA_FORK)
  4281. return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
  4282. #endif /* !DEBUG */
  4283. if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
  4284. return 0;
  4285. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4286. return 0;
  4287. ifp = XFS_IFORK_PTR(ip, whichfork);
  4288. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  4289. ep = xfs_iext_get_ext(ifp, 0);
  4290. xfs_bmbt_get_all(ep, &s);
  4291. rval = s.br_startoff == 0 && s.br_blockcount == 1;
  4292. if (rval && whichfork == XFS_DATA_FORK)
  4293. ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
  4294. return rval;
  4295. }
  4296. /*
  4297. * Read in the extents to if_extents.
  4298. * All inode fields are set up by caller, we just traverse the btree
  4299. * and copy the records in. If the file system cannot contain unwritten
  4300. * extents, the records are checked for no "state" flags.
  4301. */
  4302. int /* error */
  4303. xfs_bmap_read_extents(
  4304. xfs_trans_t *tp, /* transaction pointer */
  4305. xfs_inode_t *ip, /* incore inode */
  4306. int whichfork) /* data or attr fork */
  4307. {
  4308. xfs_bmbt_block_t *block; /* current btree block */
  4309. xfs_fsblock_t bno; /* block # of "block" */
  4310. xfs_buf_t *bp; /* buffer for "block" */
  4311. int error; /* error return value */
  4312. xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
  4313. #ifdef XFS_BMAP_TRACE
  4314. static char fname[] = "xfs_bmap_read_extents";
  4315. #endif
  4316. xfs_extnum_t i, j; /* index into the extents list */
  4317. xfs_ifork_t *ifp; /* fork structure */
  4318. int level; /* btree level, for checking */
  4319. xfs_mount_t *mp; /* file system mount structure */
  4320. __be64 *pp; /* pointer to block address */
  4321. /* REFERENCED */
  4322. xfs_extnum_t room; /* number of entries there's room for */
  4323. bno = NULLFSBLOCK;
  4324. mp = ip->i_mount;
  4325. ifp = XFS_IFORK_PTR(ip, whichfork);
  4326. exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
  4327. XFS_EXTFMT_INODE(ip);
  4328. block = ifp->if_broot;
  4329. /*
  4330. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  4331. */
  4332. level = be16_to_cpu(block->bb_level);
  4333. ASSERT(level > 0);
  4334. pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
  4335. bno = be64_to_cpu(*pp);
  4336. ASSERT(bno != NULLDFSBNO);
  4337. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  4338. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  4339. /*
  4340. * Go down the tree until leaf level is reached, following the first
  4341. * pointer (leftmost) at each level.
  4342. */
  4343. while (level-- > 0) {
  4344. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4345. XFS_BMAP_BTREE_REF)))
  4346. return error;
  4347. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  4348. XFS_WANT_CORRUPTED_GOTO(
  4349. XFS_BMAP_SANITY_CHECK(mp, block, level),
  4350. error0);
  4351. if (level == 0)
  4352. break;
  4353. pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
  4354. bno = be64_to_cpu(*pp);
  4355. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  4356. xfs_trans_brelse(tp, bp);
  4357. }
  4358. /*
  4359. * Here with bp and block set to the leftmost leaf node in the tree.
  4360. */
  4361. room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4362. i = 0;
  4363. /*
  4364. * Loop over all leaf nodes. Copy information to the extent records.
  4365. */
  4366. for (;;) {
  4367. xfs_bmbt_rec_t *frp, *trp;
  4368. xfs_fsblock_t nextbno;
  4369. xfs_extnum_t num_recs;
  4370. xfs_extnum_t start;
  4371. num_recs = be16_to_cpu(block->bb_numrecs);
  4372. if (unlikely(i + num_recs > room)) {
  4373. ASSERT(i + num_recs <= room);
  4374. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  4375. "corrupt dinode %Lu, (btree extents).",
  4376. (unsigned long long) ip->i_ino);
  4377. XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
  4378. XFS_ERRLEVEL_LOW,
  4379. ip->i_mount);
  4380. goto error0;
  4381. }
  4382. XFS_WANT_CORRUPTED_GOTO(
  4383. XFS_BMAP_SANITY_CHECK(mp, block, 0),
  4384. error0);
  4385. /*
  4386. * Read-ahead the next leaf block, if any.
  4387. */
  4388. nextbno = be64_to_cpu(block->bb_rightsib);
  4389. if (nextbno != NULLFSBLOCK)
  4390. xfs_btree_reada_bufl(mp, nextbno, 1);
  4391. /*
  4392. * Copy records into the extent records.
  4393. */
  4394. frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
  4395. start = i;
  4396. for (j = 0; j < num_recs; j++, i++, frp++) {
  4397. trp = xfs_iext_get_ext(ifp, i);
  4398. trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
  4399. trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
  4400. }
  4401. if (exntf == XFS_EXTFMT_NOSTATE) {
  4402. /*
  4403. * Check all attribute bmap btree records and
  4404. * any "older" data bmap btree records for a
  4405. * set bit in the "extent flag" position.
  4406. */
  4407. if (unlikely(xfs_check_nostate_extents(ifp,
  4408. start, num_recs))) {
  4409. XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
  4410. XFS_ERRLEVEL_LOW,
  4411. ip->i_mount);
  4412. goto error0;
  4413. }
  4414. }
  4415. xfs_trans_brelse(tp, bp);
  4416. bno = nextbno;
  4417. /*
  4418. * If we've reached the end, stop.
  4419. */
  4420. if (bno == NULLFSBLOCK)
  4421. break;
  4422. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4423. XFS_BMAP_BTREE_REF)))
  4424. return error;
  4425. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  4426. }
  4427. ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  4428. ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
  4429. xfs_bmap_trace_exlist(fname, ip, i, whichfork);
  4430. return 0;
  4431. error0:
  4432. xfs_trans_brelse(tp, bp);
  4433. return XFS_ERROR(EFSCORRUPTED);
  4434. }
  4435. #ifdef XFS_BMAP_TRACE
  4436. /*
  4437. * Add bmap trace insert entries for all the contents of the extent records.
  4438. */
  4439. void
  4440. xfs_bmap_trace_exlist(
  4441. char *fname, /* function name */
  4442. xfs_inode_t *ip, /* incore inode pointer */
  4443. xfs_extnum_t cnt, /* count of entries in the list */
  4444. int whichfork) /* data or attr fork */
  4445. {
  4446. xfs_bmbt_rec_t *ep; /* current extent record */
  4447. xfs_extnum_t idx; /* extent record index */
  4448. xfs_ifork_t *ifp; /* inode fork pointer */
  4449. xfs_bmbt_irec_t s; /* file extent record */
  4450. ifp = XFS_IFORK_PTR(ip, whichfork);
  4451. ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  4452. for (idx = 0; idx < cnt; idx++) {
  4453. ep = xfs_iext_get_ext(ifp, idx);
  4454. xfs_bmbt_get_all(ep, &s);
  4455. xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
  4456. whichfork);
  4457. }
  4458. }
  4459. #endif
  4460. #ifdef DEBUG
  4461. /*
  4462. * Validate that the bmbt_irecs being returned from bmapi are valid
  4463. * given the callers original parameters. Specifically check the
  4464. * ranges of the returned irecs to ensure that they only extent beyond
  4465. * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
  4466. */
  4467. STATIC void
  4468. xfs_bmap_validate_ret(
  4469. xfs_fileoff_t bno,
  4470. xfs_filblks_t len,
  4471. int flags,
  4472. xfs_bmbt_irec_t *mval,
  4473. int nmap,
  4474. int ret_nmap)
  4475. {
  4476. int i; /* index to map values */
  4477. ASSERT(ret_nmap <= nmap);
  4478. for (i = 0; i < ret_nmap; i++) {
  4479. ASSERT(mval[i].br_blockcount > 0);
  4480. if (!(flags & XFS_BMAPI_ENTIRE)) {
  4481. ASSERT(mval[i].br_startoff >= bno);
  4482. ASSERT(mval[i].br_blockcount <= len);
  4483. ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
  4484. bno + len);
  4485. } else {
  4486. ASSERT(mval[i].br_startoff < bno + len);
  4487. ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
  4488. bno);
  4489. }
  4490. ASSERT(i == 0 ||
  4491. mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
  4492. mval[i].br_startoff);
  4493. if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
  4494. ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
  4495. mval[i].br_startblock != HOLESTARTBLOCK);
  4496. ASSERT(mval[i].br_state == XFS_EXT_NORM ||
  4497. mval[i].br_state == XFS_EXT_UNWRITTEN);
  4498. }
  4499. }
  4500. #endif /* DEBUG */
  4501. /*
  4502. * Map file blocks to filesystem blocks.
  4503. * File range is given by the bno/len pair.
  4504. * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
  4505. * into a hole or past eof.
  4506. * Only allocates blocks from a single allocation group,
  4507. * to avoid locking problems.
  4508. * The returned value in "firstblock" from the first call in a transaction
  4509. * must be remembered and presented to subsequent calls in "firstblock".
  4510. * An upper bound for the number of blocks to be allocated is supplied to
  4511. * the first call in "total"; if no allocation group has that many free
  4512. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  4513. */
  4514. int /* error */
  4515. xfs_bmapi(
  4516. xfs_trans_t *tp, /* transaction pointer */
  4517. xfs_inode_t *ip, /* incore inode */
  4518. xfs_fileoff_t bno, /* starting file offs. mapped */
  4519. xfs_filblks_t len, /* length to map in file */
  4520. int flags, /* XFS_BMAPI_... */
  4521. xfs_fsblock_t *firstblock, /* first allocated block
  4522. controls a.g. for allocs */
  4523. xfs_extlen_t total, /* total blocks needed */
  4524. xfs_bmbt_irec_t *mval, /* output: map values */
  4525. int *nmap, /* i/o: mval size/count */
  4526. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4527. xfs_extdelta_t *delta) /* o: change made to incore extents */
  4528. {
  4529. xfs_fsblock_t abno; /* allocated block number */
  4530. xfs_extlen_t alen; /* allocated extent length */
  4531. xfs_fileoff_t aoff; /* allocated file offset */
  4532. xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
  4533. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4534. xfs_fileoff_t end; /* end of mapped file region */
  4535. int eof; /* we've hit the end of extents */
  4536. xfs_bmbt_rec_t *ep; /* extent record pointer */
  4537. int error; /* error return */
  4538. xfs_bmbt_irec_t got; /* current file extent record */
  4539. xfs_ifork_t *ifp; /* inode fork pointer */
  4540. xfs_extlen_t indlen; /* indirect blocks length */
  4541. xfs_extnum_t lastx; /* last useful extent number */
  4542. int logflags; /* flags for transaction logging */
  4543. xfs_extlen_t minleft; /* min blocks left after allocation */
  4544. xfs_extlen_t minlen; /* min allocation size */
  4545. xfs_mount_t *mp; /* xfs mount structure */
  4546. int n; /* current extent index */
  4547. int nallocs; /* number of extents alloc\'d */
  4548. xfs_extnum_t nextents; /* number of extents in file */
  4549. xfs_fileoff_t obno; /* old block number (offset) */
  4550. xfs_bmbt_irec_t prev; /* previous file extent record */
  4551. int tmp_logflags; /* temp flags holder */
  4552. int whichfork; /* data or attr fork */
  4553. char inhole; /* current location is hole in file */
  4554. char wasdelay; /* old extent was delayed */
  4555. char wr; /* this is a write request */
  4556. char rt; /* this is a realtime file */
  4557. #ifdef DEBUG
  4558. xfs_fileoff_t orig_bno; /* original block number value */
  4559. int orig_flags; /* original flags arg value */
  4560. xfs_filblks_t orig_len; /* original value of len arg */
  4561. xfs_bmbt_irec_t *orig_mval; /* original value of mval */
  4562. int orig_nmap; /* original value of *nmap */
  4563. orig_bno = bno;
  4564. orig_len = len;
  4565. orig_flags = flags;
  4566. orig_mval = mval;
  4567. orig_nmap = *nmap;
  4568. #endif
  4569. ASSERT(*nmap >= 1);
  4570. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
  4571. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4572. XFS_ATTR_FORK : XFS_DATA_FORK;
  4573. mp = ip->i_mount;
  4574. if (unlikely(XFS_TEST_ERROR(
  4575. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4576. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4577. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
  4578. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4579. XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
  4580. return XFS_ERROR(EFSCORRUPTED);
  4581. }
  4582. if (XFS_FORCED_SHUTDOWN(mp))
  4583. return XFS_ERROR(EIO);
  4584. rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4585. ifp = XFS_IFORK_PTR(ip, whichfork);
  4586. ASSERT(ifp->if_ext_max ==
  4587. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4588. if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
  4589. XFS_STATS_INC(xs_blk_mapw);
  4590. else
  4591. XFS_STATS_INC(xs_blk_mapr);
  4592. /*
  4593. * IGSTATE flag is used to combine extents which
  4594. * differ only due to the state of the extents.
  4595. * This technique is used from xfs_getbmap()
  4596. * when the caller does not wish to see the
  4597. * separation (which is the default).
  4598. *
  4599. * This technique is also used when writing a
  4600. * buffer which has been partially written,
  4601. * (usually by being flushed during a chunkread),
  4602. * to ensure one write takes place. This also
  4603. * prevents a change in the xfs inode extents at
  4604. * this time, intentionally. This change occurs
  4605. * on completion of the write operation, in
  4606. * xfs_strat_comp(), where the xfs_bmapi() call
  4607. * is transactioned, and the extents combined.
  4608. */
  4609. if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
  4610. wr = 0; /* no allocations are allowed */
  4611. ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
  4612. logflags = 0;
  4613. nallocs = 0;
  4614. cur = NULL;
  4615. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4616. ASSERT(wr && tp);
  4617. if ((error = xfs_bmap_local_to_extents(tp, ip,
  4618. firstblock, total, &logflags, whichfork)))
  4619. goto error0;
  4620. }
  4621. if (wr && *firstblock == NULLFSBLOCK) {
  4622. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
  4623. minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  4624. else
  4625. minleft = 1;
  4626. } else
  4627. minleft = 0;
  4628. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4629. (error = xfs_iread_extents(tp, ip, whichfork)))
  4630. goto error0;
  4631. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4632. &prev);
  4633. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4634. n = 0;
  4635. end = bno + len;
  4636. obno = bno;
  4637. bma.ip = NULL;
  4638. if (delta) {
  4639. delta->xed_startoff = NULLFILEOFF;
  4640. delta->xed_blockcount = 0;
  4641. }
  4642. while (bno < end && n < *nmap) {
  4643. /*
  4644. * Reading past eof, act as though there's a hole
  4645. * up to end.
  4646. */
  4647. if (eof && !wr)
  4648. got.br_startoff = end;
  4649. inhole = eof || got.br_startoff > bno;
  4650. wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
  4651. ISNULLSTARTBLOCK(got.br_startblock);
  4652. /*
  4653. * First, deal with the hole before the allocated space
  4654. * that we found, if any.
  4655. */
  4656. if (wr && (inhole || wasdelay)) {
  4657. /*
  4658. * For the wasdelay case, we could also just
  4659. * allocate the stuff asked for in this bmap call
  4660. * but that wouldn't be as good.
  4661. */
  4662. if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
  4663. alen = (xfs_extlen_t)got.br_blockcount;
  4664. aoff = got.br_startoff;
  4665. if (lastx != NULLEXTNUM && lastx) {
  4666. ep = xfs_iext_get_ext(ifp, lastx - 1);
  4667. xfs_bmbt_get_all(ep, &prev);
  4668. }
  4669. } else if (wasdelay) {
  4670. alen = (xfs_extlen_t)
  4671. XFS_FILBLKS_MIN(len,
  4672. (got.br_startoff +
  4673. got.br_blockcount) - bno);
  4674. aoff = bno;
  4675. } else {
  4676. alen = (xfs_extlen_t)
  4677. XFS_FILBLKS_MIN(len, MAXEXTLEN);
  4678. if (!eof)
  4679. alen = (xfs_extlen_t)
  4680. XFS_FILBLKS_MIN(alen,
  4681. got.br_startoff - bno);
  4682. aoff = bno;
  4683. }
  4684. minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
  4685. if (flags & XFS_BMAPI_DELAY) {
  4686. xfs_extlen_t extsz;
  4687. /* Figure out the extent size, adjust alen */
  4688. if (rt) {
  4689. if (!(extsz = ip->i_d.di_extsize))
  4690. extsz = mp->m_sb.sb_rextsize;
  4691. } else {
  4692. extsz = ip->i_d.di_extsize;
  4693. }
  4694. if (extsz) {
  4695. error = xfs_bmap_extsize_align(mp,
  4696. &got, &prev, extsz,
  4697. rt, eof,
  4698. flags&XFS_BMAPI_DELAY,
  4699. flags&XFS_BMAPI_CONVERT,
  4700. &aoff, &alen);
  4701. ASSERT(!error);
  4702. }
  4703. if (rt)
  4704. extsz = alen / mp->m_sb.sb_rextsize;
  4705. /*
  4706. * Make a transaction-less quota reservation for
  4707. * delayed allocation blocks. This number gets
  4708. * adjusted later. We return if we haven't
  4709. * allocated blocks already inside this loop.
  4710. */
  4711. if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
  4712. mp, NULL, ip, (long)alen, 0,
  4713. rt ? XFS_QMOPT_RES_RTBLKS :
  4714. XFS_QMOPT_RES_REGBLKS))) {
  4715. if (n == 0) {
  4716. *nmap = 0;
  4717. ASSERT(cur == NULL);
  4718. return error;
  4719. }
  4720. break;
  4721. }
  4722. /*
  4723. * Split changing sb for alen and indlen since
  4724. * they could be coming from different places.
  4725. */
  4726. indlen = (xfs_extlen_t)
  4727. xfs_bmap_worst_indlen(ip, alen);
  4728. ASSERT(indlen > 0);
  4729. if (rt) {
  4730. error = xfs_mod_incore_sb(mp,
  4731. XFS_SBS_FREXTENTS,
  4732. -((int64_t)extsz), (flags &
  4733. XFS_BMAPI_RSVBLOCKS));
  4734. } else {
  4735. error = xfs_mod_incore_sb(mp,
  4736. XFS_SBS_FDBLOCKS,
  4737. -((int64_t)alen), (flags &
  4738. XFS_BMAPI_RSVBLOCKS));
  4739. }
  4740. if (!error) {
  4741. error = xfs_mod_incore_sb(mp,
  4742. XFS_SBS_FDBLOCKS,
  4743. -((int64_t)indlen), (flags &
  4744. XFS_BMAPI_RSVBLOCKS));
  4745. if (error && rt)
  4746. xfs_mod_incore_sb(mp,
  4747. XFS_SBS_FREXTENTS,
  4748. (int64_t)extsz, (flags &
  4749. XFS_BMAPI_RSVBLOCKS));
  4750. else if (error)
  4751. xfs_mod_incore_sb(mp,
  4752. XFS_SBS_FDBLOCKS,
  4753. (int64_t)alen, (flags &
  4754. XFS_BMAPI_RSVBLOCKS));
  4755. }
  4756. if (error) {
  4757. if (XFS_IS_QUOTA_ON(mp))
  4758. /* unreserve the blocks now */
  4759. (void)
  4760. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
  4761. mp, NULL, ip,
  4762. (long)alen, 0, rt ?
  4763. XFS_QMOPT_RES_RTBLKS :
  4764. XFS_QMOPT_RES_REGBLKS);
  4765. break;
  4766. }
  4767. ip->i_delayed_blks += alen;
  4768. abno = NULLSTARTBLOCK(indlen);
  4769. } else {
  4770. /*
  4771. * If first time, allocate and fill in
  4772. * once-only bma fields.
  4773. */
  4774. if (bma.ip == NULL) {
  4775. bma.tp = tp;
  4776. bma.ip = ip;
  4777. bma.prevp = &prev;
  4778. bma.gotp = &got;
  4779. bma.total = total;
  4780. bma.userdata = 0;
  4781. }
  4782. /* Indicate if this is the first user data
  4783. * in the file, or just any user data.
  4784. */
  4785. if (!(flags & XFS_BMAPI_METADATA)) {
  4786. bma.userdata = (aoff == 0) ?
  4787. XFS_ALLOC_INITIAL_USER_DATA :
  4788. XFS_ALLOC_USERDATA;
  4789. }
  4790. /*
  4791. * Fill in changeable bma fields.
  4792. */
  4793. bma.eof = eof;
  4794. bma.firstblock = *firstblock;
  4795. bma.alen = alen;
  4796. bma.off = aoff;
  4797. bma.conv = !!(flags & XFS_BMAPI_CONVERT);
  4798. bma.wasdel = wasdelay;
  4799. bma.minlen = minlen;
  4800. bma.low = flist->xbf_low;
  4801. bma.minleft = minleft;
  4802. /*
  4803. * Only want to do the alignment at the
  4804. * eof if it is userdata and allocation length
  4805. * is larger than a stripe unit.
  4806. */
  4807. if (mp->m_dalign && alen >= mp->m_dalign &&
  4808. (!(flags & XFS_BMAPI_METADATA)) &&
  4809. (whichfork == XFS_DATA_FORK)) {
  4810. if ((error = xfs_bmap_isaeof(ip, aoff,
  4811. whichfork, &bma.aeof)))
  4812. goto error0;
  4813. } else
  4814. bma.aeof = 0;
  4815. /*
  4816. * Call allocator.
  4817. */
  4818. if ((error = xfs_bmap_alloc(&bma)))
  4819. goto error0;
  4820. /*
  4821. * Copy out result fields.
  4822. */
  4823. abno = bma.rval;
  4824. if ((flist->xbf_low = bma.low))
  4825. minleft = 0;
  4826. alen = bma.alen;
  4827. aoff = bma.off;
  4828. ASSERT(*firstblock == NULLFSBLOCK ||
  4829. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4830. XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
  4831. (flist->xbf_low &&
  4832. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4833. XFS_FSB_TO_AGNO(mp, bma.firstblock)));
  4834. *firstblock = bma.firstblock;
  4835. if (cur)
  4836. cur->bc_private.b.firstblock =
  4837. *firstblock;
  4838. if (abno == NULLFSBLOCK)
  4839. break;
  4840. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  4841. cur = xfs_btree_init_cursor(mp,
  4842. tp, NULL, 0, XFS_BTNUM_BMAP,
  4843. ip, whichfork);
  4844. cur->bc_private.b.firstblock =
  4845. *firstblock;
  4846. cur->bc_private.b.flist = flist;
  4847. }
  4848. /*
  4849. * Bump the number of extents we've allocated
  4850. * in this call.
  4851. */
  4852. nallocs++;
  4853. }
  4854. if (cur)
  4855. cur->bc_private.b.flags =
  4856. wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
  4857. got.br_startoff = aoff;
  4858. got.br_startblock = abno;
  4859. got.br_blockcount = alen;
  4860. got.br_state = XFS_EXT_NORM; /* assume normal */
  4861. /*
  4862. * Determine state of extent, and the filesystem.
  4863. * A wasdelay extent has been initialized, so
  4864. * shouldn't be flagged as unwritten.
  4865. */
  4866. if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  4867. if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
  4868. got.br_state = XFS_EXT_UNWRITTEN;
  4869. }
  4870. error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
  4871. firstblock, flist, &tmp_logflags, delta,
  4872. whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
  4873. logflags |= tmp_logflags;
  4874. if (error)
  4875. goto error0;
  4876. lastx = ifp->if_lastex;
  4877. ep = xfs_iext_get_ext(ifp, lastx);
  4878. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4879. xfs_bmbt_get_all(ep, &got);
  4880. ASSERT(got.br_startoff <= aoff);
  4881. ASSERT(got.br_startoff + got.br_blockcount >=
  4882. aoff + alen);
  4883. #ifdef DEBUG
  4884. if (flags & XFS_BMAPI_DELAY) {
  4885. ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
  4886. ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
  4887. }
  4888. ASSERT(got.br_state == XFS_EXT_NORM ||
  4889. got.br_state == XFS_EXT_UNWRITTEN);
  4890. #endif
  4891. /*
  4892. * Fall down into the found allocated space case.
  4893. */
  4894. } else if (inhole) {
  4895. /*
  4896. * Reading in a hole.
  4897. */
  4898. mval->br_startoff = bno;
  4899. mval->br_startblock = HOLESTARTBLOCK;
  4900. mval->br_blockcount =
  4901. XFS_FILBLKS_MIN(len, got.br_startoff - bno);
  4902. mval->br_state = XFS_EXT_NORM;
  4903. bno += mval->br_blockcount;
  4904. len -= mval->br_blockcount;
  4905. mval++;
  4906. n++;
  4907. continue;
  4908. }
  4909. /*
  4910. * Then deal with the allocated space we found.
  4911. */
  4912. ASSERT(ep != NULL);
  4913. if (!(flags & XFS_BMAPI_ENTIRE) &&
  4914. (got.br_startoff + got.br_blockcount > obno)) {
  4915. if (obno > bno)
  4916. bno = obno;
  4917. ASSERT((bno >= obno) || (n == 0));
  4918. ASSERT(bno < end);
  4919. mval->br_startoff = bno;
  4920. if (ISNULLSTARTBLOCK(got.br_startblock)) {
  4921. ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
  4922. mval->br_startblock = DELAYSTARTBLOCK;
  4923. } else
  4924. mval->br_startblock =
  4925. got.br_startblock +
  4926. (bno - got.br_startoff);
  4927. /*
  4928. * Return the minimum of what we got and what we
  4929. * asked for for the length. We can use the len
  4930. * variable here because it is modified below
  4931. * and we could have been there before coming
  4932. * here if the first part of the allocation
  4933. * didn't overlap what was asked for.
  4934. */
  4935. mval->br_blockcount =
  4936. XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
  4937. (bno - got.br_startoff));
  4938. mval->br_state = got.br_state;
  4939. ASSERT(mval->br_blockcount <= len);
  4940. } else {
  4941. *mval = got;
  4942. if (ISNULLSTARTBLOCK(mval->br_startblock)) {
  4943. ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
  4944. mval->br_startblock = DELAYSTARTBLOCK;
  4945. }
  4946. }
  4947. /*
  4948. * Check if writing previously allocated but
  4949. * unwritten extents.
  4950. */
  4951. if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
  4952. ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
  4953. /*
  4954. * Modify (by adding) the state flag, if writing.
  4955. */
  4956. ASSERT(mval->br_blockcount <= len);
  4957. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  4958. cur = xfs_btree_init_cursor(mp,
  4959. tp, NULL, 0, XFS_BTNUM_BMAP,
  4960. ip, whichfork);
  4961. cur->bc_private.b.firstblock =
  4962. *firstblock;
  4963. cur->bc_private.b.flist = flist;
  4964. }
  4965. mval->br_state = XFS_EXT_NORM;
  4966. error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
  4967. firstblock, flist, &tmp_logflags, delta,
  4968. whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
  4969. logflags |= tmp_logflags;
  4970. if (error)
  4971. goto error0;
  4972. lastx = ifp->if_lastex;
  4973. ep = xfs_iext_get_ext(ifp, lastx);
  4974. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4975. xfs_bmbt_get_all(ep, &got);
  4976. /*
  4977. * We may have combined previously unwritten
  4978. * space with written space, so generate
  4979. * another request.
  4980. */
  4981. if (mval->br_blockcount < len)
  4982. continue;
  4983. }
  4984. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  4985. ((mval->br_startoff + mval->br_blockcount) <= end));
  4986. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  4987. (mval->br_blockcount <= len) ||
  4988. (mval->br_startoff < obno));
  4989. bno = mval->br_startoff + mval->br_blockcount;
  4990. len = end - bno;
  4991. if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
  4992. ASSERT(mval->br_startblock == mval[-1].br_startblock);
  4993. ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
  4994. ASSERT(mval->br_state == mval[-1].br_state);
  4995. mval[-1].br_blockcount = mval->br_blockcount;
  4996. mval[-1].br_state = mval->br_state;
  4997. } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
  4998. mval[-1].br_startblock != DELAYSTARTBLOCK &&
  4999. mval[-1].br_startblock != HOLESTARTBLOCK &&
  5000. mval->br_startblock ==
  5001. mval[-1].br_startblock + mval[-1].br_blockcount &&
  5002. ((flags & XFS_BMAPI_IGSTATE) ||
  5003. mval[-1].br_state == mval->br_state)) {
  5004. ASSERT(mval->br_startoff ==
  5005. mval[-1].br_startoff + mval[-1].br_blockcount);
  5006. mval[-1].br_blockcount += mval->br_blockcount;
  5007. } else if (n > 0 &&
  5008. mval->br_startblock == DELAYSTARTBLOCK &&
  5009. mval[-1].br_startblock == DELAYSTARTBLOCK &&
  5010. mval->br_startoff ==
  5011. mval[-1].br_startoff + mval[-1].br_blockcount) {
  5012. mval[-1].br_blockcount += mval->br_blockcount;
  5013. mval[-1].br_state = mval->br_state;
  5014. } else if (!((n == 0) &&
  5015. ((mval->br_startoff + mval->br_blockcount) <=
  5016. obno))) {
  5017. mval++;
  5018. n++;
  5019. }
  5020. /*
  5021. * If we're done, stop now. Stop when we've allocated
  5022. * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
  5023. * the transaction may get too big.
  5024. */
  5025. if (bno >= end || n >= *nmap || nallocs >= *nmap)
  5026. break;
  5027. /*
  5028. * Else go on to the next record.
  5029. */
  5030. ep = xfs_iext_get_ext(ifp, ++lastx);
  5031. if (lastx >= nextents) {
  5032. eof = 1;
  5033. prev = got;
  5034. } else
  5035. xfs_bmbt_get_all(ep, &got);
  5036. }
  5037. ifp->if_lastex = lastx;
  5038. *nmap = n;
  5039. /*
  5040. * Transform from btree to extents, give it cur.
  5041. */
  5042. if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  5043. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  5044. ASSERT(wr && cur);
  5045. error = xfs_bmap_btree_to_extents(tp, ip, cur,
  5046. &tmp_logflags, whichfork);
  5047. logflags |= tmp_logflags;
  5048. if (error)
  5049. goto error0;
  5050. }
  5051. ASSERT(ifp->if_ext_max ==
  5052. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5053. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
  5054. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
  5055. error = 0;
  5056. if (delta && delta->xed_startoff != NULLFILEOFF) {
  5057. /* A change was actually made.
  5058. * Note that delta->xed_blockount is an offset at this
  5059. * point and needs to be converted to a block count.
  5060. */
  5061. ASSERT(delta->xed_blockcount > delta->xed_startoff);
  5062. delta->xed_blockcount -= delta->xed_startoff;
  5063. }
  5064. error0:
  5065. /*
  5066. * Log everything. Do this after conversion, there's no point in
  5067. * logging the extent records if we've converted to btree format.
  5068. */
  5069. if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
  5070. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5071. logflags &= ~XFS_ILOG_FEXT(whichfork);
  5072. else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
  5073. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5074. logflags &= ~XFS_ILOG_FBROOT(whichfork);
  5075. /*
  5076. * Log whatever the flags say, even if error. Otherwise we might miss
  5077. * detecting a case where the data is changed, there's an error,
  5078. * and it's not logged so we don't shutdown when we should.
  5079. */
  5080. if (logflags) {
  5081. ASSERT(tp && wr);
  5082. xfs_trans_log_inode(tp, ip, logflags);
  5083. }
  5084. if (cur) {
  5085. if (!error) {
  5086. ASSERT(*firstblock == NULLFSBLOCK ||
  5087. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  5088. XFS_FSB_TO_AGNO(mp,
  5089. cur->bc_private.b.firstblock) ||
  5090. (flist->xbf_low &&
  5091. XFS_FSB_TO_AGNO(mp, *firstblock) <
  5092. XFS_FSB_TO_AGNO(mp,
  5093. cur->bc_private.b.firstblock)));
  5094. *firstblock = cur->bc_private.b.firstblock;
  5095. }
  5096. xfs_btree_del_cursor(cur,
  5097. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5098. }
  5099. if (!error)
  5100. xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
  5101. orig_nmap, *nmap);
  5102. return error;
  5103. }
  5104. /*
  5105. * Map file blocks to filesystem blocks, simple version.
  5106. * One block (extent) only, read-only.
  5107. * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
  5108. * For the other flag values, the effect is as if XFS_BMAPI_METADATA
  5109. * was set and all the others were clear.
  5110. */
  5111. int /* error */
  5112. xfs_bmapi_single(
  5113. xfs_trans_t *tp, /* transaction pointer */
  5114. xfs_inode_t *ip, /* incore inode */
  5115. int whichfork, /* data or attr fork */
  5116. xfs_fsblock_t *fsb, /* output: mapped block */
  5117. xfs_fileoff_t bno) /* starting file offs. mapped */
  5118. {
  5119. int eof; /* we've hit the end of extents */
  5120. int error; /* error return */
  5121. xfs_bmbt_irec_t got; /* current file extent record */
  5122. xfs_ifork_t *ifp; /* inode fork pointer */
  5123. xfs_extnum_t lastx; /* last useful extent number */
  5124. xfs_bmbt_irec_t prev; /* previous file extent record */
  5125. ifp = XFS_IFORK_PTR(ip, whichfork);
  5126. if (unlikely(
  5127. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  5128. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
  5129. XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
  5130. ip->i_mount);
  5131. return XFS_ERROR(EFSCORRUPTED);
  5132. }
  5133. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  5134. return XFS_ERROR(EIO);
  5135. XFS_STATS_INC(xs_blk_mapr);
  5136. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5137. (error = xfs_iread_extents(tp, ip, whichfork)))
  5138. return error;
  5139. (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  5140. &prev);
  5141. /*
  5142. * Reading past eof, act as though there's a hole
  5143. * up to end.
  5144. */
  5145. if (eof || got.br_startoff > bno) {
  5146. *fsb = NULLFSBLOCK;
  5147. return 0;
  5148. }
  5149. ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
  5150. ASSERT(bno < got.br_startoff + got.br_blockcount);
  5151. *fsb = got.br_startblock + (bno - got.br_startoff);
  5152. ifp->if_lastex = lastx;
  5153. return 0;
  5154. }
  5155. /*
  5156. * Unmap (remove) blocks from a file.
  5157. * If nexts is nonzero then the number of extents to remove is limited to
  5158. * that value. If not all extents in the block range can be removed then
  5159. * *done is set.
  5160. */
  5161. int /* error */
  5162. xfs_bunmapi(
  5163. xfs_trans_t *tp, /* transaction pointer */
  5164. struct xfs_inode *ip, /* incore inode */
  5165. xfs_fileoff_t bno, /* starting offset to unmap */
  5166. xfs_filblks_t len, /* length to unmap in file */
  5167. int flags, /* misc flags */
  5168. xfs_extnum_t nexts, /* number of extents max */
  5169. xfs_fsblock_t *firstblock, /* first allocated block
  5170. controls a.g. for allocs */
  5171. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  5172. xfs_extdelta_t *delta, /* o: change made to incore
  5173. extents */
  5174. int *done) /* set if not done yet */
  5175. {
  5176. xfs_btree_cur_t *cur; /* bmap btree cursor */
  5177. xfs_bmbt_irec_t del; /* extent being deleted */
  5178. int eof; /* is deleting at eof */
  5179. xfs_bmbt_rec_t *ep; /* extent record pointer */
  5180. int error; /* error return value */
  5181. xfs_extnum_t extno; /* extent number in list */
  5182. xfs_bmbt_irec_t got; /* current extent record */
  5183. xfs_ifork_t *ifp; /* inode fork pointer */
  5184. int isrt; /* freeing in rt area */
  5185. xfs_extnum_t lastx; /* last extent index used */
  5186. int logflags; /* transaction logging flags */
  5187. xfs_extlen_t mod; /* rt extent offset */
  5188. xfs_mount_t *mp; /* mount structure */
  5189. xfs_extnum_t nextents; /* number of file extents */
  5190. xfs_bmbt_irec_t prev; /* previous extent record */
  5191. xfs_fileoff_t start; /* first file offset deleted */
  5192. int tmp_logflags; /* partial logging flags */
  5193. int wasdel; /* was a delayed alloc extent */
  5194. int whichfork; /* data or attribute fork */
  5195. int rsvd; /* OK to allocate reserved blocks */
  5196. xfs_fsblock_t sum;
  5197. xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
  5198. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  5199. XFS_ATTR_FORK : XFS_DATA_FORK;
  5200. ifp = XFS_IFORK_PTR(ip, whichfork);
  5201. if (unlikely(
  5202. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  5203. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  5204. XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
  5205. ip->i_mount);
  5206. return XFS_ERROR(EFSCORRUPTED);
  5207. }
  5208. mp = ip->i_mount;
  5209. if (XFS_FORCED_SHUTDOWN(mp))
  5210. return XFS_ERROR(EIO);
  5211. rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
  5212. ASSERT(len > 0);
  5213. ASSERT(nexts >= 0);
  5214. ASSERT(ifp->if_ext_max ==
  5215. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5216. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5217. (error = xfs_iread_extents(tp, ip, whichfork)))
  5218. return error;
  5219. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5220. if (nextents == 0) {
  5221. *done = 1;
  5222. return 0;
  5223. }
  5224. XFS_STATS_INC(xs_blk_unmap);
  5225. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  5226. start = bno;
  5227. bno = start + len - 1;
  5228. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  5229. &prev);
  5230. if (delta) {
  5231. delta->xed_startoff = NULLFILEOFF;
  5232. delta->xed_blockcount = 0;
  5233. }
  5234. /*
  5235. * Check to see if the given block number is past the end of the
  5236. * file, back up to the last block if so...
  5237. */
  5238. if (eof) {
  5239. ep = xfs_iext_get_ext(ifp, --lastx);
  5240. xfs_bmbt_get_all(ep, &got);
  5241. bno = got.br_startoff + got.br_blockcount - 1;
  5242. }
  5243. logflags = 0;
  5244. if (ifp->if_flags & XFS_IFBROOT) {
  5245. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  5246. cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
  5247. whichfork);
  5248. cur->bc_private.b.firstblock = *firstblock;
  5249. cur->bc_private.b.flist = flist;
  5250. cur->bc_private.b.flags = 0;
  5251. } else
  5252. cur = NULL;
  5253. extno = 0;
  5254. while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
  5255. (nexts == 0 || extno < nexts)) {
  5256. /*
  5257. * Is the found extent after a hole in which bno lives?
  5258. * Just back up to the previous extent, if so.
  5259. */
  5260. if (got.br_startoff > bno) {
  5261. if (--lastx < 0)
  5262. break;
  5263. ep = xfs_iext_get_ext(ifp, lastx);
  5264. xfs_bmbt_get_all(ep, &got);
  5265. }
  5266. /*
  5267. * Is the last block of this extent before the range
  5268. * we're supposed to delete? If so, we're done.
  5269. */
  5270. bno = XFS_FILEOFF_MIN(bno,
  5271. got.br_startoff + got.br_blockcount - 1);
  5272. if (bno < start)
  5273. break;
  5274. /*
  5275. * Then deal with the (possibly delayed) allocated space
  5276. * we found.
  5277. */
  5278. ASSERT(ep != NULL);
  5279. del = got;
  5280. wasdel = ISNULLSTARTBLOCK(del.br_startblock);
  5281. if (got.br_startoff < start) {
  5282. del.br_startoff = start;
  5283. del.br_blockcount -= start - got.br_startoff;
  5284. if (!wasdel)
  5285. del.br_startblock += start - got.br_startoff;
  5286. }
  5287. if (del.br_startoff + del.br_blockcount > bno + 1)
  5288. del.br_blockcount = bno + 1 - del.br_startoff;
  5289. sum = del.br_startblock + del.br_blockcount;
  5290. if (isrt &&
  5291. (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
  5292. /*
  5293. * Realtime extent not lined up at the end.
  5294. * The extent could have been split into written
  5295. * and unwritten pieces, or we could just be
  5296. * unmapping part of it. But we can't really
  5297. * get rid of part of a realtime extent.
  5298. */
  5299. if (del.br_state == XFS_EXT_UNWRITTEN ||
  5300. !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  5301. /*
  5302. * This piece is unwritten, or we're not
  5303. * using unwritten extents. Skip over it.
  5304. */
  5305. ASSERT(bno >= mod);
  5306. bno -= mod > del.br_blockcount ?
  5307. del.br_blockcount : mod;
  5308. if (bno < got.br_startoff) {
  5309. if (--lastx >= 0)
  5310. xfs_bmbt_get_all(xfs_iext_get_ext(
  5311. ifp, lastx), &got);
  5312. }
  5313. continue;
  5314. }
  5315. /*
  5316. * It's written, turn it unwritten.
  5317. * This is better than zeroing it.
  5318. */
  5319. ASSERT(del.br_state == XFS_EXT_NORM);
  5320. ASSERT(xfs_trans_get_block_res(tp) > 0);
  5321. /*
  5322. * If this spans a realtime extent boundary,
  5323. * chop it back to the start of the one we end at.
  5324. */
  5325. if (del.br_blockcount > mod) {
  5326. del.br_startoff += del.br_blockcount - mod;
  5327. del.br_startblock += del.br_blockcount - mod;
  5328. del.br_blockcount = mod;
  5329. }
  5330. del.br_state = XFS_EXT_UNWRITTEN;
  5331. error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
  5332. firstblock, flist, &logflags, delta,
  5333. XFS_DATA_FORK, 0);
  5334. if (error)
  5335. goto error0;
  5336. goto nodelete;
  5337. }
  5338. if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
  5339. /*
  5340. * Realtime extent is lined up at the end but not
  5341. * at the front. We'll get rid of full extents if
  5342. * we can.
  5343. */
  5344. mod = mp->m_sb.sb_rextsize - mod;
  5345. if (del.br_blockcount > mod) {
  5346. del.br_blockcount -= mod;
  5347. del.br_startoff += mod;
  5348. del.br_startblock += mod;
  5349. } else if ((del.br_startoff == start &&
  5350. (del.br_state == XFS_EXT_UNWRITTEN ||
  5351. xfs_trans_get_block_res(tp) == 0)) ||
  5352. !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  5353. /*
  5354. * Can't make it unwritten. There isn't
  5355. * a full extent here so just skip it.
  5356. */
  5357. ASSERT(bno >= del.br_blockcount);
  5358. bno -= del.br_blockcount;
  5359. if (bno < got.br_startoff) {
  5360. if (--lastx >= 0)
  5361. xfs_bmbt_get_all(--ep, &got);
  5362. }
  5363. continue;
  5364. } else if (del.br_state == XFS_EXT_UNWRITTEN) {
  5365. /*
  5366. * This one is already unwritten.
  5367. * It must have a written left neighbor.
  5368. * Unwrite the killed part of that one and
  5369. * try again.
  5370. */
  5371. ASSERT(lastx > 0);
  5372. xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
  5373. lastx - 1), &prev);
  5374. ASSERT(prev.br_state == XFS_EXT_NORM);
  5375. ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
  5376. ASSERT(del.br_startblock ==
  5377. prev.br_startblock + prev.br_blockcount);
  5378. if (prev.br_startoff < start) {
  5379. mod = start - prev.br_startoff;
  5380. prev.br_blockcount -= mod;
  5381. prev.br_startblock += mod;
  5382. prev.br_startoff = start;
  5383. }
  5384. prev.br_state = XFS_EXT_UNWRITTEN;
  5385. error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
  5386. &prev, firstblock, flist, &logflags,
  5387. delta, XFS_DATA_FORK, 0);
  5388. if (error)
  5389. goto error0;
  5390. goto nodelete;
  5391. } else {
  5392. ASSERT(del.br_state == XFS_EXT_NORM);
  5393. del.br_state = XFS_EXT_UNWRITTEN;
  5394. error = xfs_bmap_add_extent(ip, lastx, &cur,
  5395. &del, firstblock, flist, &logflags,
  5396. delta, XFS_DATA_FORK, 0);
  5397. if (error)
  5398. goto error0;
  5399. goto nodelete;
  5400. }
  5401. }
  5402. if (wasdel) {
  5403. ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
  5404. /* Update realtime/data freespace, unreserve quota */
  5405. if (isrt) {
  5406. xfs_filblks_t rtexts;
  5407. rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
  5408. do_div(rtexts, mp->m_sb.sb_rextsize);
  5409. xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
  5410. (int64_t)rtexts, rsvd);
  5411. (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
  5412. NULL, ip, -((long)del.br_blockcount), 0,
  5413. XFS_QMOPT_RES_RTBLKS);
  5414. } else {
  5415. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
  5416. (int64_t)del.br_blockcount, rsvd);
  5417. (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
  5418. NULL, ip, -((long)del.br_blockcount), 0,
  5419. XFS_QMOPT_RES_REGBLKS);
  5420. }
  5421. ip->i_delayed_blks -= del.br_blockcount;
  5422. if (cur)
  5423. cur->bc_private.b.flags |=
  5424. XFS_BTCUR_BPRV_WASDEL;
  5425. } else if (cur)
  5426. cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
  5427. /*
  5428. * If it's the case where the directory code is running
  5429. * with no block reservation, and the deleted block is in
  5430. * the middle of its extent, and the resulting insert
  5431. * of an extent would cause transformation to btree format,
  5432. * then reject it. The calling code will then swap
  5433. * blocks around instead.
  5434. * We have to do this now, rather than waiting for the
  5435. * conversion to btree format, since the transaction
  5436. * will be dirty.
  5437. */
  5438. if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
  5439. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5440. XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
  5441. del.br_startoff > got.br_startoff &&
  5442. del.br_startoff + del.br_blockcount <
  5443. got.br_startoff + got.br_blockcount) {
  5444. error = XFS_ERROR(ENOSPC);
  5445. goto error0;
  5446. }
  5447. error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
  5448. &tmp_logflags, delta, whichfork, rsvd);
  5449. logflags |= tmp_logflags;
  5450. if (error)
  5451. goto error0;
  5452. bno = del.br_startoff - 1;
  5453. nodelete:
  5454. lastx = ifp->if_lastex;
  5455. /*
  5456. * If not done go on to the next (previous) record.
  5457. * Reset ep in case the extents array was re-alloced.
  5458. */
  5459. ep = xfs_iext_get_ext(ifp, lastx);
  5460. if (bno != (xfs_fileoff_t)-1 && bno >= start) {
  5461. if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
  5462. xfs_bmbt_get_startoff(ep) > bno) {
  5463. if (--lastx >= 0)
  5464. ep = xfs_iext_get_ext(ifp, lastx);
  5465. }
  5466. if (lastx >= 0)
  5467. xfs_bmbt_get_all(ep, &got);
  5468. extno++;
  5469. }
  5470. }
  5471. ifp->if_lastex = lastx;
  5472. *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
  5473. ASSERT(ifp->if_ext_max ==
  5474. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5475. /*
  5476. * Convert to a btree if necessary.
  5477. */
  5478. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5479. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
  5480. ASSERT(cur == NULL);
  5481. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
  5482. &cur, 0, &tmp_logflags, whichfork);
  5483. logflags |= tmp_logflags;
  5484. if (error)
  5485. goto error0;
  5486. }
  5487. /*
  5488. * transform from btree to extents, give it cur
  5489. */
  5490. else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  5491. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  5492. ASSERT(cur != NULL);
  5493. error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
  5494. whichfork);
  5495. logflags |= tmp_logflags;
  5496. if (error)
  5497. goto error0;
  5498. }
  5499. /*
  5500. * transform from extents to local?
  5501. */
  5502. ASSERT(ifp->if_ext_max ==
  5503. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5504. error = 0;
  5505. if (delta && delta->xed_startoff != NULLFILEOFF) {
  5506. /* A change was actually made.
  5507. * Note that delta->xed_blockount is an offset at this
  5508. * point and needs to be converted to a block count.
  5509. */
  5510. ASSERT(delta->xed_blockcount > delta->xed_startoff);
  5511. delta->xed_blockcount -= delta->xed_startoff;
  5512. }
  5513. error0:
  5514. /*
  5515. * Log everything. Do this after conversion, there's no point in
  5516. * logging the extent records if we've converted to btree format.
  5517. */
  5518. if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
  5519. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5520. logflags &= ~XFS_ILOG_FEXT(whichfork);
  5521. else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
  5522. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5523. logflags &= ~XFS_ILOG_FBROOT(whichfork);
  5524. /*
  5525. * Log inode even in the error case, if the transaction
  5526. * is dirty we'll need to shut down the filesystem.
  5527. */
  5528. if (logflags)
  5529. xfs_trans_log_inode(tp, ip, logflags);
  5530. if (cur) {
  5531. if (!error) {
  5532. *firstblock = cur->bc_private.b.firstblock;
  5533. cur->bc_private.b.allocated = 0;
  5534. }
  5535. xfs_btree_del_cursor(cur,
  5536. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5537. }
  5538. return error;
  5539. }
  5540. /*
  5541. * Fcntl interface to xfs_bmapi.
  5542. */
  5543. int /* error code */
  5544. xfs_getbmap(
  5545. bhv_desc_t *bdp, /* XFS behavior descriptor*/
  5546. struct getbmap *bmv, /* user bmap structure */
  5547. void __user *ap, /* pointer to user's array */
  5548. int interface) /* interface flags */
  5549. {
  5550. __int64_t bmvend; /* last block requested */
  5551. int error; /* return value */
  5552. __int64_t fixlen; /* length for -1 case */
  5553. int i; /* extent number */
  5554. xfs_inode_t *ip; /* xfs incore inode pointer */
  5555. bhv_vnode_t *vp; /* corresponding vnode */
  5556. int lock; /* lock state */
  5557. xfs_bmbt_irec_t *map; /* buffer for user's data */
  5558. xfs_mount_t *mp; /* file system mount point */
  5559. int nex; /* # of user extents can do */
  5560. int nexleft; /* # of user extents left */
  5561. int subnex; /* # of bmapi's can do */
  5562. int nmap; /* number of map entries */
  5563. struct getbmap out; /* output structure */
  5564. int whichfork; /* data or attr fork */
  5565. int prealloced; /* this is a file with
  5566. * preallocated data space */
  5567. int sh_unwritten; /* true, if unwritten */
  5568. /* extents listed separately */
  5569. int bmapi_flags; /* flags for xfs_bmapi */
  5570. __int32_t oflags; /* getbmapx bmv_oflags field */
  5571. vp = BHV_TO_VNODE(bdp);
  5572. ip = XFS_BHVTOI(bdp);
  5573. mp = ip->i_mount;
  5574. whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
  5575. sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
  5576. /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
  5577. * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
  5578. * bit is set for the file, generate a read event in order
  5579. * that the DMAPI application may do its thing before we return
  5580. * the extents. Usually this means restoring user file data to
  5581. * regions of the file that look like holes.
  5582. *
  5583. * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
  5584. * BMV_IF_NO_DMAPI_READ so that read events are generated.
  5585. * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
  5586. * could misinterpret holes in a DMAPI file as true holes,
  5587. * when in fact they may represent offline user data.
  5588. */
  5589. if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
  5590. && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
  5591. && whichfork == XFS_DATA_FORK) {
  5592. error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
  5593. if (error)
  5594. return XFS_ERROR(error);
  5595. }
  5596. if (whichfork == XFS_ATTR_FORK) {
  5597. if (XFS_IFORK_Q(ip)) {
  5598. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
  5599. ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
  5600. ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  5601. return XFS_ERROR(EINVAL);
  5602. } else if (unlikely(
  5603. ip->i_d.di_aformat != 0 &&
  5604. ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
  5605. XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
  5606. ip->i_mount);
  5607. return XFS_ERROR(EFSCORRUPTED);
  5608. }
  5609. } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
  5610. ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
  5611. ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  5612. return XFS_ERROR(EINVAL);
  5613. if (whichfork == XFS_DATA_FORK) {
  5614. if ((ip->i_d.di_extsize && (ip->i_d.di_flags &
  5615. (XFS_DIFLAG_REALTIME|XFS_DIFLAG_EXTSIZE))) ||
  5616. ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
  5617. prealloced = 1;
  5618. fixlen = XFS_MAXIOFFSET(mp);
  5619. } else {
  5620. prealloced = 0;
  5621. fixlen = ip->i_d.di_size;
  5622. }
  5623. } else {
  5624. prealloced = 0;
  5625. fixlen = 1LL << 32;
  5626. }
  5627. if (bmv->bmv_length == -1) {
  5628. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
  5629. bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
  5630. (__int64_t)0);
  5631. } else if (bmv->bmv_length < 0)
  5632. return XFS_ERROR(EINVAL);
  5633. if (bmv->bmv_length == 0) {
  5634. bmv->bmv_entries = 0;
  5635. return 0;
  5636. }
  5637. nex = bmv->bmv_count - 1;
  5638. if (nex <= 0)
  5639. return XFS_ERROR(EINVAL);
  5640. bmvend = bmv->bmv_offset + bmv->bmv_length;
  5641. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  5642. if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
  5643. /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
  5644. error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
  5645. }
  5646. ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
  5647. lock = xfs_ilock_map_shared(ip);
  5648. /*
  5649. * Don't let nex be bigger than the number of extents
  5650. * we can have assuming alternating holes and real extents.
  5651. */
  5652. if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
  5653. nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
  5654. bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
  5655. ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
  5656. /*
  5657. * Allocate enough space to handle "subnex" maps at a time.
  5658. */
  5659. subnex = 16;
  5660. map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
  5661. bmv->bmv_entries = 0;
  5662. if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
  5663. error = 0;
  5664. goto unlock_and_return;
  5665. }
  5666. nexleft = nex;
  5667. do {
  5668. nmap = (nexleft > subnex) ? subnex : nexleft;
  5669. error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
  5670. XFS_BB_TO_FSB(mp, bmv->bmv_length),
  5671. bmapi_flags, NULL, 0, map, &nmap,
  5672. NULL, NULL);
  5673. if (error)
  5674. goto unlock_and_return;
  5675. ASSERT(nmap <= subnex);
  5676. for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
  5677. nexleft--;
  5678. oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
  5679. BMV_OF_PREALLOC : 0;
  5680. out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
  5681. out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  5682. ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
  5683. if (map[i].br_startblock == HOLESTARTBLOCK &&
  5684. ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
  5685. whichfork == XFS_ATTR_FORK )) {
  5686. /*
  5687. * came to hole at end of file or the end of
  5688. attribute fork
  5689. */
  5690. goto unlock_and_return;
  5691. } else {
  5692. out.bmv_block =
  5693. (map[i].br_startblock == HOLESTARTBLOCK) ?
  5694. -1 :
  5695. XFS_FSB_TO_DB(ip, map[i].br_startblock);
  5696. /* return either getbmap/getbmapx structure. */
  5697. if (interface & BMV_IF_EXTENDED) {
  5698. struct getbmapx outx;
  5699. GETBMAP_CONVERT(out,outx);
  5700. outx.bmv_oflags = oflags;
  5701. outx.bmv_unused1 = outx.bmv_unused2 = 0;
  5702. if (copy_to_user(ap, &outx,
  5703. sizeof(outx))) {
  5704. error = XFS_ERROR(EFAULT);
  5705. goto unlock_and_return;
  5706. }
  5707. } else {
  5708. if (copy_to_user(ap, &out,
  5709. sizeof(out))) {
  5710. error = XFS_ERROR(EFAULT);
  5711. goto unlock_and_return;
  5712. }
  5713. }
  5714. bmv->bmv_offset =
  5715. out.bmv_offset + out.bmv_length;
  5716. bmv->bmv_length = MAX((__int64_t)0,
  5717. (__int64_t)(bmvend - bmv->bmv_offset));
  5718. bmv->bmv_entries++;
  5719. ap = (interface & BMV_IF_EXTENDED) ?
  5720. (void __user *)
  5721. ((struct getbmapx __user *)ap + 1) :
  5722. (void __user *)
  5723. ((struct getbmap __user *)ap + 1);
  5724. }
  5725. }
  5726. } while (nmap && nexleft && bmv->bmv_length);
  5727. unlock_and_return:
  5728. xfs_iunlock_map_shared(ip, lock);
  5729. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  5730. kmem_free(map, subnex * sizeof(*map));
  5731. return error;
  5732. }
  5733. /*
  5734. * Check the last inode extent to determine whether this allocation will result
  5735. * in blocks being allocated at the end of the file. When we allocate new data
  5736. * blocks at the end of the file which do not start at the previous data block,
  5737. * we will try to align the new blocks at stripe unit boundaries.
  5738. */
  5739. STATIC int /* error */
  5740. xfs_bmap_isaeof(
  5741. xfs_inode_t *ip, /* incore inode pointer */
  5742. xfs_fileoff_t off, /* file offset in fsblocks */
  5743. int whichfork, /* data or attribute fork */
  5744. char *aeof) /* return value */
  5745. {
  5746. int error; /* error return value */
  5747. xfs_ifork_t *ifp; /* inode fork pointer */
  5748. xfs_bmbt_rec_t *lastrec; /* extent record pointer */
  5749. xfs_extnum_t nextents; /* number of file extents */
  5750. xfs_bmbt_irec_t s; /* expanded extent record */
  5751. ASSERT(whichfork == XFS_DATA_FORK);
  5752. ifp = XFS_IFORK_PTR(ip, whichfork);
  5753. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5754. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5755. return error;
  5756. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5757. if (nextents == 0) {
  5758. *aeof = 1;
  5759. return 0;
  5760. }
  5761. /*
  5762. * Go to the last extent
  5763. */
  5764. lastrec = xfs_iext_get_ext(ifp, nextents - 1);
  5765. xfs_bmbt_get_all(lastrec, &s);
  5766. /*
  5767. * Check we are allocating in the last extent (for delayed allocations)
  5768. * or past the last extent for non-delayed allocations.
  5769. */
  5770. *aeof = (off >= s.br_startoff &&
  5771. off < s.br_startoff + s.br_blockcount &&
  5772. ISNULLSTARTBLOCK(s.br_startblock)) ||
  5773. off >= s.br_startoff + s.br_blockcount;
  5774. return 0;
  5775. }
  5776. /*
  5777. * Check if the endoff is outside the last extent. If so the caller will grow
  5778. * the allocation to a stripe unit boundary.
  5779. */
  5780. int /* error */
  5781. xfs_bmap_eof(
  5782. xfs_inode_t *ip, /* incore inode pointer */
  5783. xfs_fileoff_t endoff, /* file offset in fsblocks */
  5784. int whichfork, /* data or attribute fork */
  5785. int *eof) /* result value */
  5786. {
  5787. xfs_fsblock_t blockcount; /* extent block count */
  5788. int error; /* error return value */
  5789. xfs_ifork_t *ifp; /* inode fork pointer */
  5790. xfs_bmbt_rec_t *lastrec; /* extent record pointer */
  5791. xfs_extnum_t nextents; /* number of file extents */
  5792. xfs_fileoff_t startoff; /* extent starting file offset */
  5793. ASSERT(whichfork == XFS_DATA_FORK);
  5794. ifp = XFS_IFORK_PTR(ip, whichfork);
  5795. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5796. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5797. return error;
  5798. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5799. if (nextents == 0) {
  5800. *eof = 1;
  5801. return 0;
  5802. }
  5803. /*
  5804. * Go to the last extent
  5805. */
  5806. lastrec = xfs_iext_get_ext(ifp, nextents - 1);
  5807. startoff = xfs_bmbt_get_startoff(lastrec);
  5808. blockcount = xfs_bmbt_get_blockcount(lastrec);
  5809. *eof = endoff >= startoff + blockcount;
  5810. return 0;
  5811. }
  5812. #ifdef DEBUG
  5813. STATIC
  5814. xfs_buf_t *
  5815. xfs_bmap_get_bp(
  5816. xfs_btree_cur_t *cur,
  5817. xfs_fsblock_t bno)
  5818. {
  5819. int i;
  5820. xfs_buf_t *bp;
  5821. if (!cur)
  5822. return(NULL);
  5823. bp = NULL;
  5824. for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
  5825. bp = cur->bc_bufs[i];
  5826. if (!bp) break;
  5827. if (XFS_BUF_ADDR(bp) == bno)
  5828. break; /* Found it */
  5829. }
  5830. if (i == XFS_BTREE_MAXLEVELS)
  5831. bp = NULL;
  5832. if (!bp) { /* Chase down all the log items to see if the bp is there */
  5833. xfs_log_item_chunk_t *licp;
  5834. xfs_trans_t *tp;
  5835. tp = cur->bc_tp;
  5836. licp = &tp->t_items;
  5837. while (!bp && licp != NULL) {
  5838. if (XFS_LIC_ARE_ALL_FREE(licp)) {
  5839. licp = licp->lic_next;
  5840. continue;
  5841. }
  5842. for (i = 0; i < licp->lic_unused; i++) {
  5843. xfs_log_item_desc_t *lidp;
  5844. xfs_log_item_t *lip;
  5845. xfs_buf_log_item_t *bip;
  5846. xfs_buf_t *lbp;
  5847. if (XFS_LIC_ISFREE(licp, i)) {
  5848. continue;
  5849. }
  5850. lidp = XFS_LIC_SLOT(licp, i);
  5851. lip = lidp->lid_item;
  5852. if (lip->li_type != XFS_LI_BUF)
  5853. continue;
  5854. bip = (xfs_buf_log_item_t *)lip;
  5855. lbp = bip->bli_buf;
  5856. if (XFS_BUF_ADDR(lbp) == bno) {
  5857. bp = lbp;
  5858. break; /* Found it */
  5859. }
  5860. }
  5861. licp = licp->lic_next;
  5862. }
  5863. }
  5864. return(bp);
  5865. }
  5866. void
  5867. xfs_check_block(
  5868. xfs_bmbt_block_t *block,
  5869. xfs_mount_t *mp,
  5870. int root,
  5871. short sz)
  5872. {
  5873. int i, j, dmxr;
  5874. __be64 *pp, *thispa; /* pointer to block address */
  5875. xfs_bmbt_key_t *prevp, *keyp;
  5876. ASSERT(be16_to_cpu(block->bb_level) > 0);
  5877. prevp = NULL;
  5878. for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
  5879. dmxr = mp->m_bmap_dmxr[0];
  5880. if (root) {
  5881. keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
  5882. } else {
  5883. keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
  5884. }
  5885. if (prevp) {
  5886. xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
  5887. }
  5888. prevp = keyp;
  5889. /*
  5890. * Compare the block numbers to see if there are dups.
  5891. */
  5892. if (root) {
  5893. pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
  5894. } else {
  5895. pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
  5896. }
  5897. for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
  5898. if (root) {
  5899. thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
  5900. } else {
  5901. thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
  5902. dmxr);
  5903. }
  5904. if (*thispa == *pp) {
  5905. cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
  5906. __FUNCTION__, j, i,
  5907. (unsigned long long)be64_to_cpu(*thispa));
  5908. panic("%s: ptrs are equal in node\n",
  5909. __FUNCTION__);
  5910. }
  5911. }
  5912. }
  5913. }
  5914. /*
  5915. * Check that the extents for the inode ip are in the right order in all
  5916. * btree leaves.
  5917. */
  5918. STATIC void
  5919. xfs_bmap_check_leaf_extents(
  5920. xfs_btree_cur_t *cur, /* btree cursor or null */
  5921. xfs_inode_t *ip, /* incore inode pointer */
  5922. int whichfork) /* data or attr fork */
  5923. {
  5924. xfs_bmbt_block_t *block; /* current btree block */
  5925. xfs_fsblock_t bno; /* block # of "block" */
  5926. xfs_buf_t *bp; /* buffer for "block" */
  5927. int error; /* error return value */
  5928. xfs_extnum_t i=0, j; /* index into the extents list */
  5929. xfs_ifork_t *ifp; /* fork structure */
  5930. int level; /* btree level, for checking */
  5931. xfs_mount_t *mp; /* file system mount structure */
  5932. __be64 *pp; /* pointer to block address */
  5933. xfs_bmbt_rec_t *ep; /* pointer to current extent */
  5934. xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
  5935. xfs_bmbt_rec_t *nextp; /* pointer to next extent */
  5936. int bp_release = 0;
  5937. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
  5938. return;
  5939. }
  5940. bno = NULLFSBLOCK;
  5941. mp = ip->i_mount;
  5942. ifp = XFS_IFORK_PTR(ip, whichfork);
  5943. block = ifp->if_broot;
  5944. /*
  5945. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  5946. */
  5947. level = be16_to_cpu(block->bb_level);
  5948. ASSERT(level > 0);
  5949. xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
  5950. pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
  5951. bno = be64_to_cpu(*pp);
  5952. ASSERT(bno != NULLDFSBNO);
  5953. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  5954. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  5955. /*
  5956. * Go down the tree until leaf level is reached, following the first
  5957. * pointer (leftmost) at each level.
  5958. */
  5959. while (level-- > 0) {
  5960. /* See if buf is in cur first */
  5961. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  5962. if (bp) {
  5963. bp_release = 0;
  5964. } else {
  5965. bp_release = 1;
  5966. }
  5967. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  5968. XFS_BMAP_BTREE_REF)))
  5969. goto error_norelse;
  5970. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  5971. XFS_WANT_CORRUPTED_GOTO(
  5972. XFS_BMAP_SANITY_CHECK(mp, block, level),
  5973. error0);
  5974. if (level == 0)
  5975. break;
  5976. /*
  5977. * Check this block for basic sanity (increasing keys and
  5978. * no duplicate blocks).
  5979. */
  5980. xfs_check_block(block, mp, 0, 0);
  5981. pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
  5982. bno = be64_to_cpu(*pp);
  5983. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  5984. if (bp_release) {
  5985. bp_release = 0;
  5986. xfs_trans_brelse(NULL, bp);
  5987. }
  5988. }
  5989. /*
  5990. * Here with bp and block set to the leftmost leaf node in the tree.
  5991. */
  5992. i = 0;
  5993. /*
  5994. * Loop over all leaf nodes checking that all extents are in the right order.
  5995. */
  5996. lastp = NULL;
  5997. for (;;) {
  5998. xfs_fsblock_t nextbno;
  5999. xfs_extnum_t num_recs;
  6000. num_recs = be16_to_cpu(block->bb_numrecs);
  6001. /*
  6002. * Read-ahead the next leaf block, if any.
  6003. */
  6004. nextbno = be64_to_cpu(block->bb_rightsib);
  6005. /*
  6006. * Check all the extents to make sure they are OK.
  6007. * If we had a previous block, the last entry should
  6008. * conform with the first entry in this one.
  6009. */
  6010. ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
  6011. for (j = 1; j < num_recs; j++) {
  6012. nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
  6013. if (lastp) {
  6014. xfs_btree_check_rec(XFS_BTNUM_BMAP,
  6015. (void *)lastp, (void *)ep);
  6016. }
  6017. xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
  6018. (void *)(nextp));
  6019. lastp = ep;
  6020. ep = nextp;
  6021. }
  6022. i += num_recs;
  6023. if (bp_release) {
  6024. bp_release = 0;
  6025. xfs_trans_brelse(NULL, bp);
  6026. }
  6027. bno = nextbno;
  6028. /*
  6029. * If we've reached the end, stop.
  6030. */
  6031. if (bno == NULLFSBLOCK)
  6032. break;
  6033. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  6034. if (bp) {
  6035. bp_release = 0;
  6036. } else {
  6037. bp_release = 1;
  6038. }
  6039. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  6040. XFS_BMAP_BTREE_REF)))
  6041. goto error_norelse;
  6042. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  6043. }
  6044. if (bp_release) {
  6045. bp_release = 0;
  6046. xfs_trans_brelse(NULL, bp);
  6047. }
  6048. return;
  6049. error0:
  6050. cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
  6051. if (bp_release)
  6052. xfs_trans_brelse(NULL, bp);
  6053. error_norelse:
  6054. cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
  6055. __FUNCTION__, i);
  6056. panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
  6057. return;
  6058. }
  6059. #endif
  6060. /*
  6061. * Count fsblocks of the given fork.
  6062. */
  6063. int /* error */
  6064. xfs_bmap_count_blocks(
  6065. xfs_trans_t *tp, /* transaction pointer */
  6066. xfs_inode_t *ip, /* incore inode */
  6067. int whichfork, /* data or attr fork */
  6068. int *count) /* out: count of blocks */
  6069. {
  6070. xfs_bmbt_block_t *block; /* current btree block */
  6071. xfs_fsblock_t bno; /* block # of "block" */
  6072. xfs_ifork_t *ifp; /* fork structure */
  6073. int level; /* btree level, for checking */
  6074. xfs_mount_t *mp; /* file system mount structure */
  6075. __be64 *pp; /* pointer to block address */
  6076. bno = NULLFSBLOCK;
  6077. mp = ip->i_mount;
  6078. ifp = XFS_IFORK_PTR(ip, whichfork);
  6079. if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
  6080. if (unlikely(xfs_bmap_count_leaves(ifp, 0,
  6081. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
  6082. count) < 0)) {
  6083. XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
  6084. XFS_ERRLEVEL_LOW, mp);
  6085. return XFS_ERROR(EFSCORRUPTED);
  6086. }
  6087. return 0;
  6088. }
  6089. /*
  6090. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  6091. */
  6092. block = ifp->if_broot;
  6093. level = be16_to_cpu(block->bb_level);
  6094. ASSERT(level > 0);
  6095. pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
  6096. bno = be64_to_cpu(*pp);
  6097. ASSERT(bno != NULLDFSBNO);
  6098. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  6099. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  6100. if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
  6101. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
  6102. mp);
  6103. return XFS_ERROR(EFSCORRUPTED);
  6104. }
  6105. return 0;
  6106. }
  6107. /*
  6108. * Recursively walks each level of a btree
  6109. * to count total fsblocks is use.
  6110. */
  6111. int /* error */
  6112. xfs_bmap_count_tree(
  6113. xfs_mount_t *mp, /* file system mount point */
  6114. xfs_trans_t *tp, /* transaction pointer */
  6115. xfs_ifork_t *ifp, /* inode fork pointer */
  6116. xfs_fsblock_t blockno, /* file system block number */
  6117. int levelin, /* level in btree */
  6118. int *count) /* Count of blocks */
  6119. {
  6120. int error;
  6121. xfs_buf_t *bp, *nbp;
  6122. int level = levelin;
  6123. __be64 *pp;
  6124. xfs_fsblock_t bno = blockno;
  6125. xfs_fsblock_t nextbno;
  6126. xfs_bmbt_block_t *block, *nextblock;
  6127. int numrecs;
  6128. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
  6129. return error;
  6130. *count += 1;
  6131. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  6132. if (--level) {
  6133. /* Not at node above leafs, count this level of nodes */
  6134. nextbno = be64_to_cpu(block->bb_rightsib);
  6135. while (nextbno != NULLFSBLOCK) {
  6136. if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
  6137. 0, &nbp, XFS_BMAP_BTREE_REF)))
  6138. return error;
  6139. *count += 1;
  6140. nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
  6141. nextbno = be64_to_cpu(nextblock->bb_rightsib);
  6142. xfs_trans_brelse(tp, nbp);
  6143. }
  6144. /* Dive to the next level */
  6145. pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
  6146. bno = be64_to_cpu(*pp);
  6147. if (unlikely((error =
  6148. xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
  6149. xfs_trans_brelse(tp, bp);
  6150. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  6151. XFS_ERRLEVEL_LOW, mp);
  6152. return XFS_ERROR(EFSCORRUPTED);
  6153. }
  6154. xfs_trans_brelse(tp, bp);
  6155. } else {
  6156. /* count all level 1 nodes and their leaves */
  6157. for (;;) {
  6158. nextbno = be64_to_cpu(block->bb_rightsib);
  6159. numrecs = be16_to_cpu(block->bb_numrecs);
  6160. if (unlikely(xfs_bmap_disk_count_leaves(0,
  6161. block, numrecs, count) < 0)) {
  6162. xfs_trans_brelse(tp, bp);
  6163. XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
  6164. XFS_ERRLEVEL_LOW, mp);
  6165. return XFS_ERROR(EFSCORRUPTED);
  6166. }
  6167. xfs_trans_brelse(tp, bp);
  6168. if (nextbno == NULLFSBLOCK)
  6169. break;
  6170. bno = nextbno;
  6171. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  6172. XFS_BMAP_BTREE_REF)))
  6173. return error;
  6174. *count += 1;
  6175. block = XFS_BUF_TO_BMBT_BLOCK(bp);
  6176. }
  6177. }
  6178. return 0;
  6179. }
  6180. /*
  6181. * Count leaf blocks given a range of extent records.
  6182. */
  6183. int
  6184. xfs_bmap_count_leaves(
  6185. xfs_ifork_t *ifp,
  6186. xfs_extnum_t idx,
  6187. int numrecs,
  6188. int *count)
  6189. {
  6190. int b;
  6191. xfs_bmbt_rec_t *frp;
  6192. for (b = 0; b < numrecs; b++) {
  6193. frp = xfs_iext_get_ext(ifp, idx + b);
  6194. *count += xfs_bmbt_get_blockcount(frp);
  6195. }
  6196. return 0;
  6197. }
  6198. /*
  6199. * Count leaf blocks given a range of extent records originally
  6200. * in btree format.
  6201. */
  6202. int
  6203. xfs_bmap_disk_count_leaves(
  6204. xfs_extnum_t idx,
  6205. xfs_bmbt_block_t *block,
  6206. int numrecs,
  6207. int *count)
  6208. {
  6209. int b;
  6210. xfs_bmbt_rec_t *frp;
  6211. for (b = 1; b <= numrecs; b++) {
  6212. frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
  6213. *count += xfs_bmbt_disk_get_blockcount(frp);
  6214. }
  6215. return 0;
  6216. }