xfs_bmap.c 195 KB

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