xfs_bmap.c 184 KB

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