xfs_bmap.c 197 KB

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