xfs_bmap.c 197 KB

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