xfs_bmap.c 200 KB

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