xfs_bmap.c 196 KB

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