xfs_bmap.c 197 KB

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