xfs_bmap.c 184 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533
  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_iext_get_extent(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 = false;
  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 = true;
  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_ifork_t *ifp; /* inode fork pointer */
  1408. int rval; /* return value */
  1409. xfs_bmbt_irec_t s; /* internal version of extent */
  1410. #ifndef DEBUG
  1411. if (whichfork == XFS_DATA_FORK)
  1412. return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
  1413. #endif /* !DEBUG */
  1414. if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
  1415. return 0;
  1416. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  1417. return 0;
  1418. ifp = XFS_IFORK_PTR(ip, whichfork);
  1419. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  1420. xfs_iext_get_extent(ifp, 0, &s);
  1421. rval = s.br_startoff == 0 && s.br_blockcount == 1;
  1422. if (rval && whichfork == XFS_DATA_FORK)
  1423. ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
  1424. return rval;
  1425. }
  1426. /*
  1427. * Extent tree manipulation functions used during allocation.
  1428. */
  1429. /*
  1430. * Convert a delayed allocation to a real allocation.
  1431. */
  1432. STATIC int /* error */
  1433. xfs_bmap_add_extent_delay_real(
  1434. struct xfs_bmalloca *bma,
  1435. int whichfork)
  1436. {
  1437. struct xfs_bmbt_irec *new = &bma->got;
  1438. int diff; /* temp value */
  1439. xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
  1440. int error; /* error return value */
  1441. int i; /* temp state */
  1442. xfs_ifork_t *ifp; /* inode fork pointer */
  1443. xfs_fileoff_t new_endoff; /* end offset of new entry */
  1444. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  1445. /* left is 0, right is 1, prev is 2 */
  1446. int rval=0; /* return value (logging flags) */
  1447. int state = 0;/* state bits, accessed thru macros */
  1448. xfs_filblks_t da_new; /* new count del alloc blocks used */
  1449. xfs_filblks_t da_old; /* old count del alloc blocks used */
  1450. xfs_filblks_t temp=0; /* value for da_new calculations */
  1451. xfs_filblks_t temp2=0;/* value for da_new calculations */
  1452. int tmp_rval; /* partial logging flags */
  1453. struct xfs_mount *mp;
  1454. xfs_extnum_t *nextents;
  1455. mp = bma->ip->i_mount;
  1456. ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  1457. ASSERT(whichfork != XFS_ATTR_FORK);
  1458. nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
  1459. &bma->ip->i_d.di_nextents);
  1460. ASSERT(bma->idx >= 0);
  1461. ASSERT(bma->idx <= xfs_iext_count(ifp));
  1462. ASSERT(!isnullstartblock(new->br_startblock));
  1463. ASSERT(!bma->cur ||
  1464. (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
  1465. XFS_STATS_INC(mp, xs_add_exlist);
  1466. #define LEFT r[0]
  1467. #define RIGHT r[1]
  1468. #define PREV r[2]
  1469. if (whichfork == XFS_COW_FORK)
  1470. state |= BMAP_COWFORK;
  1471. /*
  1472. * Set up a bunch of variables to make the tests simpler.
  1473. */
  1474. ep = xfs_iext_get_ext(ifp, bma->idx);
  1475. xfs_bmbt_get_all(ep, &PREV);
  1476. new_endoff = new->br_startoff + new->br_blockcount;
  1477. ASSERT(PREV.br_startoff <= new->br_startoff);
  1478. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  1479. da_old = startblockval(PREV.br_startblock);
  1480. da_new = 0;
  1481. /*
  1482. * Set flags determining what part of the previous delayed allocation
  1483. * extent is being replaced by a real allocation.
  1484. */
  1485. if (PREV.br_startoff == new->br_startoff)
  1486. state |= BMAP_LEFT_FILLING;
  1487. if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
  1488. state |= BMAP_RIGHT_FILLING;
  1489. /*
  1490. * Check and set flags if this segment has a left neighbor.
  1491. * Don't set contiguous if the combined extent would be too large.
  1492. */
  1493. if (bma->idx > 0) {
  1494. state |= BMAP_LEFT_VALID;
  1495. xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
  1496. if (isnullstartblock(LEFT.br_startblock))
  1497. state |= BMAP_LEFT_DELAY;
  1498. }
  1499. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  1500. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  1501. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  1502. LEFT.br_state == new->br_state &&
  1503. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  1504. state |= BMAP_LEFT_CONTIG;
  1505. /*
  1506. * Check and set flags if this segment has a right neighbor.
  1507. * Don't set contiguous if the combined extent would be too large.
  1508. * Also check for all-three-contiguous being too large.
  1509. */
  1510. if (bma->idx < xfs_iext_count(ifp) - 1) {
  1511. state |= BMAP_RIGHT_VALID;
  1512. xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
  1513. if (isnullstartblock(RIGHT.br_startblock))
  1514. state |= BMAP_RIGHT_DELAY;
  1515. }
  1516. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  1517. new_endoff == RIGHT.br_startoff &&
  1518. new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
  1519. new->br_state == RIGHT.br_state &&
  1520. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  1521. ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  1522. BMAP_RIGHT_FILLING)) !=
  1523. (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  1524. BMAP_RIGHT_FILLING) ||
  1525. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  1526. <= MAXEXTLEN))
  1527. state |= BMAP_RIGHT_CONTIG;
  1528. error = 0;
  1529. /*
  1530. * Switch out based on the FILLING and CONTIG state bits.
  1531. */
  1532. switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  1533. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
  1534. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  1535. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1536. /*
  1537. * Filling in all of a previously delayed allocation extent.
  1538. * The left and right neighbors are both contiguous with new.
  1539. */
  1540. bma->idx--;
  1541. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1542. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
  1543. LEFT.br_blockcount + PREV.br_blockcount +
  1544. RIGHT.br_blockcount);
  1545. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1546. xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
  1547. (*nextents)--;
  1548. if (bma->cur == NULL)
  1549. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1550. else {
  1551. rval = XFS_ILOG_CORE;
  1552. error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
  1553. RIGHT.br_startblock,
  1554. RIGHT.br_blockcount, &i);
  1555. if (error)
  1556. goto done;
  1557. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1558. error = xfs_btree_delete(bma->cur, &i);
  1559. if (error)
  1560. goto done;
  1561. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1562. error = xfs_btree_decrement(bma->cur, 0, &i);
  1563. if (error)
  1564. goto done;
  1565. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1566. error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
  1567. LEFT.br_startblock,
  1568. LEFT.br_blockcount +
  1569. PREV.br_blockcount +
  1570. RIGHT.br_blockcount, LEFT.br_state);
  1571. if (error)
  1572. goto done;
  1573. }
  1574. break;
  1575. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  1576. /*
  1577. * Filling in all of a previously delayed allocation extent.
  1578. * The left neighbor is contiguous, the right is not.
  1579. */
  1580. bma->idx--;
  1581. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1582. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
  1583. LEFT.br_blockcount + PREV.br_blockcount);
  1584. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1585. xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
  1586. if (bma->cur == NULL)
  1587. rval = XFS_ILOG_DEXT;
  1588. else {
  1589. rval = 0;
  1590. error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
  1591. LEFT.br_startblock, LEFT.br_blockcount,
  1592. &i);
  1593. if (error)
  1594. goto done;
  1595. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1596. error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
  1597. LEFT.br_startblock,
  1598. LEFT.br_blockcount +
  1599. PREV.br_blockcount, LEFT.br_state);
  1600. if (error)
  1601. goto done;
  1602. }
  1603. break;
  1604. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1605. /*
  1606. * Filling in all of a previously delayed allocation extent.
  1607. * The right neighbor is contiguous, the left is not.
  1608. */
  1609. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1610. xfs_bmbt_set_startblock(ep, new->br_startblock);
  1611. xfs_bmbt_set_blockcount(ep,
  1612. PREV.br_blockcount + RIGHT.br_blockcount);
  1613. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1614. xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
  1615. if (bma->cur == NULL)
  1616. rval = XFS_ILOG_DEXT;
  1617. else {
  1618. rval = 0;
  1619. error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
  1620. RIGHT.br_startblock,
  1621. RIGHT.br_blockcount, &i);
  1622. if (error)
  1623. goto done;
  1624. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1625. error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
  1626. new->br_startblock,
  1627. PREV.br_blockcount +
  1628. RIGHT.br_blockcount, PREV.br_state);
  1629. if (error)
  1630. goto done;
  1631. }
  1632. break;
  1633. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
  1634. /*
  1635. * Filling in all of a previously delayed allocation extent.
  1636. * Neither the left nor right neighbors are contiguous with
  1637. * the new one.
  1638. */
  1639. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1640. xfs_bmbt_set_startblock(ep, new->br_startblock);
  1641. xfs_bmbt_set_state(ep, new->br_state);
  1642. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1643. (*nextents)++;
  1644. if (bma->cur == NULL)
  1645. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1646. else {
  1647. rval = XFS_ILOG_CORE;
  1648. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  1649. new->br_startblock, new->br_blockcount,
  1650. &i);
  1651. if (error)
  1652. goto done;
  1653. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1654. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1655. error = xfs_btree_insert(bma->cur, &i);
  1656. if (error)
  1657. goto done;
  1658. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1659. }
  1660. break;
  1661. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
  1662. /*
  1663. * Filling in the first part of a previous delayed allocation.
  1664. * The left neighbor is contiguous.
  1665. */
  1666. trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
  1667. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
  1668. LEFT.br_blockcount + new->br_blockcount);
  1669. xfs_bmbt_set_startoff(ep,
  1670. PREV.br_startoff + new->br_blockcount);
  1671. trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
  1672. temp = PREV.br_blockcount - new->br_blockcount;
  1673. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1674. xfs_bmbt_set_blockcount(ep, temp);
  1675. if (bma->cur == NULL)
  1676. rval = XFS_ILOG_DEXT;
  1677. else {
  1678. rval = 0;
  1679. error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
  1680. LEFT.br_startblock, LEFT.br_blockcount,
  1681. &i);
  1682. if (error)
  1683. goto done;
  1684. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1685. error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
  1686. LEFT.br_startblock,
  1687. LEFT.br_blockcount +
  1688. new->br_blockcount,
  1689. LEFT.br_state);
  1690. if (error)
  1691. goto done;
  1692. }
  1693. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  1694. startblockval(PREV.br_startblock));
  1695. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  1696. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1697. bma->idx--;
  1698. break;
  1699. case BMAP_LEFT_FILLING:
  1700. /*
  1701. * Filling in the first part of a previous delayed allocation.
  1702. * The left neighbor is not contiguous.
  1703. */
  1704. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1705. xfs_bmbt_set_startoff(ep, new_endoff);
  1706. temp = PREV.br_blockcount - new->br_blockcount;
  1707. xfs_bmbt_set_blockcount(ep, temp);
  1708. xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
  1709. (*nextents)++;
  1710. if (bma->cur == NULL)
  1711. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1712. else {
  1713. rval = XFS_ILOG_CORE;
  1714. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  1715. new->br_startblock, new->br_blockcount,
  1716. &i);
  1717. if (error)
  1718. goto done;
  1719. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1720. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1721. error = xfs_btree_insert(bma->cur, &i);
  1722. if (error)
  1723. goto done;
  1724. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1725. }
  1726. if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
  1727. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  1728. bma->firstblock, bma->dfops,
  1729. &bma->cur, 1, &tmp_rval, whichfork);
  1730. rval |= tmp_rval;
  1731. if (error)
  1732. goto done;
  1733. }
  1734. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  1735. startblockval(PREV.br_startblock) -
  1736. (bma->cur ? bma->cur->bc_private.b.allocated : 0));
  1737. ep = xfs_iext_get_ext(ifp, bma->idx + 1);
  1738. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  1739. trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
  1740. break;
  1741. case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1742. /*
  1743. * Filling in the last part of a previous delayed allocation.
  1744. * The right neighbor is contiguous with the new allocation.
  1745. */
  1746. temp = PREV.br_blockcount - new->br_blockcount;
  1747. trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
  1748. xfs_bmbt_set_blockcount(ep, temp);
  1749. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
  1750. new->br_startoff, new->br_startblock,
  1751. new->br_blockcount + RIGHT.br_blockcount,
  1752. RIGHT.br_state);
  1753. trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
  1754. if (bma->cur == NULL)
  1755. rval = XFS_ILOG_DEXT;
  1756. else {
  1757. rval = 0;
  1758. error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
  1759. RIGHT.br_startblock,
  1760. RIGHT.br_blockcount, &i);
  1761. if (error)
  1762. goto done;
  1763. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1764. error = xfs_bmbt_update(bma->cur, new->br_startoff,
  1765. new->br_startblock,
  1766. new->br_blockcount +
  1767. RIGHT.br_blockcount,
  1768. RIGHT.br_state);
  1769. if (error)
  1770. goto done;
  1771. }
  1772. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  1773. startblockval(PREV.br_startblock));
  1774. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1775. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  1776. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1777. bma->idx++;
  1778. break;
  1779. case BMAP_RIGHT_FILLING:
  1780. /*
  1781. * Filling in the last part of a previous delayed allocation.
  1782. * The right neighbor is not contiguous.
  1783. */
  1784. temp = PREV.br_blockcount - new->br_blockcount;
  1785. trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
  1786. xfs_bmbt_set_blockcount(ep, temp);
  1787. xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
  1788. (*nextents)++;
  1789. if (bma->cur == NULL)
  1790. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1791. else {
  1792. rval = XFS_ILOG_CORE;
  1793. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  1794. new->br_startblock, new->br_blockcount,
  1795. &i);
  1796. if (error)
  1797. goto done;
  1798. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1799. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1800. error = xfs_btree_insert(bma->cur, &i);
  1801. if (error)
  1802. goto done;
  1803. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1804. }
  1805. if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
  1806. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  1807. bma->firstblock, bma->dfops, &bma->cur, 1,
  1808. &tmp_rval, whichfork);
  1809. rval |= tmp_rval;
  1810. if (error)
  1811. goto done;
  1812. }
  1813. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
  1814. startblockval(PREV.br_startblock) -
  1815. (bma->cur ? bma->cur->bc_private.b.allocated : 0));
  1816. ep = xfs_iext_get_ext(ifp, bma->idx);
  1817. xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
  1818. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1819. bma->idx++;
  1820. break;
  1821. case 0:
  1822. /*
  1823. * Filling in the middle part of a previous delayed allocation.
  1824. * Contiguity is impossible here.
  1825. * This case is avoided almost all the time.
  1826. *
  1827. * We start with a delayed allocation:
  1828. *
  1829. * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
  1830. * PREV @ idx
  1831. *
  1832. * and we are allocating:
  1833. * +rrrrrrrrrrrrrrrrr+
  1834. * new
  1835. *
  1836. * and we set it up for insertion as:
  1837. * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
  1838. * new
  1839. * PREV @ idx LEFT RIGHT
  1840. * inserted at idx + 1
  1841. */
  1842. temp = new->br_startoff - PREV.br_startoff;
  1843. temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
  1844. trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
  1845. xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
  1846. LEFT = *new;
  1847. RIGHT.br_state = PREV.br_state;
  1848. RIGHT.br_startblock = nullstartblock(
  1849. (int)xfs_bmap_worst_indlen(bma->ip, temp2));
  1850. RIGHT.br_startoff = new_endoff;
  1851. RIGHT.br_blockcount = temp2;
  1852. /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
  1853. xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
  1854. (*nextents)++;
  1855. if (bma->cur == NULL)
  1856. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1857. else {
  1858. rval = XFS_ILOG_CORE;
  1859. error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
  1860. new->br_startblock, new->br_blockcount,
  1861. &i);
  1862. if (error)
  1863. goto done;
  1864. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  1865. bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1866. error = xfs_btree_insert(bma->cur, &i);
  1867. if (error)
  1868. goto done;
  1869. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  1870. }
  1871. if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
  1872. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  1873. bma->firstblock, bma->dfops, &bma->cur,
  1874. 1, &tmp_rval, whichfork);
  1875. rval |= tmp_rval;
  1876. if (error)
  1877. goto done;
  1878. }
  1879. temp = xfs_bmap_worst_indlen(bma->ip, temp);
  1880. temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
  1881. diff = (int)(temp + temp2 -
  1882. (startblockval(PREV.br_startblock) -
  1883. (bma->cur ?
  1884. bma->cur->bc_private.b.allocated : 0)));
  1885. if (diff > 0) {
  1886. error = xfs_mod_fdblocks(bma->ip->i_mount,
  1887. -((int64_t)diff), false);
  1888. ASSERT(!error);
  1889. if (error)
  1890. goto done;
  1891. }
  1892. ep = xfs_iext_get_ext(ifp, bma->idx);
  1893. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  1894. trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
  1895. trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
  1896. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
  1897. nullstartblock((int)temp2));
  1898. trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
  1899. bma->idx++;
  1900. da_new = temp + temp2;
  1901. break;
  1902. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1903. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1904. case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
  1905. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  1906. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1907. case BMAP_LEFT_CONTIG:
  1908. case BMAP_RIGHT_CONTIG:
  1909. /*
  1910. * These cases are all impossible.
  1911. */
  1912. ASSERT(0);
  1913. }
  1914. /* add reverse mapping */
  1915. error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
  1916. if (error)
  1917. goto done;
  1918. /* convert to a btree if necessary */
  1919. if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
  1920. int tmp_logflags; /* partial log flag return val */
  1921. ASSERT(bma->cur == NULL);
  1922. error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
  1923. bma->firstblock, bma->dfops, &bma->cur,
  1924. da_old > 0, &tmp_logflags, whichfork);
  1925. bma->logflags |= tmp_logflags;
  1926. if (error)
  1927. goto done;
  1928. }
  1929. /* adjust for changes in reserved delayed indirect blocks */
  1930. if (da_old || da_new) {
  1931. temp = da_new;
  1932. if (bma->cur)
  1933. temp += bma->cur->bc_private.b.allocated;
  1934. if (temp < da_old)
  1935. xfs_mod_fdblocks(bma->ip->i_mount,
  1936. (int64_t)(da_old - temp), false);
  1937. }
  1938. /* clear out the allocated field, done with it now in any case. */
  1939. if (bma->cur)
  1940. bma->cur->bc_private.b.allocated = 0;
  1941. xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
  1942. done:
  1943. if (whichfork != XFS_COW_FORK)
  1944. bma->logflags |= rval;
  1945. return error;
  1946. #undef LEFT
  1947. #undef RIGHT
  1948. #undef PREV
  1949. }
  1950. /*
  1951. * Convert an unwritten allocation to a real allocation or vice versa.
  1952. */
  1953. STATIC int /* error */
  1954. xfs_bmap_add_extent_unwritten_real(
  1955. struct xfs_trans *tp,
  1956. xfs_inode_t *ip, /* incore inode pointer */
  1957. int whichfork,
  1958. xfs_extnum_t *idx, /* extent number to update/insert */
  1959. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  1960. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  1961. xfs_fsblock_t *first, /* pointer to firstblock variable */
  1962. struct xfs_defer_ops *dfops, /* list of extents to be freed */
  1963. int *logflagsp) /* inode logging flags */
  1964. {
  1965. xfs_btree_cur_t *cur; /* btree cursor */
  1966. xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
  1967. int error; /* error return value */
  1968. int i; /* temp state */
  1969. xfs_ifork_t *ifp; /* inode fork pointer */
  1970. xfs_fileoff_t new_endoff; /* end offset of new entry */
  1971. xfs_exntst_t newext; /* new extent state */
  1972. xfs_exntst_t oldext; /* old extent state */
  1973. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  1974. /* left is 0, right is 1, prev is 2 */
  1975. int rval=0; /* return value (logging flags) */
  1976. int state = 0;/* state bits, accessed thru macros */
  1977. struct xfs_mount *mp = ip->i_mount;
  1978. *logflagsp = 0;
  1979. cur = *curp;
  1980. ifp = XFS_IFORK_PTR(ip, whichfork);
  1981. if (whichfork == XFS_COW_FORK)
  1982. state |= BMAP_COWFORK;
  1983. ASSERT(*idx >= 0);
  1984. ASSERT(*idx <= xfs_iext_count(ifp));
  1985. ASSERT(!isnullstartblock(new->br_startblock));
  1986. XFS_STATS_INC(mp, xs_add_exlist);
  1987. #define LEFT r[0]
  1988. #define RIGHT r[1]
  1989. #define PREV r[2]
  1990. /*
  1991. * Set up a bunch of variables to make the tests simpler.
  1992. */
  1993. error = 0;
  1994. ep = xfs_iext_get_ext(ifp, *idx);
  1995. xfs_bmbt_get_all(ep, &PREV);
  1996. newext = new->br_state;
  1997. oldext = (newext == XFS_EXT_UNWRITTEN) ?
  1998. XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  1999. ASSERT(PREV.br_state == oldext);
  2000. new_endoff = new->br_startoff + new->br_blockcount;
  2001. ASSERT(PREV.br_startoff <= new->br_startoff);
  2002. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  2003. /*
  2004. * Set flags determining what part of the previous oldext allocation
  2005. * extent is being replaced by a newext allocation.
  2006. */
  2007. if (PREV.br_startoff == new->br_startoff)
  2008. state |= BMAP_LEFT_FILLING;
  2009. if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
  2010. state |= BMAP_RIGHT_FILLING;
  2011. /*
  2012. * Check and set flags if this segment has a left neighbor.
  2013. * Don't set contiguous if the combined extent would be too large.
  2014. */
  2015. if (*idx > 0) {
  2016. state |= BMAP_LEFT_VALID;
  2017. xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
  2018. if (isnullstartblock(LEFT.br_startblock))
  2019. state |= BMAP_LEFT_DELAY;
  2020. }
  2021. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  2022. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  2023. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  2024. LEFT.br_state == newext &&
  2025. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  2026. state |= BMAP_LEFT_CONTIG;
  2027. /*
  2028. * Check and set flags if this segment has a right neighbor.
  2029. * Don't set contiguous if the combined extent would be too large.
  2030. * Also check for all-three-contiguous being too large.
  2031. */
  2032. if (*idx < xfs_iext_count(ifp) - 1) {
  2033. state |= BMAP_RIGHT_VALID;
  2034. xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
  2035. if (isnullstartblock(RIGHT.br_startblock))
  2036. state |= BMAP_RIGHT_DELAY;
  2037. }
  2038. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  2039. new_endoff == RIGHT.br_startoff &&
  2040. new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
  2041. newext == RIGHT.br_state &&
  2042. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  2043. ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  2044. BMAP_RIGHT_FILLING)) !=
  2045. (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  2046. BMAP_RIGHT_FILLING) ||
  2047. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  2048. <= MAXEXTLEN))
  2049. state |= BMAP_RIGHT_CONTIG;
  2050. /*
  2051. * Switch out based on the FILLING and CONTIG state bits.
  2052. */
  2053. switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  2054. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
  2055. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  2056. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  2057. /*
  2058. * Setting all of a previous oldext extent to newext.
  2059. * The left and right neighbors are both contiguous with new.
  2060. */
  2061. --*idx;
  2062. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2063. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  2064. LEFT.br_blockcount + PREV.br_blockcount +
  2065. RIGHT.br_blockcount);
  2066. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2067. xfs_iext_remove(ip, *idx + 1, 2, state);
  2068. XFS_IFORK_NEXT_SET(ip, whichfork,
  2069. XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
  2070. if (cur == NULL)
  2071. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2072. else {
  2073. rval = XFS_ILOG_CORE;
  2074. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  2075. RIGHT.br_startblock,
  2076. RIGHT.br_blockcount, &i)))
  2077. goto done;
  2078. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2079. if ((error = xfs_btree_delete(cur, &i)))
  2080. goto done;
  2081. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2082. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2083. goto done;
  2084. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2085. if ((error = xfs_btree_delete(cur, &i)))
  2086. goto done;
  2087. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2088. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2089. goto done;
  2090. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2091. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  2092. LEFT.br_startblock,
  2093. LEFT.br_blockcount + PREV.br_blockcount +
  2094. RIGHT.br_blockcount, LEFT.br_state)))
  2095. goto done;
  2096. }
  2097. break;
  2098. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  2099. /*
  2100. * Setting all of a previous oldext extent to newext.
  2101. * The left neighbor is contiguous, the right is not.
  2102. */
  2103. --*idx;
  2104. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2105. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  2106. LEFT.br_blockcount + PREV.br_blockcount);
  2107. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2108. xfs_iext_remove(ip, *idx + 1, 1, state);
  2109. XFS_IFORK_NEXT_SET(ip, whichfork,
  2110. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2111. if (cur == NULL)
  2112. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2113. else {
  2114. rval = XFS_ILOG_CORE;
  2115. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2116. PREV.br_startblock, PREV.br_blockcount,
  2117. &i)))
  2118. goto done;
  2119. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2120. if ((error = xfs_btree_delete(cur, &i)))
  2121. goto done;
  2122. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2123. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2124. goto done;
  2125. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2126. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  2127. LEFT.br_startblock,
  2128. LEFT.br_blockcount + PREV.br_blockcount,
  2129. LEFT.br_state)))
  2130. goto done;
  2131. }
  2132. break;
  2133. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  2134. /*
  2135. * Setting all of a previous oldext extent to newext.
  2136. * The right neighbor is contiguous, the left is not.
  2137. */
  2138. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2139. xfs_bmbt_set_blockcount(ep,
  2140. PREV.br_blockcount + RIGHT.br_blockcount);
  2141. xfs_bmbt_set_state(ep, newext);
  2142. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2143. xfs_iext_remove(ip, *idx + 1, 1, state);
  2144. XFS_IFORK_NEXT_SET(ip, whichfork,
  2145. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2146. if (cur == NULL)
  2147. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2148. else {
  2149. rval = XFS_ILOG_CORE;
  2150. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  2151. RIGHT.br_startblock,
  2152. RIGHT.br_blockcount, &i)))
  2153. goto done;
  2154. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2155. if ((error = xfs_btree_delete(cur, &i)))
  2156. goto done;
  2157. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2158. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2159. goto done;
  2160. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2161. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  2162. new->br_startblock,
  2163. new->br_blockcount + RIGHT.br_blockcount,
  2164. newext)))
  2165. goto done;
  2166. }
  2167. break;
  2168. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
  2169. /*
  2170. * Setting all of a previous oldext extent to newext.
  2171. * Neither the left nor right neighbors are contiguous with
  2172. * the new one.
  2173. */
  2174. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2175. xfs_bmbt_set_state(ep, newext);
  2176. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2177. if (cur == NULL)
  2178. rval = XFS_ILOG_DEXT;
  2179. else {
  2180. rval = 0;
  2181. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  2182. new->br_startblock, new->br_blockcount,
  2183. &i)))
  2184. goto done;
  2185. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2186. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  2187. new->br_startblock, new->br_blockcount,
  2188. newext)))
  2189. goto done;
  2190. }
  2191. break;
  2192. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
  2193. /*
  2194. * Setting the first part of a previous oldext extent to newext.
  2195. * The left neighbor is contiguous.
  2196. */
  2197. trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
  2198. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
  2199. LEFT.br_blockcount + new->br_blockcount);
  2200. xfs_bmbt_set_startoff(ep,
  2201. PREV.br_startoff + new->br_blockcount);
  2202. trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
  2203. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2204. xfs_bmbt_set_startblock(ep,
  2205. new->br_startblock + new->br_blockcount);
  2206. xfs_bmbt_set_blockcount(ep,
  2207. PREV.br_blockcount - new->br_blockcount);
  2208. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2209. --*idx;
  2210. if (cur == NULL)
  2211. rval = XFS_ILOG_DEXT;
  2212. else {
  2213. rval = 0;
  2214. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2215. PREV.br_startblock, PREV.br_blockcount,
  2216. &i)))
  2217. goto done;
  2218. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2219. if ((error = xfs_bmbt_update(cur,
  2220. PREV.br_startoff + new->br_blockcount,
  2221. PREV.br_startblock + new->br_blockcount,
  2222. PREV.br_blockcount - new->br_blockcount,
  2223. oldext)))
  2224. goto done;
  2225. if ((error = xfs_btree_decrement(cur, 0, &i)))
  2226. goto done;
  2227. error = xfs_bmbt_update(cur, LEFT.br_startoff,
  2228. LEFT.br_startblock,
  2229. LEFT.br_blockcount + new->br_blockcount,
  2230. LEFT.br_state);
  2231. if (error)
  2232. goto done;
  2233. }
  2234. break;
  2235. case BMAP_LEFT_FILLING:
  2236. /*
  2237. * Setting the first part of a previous oldext extent to newext.
  2238. * The left neighbor is not contiguous.
  2239. */
  2240. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2241. ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
  2242. xfs_bmbt_set_startoff(ep, new_endoff);
  2243. xfs_bmbt_set_blockcount(ep,
  2244. PREV.br_blockcount - new->br_blockcount);
  2245. xfs_bmbt_set_startblock(ep,
  2246. new->br_startblock + new->br_blockcount);
  2247. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2248. xfs_iext_insert(ip, *idx, 1, new, state);
  2249. XFS_IFORK_NEXT_SET(ip, whichfork,
  2250. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2251. if (cur == NULL)
  2252. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2253. else {
  2254. rval = XFS_ILOG_CORE;
  2255. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2256. PREV.br_startblock, PREV.br_blockcount,
  2257. &i)))
  2258. goto done;
  2259. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2260. if ((error = xfs_bmbt_update(cur,
  2261. PREV.br_startoff + new->br_blockcount,
  2262. PREV.br_startblock + new->br_blockcount,
  2263. PREV.br_blockcount - new->br_blockcount,
  2264. oldext)))
  2265. goto done;
  2266. cur->bc_rec.b = *new;
  2267. if ((error = xfs_btree_insert(cur, &i)))
  2268. goto done;
  2269. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2270. }
  2271. break;
  2272. case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  2273. /*
  2274. * Setting the last part of a previous oldext extent to newext.
  2275. * The right neighbor is contiguous with the new allocation.
  2276. */
  2277. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2278. xfs_bmbt_set_blockcount(ep,
  2279. PREV.br_blockcount - new->br_blockcount);
  2280. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2281. ++*idx;
  2282. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2283. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
  2284. new->br_startoff, new->br_startblock,
  2285. new->br_blockcount + RIGHT.br_blockcount, newext);
  2286. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2287. if (cur == NULL)
  2288. rval = XFS_ILOG_DEXT;
  2289. else {
  2290. rval = 0;
  2291. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2292. PREV.br_startblock,
  2293. PREV.br_blockcount, &i)))
  2294. goto done;
  2295. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2296. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  2297. PREV.br_startblock,
  2298. PREV.br_blockcount - new->br_blockcount,
  2299. oldext)))
  2300. goto done;
  2301. if ((error = xfs_btree_increment(cur, 0, &i)))
  2302. goto done;
  2303. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  2304. new->br_startblock,
  2305. new->br_blockcount + RIGHT.br_blockcount,
  2306. newext)))
  2307. goto done;
  2308. }
  2309. break;
  2310. case BMAP_RIGHT_FILLING:
  2311. /*
  2312. * Setting the last part of a previous oldext extent to newext.
  2313. * The right neighbor is not contiguous.
  2314. */
  2315. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2316. xfs_bmbt_set_blockcount(ep,
  2317. PREV.br_blockcount - new->br_blockcount);
  2318. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2319. ++*idx;
  2320. xfs_iext_insert(ip, *idx, 1, new, state);
  2321. XFS_IFORK_NEXT_SET(ip, whichfork,
  2322. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2323. if (cur == NULL)
  2324. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2325. else {
  2326. rval = XFS_ILOG_CORE;
  2327. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2328. PREV.br_startblock, PREV.br_blockcount,
  2329. &i)))
  2330. goto done;
  2331. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2332. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  2333. PREV.br_startblock,
  2334. PREV.br_blockcount - new->br_blockcount,
  2335. oldext)))
  2336. goto done;
  2337. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  2338. new->br_startblock, new->br_blockcount,
  2339. &i)))
  2340. goto done;
  2341. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  2342. cur->bc_rec.b.br_state = new->br_state;
  2343. if ((error = xfs_btree_insert(cur, &i)))
  2344. goto done;
  2345. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2346. }
  2347. break;
  2348. case 0:
  2349. /*
  2350. * Setting the middle part of a previous oldext extent to
  2351. * newext. Contiguity is impossible here.
  2352. * One extent becomes three extents.
  2353. */
  2354. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2355. xfs_bmbt_set_blockcount(ep,
  2356. new->br_startoff - PREV.br_startoff);
  2357. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2358. r[0] = *new;
  2359. r[1].br_startoff = new_endoff;
  2360. r[1].br_blockcount =
  2361. PREV.br_startoff + PREV.br_blockcount - new_endoff;
  2362. r[1].br_startblock = new->br_startblock + new->br_blockcount;
  2363. r[1].br_state = oldext;
  2364. ++*idx;
  2365. xfs_iext_insert(ip, *idx, 2, &r[0], state);
  2366. XFS_IFORK_NEXT_SET(ip, whichfork,
  2367. XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
  2368. if (cur == NULL)
  2369. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  2370. else {
  2371. rval = XFS_ILOG_CORE;
  2372. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  2373. PREV.br_startblock, PREV.br_blockcount,
  2374. &i)))
  2375. goto done;
  2376. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2377. /* new right extent - oldext */
  2378. if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
  2379. r[1].br_startblock, r[1].br_blockcount,
  2380. r[1].br_state)))
  2381. goto done;
  2382. /* new left extent - oldext */
  2383. cur->bc_rec.b = PREV;
  2384. cur->bc_rec.b.br_blockcount =
  2385. new->br_startoff - PREV.br_startoff;
  2386. if ((error = xfs_btree_insert(cur, &i)))
  2387. goto done;
  2388. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2389. /*
  2390. * Reset the cursor to the position of the new extent
  2391. * we are about to insert as we can't trust it after
  2392. * the previous insert.
  2393. */
  2394. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  2395. new->br_startblock, new->br_blockcount,
  2396. &i)))
  2397. goto done;
  2398. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  2399. /* new middle extent - newext */
  2400. cur->bc_rec.b.br_state = new->br_state;
  2401. if ((error = xfs_btree_insert(cur, &i)))
  2402. goto done;
  2403. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2404. }
  2405. break;
  2406. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2407. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2408. case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
  2409. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  2410. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2411. case BMAP_LEFT_CONTIG:
  2412. case BMAP_RIGHT_CONTIG:
  2413. /*
  2414. * These cases are all impossible.
  2415. */
  2416. ASSERT(0);
  2417. }
  2418. /* update reverse mappings */
  2419. error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
  2420. if (error)
  2421. goto done;
  2422. /* convert to a btree if necessary */
  2423. if (xfs_bmap_needs_btree(ip, whichfork)) {
  2424. int tmp_logflags; /* partial log flag return val */
  2425. ASSERT(cur == NULL);
  2426. error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
  2427. 0, &tmp_logflags, whichfork);
  2428. *logflagsp |= tmp_logflags;
  2429. if (error)
  2430. goto done;
  2431. }
  2432. /* clear out the allocated field, done with it now in any case. */
  2433. if (cur) {
  2434. cur->bc_private.b.allocated = 0;
  2435. *curp = cur;
  2436. }
  2437. xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
  2438. done:
  2439. *logflagsp |= rval;
  2440. return error;
  2441. #undef LEFT
  2442. #undef RIGHT
  2443. #undef PREV
  2444. }
  2445. /*
  2446. * Convert a hole to a delayed allocation.
  2447. */
  2448. STATIC void
  2449. xfs_bmap_add_extent_hole_delay(
  2450. xfs_inode_t *ip, /* incore inode pointer */
  2451. int whichfork,
  2452. xfs_extnum_t *idx, /* extent number to update/insert */
  2453. xfs_bmbt_irec_t *new) /* new data to add to file extents */
  2454. {
  2455. xfs_ifork_t *ifp; /* inode fork pointer */
  2456. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  2457. xfs_filblks_t newlen=0; /* new indirect size */
  2458. xfs_filblks_t oldlen=0; /* old indirect size */
  2459. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  2460. int state; /* state bits, accessed thru macros */
  2461. xfs_filblks_t temp=0; /* temp for indirect calculations */
  2462. ifp = XFS_IFORK_PTR(ip, whichfork);
  2463. state = 0;
  2464. if (whichfork == XFS_COW_FORK)
  2465. state |= BMAP_COWFORK;
  2466. ASSERT(isnullstartblock(new->br_startblock));
  2467. /*
  2468. * Check and set flags if this segment has a left neighbor
  2469. */
  2470. if (*idx > 0) {
  2471. state |= BMAP_LEFT_VALID;
  2472. xfs_iext_get_extent(ifp, *idx - 1, &left);
  2473. if (isnullstartblock(left.br_startblock))
  2474. state |= BMAP_LEFT_DELAY;
  2475. }
  2476. /*
  2477. * Check and set flags if the current (right) segment exists.
  2478. * If it doesn't exist, we're converting the hole at end-of-file.
  2479. */
  2480. if (*idx < xfs_iext_count(ifp)) {
  2481. state |= BMAP_RIGHT_VALID;
  2482. xfs_iext_get_extent(ifp, *idx, &right);
  2483. if (isnullstartblock(right.br_startblock))
  2484. state |= BMAP_RIGHT_DELAY;
  2485. }
  2486. /*
  2487. * Set contiguity flags on the left and right neighbors.
  2488. * Don't let extents get too large, even if the pieces are contiguous.
  2489. */
  2490. if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
  2491. left.br_startoff + left.br_blockcount == new->br_startoff &&
  2492. left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  2493. state |= BMAP_LEFT_CONTIG;
  2494. if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
  2495. new->br_startoff + new->br_blockcount == right.br_startoff &&
  2496. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  2497. (!(state & BMAP_LEFT_CONTIG) ||
  2498. (left.br_blockcount + new->br_blockcount +
  2499. right.br_blockcount <= MAXEXTLEN)))
  2500. state |= BMAP_RIGHT_CONTIG;
  2501. /*
  2502. * Switch out based on the contiguity flags.
  2503. */
  2504. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  2505. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2506. /*
  2507. * New allocation is contiguous with delayed allocations
  2508. * on the left and on the right.
  2509. * Merge all three into a single extent record.
  2510. */
  2511. --*idx;
  2512. temp = left.br_blockcount + new->br_blockcount +
  2513. right.br_blockcount;
  2514. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2515. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
  2516. oldlen = startblockval(left.br_startblock) +
  2517. startblockval(new->br_startblock) +
  2518. startblockval(right.br_startblock);
  2519. newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2520. oldlen);
  2521. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
  2522. nullstartblock((int)newlen));
  2523. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2524. xfs_iext_remove(ip, *idx + 1, 1, state);
  2525. break;
  2526. case BMAP_LEFT_CONTIG:
  2527. /*
  2528. * New allocation is contiguous with a delayed allocation
  2529. * on the left.
  2530. * Merge the new allocation with the left neighbor.
  2531. */
  2532. --*idx;
  2533. temp = left.br_blockcount + new->br_blockcount;
  2534. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2535. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
  2536. oldlen = startblockval(left.br_startblock) +
  2537. startblockval(new->br_startblock);
  2538. newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2539. oldlen);
  2540. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
  2541. nullstartblock((int)newlen));
  2542. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2543. break;
  2544. case BMAP_RIGHT_CONTIG:
  2545. /*
  2546. * New allocation is contiguous with a delayed allocation
  2547. * on the right.
  2548. * Merge the new allocation with the right neighbor.
  2549. */
  2550. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2551. temp = new->br_blockcount + right.br_blockcount;
  2552. oldlen = startblockval(new->br_startblock) +
  2553. startblockval(right.br_startblock);
  2554. newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2555. oldlen);
  2556. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
  2557. new->br_startoff,
  2558. nullstartblock((int)newlen), temp, right.br_state);
  2559. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2560. break;
  2561. case 0:
  2562. /*
  2563. * New allocation is not contiguous with another
  2564. * delayed allocation.
  2565. * Insert a new entry.
  2566. */
  2567. oldlen = newlen = 0;
  2568. xfs_iext_insert(ip, *idx, 1, new, state);
  2569. break;
  2570. }
  2571. if (oldlen != newlen) {
  2572. ASSERT(oldlen > newlen);
  2573. xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
  2574. false);
  2575. /*
  2576. * Nothing to do for disk quota accounting here.
  2577. */
  2578. }
  2579. }
  2580. /*
  2581. * Convert a hole to a real allocation.
  2582. */
  2583. STATIC int /* error */
  2584. xfs_bmap_add_extent_hole_real(
  2585. struct xfs_trans *tp,
  2586. struct xfs_inode *ip,
  2587. int whichfork,
  2588. xfs_extnum_t *idx,
  2589. struct xfs_btree_cur **curp,
  2590. struct xfs_bmbt_irec *new,
  2591. xfs_fsblock_t *first,
  2592. struct xfs_defer_ops *dfops,
  2593. int *logflagsp)
  2594. {
  2595. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  2596. struct xfs_mount *mp = ip->i_mount;
  2597. struct xfs_btree_cur *cur = *curp;
  2598. int error; /* error return value */
  2599. int i; /* temp state */
  2600. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  2601. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  2602. int rval=0; /* return value (logging flags) */
  2603. int state; /* state bits, accessed thru macros */
  2604. ASSERT(*idx >= 0);
  2605. ASSERT(*idx <= xfs_iext_count(ifp));
  2606. ASSERT(!isnullstartblock(new->br_startblock));
  2607. ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
  2608. XFS_STATS_INC(mp, xs_add_exlist);
  2609. state = 0;
  2610. if (whichfork == XFS_ATTR_FORK)
  2611. state |= BMAP_ATTRFORK;
  2612. if (whichfork == XFS_COW_FORK)
  2613. state |= BMAP_COWFORK;
  2614. /*
  2615. * Check and set flags if this segment has a left neighbor.
  2616. */
  2617. if (*idx > 0) {
  2618. state |= BMAP_LEFT_VALID;
  2619. xfs_iext_get_extent(ifp, *idx - 1, &left);
  2620. if (isnullstartblock(left.br_startblock))
  2621. state |= BMAP_LEFT_DELAY;
  2622. }
  2623. /*
  2624. * Check and set flags if this segment has a current value.
  2625. * Not true if we're inserting into the "hole" at eof.
  2626. */
  2627. if (*idx < xfs_iext_count(ifp)) {
  2628. state |= BMAP_RIGHT_VALID;
  2629. xfs_iext_get_extent(ifp, *idx, &right);
  2630. if (isnullstartblock(right.br_startblock))
  2631. state |= BMAP_RIGHT_DELAY;
  2632. }
  2633. /*
  2634. * We're inserting a real allocation between "left" and "right".
  2635. * Set the contiguity flags. Don't let extents get too large.
  2636. */
  2637. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  2638. left.br_startoff + left.br_blockcount == new->br_startoff &&
  2639. left.br_startblock + left.br_blockcount == new->br_startblock &&
  2640. left.br_state == new->br_state &&
  2641. left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  2642. state |= BMAP_LEFT_CONTIG;
  2643. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  2644. new->br_startoff + new->br_blockcount == right.br_startoff &&
  2645. new->br_startblock + new->br_blockcount == right.br_startblock &&
  2646. new->br_state == right.br_state &&
  2647. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  2648. (!(state & BMAP_LEFT_CONTIG) ||
  2649. left.br_blockcount + new->br_blockcount +
  2650. right.br_blockcount <= MAXEXTLEN))
  2651. state |= BMAP_RIGHT_CONTIG;
  2652. error = 0;
  2653. /*
  2654. * Select which case we're in here, and implement it.
  2655. */
  2656. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  2657. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  2658. /*
  2659. * New allocation is contiguous with real allocations on the
  2660. * left and on the right.
  2661. * Merge all three into a single extent record.
  2662. */
  2663. --*idx;
  2664. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2665. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  2666. left.br_blockcount + new->br_blockcount +
  2667. right.br_blockcount);
  2668. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2669. xfs_iext_remove(ip, *idx + 1, 1, state);
  2670. XFS_IFORK_NEXT_SET(ip, whichfork,
  2671. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2672. if (cur == NULL) {
  2673. rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  2674. } else {
  2675. rval = XFS_ILOG_CORE;
  2676. error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
  2677. right.br_startblock, right.br_blockcount,
  2678. &i);
  2679. if (error)
  2680. goto done;
  2681. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2682. error = xfs_btree_delete(cur, &i);
  2683. if (error)
  2684. goto done;
  2685. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2686. error = xfs_btree_decrement(cur, 0, &i);
  2687. if (error)
  2688. goto done;
  2689. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2690. error = xfs_bmbt_update(cur, left.br_startoff,
  2691. left.br_startblock,
  2692. left.br_blockcount +
  2693. new->br_blockcount +
  2694. right.br_blockcount,
  2695. left.br_state);
  2696. if (error)
  2697. goto done;
  2698. }
  2699. break;
  2700. case BMAP_LEFT_CONTIG:
  2701. /*
  2702. * New allocation is contiguous with a real allocation
  2703. * on the left.
  2704. * Merge the new allocation with the left neighbor.
  2705. */
  2706. --*idx;
  2707. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2708. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
  2709. left.br_blockcount + new->br_blockcount);
  2710. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2711. if (cur == NULL) {
  2712. rval = xfs_ilog_fext(whichfork);
  2713. } else {
  2714. rval = 0;
  2715. error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
  2716. left.br_startblock, left.br_blockcount,
  2717. &i);
  2718. if (error)
  2719. goto done;
  2720. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2721. error = xfs_bmbt_update(cur, left.br_startoff,
  2722. left.br_startblock,
  2723. left.br_blockcount +
  2724. new->br_blockcount,
  2725. left.br_state);
  2726. if (error)
  2727. goto done;
  2728. }
  2729. break;
  2730. case BMAP_RIGHT_CONTIG:
  2731. /*
  2732. * New allocation is contiguous with a real allocation
  2733. * on the right.
  2734. * Merge the new allocation with the right neighbor.
  2735. */
  2736. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  2737. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
  2738. new->br_startoff, new->br_startblock,
  2739. new->br_blockcount + right.br_blockcount,
  2740. right.br_state);
  2741. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  2742. if (cur == NULL) {
  2743. rval = xfs_ilog_fext(whichfork);
  2744. } else {
  2745. rval = 0;
  2746. error = xfs_bmbt_lookup_eq(cur,
  2747. right.br_startoff,
  2748. right.br_startblock,
  2749. right.br_blockcount, &i);
  2750. if (error)
  2751. goto done;
  2752. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2753. error = xfs_bmbt_update(cur, new->br_startoff,
  2754. new->br_startblock,
  2755. new->br_blockcount +
  2756. right.br_blockcount,
  2757. right.br_state);
  2758. if (error)
  2759. goto done;
  2760. }
  2761. break;
  2762. case 0:
  2763. /*
  2764. * New allocation is not contiguous with another
  2765. * real allocation.
  2766. * Insert a new entry.
  2767. */
  2768. xfs_iext_insert(ip, *idx, 1, new, state);
  2769. XFS_IFORK_NEXT_SET(ip, whichfork,
  2770. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  2771. if (cur == NULL) {
  2772. rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  2773. } else {
  2774. rval = XFS_ILOG_CORE;
  2775. error = xfs_bmbt_lookup_eq(cur,
  2776. new->br_startoff,
  2777. new->br_startblock,
  2778. new->br_blockcount, &i);
  2779. if (error)
  2780. goto done;
  2781. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
  2782. cur->bc_rec.b.br_state = new->br_state;
  2783. error = xfs_btree_insert(cur, &i);
  2784. if (error)
  2785. goto done;
  2786. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  2787. }
  2788. break;
  2789. }
  2790. /* add reverse mapping */
  2791. error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
  2792. if (error)
  2793. goto done;
  2794. /* convert to a btree if necessary */
  2795. if (xfs_bmap_needs_btree(ip, whichfork)) {
  2796. int tmp_logflags; /* partial log flag return val */
  2797. ASSERT(cur == NULL);
  2798. error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
  2799. 0, &tmp_logflags, whichfork);
  2800. *logflagsp |= tmp_logflags;
  2801. cur = *curp;
  2802. if (error)
  2803. goto done;
  2804. }
  2805. /* clear out the allocated field, done with it now in any case. */
  2806. if (cur)
  2807. cur->bc_private.b.allocated = 0;
  2808. xfs_bmap_check_leaf_extents(cur, ip, whichfork);
  2809. done:
  2810. *logflagsp |= rval;
  2811. return error;
  2812. }
  2813. /*
  2814. * Functions used in the extent read, allocate and remove paths
  2815. */
  2816. /*
  2817. * Adjust the size of the new extent based on di_extsize and rt extsize.
  2818. */
  2819. int
  2820. xfs_bmap_extsize_align(
  2821. xfs_mount_t *mp,
  2822. xfs_bmbt_irec_t *gotp, /* next extent pointer */
  2823. xfs_bmbt_irec_t *prevp, /* previous extent pointer */
  2824. xfs_extlen_t extsz, /* align to this extent size */
  2825. int rt, /* is this a realtime inode? */
  2826. int eof, /* is extent at end-of-file? */
  2827. int delay, /* creating delalloc extent? */
  2828. int convert, /* overwriting unwritten extent? */
  2829. xfs_fileoff_t *offp, /* in/out: aligned offset */
  2830. xfs_extlen_t *lenp) /* in/out: aligned length */
  2831. {
  2832. xfs_fileoff_t orig_off; /* original offset */
  2833. xfs_extlen_t orig_alen; /* original length */
  2834. xfs_fileoff_t orig_end; /* original off+len */
  2835. xfs_fileoff_t nexto; /* next file offset */
  2836. xfs_fileoff_t prevo; /* previous file offset */
  2837. xfs_fileoff_t align_off; /* temp for offset */
  2838. xfs_extlen_t align_alen; /* temp for length */
  2839. xfs_extlen_t temp; /* temp for calculations */
  2840. if (convert)
  2841. return 0;
  2842. orig_off = align_off = *offp;
  2843. orig_alen = align_alen = *lenp;
  2844. orig_end = orig_off + orig_alen;
  2845. /*
  2846. * If this request overlaps an existing extent, then don't
  2847. * attempt to perform any additional alignment.
  2848. */
  2849. if (!delay && !eof &&
  2850. (orig_off >= gotp->br_startoff) &&
  2851. (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
  2852. return 0;
  2853. }
  2854. /*
  2855. * If the file offset is unaligned vs. the extent size
  2856. * we need to align it. This will be possible unless
  2857. * the file was previously written with a kernel that didn't
  2858. * perform this alignment, or if a truncate shot us in the
  2859. * foot.
  2860. */
  2861. temp = do_mod(orig_off, extsz);
  2862. if (temp) {
  2863. align_alen += temp;
  2864. align_off -= temp;
  2865. }
  2866. /* Same adjustment for the end of the requested area. */
  2867. temp = (align_alen % extsz);
  2868. if (temp)
  2869. align_alen += extsz - temp;
  2870. /*
  2871. * For large extent hint sizes, the aligned extent might be larger than
  2872. * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
  2873. * the length back under MAXEXTLEN. The outer allocation loops handle
  2874. * short allocation just fine, so it is safe to do this. We only want to
  2875. * do it when we are forced to, though, because it means more allocation
  2876. * operations are required.
  2877. */
  2878. while (align_alen > MAXEXTLEN)
  2879. align_alen -= extsz;
  2880. ASSERT(align_alen <= MAXEXTLEN);
  2881. /*
  2882. * If the previous block overlaps with this proposed allocation
  2883. * then move the start forward without adjusting the length.
  2884. */
  2885. if (prevp->br_startoff != NULLFILEOFF) {
  2886. if (prevp->br_startblock == HOLESTARTBLOCK)
  2887. prevo = prevp->br_startoff;
  2888. else
  2889. prevo = prevp->br_startoff + prevp->br_blockcount;
  2890. } else
  2891. prevo = 0;
  2892. if (align_off != orig_off && align_off < prevo)
  2893. align_off = prevo;
  2894. /*
  2895. * If the next block overlaps with this proposed allocation
  2896. * then move the start back without adjusting the length,
  2897. * but not before offset 0.
  2898. * This may of course make the start overlap previous block,
  2899. * and if we hit the offset 0 limit then the next block
  2900. * can still overlap too.
  2901. */
  2902. if (!eof && gotp->br_startoff != NULLFILEOFF) {
  2903. if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
  2904. (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
  2905. nexto = gotp->br_startoff + gotp->br_blockcount;
  2906. else
  2907. nexto = gotp->br_startoff;
  2908. } else
  2909. nexto = NULLFILEOFF;
  2910. if (!eof &&
  2911. align_off + align_alen != orig_end &&
  2912. align_off + align_alen > nexto)
  2913. align_off = nexto > align_alen ? nexto - align_alen : 0;
  2914. /*
  2915. * If we're now overlapping the next or previous extent that
  2916. * means we can't fit an extsz piece in this hole. Just move
  2917. * the start forward to the first valid spot and set
  2918. * the length so we hit the end.
  2919. */
  2920. if (align_off != orig_off && align_off < prevo)
  2921. align_off = prevo;
  2922. if (align_off + align_alen != orig_end &&
  2923. align_off + align_alen > nexto &&
  2924. nexto != NULLFILEOFF) {
  2925. ASSERT(nexto > prevo);
  2926. align_alen = nexto - align_off;
  2927. }
  2928. /*
  2929. * If realtime, and the result isn't a multiple of the realtime
  2930. * extent size we need to remove blocks until it is.
  2931. */
  2932. if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
  2933. /*
  2934. * We're not covering the original request, or
  2935. * we won't be able to once we fix the length.
  2936. */
  2937. if (orig_off < align_off ||
  2938. orig_end > align_off + align_alen ||
  2939. align_alen - temp < orig_alen)
  2940. return -EINVAL;
  2941. /*
  2942. * Try to fix it by moving the start up.
  2943. */
  2944. if (align_off + temp <= orig_off) {
  2945. align_alen -= temp;
  2946. align_off += temp;
  2947. }
  2948. /*
  2949. * Try to fix it by moving the end in.
  2950. */
  2951. else if (align_off + align_alen - temp >= orig_end)
  2952. align_alen -= temp;
  2953. /*
  2954. * Set the start to the minimum then trim the length.
  2955. */
  2956. else {
  2957. align_alen -= orig_off - align_off;
  2958. align_off = orig_off;
  2959. align_alen -= align_alen % mp->m_sb.sb_rextsize;
  2960. }
  2961. /*
  2962. * Result doesn't cover the request, fail it.
  2963. */
  2964. if (orig_off < align_off || orig_end > align_off + align_alen)
  2965. return -EINVAL;
  2966. } else {
  2967. ASSERT(orig_off >= align_off);
  2968. /* see MAXEXTLEN handling above */
  2969. ASSERT(orig_end <= align_off + align_alen ||
  2970. align_alen + extsz > MAXEXTLEN);
  2971. }
  2972. #ifdef DEBUG
  2973. if (!eof && gotp->br_startoff != NULLFILEOFF)
  2974. ASSERT(align_off + align_alen <= gotp->br_startoff);
  2975. if (prevp->br_startoff != NULLFILEOFF)
  2976. ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
  2977. #endif
  2978. *lenp = align_alen;
  2979. *offp = align_off;
  2980. return 0;
  2981. }
  2982. #define XFS_ALLOC_GAP_UNITS 4
  2983. void
  2984. xfs_bmap_adjacent(
  2985. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  2986. {
  2987. xfs_fsblock_t adjust; /* adjustment to block numbers */
  2988. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  2989. xfs_mount_t *mp; /* mount point structure */
  2990. int nullfb; /* true if ap->firstblock isn't set */
  2991. int rt; /* true if inode is realtime */
  2992. #define ISVALID(x,y) \
  2993. (rt ? \
  2994. (x) < mp->m_sb.sb_rblocks : \
  2995. XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
  2996. XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
  2997. XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
  2998. mp = ap->ip->i_mount;
  2999. nullfb = *ap->firstblock == NULLFSBLOCK;
  3000. rt = XFS_IS_REALTIME_INODE(ap->ip) &&
  3001. xfs_alloc_is_userdata(ap->datatype);
  3002. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
  3003. /*
  3004. * If allocating at eof, and there's a previous real block,
  3005. * try to use its last block as our starting point.
  3006. */
  3007. if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
  3008. !isnullstartblock(ap->prev.br_startblock) &&
  3009. ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
  3010. ap->prev.br_startblock)) {
  3011. ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
  3012. /*
  3013. * Adjust for the gap between prevp and us.
  3014. */
  3015. adjust = ap->offset -
  3016. (ap->prev.br_startoff + ap->prev.br_blockcount);
  3017. if (adjust &&
  3018. ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
  3019. ap->blkno += adjust;
  3020. }
  3021. /*
  3022. * If not at eof, then compare the two neighbor blocks.
  3023. * Figure out whether either one gives us a good starting point,
  3024. * and pick the better one.
  3025. */
  3026. else if (!ap->eof) {
  3027. xfs_fsblock_t gotbno; /* right side block number */
  3028. xfs_fsblock_t gotdiff=0; /* right side difference */
  3029. xfs_fsblock_t prevbno; /* left side block number */
  3030. xfs_fsblock_t prevdiff=0; /* left side difference */
  3031. /*
  3032. * If there's a previous (left) block, select a requested
  3033. * start block based on it.
  3034. */
  3035. if (ap->prev.br_startoff != NULLFILEOFF &&
  3036. !isnullstartblock(ap->prev.br_startblock) &&
  3037. (prevbno = ap->prev.br_startblock +
  3038. ap->prev.br_blockcount) &&
  3039. ISVALID(prevbno, ap->prev.br_startblock)) {
  3040. /*
  3041. * Calculate gap to end of previous block.
  3042. */
  3043. adjust = prevdiff = ap->offset -
  3044. (ap->prev.br_startoff +
  3045. ap->prev.br_blockcount);
  3046. /*
  3047. * Figure the startblock based on the previous block's
  3048. * end and the gap size.
  3049. * Heuristic!
  3050. * If the gap is large relative to the piece we're
  3051. * allocating, or using it gives us an invalid block
  3052. * number, then just use the end of the previous block.
  3053. */
  3054. if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
  3055. ISVALID(prevbno + prevdiff,
  3056. ap->prev.br_startblock))
  3057. prevbno += adjust;
  3058. else
  3059. prevdiff += adjust;
  3060. /*
  3061. * If the firstblock forbids it, can't use it,
  3062. * must use default.
  3063. */
  3064. if (!rt && !nullfb &&
  3065. XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
  3066. prevbno = NULLFSBLOCK;
  3067. }
  3068. /*
  3069. * No previous block or can't follow it, just default.
  3070. */
  3071. else
  3072. prevbno = NULLFSBLOCK;
  3073. /*
  3074. * If there's a following (right) block, select a requested
  3075. * start block based on it.
  3076. */
  3077. if (!isnullstartblock(ap->got.br_startblock)) {
  3078. /*
  3079. * Calculate gap to start of next block.
  3080. */
  3081. adjust = gotdiff = ap->got.br_startoff - ap->offset;
  3082. /*
  3083. * Figure the startblock based on the next block's
  3084. * start and the gap size.
  3085. */
  3086. gotbno = ap->got.br_startblock;
  3087. /*
  3088. * Heuristic!
  3089. * If the gap is large relative to the piece we're
  3090. * allocating, or using it gives us an invalid block
  3091. * number, then just use the start of the next block
  3092. * offset by our length.
  3093. */
  3094. if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
  3095. ISVALID(gotbno - gotdiff, gotbno))
  3096. gotbno -= adjust;
  3097. else if (ISVALID(gotbno - ap->length, gotbno)) {
  3098. gotbno -= ap->length;
  3099. gotdiff += adjust - ap->length;
  3100. } else
  3101. gotdiff += adjust;
  3102. /*
  3103. * If the firstblock forbids it, can't use it,
  3104. * must use default.
  3105. */
  3106. if (!rt && !nullfb &&
  3107. XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
  3108. gotbno = NULLFSBLOCK;
  3109. }
  3110. /*
  3111. * No next block, just default.
  3112. */
  3113. else
  3114. gotbno = NULLFSBLOCK;
  3115. /*
  3116. * If both valid, pick the better one, else the only good
  3117. * one, else ap->blkno is already set (to 0 or the inode block).
  3118. */
  3119. if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
  3120. ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
  3121. else if (prevbno != NULLFSBLOCK)
  3122. ap->blkno = prevbno;
  3123. else if (gotbno != NULLFSBLOCK)
  3124. ap->blkno = gotbno;
  3125. }
  3126. #undef ISVALID
  3127. }
  3128. static int
  3129. xfs_bmap_longest_free_extent(
  3130. struct xfs_trans *tp,
  3131. xfs_agnumber_t ag,
  3132. xfs_extlen_t *blen,
  3133. int *notinit)
  3134. {
  3135. struct xfs_mount *mp = tp->t_mountp;
  3136. struct xfs_perag *pag;
  3137. xfs_extlen_t longest;
  3138. int error = 0;
  3139. pag = xfs_perag_get(mp, ag);
  3140. if (!pag->pagf_init) {
  3141. error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
  3142. if (error)
  3143. goto out;
  3144. if (!pag->pagf_init) {
  3145. *notinit = 1;
  3146. goto out;
  3147. }
  3148. }
  3149. longest = xfs_alloc_longest_free_extent(mp, pag,
  3150. xfs_alloc_min_freelist(mp, pag),
  3151. xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
  3152. if (*blen < longest)
  3153. *blen = longest;
  3154. out:
  3155. xfs_perag_put(pag);
  3156. return error;
  3157. }
  3158. static void
  3159. xfs_bmap_select_minlen(
  3160. struct xfs_bmalloca *ap,
  3161. struct xfs_alloc_arg *args,
  3162. xfs_extlen_t *blen,
  3163. int notinit)
  3164. {
  3165. if (notinit || *blen < ap->minlen) {
  3166. /*
  3167. * Since we did a BUF_TRYLOCK above, it is possible that
  3168. * there is space for this request.
  3169. */
  3170. args->minlen = ap->minlen;
  3171. } else if (*blen < args->maxlen) {
  3172. /*
  3173. * If the best seen length is less than the request length,
  3174. * use the best as the minimum.
  3175. */
  3176. args->minlen = *blen;
  3177. } else {
  3178. /*
  3179. * Otherwise we've seen an extent as big as maxlen, use that
  3180. * as the minimum.
  3181. */
  3182. args->minlen = args->maxlen;
  3183. }
  3184. }
  3185. STATIC int
  3186. xfs_bmap_btalloc_nullfb(
  3187. struct xfs_bmalloca *ap,
  3188. struct xfs_alloc_arg *args,
  3189. xfs_extlen_t *blen)
  3190. {
  3191. struct xfs_mount *mp = ap->ip->i_mount;
  3192. xfs_agnumber_t ag, startag;
  3193. int notinit = 0;
  3194. int error;
  3195. args->type = XFS_ALLOCTYPE_START_BNO;
  3196. args->total = ap->total;
  3197. startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
  3198. if (startag == NULLAGNUMBER)
  3199. startag = ag = 0;
  3200. while (*blen < args->maxlen) {
  3201. error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
  3202. &notinit);
  3203. if (error)
  3204. return error;
  3205. if (++ag == mp->m_sb.sb_agcount)
  3206. ag = 0;
  3207. if (ag == startag)
  3208. break;
  3209. }
  3210. xfs_bmap_select_minlen(ap, args, blen, notinit);
  3211. return 0;
  3212. }
  3213. STATIC int
  3214. xfs_bmap_btalloc_filestreams(
  3215. struct xfs_bmalloca *ap,
  3216. struct xfs_alloc_arg *args,
  3217. xfs_extlen_t *blen)
  3218. {
  3219. struct xfs_mount *mp = ap->ip->i_mount;
  3220. xfs_agnumber_t ag;
  3221. int notinit = 0;
  3222. int error;
  3223. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  3224. args->total = ap->total;
  3225. ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
  3226. if (ag == NULLAGNUMBER)
  3227. ag = 0;
  3228. error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
  3229. if (error)
  3230. return error;
  3231. if (*blen < args->maxlen) {
  3232. error = xfs_filestream_new_ag(ap, &ag);
  3233. if (error)
  3234. return error;
  3235. error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
  3236. &notinit);
  3237. if (error)
  3238. return error;
  3239. }
  3240. xfs_bmap_select_minlen(ap, args, blen, notinit);
  3241. /*
  3242. * Set the failure fallback case to look in the selected AG as stream
  3243. * may have moved.
  3244. */
  3245. ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
  3246. return 0;
  3247. }
  3248. STATIC int
  3249. xfs_bmap_btalloc(
  3250. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  3251. {
  3252. xfs_mount_t *mp; /* mount point structure */
  3253. xfs_alloctype_t atype = 0; /* type for allocation routines */
  3254. xfs_extlen_t align = 0; /* minimum allocation alignment */
  3255. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  3256. xfs_agnumber_t ag;
  3257. xfs_alloc_arg_t args;
  3258. xfs_extlen_t blen;
  3259. xfs_extlen_t nextminlen = 0;
  3260. int nullfb; /* true if ap->firstblock isn't set */
  3261. int isaligned;
  3262. int tryagain;
  3263. int error;
  3264. int stripe_align;
  3265. ASSERT(ap->length);
  3266. mp = ap->ip->i_mount;
  3267. /* stripe alignment for allocation is determined by mount parameters */
  3268. stripe_align = 0;
  3269. if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
  3270. stripe_align = mp->m_swidth;
  3271. else if (mp->m_dalign)
  3272. stripe_align = mp->m_dalign;
  3273. if (ap->flags & XFS_BMAPI_COWFORK)
  3274. align = xfs_get_cowextsz_hint(ap->ip);
  3275. else if (xfs_alloc_is_userdata(ap->datatype))
  3276. align = xfs_get_extsz_hint(ap->ip);
  3277. if (align) {
  3278. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
  3279. align, 0, ap->eof, 0, ap->conv,
  3280. &ap->offset, &ap->length);
  3281. ASSERT(!error);
  3282. ASSERT(ap->length);
  3283. }
  3284. nullfb = *ap->firstblock == NULLFSBLOCK;
  3285. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
  3286. if (nullfb) {
  3287. if (xfs_alloc_is_userdata(ap->datatype) &&
  3288. xfs_inode_is_filestream(ap->ip)) {
  3289. ag = xfs_filestream_lookup_ag(ap->ip);
  3290. ag = (ag != NULLAGNUMBER) ? ag : 0;
  3291. ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
  3292. } else {
  3293. ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
  3294. }
  3295. } else
  3296. ap->blkno = *ap->firstblock;
  3297. xfs_bmap_adjacent(ap);
  3298. /*
  3299. * If allowed, use ap->blkno; otherwise must use firstblock since
  3300. * it's in the right allocation group.
  3301. */
  3302. if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
  3303. ;
  3304. else
  3305. ap->blkno = *ap->firstblock;
  3306. /*
  3307. * Normal allocation, done through xfs_alloc_vextent.
  3308. */
  3309. tryagain = isaligned = 0;
  3310. memset(&args, 0, sizeof(args));
  3311. args.tp = ap->tp;
  3312. args.mp = mp;
  3313. args.fsbno = ap->blkno;
  3314. xfs_rmap_skip_owner_update(&args.oinfo);
  3315. /* Trim the allocation back to the maximum an AG can fit. */
  3316. args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
  3317. args.firstblock = *ap->firstblock;
  3318. blen = 0;
  3319. if (nullfb) {
  3320. /*
  3321. * Search for an allocation group with a single extent large
  3322. * enough for the request. If one isn't found, then adjust
  3323. * the minimum allocation size to the largest space found.
  3324. */
  3325. if (xfs_alloc_is_userdata(ap->datatype) &&
  3326. xfs_inode_is_filestream(ap->ip))
  3327. error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
  3328. else
  3329. error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
  3330. if (error)
  3331. return error;
  3332. } else if (ap->dfops->dop_low) {
  3333. if (xfs_inode_is_filestream(ap->ip))
  3334. args.type = XFS_ALLOCTYPE_FIRST_AG;
  3335. else
  3336. args.type = XFS_ALLOCTYPE_START_BNO;
  3337. args.total = args.minlen = ap->minlen;
  3338. } else {
  3339. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3340. args.total = ap->total;
  3341. args.minlen = ap->minlen;
  3342. }
  3343. /* apply extent size hints if obtained earlier */
  3344. if (align) {
  3345. args.prod = align;
  3346. if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
  3347. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  3348. } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
  3349. args.prod = 1;
  3350. args.mod = 0;
  3351. } else {
  3352. args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
  3353. if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
  3354. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  3355. }
  3356. /*
  3357. * If we are not low on available data blocks, and the
  3358. * underlying logical volume manager is a stripe, and
  3359. * the file offset is zero then try to allocate data
  3360. * blocks on stripe unit boundary.
  3361. * NOTE: ap->aeof is only set if the allocation length
  3362. * is >= the stripe unit and the allocation offset is
  3363. * at the end of file.
  3364. */
  3365. if (!ap->dfops->dop_low && ap->aeof) {
  3366. if (!ap->offset) {
  3367. args.alignment = stripe_align;
  3368. atype = args.type;
  3369. isaligned = 1;
  3370. /*
  3371. * Adjust for alignment
  3372. */
  3373. if (blen > args.alignment && blen <= args.maxlen)
  3374. args.minlen = blen - args.alignment;
  3375. args.minalignslop = 0;
  3376. } else {
  3377. /*
  3378. * First try an exact bno allocation.
  3379. * If it fails then do a near or start bno
  3380. * allocation with alignment turned on.
  3381. */
  3382. atype = args.type;
  3383. tryagain = 1;
  3384. args.type = XFS_ALLOCTYPE_THIS_BNO;
  3385. args.alignment = 1;
  3386. /*
  3387. * Compute the minlen+alignment for the
  3388. * next case. Set slop so that the value
  3389. * of minlen+alignment+slop doesn't go up
  3390. * between the calls.
  3391. */
  3392. if (blen > stripe_align && blen <= args.maxlen)
  3393. nextminlen = blen - stripe_align;
  3394. else
  3395. nextminlen = args.minlen;
  3396. if (nextminlen + stripe_align > args.minlen + 1)
  3397. args.minalignslop =
  3398. nextminlen + stripe_align -
  3399. args.minlen - 1;
  3400. else
  3401. args.minalignslop = 0;
  3402. }
  3403. } else {
  3404. args.alignment = 1;
  3405. args.minalignslop = 0;
  3406. }
  3407. args.minleft = ap->minleft;
  3408. args.wasdel = ap->wasdel;
  3409. args.resv = XFS_AG_RESV_NONE;
  3410. args.datatype = ap->datatype;
  3411. if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
  3412. args.ip = ap->ip;
  3413. error = xfs_alloc_vextent(&args);
  3414. if (error)
  3415. return error;
  3416. if (tryagain && args.fsbno == NULLFSBLOCK) {
  3417. /*
  3418. * Exact allocation failed. Now try with alignment
  3419. * turned on.
  3420. */
  3421. args.type = atype;
  3422. args.fsbno = ap->blkno;
  3423. args.alignment = stripe_align;
  3424. args.minlen = nextminlen;
  3425. args.minalignslop = 0;
  3426. isaligned = 1;
  3427. if ((error = xfs_alloc_vextent(&args)))
  3428. return error;
  3429. }
  3430. if (isaligned && args.fsbno == NULLFSBLOCK) {
  3431. /*
  3432. * allocation failed, so turn off alignment and
  3433. * try again.
  3434. */
  3435. args.type = atype;
  3436. args.fsbno = ap->blkno;
  3437. args.alignment = 0;
  3438. if ((error = xfs_alloc_vextent(&args)))
  3439. return error;
  3440. }
  3441. if (args.fsbno == NULLFSBLOCK && nullfb &&
  3442. args.minlen > ap->minlen) {
  3443. args.minlen = ap->minlen;
  3444. args.type = XFS_ALLOCTYPE_START_BNO;
  3445. args.fsbno = ap->blkno;
  3446. if ((error = xfs_alloc_vextent(&args)))
  3447. return error;
  3448. }
  3449. if (args.fsbno == NULLFSBLOCK && nullfb) {
  3450. args.fsbno = 0;
  3451. args.type = XFS_ALLOCTYPE_FIRST_AG;
  3452. args.total = ap->minlen;
  3453. if ((error = xfs_alloc_vextent(&args)))
  3454. return error;
  3455. ap->dfops->dop_low = true;
  3456. }
  3457. if (args.fsbno != NULLFSBLOCK) {
  3458. /*
  3459. * check the allocation happened at the same or higher AG than
  3460. * the first block that was allocated.
  3461. */
  3462. ASSERT(*ap->firstblock == NULLFSBLOCK ||
  3463. XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
  3464. XFS_FSB_TO_AGNO(mp, args.fsbno));
  3465. ap->blkno = args.fsbno;
  3466. if (*ap->firstblock == NULLFSBLOCK)
  3467. *ap->firstblock = args.fsbno;
  3468. ASSERT(nullfb || fb_agno <= args.agno);
  3469. ap->length = args.len;
  3470. if (!(ap->flags & XFS_BMAPI_COWFORK))
  3471. ap->ip->i_d.di_nblocks += args.len;
  3472. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  3473. if (ap->wasdel)
  3474. ap->ip->i_delayed_blks -= args.len;
  3475. /*
  3476. * Adjust the disk quota also. This was reserved
  3477. * earlier.
  3478. */
  3479. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  3480. ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
  3481. XFS_TRANS_DQ_BCOUNT,
  3482. (long) args.len);
  3483. } else {
  3484. ap->blkno = NULLFSBLOCK;
  3485. ap->length = 0;
  3486. }
  3487. return 0;
  3488. }
  3489. /*
  3490. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  3491. * It figures out where to ask the underlying allocator to put the new extent.
  3492. */
  3493. STATIC int
  3494. xfs_bmap_alloc(
  3495. struct xfs_bmalloca *ap) /* bmap alloc argument struct */
  3496. {
  3497. if (XFS_IS_REALTIME_INODE(ap->ip) &&
  3498. xfs_alloc_is_userdata(ap->datatype))
  3499. return xfs_bmap_rtalloc(ap);
  3500. return xfs_bmap_btalloc(ap);
  3501. }
  3502. /* Trim extent to fit a logical block range. */
  3503. void
  3504. xfs_trim_extent(
  3505. struct xfs_bmbt_irec *irec,
  3506. xfs_fileoff_t bno,
  3507. xfs_filblks_t len)
  3508. {
  3509. xfs_fileoff_t distance;
  3510. xfs_fileoff_t end = bno + len;
  3511. if (irec->br_startoff + irec->br_blockcount <= bno ||
  3512. irec->br_startoff >= end) {
  3513. irec->br_blockcount = 0;
  3514. return;
  3515. }
  3516. if (irec->br_startoff < bno) {
  3517. distance = bno - irec->br_startoff;
  3518. if (isnullstartblock(irec->br_startblock))
  3519. irec->br_startblock = DELAYSTARTBLOCK;
  3520. if (irec->br_startblock != DELAYSTARTBLOCK &&
  3521. irec->br_startblock != HOLESTARTBLOCK)
  3522. irec->br_startblock += distance;
  3523. irec->br_startoff += distance;
  3524. irec->br_blockcount -= distance;
  3525. }
  3526. if (end < irec->br_startoff + irec->br_blockcount) {
  3527. distance = irec->br_startoff + irec->br_blockcount - end;
  3528. irec->br_blockcount -= distance;
  3529. }
  3530. }
  3531. /* trim extent to within eof */
  3532. void
  3533. xfs_trim_extent_eof(
  3534. struct xfs_bmbt_irec *irec,
  3535. struct xfs_inode *ip)
  3536. {
  3537. xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
  3538. i_size_read(VFS_I(ip))));
  3539. }
  3540. /*
  3541. * Trim the returned map to the required bounds
  3542. */
  3543. STATIC void
  3544. xfs_bmapi_trim_map(
  3545. struct xfs_bmbt_irec *mval,
  3546. struct xfs_bmbt_irec *got,
  3547. xfs_fileoff_t *bno,
  3548. xfs_filblks_t len,
  3549. xfs_fileoff_t obno,
  3550. xfs_fileoff_t end,
  3551. int n,
  3552. int flags)
  3553. {
  3554. if ((flags & XFS_BMAPI_ENTIRE) ||
  3555. got->br_startoff + got->br_blockcount <= obno) {
  3556. *mval = *got;
  3557. if (isnullstartblock(got->br_startblock))
  3558. mval->br_startblock = DELAYSTARTBLOCK;
  3559. return;
  3560. }
  3561. if (obno > *bno)
  3562. *bno = obno;
  3563. ASSERT((*bno >= obno) || (n == 0));
  3564. ASSERT(*bno < end);
  3565. mval->br_startoff = *bno;
  3566. if (isnullstartblock(got->br_startblock))
  3567. mval->br_startblock = DELAYSTARTBLOCK;
  3568. else
  3569. mval->br_startblock = got->br_startblock +
  3570. (*bno - got->br_startoff);
  3571. /*
  3572. * Return the minimum of what we got and what we asked for for
  3573. * the length. We can use the len variable here because it is
  3574. * modified below and we could have been there before coming
  3575. * here if the first part of the allocation didn't overlap what
  3576. * was asked for.
  3577. */
  3578. mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
  3579. got->br_blockcount - (*bno - got->br_startoff));
  3580. mval->br_state = got->br_state;
  3581. ASSERT(mval->br_blockcount <= len);
  3582. return;
  3583. }
  3584. /*
  3585. * Update and validate the extent map to return
  3586. */
  3587. STATIC void
  3588. xfs_bmapi_update_map(
  3589. struct xfs_bmbt_irec **map,
  3590. xfs_fileoff_t *bno,
  3591. xfs_filblks_t *len,
  3592. xfs_fileoff_t obno,
  3593. xfs_fileoff_t end,
  3594. int *n,
  3595. int flags)
  3596. {
  3597. xfs_bmbt_irec_t *mval = *map;
  3598. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  3599. ((mval->br_startoff + mval->br_blockcount) <= end));
  3600. ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
  3601. (mval->br_startoff < obno));
  3602. *bno = mval->br_startoff + mval->br_blockcount;
  3603. *len = end - *bno;
  3604. if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
  3605. /* update previous map with new information */
  3606. ASSERT(mval->br_startblock == mval[-1].br_startblock);
  3607. ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
  3608. ASSERT(mval->br_state == mval[-1].br_state);
  3609. mval[-1].br_blockcount = mval->br_blockcount;
  3610. mval[-1].br_state = mval->br_state;
  3611. } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
  3612. mval[-1].br_startblock != DELAYSTARTBLOCK &&
  3613. mval[-1].br_startblock != HOLESTARTBLOCK &&
  3614. mval->br_startblock == mval[-1].br_startblock +
  3615. mval[-1].br_blockcount &&
  3616. ((flags & XFS_BMAPI_IGSTATE) ||
  3617. mval[-1].br_state == mval->br_state)) {
  3618. ASSERT(mval->br_startoff ==
  3619. mval[-1].br_startoff + mval[-1].br_blockcount);
  3620. mval[-1].br_blockcount += mval->br_blockcount;
  3621. } else if (*n > 0 &&
  3622. mval->br_startblock == DELAYSTARTBLOCK &&
  3623. mval[-1].br_startblock == DELAYSTARTBLOCK &&
  3624. mval->br_startoff ==
  3625. mval[-1].br_startoff + mval[-1].br_blockcount) {
  3626. mval[-1].br_blockcount += mval->br_blockcount;
  3627. mval[-1].br_state = mval->br_state;
  3628. } else if (!((*n == 0) &&
  3629. ((mval->br_startoff + mval->br_blockcount) <=
  3630. obno))) {
  3631. mval++;
  3632. (*n)++;
  3633. }
  3634. *map = mval;
  3635. }
  3636. /*
  3637. * Map file blocks to filesystem blocks without allocation.
  3638. */
  3639. int
  3640. xfs_bmapi_read(
  3641. struct xfs_inode *ip,
  3642. xfs_fileoff_t bno,
  3643. xfs_filblks_t len,
  3644. struct xfs_bmbt_irec *mval,
  3645. int *nmap,
  3646. int flags)
  3647. {
  3648. struct xfs_mount *mp = ip->i_mount;
  3649. struct xfs_ifork *ifp;
  3650. struct xfs_bmbt_irec got;
  3651. xfs_fileoff_t obno;
  3652. xfs_fileoff_t end;
  3653. xfs_extnum_t idx;
  3654. int error;
  3655. bool eof = false;
  3656. int n = 0;
  3657. int whichfork = xfs_bmapi_whichfork(flags);
  3658. ASSERT(*nmap >= 1);
  3659. ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
  3660. XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
  3661. ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
  3662. if (unlikely(XFS_TEST_ERROR(
  3663. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  3664. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  3665. mp, XFS_ERRTAG_BMAPIFORMAT))) {
  3666. XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
  3667. return -EFSCORRUPTED;
  3668. }
  3669. if (XFS_FORCED_SHUTDOWN(mp))
  3670. return -EIO;
  3671. XFS_STATS_INC(mp, xs_blk_mapr);
  3672. ifp = XFS_IFORK_PTR(ip, whichfork);
  3673. /* No CoW fork? Return a hole. */
  3674. if (whichfork == XFS_COW_FORK && !ifp) {
  3675. mval->br_startoff = bno;
  3676. mval->br_startblock = HOLESTARTBLOCK;
  3677. mval->br_blockcount = len;
  3678. mval->br_state = XFS_EXT_NORM;
  3679. *nmap = 1;
  3680. return 0;
  3681. }
  3682. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  3683. error = xfs_iread_extents(NULL, ip, whichfork);
  3684. if (error)
  3685. return error;
  3686. }
  3687. if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
  3688. eof = true;
  3689. end = bno + len;
  3690. obno = bno;
  3691. while (bno < end && n < *nmap) {
  3692. /* Reading past eof, act as though there's a hole up to end. */
  3693. if (eof)
  3694. got.br_startoff = end;
  3695. if (got.br_startoff > bno) {
  3696. /* Reading in a hole. */
  3697. mval->br_startoff = bno;
  3698. mval->br_startblock = HOLESTARTBLOCK;
  3699. mval->br_blockcount =
  3700. XFS_FILBLKS_MIN(len, got.br_startoff - bno);
  3701. mval->br_state = XFS_EXT_NORM;
  3702. bno += mval->br_blockcount;
  3703. len -= mval->br_blockcount;
  3704. mval++;
  3705. n++;
  3706. continue;
  3707. }
  3708. /* set up the extent map to return. */
  3709. xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
  3710. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  3711. /* If we're done, stop now. */
  3712. if (bno >= end || n >= *nmap)
  3713. break;
  3714. /* Else go on to the next record. */
  3715. if (!xfs_iext_get_extent(ifp, ++idx, &got))
  3716. eof = true;
  3717. }
  3718. *nmap = n;
  3719. return 0;
  3720. }
  3721. /*
  3722. * Add a delayed allocation extent to an inode. Blocks are reserved from the
  3723. * global pool and the extent inserted into the inode in-core extent tree.
  3724. *
  3725. * On entry, got refers to the first extent beyond the offset of the extent to
  3726. * allocate or eof is specified if no such extent exists. On return, got refers
  3727. * to the extent record that was inserted to the inode fork.
  3728. *
  3729. * Note that the allocated extent may have been merged with contiguous extents
  3730. * during insertion into the inode fork. Thus, got does not reflect the current
  3731. * state of the inode fork on return. If necessary, the caller can use lastx to
  3732. * look up the updated record in the inode fork.
  3733. */
  3734. int
  3735. xfs_bmapi_reserve_delalloc(
  3736. struct xfs_inode *ip,
  3737. int whichfork,
  3738. xfs_fileoff_t off,
  3739. xfs_filblks_t len,
  3740. xfs_filblks_t prealloc,
  3741. struct xfs_bmbt_irec *got,
  3742. xfs_extnum_t *lastx,
  3743. int eof)
  3744. {
  3745. struct xfs_mount *mp = ip->i_mount;
  3746. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  3747. xfs_extlen_t alen;
  3748. xfs_extlen_t indlen;
  3749. char rt = XFS_IS_REALTIME_INODE(ip);
  3750. xfs_extlen_t extsz;
  3751. int error;
  3752. xfs_fileoff_t aoff = off;
  3753. /*
  3754. * Cap the alloc length. Keep track of prealloc so we know whether to
  3755. * tag the inode before we return.
  3756. */
  3757. alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
  3758. if (!eof)
  3759. alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
  3760. if (prealloc && alen >= len)
  3761. prealloc = alen - len;
  3762. /* Figure out the extent size, adjust alen */
  3763. if (whichfork == XFS_COW_FORK)
  3764. extsz = xfs_get_cowextsz_hint(ip);
  3765. else
  3766. extsz = xfs_get_extsz_hint(ip);
  3767. if (extsz) {
  3768. struct xfs_bmbt_irec prev;
  3769. if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
  3770. prev.br_startoff = NULLFILEOFF;
  3771. error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
  3772. 1, 0, &aoff, &alen);
  3773. ASSERT(!error);
  3774. }
  3775. if (rt)
  3776. extsz = alen / mp->m_sb.sb_rextsize;
  3777. /*
  3778. * Make a transaction-less quota reservation for delayed allocation
  3779. * blocks. This number gets adjusted later. We return if we haven't
  3780. * allocated blocks already inside this loop.
  3781. */
  3782. error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
  3783. rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  3784. if (error)
  3785. return error;
  3786. /*
  3787. * Split changing sb for alen and indlen since they could be coming
  3788. * from different places.
  3789. */
  3790. indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
  3791. ASSERT(indlen > 0);
  3792. if (rt) {
  3793. error = xfs_mod_frextents(mp, -((int64_t)extsz));
  3794. } else {
  3795. error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
  3796. }
  3797. if (error)
  3798. goto out_unreserve_quota;
  3799. error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
  3800. if (error)
  3801. goto out_unreserve_blocks;
  3802. ip->i_delayed_blks += alen;
  3803. got->br_startoff = aoff;
  3804. got->br_startblock = nullstartblock(indlen);
  3805. got->br_blockcount = alen;
  3806. got->br_state = XFS_EXT_NORM;
  3807. xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
  3808. /*
  3809. * Tag the inode if blocks were preallocated. Note that COW fork
  3810. * preallocation can occur at the start or end of the extent, even when
  3811. * prealloc == 0, so we must also check the aligned offset and length.
  3812. */
  3813. if (whichfork == XFS_DATA_FORK && prealloc)
  3814. xfs_inode_set_eofblocks_tag(ip);
  3815. if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
  3816. xfs_inode_set_cowblocks_tag(ip);
  3817. return 0;
  3818. out_unreserve_blocks:
  3819. if (rt)
  3820. xfs_mod_frextents(mp, extsz);
  3821. else
  3822. xfs_mod_fdblocks(mp, alen, false);
  3823. out_unreserve_quota:
  3824. if (XFS_IS_QUOTA_ON(mp))
  3825. xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
  3826. XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  3827. return error;
  3828. }
  3829. static int
  3830. xfs_bmapi_allocate(
  3831. struct xfs_bmalloca *bma)
  3832. {
  3833. struct xfs_mount *mp = bma->ip->i_mount;
  3834. int whichfork = xfs_bmapi_whichfork(bma->flags);
  3835. struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  3836. int tmp_logflags = 0;
  3837. int error;
  3838. ASSERT(bma->length > 0);
  3839. /*
  3840. * For the wasdelay case, we could also just allocate the stuff asked
  3841. * for in this bmap call but that wouldn't be as good.
  3842. */
  3843. if (bma->wasdel) {
  3844. bma->length = (xfs_extlen_t)bma->got.br_blockcount;
  3845. bma->offset = bma->got.br_startoff;
  3846. if (bma->idx)
  3847. xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
  3848. } else {
  3849. bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
  3850. if (!bma->eof)
  3851. bma->length = XFS_FILBLKS_MIN(bma->length,
  3852. bma->got.br_startoff - bma->offset);
  3853. }
  3854. /*
  3855. * Set the data type being allocated. For the data fork, the first data
  3856. * in the file is treated differently to all other allocations. For the
  3857. * attribute fork, we only need to ensure the allocated range is not on
  3858. * the busy list.
  3859. */
  3860. if (!(bma->flags & XFS_BMAPI_METADATA)) {
  3861. bma->datatype = XFS_ALLOC_NOBUSY;
  3862. if (whichfork == XFS_DATA_FORK) {
  3863. if (bma->offset == 0)
  3864. bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
  3865. else
  3866. bma->datatype |= XFS_ALLOC_USERDATA;
  3867. }
  3868. if (bma->flags & XFS_BMAPI_ZERO)
  3869. bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
  3870. }
  3871. bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
  3872. /*
  3873. * Only want to do the alignment at the eof if it is userdata and
  3874. * allocation length is larger than a stripe unit.
  3875. */
  3876. if (mp->m_dalign && bma->length >= mp->m_dalign &&
  3877. !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
  3878. error = xfs_bmap_isaeof(bma, whichfork);
  3879. if (error)
  3880. return error;
  3881. }
  3882. error = xfs_bmap_alloc(bma);
  3883. if (error)
  3884. return error;
  3885. if (bma->cur)
  3886. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  3887. if (bma->blkno == NULLFSBLOCK)
  3888. return 0;
  3889. if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
  3890. bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
  3891. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  3892. bma->cur->bc_private.b.dfops = bma->dfops;
  3893. }
  3894. /*
  3895. * Bump the number of extents we've allocated
  3896. * in this call.
  3897. */
  3898. bma->nallocs++;
  3899. if (bma->cur)
  3900. bma->cur->bc_private.b.flags =
  3901. bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  3902. bma->got.br_startoff = bma->offset;
  3903. bma->got.br_startblock = bma->blkno;
  3904. bma->got.br_blockcount = bma->length;
  3905. bma->got.br_state = XFS_EXT_NORM;
  3906. /*
  3907. * In the data fork, a wasdelay extent has been initialized, so
  3908. * shouldn't be flagged as unwritten.
  3909. *
  3910. * For the cow fork, however, we convert delalloc reservations
  3911. * (extents allocated for speculative preallocation) to
  3912. * allocated unwritten extents, and only convert the unwritten
  3913. * extents to real extents when we're about to write the data.
  3914. */
  3915. if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
  3916. (bma->flags & XFS_BMAPI_PREALLOC) &&
  3917. xfs_sb_version_hasextflgbit(&mp->m_sb))
  3918. bma->got.br_state = XFS_EXT_UNWRITTEN;
  3919. if (bma->wasdel)
  3920. error = xfs_bmap_add_extent_delay_real(bma, whichfork);
  3921. else
  3922. error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
  3923. whichfork, &bma->idx, &bma->cur, &bma->got,
  3924. bma->firstblock, bma->dfops, &bma->logflags);
  3925. bma->logflags |= tmp_logflags;
  3926. if (error)
  3927. return error;
  3928. /*
  3929. * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
  3930. * or xfs_bmap_add_extent_hole_real might have merged it into one of
  3931. * the neighbouring ones.
  3932. */
  3933. xfs_iext_get_extent(ifp, bma->idx, &bma->got);
  3934. ASSERT(bma->got.br_startoff <= bma->offset);
  3935. ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
  3936. bma->offset + bma->length);
  3937. ASSERT(bma->got.br_state == XFS_EXT_NORM ||
  3938. bma->got.br_state == XFS_EXT_UNWRITTEN);
  3939. return 0;
  3940. }
  3941. STATIC int
  3942. xfs_bmapi_convert_unwritten(
  3943. struct xfs_bmalloca *bma,
  3944. struct xfs_bmbt_irec *mval,
  3945. xfs_filblks_t len,
  3946. int flags)
  3947. {
  3948. int whichfork = xfs_bmapi_whichfork(flags);
  3949. struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
  3950. int tmp_logflags = 0;
  3951. int error;
  3952. /* check if we need to do unwritten->real conversion */
  3953. if (mval->br_state == XFS_EXT_UNWRITTEN &&
  3954. (flags & XFS_BMAPI_PREALLOC))
  3955. return 0;
  3956. /* check if we need to do real->unwritten conversion */
  3957. if (mval->br_state == XFS_EXT_NORM &&
  3958. (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
  3959. (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
  3960. return 0;
  3961. /*
  3962. * Modify (by adding) the state flag, if writing.
  3963. */
  3964. ASSERT(mval->br_blockcount <= len);
  3965. if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
  3966. bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
  3967. bma->ip, whichfork);
  3968. bma->cur->bc_private.b.firstblock = *bma->firstblock;
  3969. bma->cur->bc_private.b.dfops = bma->dfops;
  3970. }
  3971. mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
  3972. ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  3973. /*
  3974. * Before insertion into the bmbt, zero the range being converted
  3975. * if required.
  3976. */
  3977. if (flags & XFS_BMAPI_ZERO) {
  3978. error = xfs_zero_extent(bma->ip, mval->br_startblock,
  3979. mval->br_blockcount);
  3980. if (error)
  3981. return error;
  3982. }
  3983. error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
  3984. &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
  3985. &tmp_logflags);
  3986. /*
  3987. * Log the inode core unconditionally in the unwritten extent conversion
  3988. * path because the conversion might not have done so (e.g., if the
  3989. * extent count hasn't changed). We need to make sure the inode is dirty
  3990. * in the transaction for the sake of fsync(), even if nothing has
  3991. * changed, because fsync() will not force the log for this transaction
  3992. * unless it sees the inode pinned.
  3993. *
  3994. * Note: If we're only converting cow fork extents, there aren't
  3995. * any on-disk updates to make, so we don't need to log anything.
  3996. */
  3997. if (whichfork != XFS_COW_FORK)
  3998. bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
  3999. if (error)
  4000. return error;
  4001. /*
  4002. * Update our extent pointer, given that
  4003. * xfs_bmap_add_extent_unwritten_real might have merged it into one
  4004. * of the neighbouring ones.
  4005. */
  4006. xfs_iext_get_extent(ifp, bma->idx, &bma->got);
  4007. /*
  4008. * We may have combined previously unwritten space with written space,
  4009. * so generate another request.
  4010. */
  4011. if (mval->br_blockcount < len)
  4012. return -EAGAIN;
  4013. return 0;
  4014. }
  4015. /*
  4016. * Map file blocks to filesystem blocks, and allocate blocks or convert the
  4017. * extent state if necessary. Details behaviour is controlled by the flags
  4018. * parameter. Only allocates blocks from a single allocation group, to avoid
  4019. * locking problems.
  4020. *
  4021. * The returned value in "firstblock" from the first call in a transaction
  4022. * must be remembered and presented to subsequent calls in "firstblock".
  4023. * An upper bound for the number of blocks to be allocated is supplied to
  4024. * the first call in "total"; if no allocation group has that many free
  4025. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  4026. */
  4027. int
  4028. xfs_bmapi_write(
  4029. struct xfs_trans *tp, /* transaction pointer */
  4030. struct xfs_inode *ip, /* incore inode */
  4031. xfs_fileoff_t bno, /* starting file offs. mapped */
  4032. xfs_filblks_t len, /* length to map in file */
  4033. int flags, /* XFS_BMAPI_... */
  4034. xfs_fsblock_t *firstblock, /* first allocated block
  4035. controls a.g. for allocs */
  4036. xfs_extlen_t total, /* total blocks needed */
  4037. struct xfs_bmbt_irec *mval, /* output: map values */
  4038. int *nmap, /* i/o: mval size/count */
  4039. struct xfs_defer_ops *dfops) /* i/o: list extents to free */
  4040. {
  4041. struct xfs_mount *mp = ip->i_mount;
  4042. struct xfs_ifork *ifp;
  4043. struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
  4044. xfs_fileoff_t end; /* end of mapped file region */
  4045. bool eof = false; /* after the end of extents */
  4046. int error; /* error return */
  4047. int n; /* current extent index */
  4048. xfs_fileoff_t obno; /* old block number (offset) */
  4049. int whichfork; /* data or attr fork */
  4050. #ifdef DEBUG
  4051. xfs_fileoff_t orig_bno; /* original block number value */
  4052. int orig_flags; /* original flags arg value */
  4053. xfs_filblks_t orig_len; /* original value of len arg */
  4054. struct xfs_bmbt_irec *orig_mval; /* original value of mval */
  4055. int orig_nmap; /* original value of *nmap */
  4056. orig_bno = bno;
  4057. orig_len = len;
  4058. orig_flags = flags;
  4059. orig_mval = mval;
  4060. orig_nmap = *nmap;
  4061. #endif
  4062. whichfork = xfs_bmapi_whichfork(flags);
  4063. ASSERT(*nmap >= 1);
  4064. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
  4065. ASSERT(!(flags & XFS_BMAPI_IGSTATE));
  4066. ASSERT(tp != NULL ||
  4067. (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
  4068. (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
  4069. ASSERT(len > 0);
  4070. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
  4071. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  4072. ASSERT(!(flags & XFS_BMAPI_REMAP));
  4073. /* zeroing is for currently only for data extents, not metadata */
  4074. ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
  4075. (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
  4076. /*
  4077. * we can allocate unwritten extents or pre-zero allocated blocks,
  4078. * but it makes no sense to do both at once. This would result in
  4079. * zeroing the unwritten extent twice, but it still being an
  4080. * unwritten extent....
  4081. */
  4082. ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
  4083. (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
  4084. if (unlikely(XFS_TEST_ERROR(
  4085. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4086. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  4087. mp, XFS_ERRTAG_BMAPIFORMAT))) {
  4088. XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
  4089. return -EFSCORRUPTED;
  4090. }
  4091. if (XFS_FORCED_SHUTDOWN(mp))
  4092. return -EIO;
  4093. ifp = XFS_IFORK_PTR(ip, whichfork);
  4094. XFS_STATS_INC(mp, xs_blk_mapw);
  4095. if (*firstblock == NULLFSBLOCK) {
  4096. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
  4097. bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  4098. else
  4099. bma.minleft = 1;
  4100. } else {
  4101. bma.minleft = 0;
  4102. }
  4103. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  4104. error = xfs_iread_extents(tp, ip, whichfork);
  4105. if (error)
  4106. goto error0;
  4107. }
  4108. n = 0;
  4109. end = bno + len;
  4110. obno = bno;
  4111. if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
  4112. eof = true;
  4113. if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
  4114. bma.prev.br_startoff = NULLFILEOFF;
  4115. bma.tp = tp;
  4116. bma.ip = ip;
  4117. bma.total = total;
  4118. bma.datatype = 0;
  4119. bma.dfops = dfops;
  4120. bma.firstblock = firstblock;
  4121. while (bno < end && n < *nmap) {
  4122. bool need_alloc = false, wasdelay = false;
  4123. /* in hole or beyoned EOF? */
  4124. if (eof || bma.got.br_startoff > bno) {
  4125. if (flags & XFS_BMAPI_DELALLOC) {
  4126. /*
  4127. * For the COW fork we can reasonably get a
  4128. * request for converting an extent that races
  4129. * with other threads already having converted
  4130. * part of it, as there converting COW to
  4131. * regular blocks is not protected using the
  4132. * IOLOCK.
  4133. */
  4134. ASSERT(flags & XFS_BMAPI_COWFORK);
  4135. if (!(flags & XFS_BMAPI_COWFORK)) {
  4136. error = -EIO;
  4137. goto error0;
  4138. }
  4139. if (eof || bno >= end)
  4140. break;
  4141. } else {
  4142. need_alloc = true;
  4143. }
  4144. } else if (isnullstartblock(bma.got.br_startblock)) {
  4145. wasdelay = true;
  4146. }
  4147. /*
  4148. * First, deal with the hole before the allocated space
  4149. * that we found, if any.
  4150. */
  4151. if (need_alloc || wasdelay) {
  4152. bma.eof = eof;
  4153. bma.conv = !!(flags & XFS_BMAPI_CONVERT);
  4154. bma.wasdel = wasdelay;
  4155. bma.offset = bno;
  4156. bma.flags = flags;
  4157. /*
  4158. * There's a 32/64 bit type mismatch between the
  4159. * allocation length request (which can be 64 bits in
  4160. * length) and the bma length request, which is
  4161. * xfs_extlen_t and therefore 32 bits. Hence we have to
  4162. * check for 32-bit overflows and handle them here.
  4163. */
  4164. if (len > (xfs_filblks_t)MAXEXTLEN)
  4165. bma.length = MAXEXTLEN;
  4166. else
  4167. bma.length = len;
  4168. ASSERT(len > 0);
  4169. ASSERT(bma.length > 0);
  4170. error = xfs_bmapi_allocate(&bma);
  4171. if (error)
  4172. goto error0;
  4173. if (bma.blkno == NULLFSBLOCK)
  4174. break;
  4175. /*
  4176. * If this is a CoW allocation, record the data in
  4177. * the refcount btree for orphan recovery.
  4178. */
  4179. if (whichfork == XFS_COW_FORK) {
  4180. error = xfs_refcount_alloc_cow_extent(mp, dfops,
  4181. bma.blkno, bma.length);
  4182. if (error)
  4183. goto error0;
  4184. }
  4185. }
  4186. /* Deal with the allocated space we found. */
  4187. xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
  4188. end, n, flags);
  4189. /* Execute unwritten extent conversion if necessary */
  4190. error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
  4191. if (error == -EAGAIN)
  4192. continue;
  4193. if (error)
  4194. goto error0;
  4195. /* update the extent map to return */
  4196. xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
  4197. /*
  4198. * If we're done, stop now. Stop when we've allocated
  4199. * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
  4200. * the transaction may get too big.
  4201. */
  4202. if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
  4203. break;
  4204. /* Else go on to the next record. */
  4205. bma.prev = bma.got;
  4206. if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
  4207. eof = true;
  4208. }
  4209. *nmap = n;
  4210. /*
  4211. * Transform from btree to extents, give it cur.
  4212. */
  4213. if (xfs_bmap_wants_extents(ip, whichfork)) {
  4214. int tmp_logflags = 0;
  4215. ASSERT(bma.cur);
  4216. error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
  4217. &tmp_logflags, whichfork);
  4218. bma.logflags |= tmp_logflags;
  4219. if (error)
  4220. goto error0;
  4221. }
  4222. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
  4223. XFS_IFORK_NEXTENTS(ip, whichfork) >
  4224. XFS_IFORK_MAXEXT(ip, whichfork));
  4225. error = 0;
  4226. error0:
  4227. /*
  4228. * Log everything. Do this after conversion, there's no point in
  4229. * logging the extent records if we've converted to btree format.
  4230. */
  4231. if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
  4232. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4233. bma.logflags &= ~xfs_ilog_fext(whichfork);
  4234. else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
  4235. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  4236. bma.logflags &= ~xfs_ilog_fbroot(whichfork);
  4237. /*
  4238. * Log whatever the flags say, even if error. Otherwise we might miss
  4239. * detecting a case where the data is changed, there's an error,
  4240. * and it's not logged so we don't shutdown when we should.
  4241. */
  4242. if (bma.logflags)
  4243. xfs_trans_log_inode(tp, ip, bma.logflags);
  4244. if (bma.cur) {
  4245. if (!error) {
  4246. ASSERT(*firstblock == NULLFSBLOCK ||
  4247. XFS_FSB_TO_AGNO(mp, *firstblock) <=
  4248. XFS_FSB_TO_AGNO(mp,
  4249. bma.cur->bc_private.b.firstblock));
  4250. *firstblock = bma.cur->bc_private.b.firstblock;
  4251. }
  4252. xfs_btree_del_cursor(bma.cur,
  4253. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4254. }
  4255. if (!error)
  4256. xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
  4257. orig_nmap, *nmap);
  4258. return error;
  4259. }
  4260. static int
  4261. xfs_bmapi_remap(
  4262. struct xfs_trans *tp,
  4263. struct xfs_inode *ip,
  4264. xfs_fileoff_t bno,
  4265. xfs_filblks_t len,
  4266. xfs_fsblock_t startblock,
  4267. struct xfs_defer_ops *dfops)
  4268. {
  4269. struct xfs_mount *mp = ip->i_mount;
  4270. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  4271. struct xfs_btree_cur *cur = NULL;
  4272. xfs_fsblock_t firstblock = NULLFSBLOCK;
  4273. struct xfs_bmbt_irec got;
  4274. xfs_extnum_t idx;
  4275. int logflags = 0, error;
  4276. ASSERT(len > 0);
  4277. ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
  4278. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  4279. if (unlikely(XFS_TEST_ERROR(
  4280. (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
  4281. XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
  4282. mp, XFS_ERRTAG_BMAPIFORMAT))) {
  4283. XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
  4284. return -EFSCORRUPTED;
  4285. }
  4286. if (XFS_FORCED_SHUTDOWN(mp))
  4287. return -EIO;
  4288. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  4289. error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
  4290. if (error)
  4291. return error;
  4292. }
  4293. if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
  4294. /* make sure we only reflink into a hole. */
  4295. ASSERT(got.br_startoff > bno);
  4296. ASSERT(got.br_startoff - bno >= len);
  4297. }
  4298. ip->i_d.di_nblocks += len;
  4299. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  4300. if (ifp->if_flags & XFS_IFBROOT) {
  4301. cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
  4302. cur->bc_private.b.firstblock = firstblock;
  4303. cur->bc_private.b.dfops = dfops;
  4304. cur->bc_private.b.flags = 0;
  4305. }
  4306. got.br_startoff = bno;
  4307. got.br_startblock = startblock;
  4308. got.br_blockcount = len;
  4309. got.br_state = XFS_EXT_NORM;
  4310. error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
  4311. &got, &firstblock, dfops, &logflags);
  4312. if (error)
  4313. goto error0;
  4314. if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
  4315. int tmp_logflags = 0;
  4316. error = xfs_bmap_btree_to_extents(tp, ip, cur,
  4317. &tmp_logflags, XFS_DATA_FORK);
  4318. logflags |= tmp_logflags;
  4319. }
  4320. error0:
  4321. if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
  4322. logflags &= ~XFS_ILOG_DEXT;
  4323. else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
  4324. logflags &= ~XFS_ILOG_DBROOT;
  4325. if (logflags)
  4326. xfs_trans_log_inode(tp, ip, logflags);
  4327. if (cur) {
  4328. xfs_btree_del_cursor(cur,
  4329. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4330. }
  4331. return error;
  4332. }
  4333. /*
  4334. * When a delalloc extent is split (e.g., due to a hole punch), the original
  4335. * indlen reservation must be shared across the two new extents that are left
  4336. * behind.
  4337. *
  4338. * Given the original reservation and the worst case indlen for the two new
  4339. * extents (as calculated by xfs_bmap_worst_indlen()), split the original
  4340. * reservation fairly across the two new extents. If necessary, steal available
  4341. * blocks from a deleted extent to make up a reservation deficiency (e.g., if
  4342. * ores == 1). The number of stolen blocks is returned. The availability and
  4343. * subsequent accounting of stolen blocks is the responsibility of the caller.
  4344. */
  4345. static xfs_filblks_t
  4346. xfs_bmap_split_indlen(
  4347. xfs_filblks_t ores, /* original res. */
  4348. xfs_filblks_t *indlen1, /* ext1 worst indlen */
  4349. xfs_filblks_t *indlen2, /* ext2 worst indlen */
  4350. xfs_filblks_t avail) /* stealable blocks */
  4351. {
  4352. xfs_filblks_t len1 = *indlen1;
  4353. xfs_filblks_t len2 = *indlen2;
  4354. xfs_filblks_t nres = len1 + len2; /* new total res. */
  4355. xfs_filblks_t stolen = 0;
  4356. xfs_filblks_t resfactor;
  4357. /*
  4358. * Steal as many blocks as we can to try and satisfy the worst case
  4359. * indlen for both new extents.
  4360. */
  4361. if (ores < nres && avail)
  4362. stolen = XFS_FILBLKS_MIN(nres - ores, avail);
  4363. ores += stolen;
  4364. /* nothing else to do if we've satisfied the new reservation */
  4365. if (ores >= nres)
  4366. return stolen;
  4367. /*
  4368. * We can't meet the total required reservation for the two extents.
  4369. * Calculate the percent of the overall shortage between both extents
  4370. * and apply this percentage to each of the requested indlen values.
  4371. * This distributes the shortage fairly and reduces the chances that one
  4372. * of the two extents is left with nothing when extents are repeatedly
  4373. * split.
  4374. */
  4375. resfactor = (ores * 100);
  4376. do_div(resfactor, nres);
  4377. len1 *= resfactor;
  4378. do_div(len1, 100);
  4379. len2 *= resfactor;
  4380. do_div(len2, 100);
  4381. ASSERT(len1 + len2 <= ores);
  4382. ASSERT(len1 < *indlen1 && len2 < *indlen2);
  4383. /*
  4384. * Hand out the remainder to each extent. If one of the two reservations
  4385. * is zero, we want to make sure that one gets a block first. The loop
  4386. * below starts with len1, so hand len2 a block right off the bat if it
  4387. * is zero.
  4388. */
  4389. ores -= (len1 + len2);
  4390. ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
  4391. if (ores && !len2 && *indlen2) {
  4392. len2++;
  4393. ores--;
  4394. }
  4395. while (ores) {
  4396. if (len1 < *indlen1) {
  4397. len1++;
  4398. ores--;
  4399. }
  4400. if (!ores)
  4401. break;
  4402. if (len2 < *indlen2) {
  4403. len2++;
  4404. ores--;
  4405. }
  4406. }
  4407. *indlen1 = len1;
  4408. *indlen2 = len2;
  4409. return stolen;
  4410. }
  4411. int
  4412. xfs_bmap_del_extent_delay(
  4413. struct xfs_inode *ip,
  4414. int whichfork,
  4415. xfs_extnum_t *idx,
  4416. struct xfs_bmbt_irec *got,
  4417. struct xfs_bmbt_irec *del)
  4418. {
  4419. struct xfs_mount *mp = ip->i_mount;
  4420. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  4421. struct xfs_bmbt_irec new;
  4422. int64_t da_old, da_new, da_diff = 0;
  4423. xfs_fileoff_t del_endoff, got_endoff;
  4424. xfs_filblks_t got_indlen, new_indlen, stolen;
  4425. int error = 0, state = 0;
  4426. bool isrt;
  4427. XFS_STATS_INC(mp, xs_del_exlist);
  4428. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4429. del_endoff = del->br_startoff + del->br_blockcount;
  4430. got_endoff = got->br_startoff + got->br_blockcount;
  4431. da_old = startblockval(got->br_startblock);
  4432. da_new = 0;
  4433. ASSERT(*idx >= 0);
  4434. ASSERT(*idx <= xfs_iext_count(ifp));
  4435. ASSERT(del->br_blockcount > 0);
  4436. ASSERT(got->br_startoff <= del->br_startoff);
  4437. ASSERT(got_endoff >= del_endoff);
  4438. if (isrt) {
  4439. uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
  4440. do_div(rtexts, mp->m_sb.sb_rextsize);
  4441. xfs_mod_frextents(mp, rtexts);
  4442. }
  4443. /*
  4444. * Update the inode delalloc counter now and wait to update the
  4445. * sb counters as we might have to borrow some blocks for the
  4446. * indirect block accounting.
  4447. */
  4448. error = xfs_trans_reserve_quota_nblks(NULL, ip,
  4449. -((long)del->br_blockcount), 0,
  4450. isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  4451. if (error)
  4452. return error;
  4453. ip->i_delayed_blks -= del->br_blockcount;
  4454. if (whichfork == XFS_COW_FORK)
  4455. state |= BMAP_COWFORK;
  4456. if (got->br_startoff == del->br_startoff)
  4457. state |= BMAP_LEFT_CONTIG;
  4458. if (got_endoff == del_endoff)
  4459. state |= BMAP_RIGHT_CONTIG;
  4460. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  4461. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  4462. /*
  4463. * Matches the whole extent. Delete the entry.
  4464. */
  4465. xfs_iext_remove(ip, *idx, 1, state);
  4466. --*idx;
  4467. break;
  4468. case BMAP_LEFT_CONTIG:
  4469. /*
  4470. * Deleting the first part of the extent.
  4471. */
  4472. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4473. got->br_startoff = del_endoff;
  4474. got->br_blockcount -= del->br_blockcount;
  4475. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
  4476. got->br_blockcount), da_old);
  4477. got->br_startblock = nullstartblock((int)da_new);
  4478. xfs_iext_update_extent(ifp, *idx, got);
  4479. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4480. break;
  4481. case BMAP_RIGHT_CONTIG:
  4482. /*
  4483. * Deleting the last part of the extent.
  4484. */
  4485. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4486. got->br_blockcount = got->br_blockcount - del->br_blockcount;
  4487. da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
  4488. got->br_blockcount), da_old);
  4489. got->br_startblock = nullstartblock((int)da_new);
  4490. xfs_iext_update_extent(ifp, *idx, got);
  4491. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4492. break;
  4493. case 0:
  4494. /*
  4495. * Deleting the middle of the extent.
  4496. *
  4497. * Distribute the original indlen reservation across the two new
  4498. * extents. Steal blocks from the deleted extent if necessary.
  4499. * Stealing blocks simply fudges the fdblocks accounting below.
  4500. * Warn if either of the new indlen reservations is zero as this
  4501. * can lead to delalloc problems.
  4502. */
  4503. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4504. got->br_blockcount = del->br_startoff - got->br_startoff;
  4505. got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
  4506. new.br_blockcount = got_endoff - del_endoff;
  4507. new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
  4508. WARN_ON_ONCE(!got_indlen || !new_indlen);
  4509. stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
  4510. del->br_blockcount);
  4511. got->br_startblock = nullstartblock((int)got_indlen);
  4512. xfs_iext_update_extent(ifp, *idx, got);
  4513. trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
  4514. new.br_startoff = del_endoff;
  4515. new.br_state = got->br_state;
  4516. new.br_startblock = nullstartblock((int)new_indlen);
  4517. ++*idx;
  4518. xfs_iext_insert(ip, *idx, 1, &new, state);
  4519. da_new = got_indlen + new_indlen - stolen;
  4520. del->br_blockcount -= stolen;
  4521. break;
  4522. }
  4523. ASSERT(da_old >= da_new);
  4524. da_diff = da_old - da_new;
  4525. if (!isrt)
  4526. da_diff += del->br_blockcount;
  4527. if (da_diff)
  4528. xfs_mod_fdblocks(mp, da_diff, false);
  4529. return error;
  4530. }
  4531. void
  4532. xfs_bmap_del_extent_cow(
  4533. struct xfs_inode *ip,
  4534. xfs_extnum_t *idx,
  4535. struct xfs_bmbt_irec *got,
  4536. struct xfs_bmbt_irec *del)
  4537. {
  4538. struct xfs_mount *mp = ip->i_mount;
  4539. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
  4540. struct xfs_bmbt_irec new;
  4541. xfs_fileoff_t del_endoff, got_endoff;
  4542. int state = BMAP_COWFORK;
  4543. XFS_STATS_INC(mp, xs_del_exlist);
  4544. del_endoff = del->br_startoff + del->br_blockcount;
  4545. got_endoff = got->br_startoff + got->br_blockcount;
  4546. ASSERT(*idx >= 0);
  4547. ASSERT(*idx <= xfs_iext_count(ifp));
  4548. ASSERT(del->br_blockcount > 0);
  4549. ASSERT(got->br_startoff <= del->br_startoff);
  4550. ASSERT(got_endoff >= del_endoff);
  4551. ASSERT(!isnullstartblock(got->br_startblock));
  4552. if (got->br_startoff == del->br_startoff)
  4553. state |= BMAP_LEFT_CONTIG;
  4554. if (got_endoff == del_endoff)
  4555. state |= BMAP_RIGHT_CONTIG;
  4556. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  4557. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  4558. /*
  4559. * Matches the whole extent. Delete the entry.
  4560. */
  4561. xfs_iext_remove(ip, *idx, 1, state);
  4562. --*idx;
  4563. break;
  4564. case BMAP_LEFT_CONTIG:
  4565. /*
  4566. * Deleting the first part of the extent.
  4567. */
  4568. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4569. got->br_startoff = del_endoff;
  4570. got->br_blockcount -= del->br_blockcount;
  4571. got->br_startblock = del->br_startblock + del->br_blockcount;
  4572. xfs_iext_update_extent(ifp, *idx, got);
  4573. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4574. break;
  4575. case BMAP_RIGHT_CONTIG:
  4576. /*
  4577. * Deleting the last part of the extent.
  4578. */
  4579. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4580. got->br_blockcount -= del->br_blockcount;
  4581. xfs_iext_update_extent(ifp, *idx, got);
  4582. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4583. break;
  4584. case 0:
  4585. /*
  4586. * Deleting the middle of the extent.
  4587. */
  4588. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4589. got->br_blockcount = del->br_startoff - got->br_startoff;
  4590. xfs_iext_update_extent(ifp, *idx, got);
  4591. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4592. new.br_startoff = del_endoff;
  4593. new.br_blockcount = got_endoff - del_endoff;
  4594. new.br_state = got->br_state;
  4595. new.br_startblock = del->br_startblock + del->br_blockcount;
  4596. ++*idx;
  4597. xfs_iext_insert(ip, *idx, 1, &new, state);
  4598. break;
  4599. }
  4600. }
  4601. /*
  4602. * Called by xfs_bmapi to update file extent records and the btree
  4603. * after removing space (or undoing a delayed allocation).
  4604. */
  4605. STATIC int /* error */
  4606. xfs_bmap_del_extent(
  4607. xfs_inode_t *ip, /* incore inode pointer */
  4608. xfs_trans_t *tp, /* current transaction pointer */
  4609. xfs_extnum_t *idx, /* extent number to update/delete */
  4610. struct xfs_defer_ops *dfops, /* list of extents to be freed */
  4611. xfs_btree_cur_t *cur, /* if null, not a btree */
  4612. xfs_bmbt_irec_t *del, /* data to remove from extents */
  4613. int *logflagsp, /* inode logging flags */
  4614. int whichfork, /* data or attr fork */
  4615. int bflags) /* bmapi flags */
  4616. {
  4617. xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
  4618. xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
  4619. xfs_fsblock_t del_endblock=0; /* first block past del */
  4620. xfs_fileoff_t del_endoff; /* first offset past del */
  4621. int delay; /* current block is delayed allocated */
  4622. int do_fx; /* free extent at end of routine */
  4623. xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
  4624. int error; /* error return value */
  4625. int flags; /* inode logging flags */
  4626. xfs_bmbt_irec_t got; /* current extent entry */
  4627. xfs_fileoff_t got_endoff; /* first offset past got */
  4628. int i; /* temp state */
  4629. xfs_ifork_t *ifp; /* inode fork pointer */
  4630. xfs_mount_t *mp; /* mount structure */
  4631. xfs_filblks_t nblks; /* quota/sb block count */
  4632. xfs_bmbt_irec_t new; /* new record to be inserted */
  4633. /* REFERENCED */
  4634. uint qfield; /* quota field to update */
  4635. xfs_filblks_t temp; /* for indirect length calculations */
  4636. xfs_filblks_t temp2; /* for indirect length calculations */
  4637. int state = 0;
  4638. mp = ip->i_mount;
  4639. XFS_STATS_INC(mp, xs_del_exlist);
  4640. if (whichfork == XFS_ATTR_FORK)
  4641. state |= BMAP_ATTRFORK;
  4642. else if (whichfork == XFS_COW_FORK)
  4643. state |= BMAP_COWFORK;
  4644. ifp = XFS_IFORK_PTR(ip, whichfork);
  4645. ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
  4646. ASSERT(del->br_blockcount > 0);
  4647. ep = xfs_iext_get_ext(ifp, *idx);
  4648. xfs_bmbt_get_all(ep, &got);
  4649. ASSERT(got.br_startoff <= del->br_startoff);
  4650. del_endoff = del->br_startoff + del->br_blockcount;
  4651. got_endoff = got.br_startoff + got.br_blockcount;
  4652. ASSERT(got_endoff >= del_endoff);
  4653. delay = isnullstartblock(got.br_startblock);
  4654. ASSERT(isnullstartblock(del->br_startblock) == delay);
  4655. flags = 0;
  4656. qfield = 0;
  4657. error = 0;
  4658. /*
  4659. * If deleting a real allocation, must free up the disk space.
  4660. */
  4661. if (!delay) {
  4662. flags = XFS_ILOG_CORE;
  4663. /*
  4664. * Realtime allocation. Free it and record di_nblocks update.
  4665. */
  4666. if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
  4667. xfs_fsblock_t bno;
  4668. xfs_filblks_t len;
  4669. ASSERT(do_mod(del->br_blockcount,
  4670. mp->m_sb.sb_rextsize) == 0);
  4671. ASSERT(do_mod(del->br_startblock,
  4672. mp->m_sb.sb_rextsize) == 0);
  4673. bno = del->br_startblock;
  4674. len = del->br_blockcount;
  4675. do_div(bno, mp->m_sb.sb_rextsize);
  4676. do_div(len, mp->m_sb.sb_rextsize);
  4677. error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
  4678. if (error)
  4679. goto done;
  4680. do_fx = 0;
  4681. nblks = len * mp->m_sb.sb_rextsize;
  4682. qfield = XFS_TRANS_DQ_RTBCOUNT;
  4683. }
  4684. /*
  4685. * Ordinary allocation.
  4686. */
  4687. else {
  4688. do_fx = 1;
  4689. nblks = del->br_blockcount;
  4690. qfield = XFS_TRANS_DQ_BCOUNT;
  4691. }
  4692. /*
  4693. * Set up del_endblock and cur for later.
  4694. */
  4695. del_endblock = del->br_startblock + del->br_blockcount;
  4696. if (cur) {
  4697. if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  4698. got.br_startblock, got.br_blockcount,
  4699. &i)))
  4700. goto done;
  4701. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  4702. }
  4703. da_old = da_new = 0;
  4704. } else {
  4705. da_old = startblockval(got.br_startblock);
  4706. da_new = 0;
  4707. nblks = 0;
  4708. do_fx = 0;
  4709. }
  4710. /*
  4711. * Set flag value to use in switch statement.
  4712. * Left-contig is 2, right-contig is 1.
  4713. */
  4714. switch (((got.br_startoff == del->br_startoff) << 1) |
  4715. (got_endoff == del_endoff)) {
  4716. case 3:
  4717. /*
  4718. * Matches the whole extent. Delete the entry.
  4719. */
  4720. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4721. xfs_iext_remove(ip, *idx, 1,
  4722. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
  4723. --*idx;
  4724. if (delay)
  4725. break;
  4726. XFS_IFORK_NEXT_SET(ip, whichfork,
  4727. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  4728. flags |= XFS_ILOG_CORE;
  4729. if (!cur) {
  4730. flags |= xfs_ilog_fext(whichfork);
  4731. break;
  4732. }
  4733. if ((error = xfs_btree_delete(cur, &i)))
  4734. goto done;
  4735. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  4736. break;
  4737. case 2:
  4738. /*
  4739. * Deleting the first part of the extent.
  4740. */
  4741. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4742. xfs_bmbt_set_startoff(ep, del_endoff);
  4743. temp = got.br_blockcount - del->br_blockcount;
  4744. xfs_bmbt_set_blockcount(ep, temp);
  4745. if (delay) {
  4746. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  4747. da_old);
  4748. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  4749. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4750. da_new = temp;
  4751. break;
  4752. }
  4753. xfs_bmbt_set_startblock(ep, del_endblock);
  4754. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4755. if (!cur) {
  4756. flags |= xfs_ilog_fext(whichfork);
  4757. break;
  4758. }
  4759. if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
  4760. got.br_blockcount - del->br_blockcount,
  4761. got.br_state)))
  4762. goto done;
  4763. break;
  4764. case 1:
  4765. /*
  4766. * Deleting the last part of the extent.
  4767. */
  4768. temp = got.br_blockcount - del->br_blockcount;
  4769. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4770. xfs_bmbt_set_blockcount(ep, temp);
  4771. if (delay) {
  4772. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  4773. da_old);
  4774. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  4775. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4776. da_new = temp;
  4777. break;
  4778. }
  4779. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4780. if (!cur) {
  4781. flags |= xfs_ilog_fext(whichfork);
  4782. break;
  4783. }
  4784. if ((error = xfs_bmbt_update(cur, got.br_startoff,
  4785. got.br_startblock,
  4786. got.br_blockcount - del->br_blockcount,
  4787. got.br_state)))
  4788. goto done;
  4789. break;
  4790. case 0:
  4791. /*
  4792. * Deleting the middle of the extent.
  4793. */
  4794. temp = del->br_startoff - got.br_startoff;
  4795. trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
  4796. xfs_bmbt_set_blockcount(ep, temp);
  4797. new.br_startoff = del_endoff;
  4798. temp2 = got_endoff - del_endoff;
  4799. new.br_blockcount = temp2;
  4800. new.br_state = got.br_state;
  4801. if (!delay) {
  4802. new.br_startblock = del_endblock;
  4803. flags |= XFS_ILOG_CORE;
  4804. if (cur) {
  4805. if ((error = xfs_bmbt_update(cur,
  4806. got.br_startoff,
  4807. got.br_startblock, temp,
  4808. got.br_state)))
  4809. goto done;
  4810. if ((error = xfs_btree_increment(cur, 0, &i)))
  4811. goto done;
  4812. cur->bc_rec.b = new;
  4813. error = xfs_btree_insert(cur, &i);
  4814. if (error && error != -ENOSPC)
  4815. goto done;
  4816. /*
  4817. * If get no-space back from btree insert,
  4818. * it tried a split, and we have a zero
  4819. * block reservation.
  4820. * Fix up our state and return the error.
  4821. */
  4822. if (error == -ENOSPC) {
  4823. /*
  4824. * Reset the cursor, don't trust
  4825. * it after any insert operation.
  4826. */
  4827. if ((error = xfs_bmbt_lookup_eq(cur,
  4828. got.br_startoff,
  4829. got.br_startblock,
  4830. temp, &i)))
  4831. goto done;
  4832. XFS_WANT_CORRUPTED_GOTO(mp,
  4833. i == 1, done);
  4834. /*
  4835. * Update the btree record back
  4836. * to the original value.
  4837. */
  4838. if ((error = xfs_bmbt_update(cur,
  4839. got.br_startoff,
  4840. got.br_startblock,
  4841. got.br_blockcount,
  4842. got.br_state)))
  4843. goto done;
  4844. /*
  4845. * Reset the extent record back
  4846. * to the original value.
  4847. */
  4848. xfs_bmbt_set_blockcount(ep,
  4849. got.br_blockcount);
  4850. flags = 0;
  4851. error = -ENOSPC;
  4852. goto done;
  4853. }
  4854. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
  4855. } else
  4856. flags |= xfs_ilog_fext(whichfork);
  4857. XFS_IFORK_NEXT_SET(ip, whichfork,
  4858. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  4859. } else {
  4860. xfs_filblks_t stolen;
  4861. ASSERT(whichfork == XFS_DATA_FORK);
  4862. /*
  4863. * Distribute the original indlen reservation across the
  4864. * two new extents. Steal blocks from the deleted extent
  4865. * if necessary. Stealing blocks simply fudges the
  4866. * fdblocks accounting in xfs_bunmapi().
  4867. */
  4868. temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
  4869. temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
  4870. stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
  4871. del->br_blockcount);
  4872. da_new = temp + temp2 - stolen;
  4873. del->br_blockcount -= stolen;
  4874. /*
  4875. * Set the reservation for each extent. Warn if either
  4876. * is zero as this can lead to delalloc problems.
  4877. */
  4878. WARN_ON_ONCE(!temp || !temp2);
  4879. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  4880. new.br_startblock = nullstartblock((int)temp2);
  4881. }
  4882. trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
  4883. xfs_iext_insert(ip, *idx + 1, 1, &new, state);
  4884. ++*idx;
  4885. break;
  4886. }
  4887. /* remove reverse mapping */
  4888. if (!delay) {
  4889. error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
  4890. if (error)
  4891. goto done;
  4892. }
  4893. /*
  4894. * If we need to, add to list of extents to delete.
  4895. */
  4896. if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
  4897. if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
  4898. error = xfs_refcount_decrease_extent(mp, dfops, del);
  4899. if (error)
  4900. goto done;
  4901. } else
  4902. xfs_bmap_add_free(mp, dfops, del->br_startblock,
  4903. del->br_blockcount, NULL);
  4904. }
  4905. /*
  4906. * Adjust inode # blocks in the file.
  4907. */
  4908. if (nblks)
  4909. ip->i_d.di_nblocks -= nblks;
  4910. /*
  4911. * Adjust quota data.
  4912. */
  4913. if (qfield && !(bflags & XFS_BMAPI_REMAP))
  4914. xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
  4915. /*
  4916. * Account for change in delayed indirect blocks.
  4917. * Nothing to do for disk quota accounting here.
  4918. */
  4919. ASSERT(da_old >= da_new);
  4920. if (da_old > da_new)
  4921. xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
  4922. done:
  4923. *logflagsp = flags;
  4924. return error;
  4925. }
  4926. /*
  4927. * Unmap (remove) blocks from a file.
  4928. * If nexts is nonzero then the number of extents to remove is limited to
  4929. * that value. If not all extents in the block range can be removed then
  4930. * *done is set.
  4931. */
  4932. int /* error */
  4933. __xfs_bunmapi(
  4934. xfs_trans_t *tp, /* transaction pointer */
  4935. struct xfs_inode *ip, /* incore inode */
  4936. xfs_fileoff_t bno, /* starting offset to unmap */
  4937. xfs_filblks_t *rlen, /* i/o: amount remaining */
  4938. int flags, /* misc flags */
  4939. xfs_extnum_t nexts, /* number of extents max */
  4940. xfs_fsblock_t *firstblock, /* first allocated block
  4941. controls a.g. for allocs */
  4942. struct xfs_defer_ops *dfops) /* i/o: deferred updates */
  4943. {
  4944. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4945. xfs_bmbt_irec_t del; /* extent being deleted */
  4946. int error; /* error return value */
  4947. xfs_extnum_t extno; /* extent number in list */
  4948. xfs_bmbt_irec_t got; /* current extent record */
  4949. xfs_ifork_t *ifp; /* inode fork pointer */
  4950. int isrt; /* freeing in rt area */
  4951. xfs_extnum_t lastx; /* last extent index used */
  4952. int logflags; /* transaction logging flags */
  4953. xfs_extlen_t mod; /* rt extent offset */
  4954. xfs_mount_t *mp; /* mount structure */
  4955. xfs_fileoff_t start; /* first file offset deleted */
  4956. int tmp_logflags; /* partial logging flags */
  4957. int wasdel; /* was a delayed alloc extent */
  4958. int whichfork; /* data or attribute fork */
  4959. xfs_fsblock_t sum;
  4960. xfs_filblks_t len = *rlen; /* length to unmap in file */
  4961. xfs_fileoff_t max_len;
  4962. xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
  4963. trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
  4964. whichfork = xfs_bmapi_whichfork(flags);
  4965. ASSERT(whichfork != XFS_COW_FORK);
  4966. ifp = XFS_IFORK_PTR(ip, whichfork);
  4967. if (unlikely(
  4968. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4969. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  4970. XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
  4971. ip->i_mount);
  4972. return -EFSCORRUPTED;
  4973. }
  4974. mp = ip->i_mount;
  4975. if (XFS_FORCED_SHUTDOWN(mp))
  4976. return -EIO;
  4977. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  4978. ASSERT(len > 0);
  4979. ASSERT(nexts >= 0);
  4980. /*
  4981. * Guesstimate how many blocks we can unmap without running the risk of
  4982. * blowing out the transaction with a mix of EFIs and reflink
  4983. * adjustments.
  4984. */
  4985. if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
  4986. max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
  4987. else
  4988. max_len = len;
  4989. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4990. (error = xfs_iread_extents(tp, ip, whichfork)))
  4991. return error;
  4992. if (xfs_iext_count(ifp) == 0) {
  4993. *rlen = 0;
  4994. return 0;
  4995. }
  4996. XFS_STATS_INC(mp, xs_blk_unmap);
  4997. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4998. start = bno;
  4999. bno = start + len - 1;
  5000. /*
  5001. * Check to see if the given block number is past the end of the
  5002. * file, back up to the last block if so...
  5003. */
  5004. if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
  5005. ASSERT(lastx > 0);
  5006. xfs_iext_get_extent(ifp, --lastx, &got);
  5007. bno = got.br_startoff + got.br_blockcount - 1;
  5008. }
  5009. logflags = 0;
  5010. if (ifp->if_flags & XFS_IFBROOT) {
  5011. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  5012. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  5013. cur->bc_private.b.firstblock = *firstblock;
  5014. cur->bc_private.b.dfops = dfops;
  5015. cur->bc_private.b.flags = 0;
  5016. } else
  5017. cur = NULL;
  5018. if (isrt) {
  5019. /*
  5020. * Synchronize by locking the bitmap inode.
  5021. */
  5022. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
  5023. xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  5024. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
  5025. xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  5026. }
  5027. extno = 0;
  5028. while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
  5029. (nexts == 0 || extno < nexts) && max_len > 0) {
  5030. /*
  5031. * Is the found extent after a hole in which bno lives?
  5032. * Just back up to the previous extent, if so.
  5033. */
  5034. if (got.br_startoff > bno) {
  5035. if (--lastx < 0)
  5036. break;
  5037. xfs_iext_get_extent(ifp, lastx, &got);
  5038. }
  5039. /*
  5040. * Is the last block of this extent before the range
  5041. * we're supposed to delete? If so, we're done.
  5042. */
  5043. bno = XFS_FILEOFF_MIN(bno,
  5044. got.br_startoff + got.br_blockcount - 1);
  5045. if (bno < start)
  5046. break;
  5047. /*
  5048. * Then deal with the (possibly delayed) allocated space
  5049. * we found.
  5050. */
  5051. del = got;
  5052. wasdel = isnullstartblock(del.br_startblock);
  5053. /*
  5054. * Make sure we don't touch multiple AGF headers out of order
  5055. * in a single transaction, as that could cause AB-BA deadlocks.
  5056. */
  5057. if (!wasdel) {
  5058. agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
  5059. if (prev_agno != NULLAGNUMBER && prev_agno > agno)
  5060. break;
  5061. prev_agno = agno;
  5062. }
  5063. if (got.br_startoff < start) {
  5064. del.br_startoff = start;
  5065. del.br_blockcount -= start - got.br_startoff;
  5066. if (!wasdel)
  5067. del.br_startblock += start - got.br_startoff;
  5068. }
  5069. if (del.br_startoff + del.br_blockcount > bno + 1)
  5070. del.br_blockcount = bno + 1 - del.br_startoff;
  5071. /* How much can we safely unmap? */
  5072. if (max_len < del.br_blockcount) {
  5073. del.br_startoff += del.br_blockcount - max_len;
  5074. if (!wasdel)
  5075. del.br_startblock += del.br_blockcount - max_len;
  5076. del.br_blockcount = max_len;
  5077. }
  5078. sum = del.br_startblock + del.br_blockcount;
  5079. if (isrt &&
  5080. (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
  5081. /*
  5082. * Realtime extent not lined up at the end.
  5083. * The extent could have been split into written
  5084. * and unwritten pieces, or we could just be
  5085. * unmapping part of it. But we can't really
  5086. * get rid of part of a realtime extent.
  5087. */
  5088. if (del.br_state == XFS_EXT_UNWRITTEN ||
  5089. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5090. /*
  5091. * This piece is unwritten, or we're not
  5092. * using unwritten extents. Skip over it.
  5093. */
  5094. ASSERT(bno >= mod);
  5095. bno -= mod > del.br_blockcount ?
  5096. del.br_blockcount : mod;
  5097. if (bno < got.br_startoff) {
  5098. if (--lastx >= 0)
  5099. xfs_iext_get_extent(ifp, lastx,
  5100. &got);
  5101. }
  5102. continue;
  5103. }
  5104. /*
  5105. * It's written, turn it unwritten.
  5106. * This is better than zeroing it.
  5107. */
  5108. ASSERT(del.br_state == XFS_EXT_NORM);
  5109. ASSERT(tp->t_blk_res > 0);
  5110. /*
  5111. * If this spans a realtime extent boundary,
  5112. * chop it back to the start of the one we end at.
  5113. */
  5114. if (del.br_blockcount > mod) {
  5115. del.br_startoff += del.br_blockcount - mod;
  5116. del.br_startblock += del.br_blockcount - mod;
  5117. del.br_blockcount = mod;
  5118. }
  5119. del.br_state = XFS_EXT_UNWRITTEN;
  5120. error = xfs_bmap_add_extent_unwritten_real(tp, ip,
  5121. whichfork, &lastx, &cur, &del,
  5122. firstblock, dfops, &logflags);
  5123. if (error)
  5124. goto error0;
  5125. goto nodelete;
  5126. }
  5127. if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
  5128. /*
  5129. * Realtime extent is lined up at the end but not
  5130. * at the front. We'll get rid of full extents if
  5131. * we can.
  5132. */
  5133. mod = mp->m_sb.sb_rextsize - mod;
  5134. if (del.br_blockcount > mod) {
  5135. del.br_blockcount -= mod;
  5136. del.br_startoff += mod;
  5137. del.br_startblock += mod;
  5138. } else if ((del.br_startoff == start &&
  5139. (del.br_state == XFS_EXT_UNWRITTEN ||
  5140. tp->t_blk_res == 0)) ||
  5141. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5142. /*
  5143. * Can't make it unwritten. There isn't
  5144. * a full extent here so just skip it.
  5145. */
  5146. ASSERT(bno >= del.br_blockcount);
  5147. bno -= del.br_blockcount;
  5148. if (got.br_startoff > bno && --lastx >= 0)
  5149. xfs_iext_get_extent(ifp, lastx, &got);
  5150. continue;
  5151. } else if (del.br_state == XFS_EXT_UNWRITTEN) {
  5152. struct xfs_bmbt_irec prev;
  5153. /*
  5154. * This one is already unwritten.
  5155. * It must have a written left neighbor.
  5156. * Unwrite the killed part of that one and
  5157. * try again.
  5158. */
  5159. ASSERT(lastx > 0);
  5160. xfs_iext_get_extent(ifp, lastx - 1, &prev);
  5161. ASSERT(prev.br_state == XFS_EXT_NORM);
  5162. ASSERT(!isnullstartblock(prev.br_startblock));
  5163. ASSERT(del.br_startblock ==
  5164. prev.br_startblock + prev.br_blockcount);
  5165. if (prev.br_startoff < start) {
  5166. mod = start - prev.br_startoff;
  5167. prev.br_blockcount -= mod;
  5168. prev.br_startblock += mod;
  5169. prev.br_startoff = start;
  5170. }
  5171. prev.br_state = XFS_EXT_UNWRITTEN;
  5172. lastx--;
  5173. error = xfs_bmap_add_extent_unwritten_real(tp,
  5174. ip, whichfork, &lastx, &cur,
  5175. &prev, firstblock, dfops,
  5176. &logflags);
  5177. if (error)
  5178. goto error0;
  5179. goto nodelete;
  5180. } else {
  5181. ASSERT(del.br_state == XFS_EXT_NORM);
  5182. del.br_state = XFS_EXT_UNWRITTEN;
  5183. error = xfs_bmap_add_extent_unwritten_real(tp,
  5184. ip, whichfork, &lastx, &cur,
  5185. &del, firstblock, dfops,
  5186. &logflags);
  5187. if (error)
  5188. goto error0;
  5189. goto nodelete;
  5190. }
  5191. }
  5192. /*
  5193. * If it's the case where the directory code is running
  5194. * with no block reservation, and the deleted block is in
  5195. * the middle of its extent, and the resulting insert
  5196. * of an extent would cause transformation to btree format,
  5197. * then reject it. The calling code will then swap
  5198. * blocks around instead.
  5199. * We have to do this now, rather than waiting for the
  5200. * conversion to btree format, since the transaction
  5201. * will be dirty.
  5202. */
  5203. if (!wasdel && tp->t_blk_res == 0 &&
  5204. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5205. XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
  5206. XFS_IFORK_MAXEXT(ip, whichfork) &&
  5207. del.br_startoff > got.br_startoff &&
  5208. del.br_startoff + del.br_blockcount <
  5209. got.br_startoff + got.br_blockcount) {
  5210. error = -ENOSPC;
  5211. goto error0;
  5212. }
  5213. /*
  5214. * Unreserve quota and update realtime free space, if
  5215. * appropriate. If delayed allocation, update the inode delalloc
  5216. * counter now and wait to update the sb counters as
  5217. * xfs_bmap_del_extent() might need to borrow some blocks.
  5218. */
  5219. if (wasdel) {
  5220. ASSERT(startblockval(del.br_startblock) > 0);
  5221. if (isrt) {
  5222. xfs_filblks_t rtexts;
  5223. rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
  5224. do_div(rtexts, mp->m_sb.sb_rextsize);
  5225. xfs_mod_frextents(mp, (int64_t)rtexts);
  5226. (void)xfs_trans_reserve_quota_nblks(NULL,
  5227. ip, -((long)del.br_blockcount), 0,
  5228. XFS_QMOPT_RES_RTBLKS);
  5229. } else {
  5230. (void)xfs_trans_reserve_quota_nblks(NULL,
  5231. ip, -((long)del.br_blockcount), 0,
  5232. XFS_QMOPT_RES_REGBLKS);
  5233. }
  5234. ip->i_delayed_blks -= del.br_blockcount;
  5235. }
  5236. error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
  5237. &tmp_logflags, whichfork, flags);
  5238. logflags |= tmp_logflags;
  5239. if (error)
  5240. goto error0;
  5241. if (!isrt && wasdel)
  5242. xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
  5243. max_len -= del.br_blockcount;
  5244. bno = del.br_startoff - 1;
  5245. nodelete:
  5246. /*
  5247. * If not done go on to the next (previous) record.
  5248. */
  5249. if (bno != (xfs_fileoff_t)-1 && bno >= start) {
  5250. if (lastx >= 0) {
  5251. xfs_iext_get_extent(ifp, lastx, &got);
  5252. if (got.br_startoff > bno && --lastx >= 0)
  5253. xfs_iext_get_extent(ifp, lastx, &got);
  5254. }
  5255. extno++;
  5256. }
  5257. }
  5258. if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
  5259. *rlen = 0;
  5260. else
  5261. *rlen = bno - start + 1;
  5262. /*
  5263. * Convert to a btree if necessary.
  5264. */
  5265. if (xfs_bmap_needs_btree(ip, whichfork)) {
  5266. ASSERT(cur == NULL);
  5267. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
  5268. &cur, 0, &tmp_logflags, whichfork);
  5269. logflags |= tmp_logflags;
  5270. if (error)
  5271. goto error0;
  5272. }
  5273. /*
  5274. * transform from btree to extents, give it cur
  5275. */
  5276. else if (xfs_bmap_wants_extents(ip, whichfork)) {
  5277. ASSERT(cur != NULL);
  5278. error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
  5279. whichfork);
  5280. logflags |= tmp_logflags;
  5281. if (error)
  5282. goto error0;
  5283. }
  5284. /*
  5285. * transform from extents to local?
  5286. */
  5287. error = 0;
  5288. error0:
  5289. /*
  5290. * Log everything. Do this after conversion, there's no point in
  5291. * logging the extent records if we've converted to btree format.
  5292. */
  5293. if ((logflags & xfs_ilog_fext(whichfork)) &&
  5294. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5295. logflags &= ~xfs_ilog_fext(whichfork);
  5296. else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
  5297. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5298. logflags &= ~xfs_ilog_fbroot(whichfork);
  5299. /*
  5300. * Log inode even in the error case, if the transaction
  5301. * is dirty we'll need to shut down the filesystem.
  5302. */
  5303. if (logflags)
  5304. xfs_trans_log_inode(tp, ip, logflags);
  5305. if (cur) {
  5306. if (!error) {
  5307. *firstblock = cur->bc_private.b.firstblock;
  5308. cur->bc_private.b.allocated = 0;
  5309. }
  5310. xfs_btree_del_cursor(cur,
  5311. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5312. }
  5313. return error;
  5314. }
  5315. /* Unmap a range of a file. */
  5316. int
  5317. xfs_bunmapi(
  5318. xfs_trans_t *tp,
  5319. struct xfs_inode *ip,
  5320. xfs_fileoff_t bno,
  5321. xfs_filblks_t len,
  5322. int flags,
  5323. xfs_extnum_t nexts,
  5324. xfs_fsblock_t *firstblock,
  5325. struct xfs_defer_ops *dfops,
  5326. int *done)
  5327. {
  5328. int error;
  5329. error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
  5330. dfops);
  5331. *done = (len == 0);
  5332. return error;
  5333. }
  5334. /*
  5335. * Determine whether an extent shift can be accomplished by a merge with the
  5336. * extent that precedes the target hole of the shift.
  5337. */
  5338. STATIC bool
  5339. xfs_bmse_can_merge(
  5340. struct xfs_bmbt_irec *left, /* preceding extent */
  5341. struct xfs_bmbt_irec *got, /* current extent to shift */
  5342. xfs_fileoff_t shift) /* shift fsb */
  5343. {
  5344. xfs_fileoff_t startoff;
  5345. startoff = got->br_startoff - shift;
  5346. /*
  5347. * The extent, once shifted, must be adjacent in-file and on-disk with
  5348. * the preceding extent.
  5349. */
  5350. if ((left->br_startoff + left->br_blockcount != startoff) ||
  5351. (left->br_startblock + left->br_blockcount != got->br_startblock) ||
  5352. (left->br_state != got->br_state) ||
  5353. (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
  5354. return false;
  5355. return true;
  5356. }
  5357. /*
  5358. * A bmap extent shift adjusts the file offset of an extent to fill a preceding
  5359. * hole in the file. If an extent shift would result in the extent being fully
  5360. * adjacent to the extent that currently precedes the hole, we can merge with
  5361. * the preceding extent rather than do the shift.
  5362. *
  5363. * This function assumes the caller has verified a shift-by-merge is possible
  5364. * with the provided extents via xfs_bmse_can_merge().
  5365. */
  5366. STATIC int
  5367. xfs_bmse_merge(
  5368. struct xfs_inode *ip,
  5369. int whichfork,
  5370. xfs_fileoff_t shift, /* shift fsb */
  5371. int current_ext, /* idx of gotp */
  5372. struct xfs_bmbt_irec *got, /* extent to shift */
  5373. struct xfs_bmbt_irec *left, /* preceding extent */
  5374. struct xfs_btree_cur *cur,
  5375. int *logflags, /* output */
  5376. struct xfs_defer_ops *dfops)
  5377. {
  5378. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  5379. struct xfs_bmbt_irec new;
  5380. xfs_filblks_t blockcount;
  5381. int error, i;
  5382. struct xfs_mount *mp = ip->i_mount;
  5383. blockcount = left->br_blockcount + got->br_blockcount;
  5384. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  5385. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  5386. ASSERT(xfs_bmse_can_merge(left, got, shift));
  5387. new = *left;
  5388. new.br_blockcount = blockcount;
  5389. /*
  5390. * Update the on-disk extent count, the btree if necessary and log the
  5391. * inode.
  5392. */
  5393. XFS_IFORK_NEXT_SET(ip, whichfork,
  5394. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  5395. *logflags |= XFS_ILOG_CORE;
  5396. if (!cur) {
  5397. *logflags |= XFS_ILOG_DEXT;
  5398. goto done;
  5399. }
  5400. /* lookup and remove the extent to merge */
  5401. error = xfs_bmbt_lookup_eq(cur, got->br_startoff, got->br_startblock,
  5402. got->br_blockcount, &i);
  5403. if (error)
  5404. return error;
  5405. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5406. error = xfs_btree_delete(cur, &i);
  5407. if (error)
  5408. return error;
  5409. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5410. /* lookup and update size of the previous extent */
  5411. error = xfs_bmbt_lookup_eq(cur, left->br_startoff, left->br_startblock,
  5412. left->br_blockcount, &i);
  5413. if (error)
  5414. return error;
  5415. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5416. error = xfs_bmbt_update(cur, new.br_startoff, new.br_startblock,
  5417. new.br_blockcount, new.br_state);
  5418. if (error)
  5419. return error;
  5420. done:
  5421. xfs_iext_update_extent(ifp, current_ext - 1, &new);
  5422. xfs_iext_remove(ip, current_ext, 1, 0);
  5423. /* update reverse mapping. rmap functions merge the rmaps for us */
  5424. error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
  5425. if (error)
  5426. return error;
  5427. memcpy(&new, got, sizeof(new));
  5428. new.br_startoff = left->br_startoff + left->br_blockcount;
  5429. return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
  5430. }
  5431. /*
  5432. * Shift a single extent.
  5433. */
  5434. STATIC int
  5435. xfs_bmse_shift_one(
  5436. struct xfs_inode *ip,
  5437. int whichfork,
  5438. xfs_fileoff_t offset_shift_fsb,
  5439. int *current_ext,
  5440. struct xfs_bmbt_irec *got,
  5441. struct xfs_btree_cur *cur,
  5442. int *logflags,
  5443. enum shift_direction direction,
  5444. struct xfs_defer_ops *dfops)
  5445. {
  5446. struct xfs_ifork *ifp;
  5447. struct xfs_mount *mp;
  5448. xfs_fileoff_t startoff;
  5449. struct xfs_bmbt_irec adj_irec, new;
  5450. int error;
  5451. int i;
  5452. int total_extents;
  5453. mp = ip->i_mount;
  5454. ifp = XFS_IFORK_PTR(ip, whichfork);
  5455. total_extents = xfs_iext_count(ifp);
  5456. /* delalloc extents should be prevented by caller */
  5457. XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
  5458. if (direction == SHIFT_LEFT) {
  5459. startoff = got->br_startoff - offset_shift_fsb;
  5460. /*
  5461. * Check for merge if we've got an extent to the left,
  5462. * otherwise make sure there's enough room at the start
  5463. * of the file for the shift.
  5464. */
  5465. if (!*current_ext) {
  5466. if (got->br_startoff < offset_shift_fsb)
  5467. return -EINVAL;
  5468. goto update_current_ext;
  5469. }
  5470. /*
  5471. * grab the left extent and check for a large enough hole.
  5472. */
  5473. xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
  5474. if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
  5475. return -EINVAL;
  5476. /* check whether to merge the extent or shift it down */
  5477. if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
  5478. return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
  5479. *current_ext, got, &adj_irec,
  5480. cur, logflags, dfops);
  5481. }
  5482. } else {
  5483. startoff = got->br_startoff + offset_shift_fsb;
  5484. /* nothing to move if this is the last extent */
  5485. if (*current_ext >= (total_extents - 1))
  5486. goto update_current_ext;
  5487. /*
  5488. * If this is not the last extent in the file, make sure there
  5489. * is enough room between current extent and next extent for
  5490. * accommodating the shift.
  5491. */
  5492. xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
  5493. if (startoff + got->br_blockcount > adj_irec.br_startoff)
  5494. return -EINVAL;
  5495. /*
  5496. * Unlike a left shift (which involves a hole punch),
  5497. * a right shift does not modify extent neighbors
  5498. * in any way. We should never find mergeable extents
  5499. * in this scenario. Check anyways and warn if we
  5500. * encounter two extents that could be one.
  5501. */
  5502. if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
  5503. WARN_ON_ONCE(1);
  5504. }
  5505. /*
  5506. * Increment the extent index for the next iteration, update the start
  5507. * offset of the in-core extent and update the btree if applicable.
  5508. */
  5509. update_current_ext:
  5510. *logflags |= XFS_ILOG_CORE;
  5511. new = *got;
  5512. new.br_startoff = startoff;
  5513. if (cur) {
  5514. error = xfs_bmbt_lookup_eq(cur, got->br_startoff,
  5515. got->br_startblock, got->br_blockcount, &i);
  5516. if (error)
  5517. return error;
  5518. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  5519. error = xfs_bmbt_update(cur, new.br_startoff,
  5520. new.br_startblock, new.br_blockcount,
  5521. new.br_state);
  5522. if (error)
  5523. return error;
  5524. } else {
  5525. *logflags |= XFS_ILOG_DEXT;
  5526. }
  5527. xfs_iext_update_extent(ifp, *current_ext, &new);
  5528. if (direction == SHIFT_LEFT)
  5529. (*current_ext)++;
  5530. else
  5531. (*current_ext)--;
  5532. /* update reverse mapping */
  5533. error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
  5534. if (error)
  5535. return error;
  5536. return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
  5537. }
  5538. /*
  5539. * Shift extent records to the left/right to cover/create a hole.
  5540. *
  5541. * The maximum number of extents to be shifted in a single operation is
  5542. * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
  5543. * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
  5544. * is the length by which each extent is shifted. If there is no hole to shift
  5545. * the extents into, this will be considered invalid operation and we abort
  5546. * immediately.
  5547. */
  5548. int
  5549. xfs_bmap_shift_extents(
  5550. struct xfs_trans *tp,
  5551. struct xfs_inode *ip,
  5552. xfs_fileoff_t *next_fsb,
  5553. xfs_fileoff_t offset_shift_fsb,
  5554. int *done,
  5555. xfs_fileoff_t stop_fsb,
  5556. xfs_fsblock_t *firstblock,
  5557. struct xfs_defer_ops *dfops,
  5558. enum shift_direction direction,
  5559. int num_exts)
  5560. {
  5561. struct xfs_btree_cur *cur = NULL;
  5562. struct xfs_bmbt_irec got;
  5563. struct xfs_mount *mp = ip->i_mount;
  5564. struct xfs_ifork *ifp;
  5565. xfs_extnum_t nexts = 0;
  5566. xfs_extnum_t current_ext;
  5567. xfs_extnum_t total_extents;
  5568. xfs_extnum_t stop_extent;
  5569. int error = 0;
  5570. int whichfork = XFS_DATA_FORK;
  5571. int logflags = 0;
  5572. if (unlikely(XFS_TEST_ERROR(
  5573. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  5574. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  5575. mp, XFS_ERRTAG_BMAPIFORMAT))) {
  5576. XFS_ERROR_REPORT("xfs_bmap_shift_extents",
  5577. XFS_ERRLEVEL_LOW, mp);
  5578. return -EFSCORRUPTED;
  5579. }
  5580. if (XFS_FORCED_SHUTDOWN(mp))
  5581. return -EIO;
  5582. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  5583. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  5584. ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
  5585. ifp = XFS_IFORK_PTR(ip, whichfork);
  5586. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  5587. /* Read in all the extents */
  5588. error = xfs_iread_extents(tp, ip, whichfork);
  5589. if (error)
  5590. return error;
  5591. }
  5592. if (ifp->if_flags & XFS_IFBROOT) {
  5593. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  5594. cur->bc_private.b.firstblock = *firstblock;
  5595. cur->bc_private.b.dfops = dfops;
  5596. cur->bc_private.b.flags = 0;
  5597. }
  5598. /*
  5599. * There may be delalloc extents in the data fork before the range we
  5600. * are collapsing out, so we cannot use the count of real extents here.
  5601. * Instead we have to calculate it from the incore fork.
  5602. */
  5603. total_extents = xfs_iext_count(ifp);
  5604. if (total_extents == 0) {
  5605. *done = 1;
  5606. goto del_cursor;
  5607. }
  5608. /*
  5609. * In case of first right shift, we need to initialize next_fsb
  5610. */
  5611. if (*next_fsb == NULLFSBLOCK) {
  5612. ASSERT(direction == SHIFT_RIGHT);
  5613. current_ext = total_extents - 1;
  5614. xfs_iext_get_extent(ifp, current_ext, &got);
  5615. if (stop_fsb > got.br_startoff) {
  5616. *done = 1;
  5617. goto del_cursor;
  5618. }
  5619. *next_fsb = got.br_startoff;
  5620. } else {
  5621. /*
  5622. * Look up the extent index for the fsb where we start shifting. We can
  5623. * henceforth iterate with current_ext as extent list changes are locked
  5624. * out via ilock.
  5625. *
  5626. * If next_fsb lies in a hole beyond which there are no extents we are
  5627. * done.
  5628. */
  5629. if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
  5630. &got)) {
  5631. *done = 1;
  5632. goto del_cursor;
  5633. }
  5634. }
  5635. /* Lookup the extent index at which we have to stop */
  5636. if (direction == SHIFT_RIGHT) {
  5637. struct xfs_bmbt_irec s;
  5638. xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
  5639. /* Make stop_extent exclusive of shift range */
  5640. stop_extent--;
  5641. if (current_ext <= stop_extent) {
  5642. error = -EIO;
  5643. goto del_cursor;
  5644. }
  5645. } else {
  5646. stop_extent = total_extents;
  5647. if (current_ext >= stop_extent) {
  5648. error = -EIO;
  5649. goto del_cursor;
  5650. }
  5651. }
  5652. while (nexts++ < num_exts) {
  5653. error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
  5654. &current_ext, &got, cur, &logflags,
  5655. direction, dfops);
  5656. if (error)
  5657. goto del_cursor;
  5658. /*
  5659. * If there was an extent merge during the shift, the extent
  5660. * count can change. Update the total and grade the next record.
  5661. */
  5662. if (direction == SHIFT_LEFT) {
  5663. total_extents = xfs_iext_count(ifp);
  5664. stop_extent = total_extents;
  5665. }
  5666. if (current_ext == stop_extent) {
  5667. *done = 1;
  5668. *next_fsb = NULLFSBLOCK;
  5669. break;
  5670. }
  5671. xfs_iext_get_extent(ifp, current_ext, &got);
  5672. }
  5673. if (!*done)
  5674. *next_fsb = got.br_startoff;
  5675. del_cursor:
  5676. if (cur)
  5677. xfs_btree_del_cursor(cur,
  5678. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5679. if (logflags)
  5680. xfs_trans_log_inode(tp, ip, logflags);
  5681. return error;
  5682. }
  5683. /*
  5684. * Splits an extent into two extents at split_fsb block such that it is
  5685. * the first block of the current_ext. @current_ext is a target extent
  5686. * to be split. @split_fsb is a block where the extents is split.
  5687. * If split_fsb lies in a hole or the first block of extents, just return 0.
  5688. */
  5689. STATIC int
  5690. xfs_bmap_split_extent_at(
  5691. struct xfs_trans *tp,
  5692. struct xfs_inode *ip,
  5693. xfs_fileoff_t split_fsb,
  5694. xfs_fsblock_t *firstfsb,
  5695. struct xfs_defer_ops *dfops)
  5696. {
  5697. int whichfork = XFS_DATA_FORK;
  5698. struct xfs_btree_cur *cur = NULL;
  5699. struct xfs_bmbt_irec got;
  5700. struct xfs_bmbt_irec new; /* split extent */
  5701. struct xfs_mount *mp = ip->i_mount;
  5702. struct xfs_ifork *ifp;
  5703. xfs_fsblock_t gotblkcnt; /* new block count for got */
  5704. xfs_extnum_t current_ext;
  5705. int error = 0;
  5706. int logflags = 0;
  5707. int i = 0;
  5708. if (unlikely(XFS_TEST_ERROR(
  5709. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  5710. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
  5711. mp, XFS_ERRTAG_BMAPIFORMAT))) {
  5712. XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
  5713. XFS_ERRLEVEL_LOW, mp);
  5714. return -EFSCORRUPTED;
  5715. }
  5716. if (XFS_FORCED_SHUTDOWN(mp))
  5717. return -EIO;
  5718. ifp = XFS_IFORK_PTR(ip, whichfork);
  5719. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  5720. /* Read in all the extents */
  5721. error = xfs_iread_extents(tp, ip, whichfork);
  5722. if (error)
  5723. return error;
  5724. }
  5725. /*
  5726. * If there are not extents, or split_fsb lies in a hole we are done.
  5727. */
  5728. if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
  5729. got.br_startoff >= split_fsb)
  5730. return 0;
  5731. gotblkcnt = split_fsb - got.br_startoff;
  5732. new.br_startoff = split_fsb;
  5733. new.br_startblock = got.br_startblock + gotblkcnt;
  5734. new.br_blockcount = got.br_blockcount - gotblkcnt;
  5735. new.br_state = got.br_state;
  5736. if (ifp->if_flags & XFS_IFBROOT) {
  5737. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  5738. cur->bc_private.b.firstblock = *firstfsb;
  5739. cur->bc_private.b.dfops = dfops;
  5740. cur->bc_private.b.flags = 0;
  5741. error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  5742. got.br_startblock,
  5743. got.br_blockcount,
  5744. &i);
  5745. if (error)
  5746. goto del_cursor;
  5747. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
  5748. }
  5749. got.br_blockcount = gotblkcnt;
  5750. xfs_iext_update_extent(ifp, current_ext, &got);
  5751. logflags = XFS_ILOG_CORE;
  5752. if (cur) {
  5753. error = xfs_bmbt_update(cur, got.br_startoff,
  5754. got.br_startblock,
  5755. got.br_blockcount,
  5756. got.br_state);
  5757. if (error)
  5758. goto del_cursor;
  5759. } else
  5760. logflags |= XFS_ILOG_DEXT;
  5761. /* Add new extent */
  5762. current_ext++;
  5763. xfs_iext_insert(ip, current_ext, 1, &new, 0);
  5764. XFS_IFORK_NEXT_SET(ip, whichfork,
  5765. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  5766. if (cur) {
  5767. error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
  5768. new.br_startblock, new.br_blockcount,
  5769. &i);
  5770. if (error)
  5771. goto del_cursor;
  5772. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
  5773. cur->bc_rec.b.br_state = new.br_state;
  5774. error = xfs_btree_insert(cur, &i);
  5775. if (error)
  5776. goto del_cursor;
  5777. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
  5778. }
  5779. /*
  5780. * Convert to a btree if necessary.
  5781. */
  5782. if (xfs_bmap_needs_btree(ip, whichfork)) {
  5783. int tmp_logflags; /* partial log flag return val */
  5784. ASSERT(cur == NULL);
  5785. error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
  5786. &cur, 0, &tmp_logflags, whichfork);
  5787. logflags |= tmp_logflags;
  5788. }
  5789. del_cursor:
  5790. if (cur) {
  5791. cur->bc_private.b.allocated = 0;
  5792. xfs_btree_del_cursor(cur,
  5793. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5794. }
  5795. if (logflags)
  5796. xfs_trans_log_inode(tp, ip, logflags);
  5797. return error;
  5798. }
  5799. int
  5800. xfs_bmap_split_extent(
  5801. struct xfs_inode *ip,
  5802. xfs_fileoff_t split_fsb)
  5803. {
  5804. struct xfs_mount *mp = ip->i_mount;
  5805. struct xfs_trans *tp;
  5806. struct xfs_defer_ops dfops;
  5807. xfs_fsblock_t firstfsb;
  5808. int error;
  5809. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
  5810. XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
  5811. if (error)
  5812. return error;
  5813. xfs_ilock(ip, XFS_ILOCK_EXCL);
  5814. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  5815. xfs_defer_init(&dfops, &firstfsb);
  5816. error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
  5817. &firstfsb, &dfops);
  5818. if (error)
  5819. goto out;
  5820. error = xfs_defer_finish(&tp, &dfops);
  5821. if (error)
  5822. goto out;
  5823. return xfs_trans_commit(tp);
  5824. out:
  5825. xfs_defer_cancel(&dfops);
  5826. xfs_trans_cancel(tp);
  5827. return error;
  5828. }
  5829. /* Deferred mapping is only for real extents in the data fork. */
  5830. static bool
  5831. xfs_bmap_is_update_needed(
  5832. struct xfs_bmbt_irec *bmap)
  5833. {
  5834. return bmap->br_startblock != HOLESTARTBLOCK &&
  5835. bmap->br_startblock != DELAYSTARTBLOCK;
  5836. }
  5837. /* Record a bmap intent. */
  5838. static int
  5839. __xfs_bmap_add(
  5840. struct xfs_mount *mp,
  5841. struct xfs_defer_ops *dfops,
  5842. enum xfs_bmap_intent_type type,
  5843. struct xfs_inode *ip,
  5844. int whichfork,
  5845. struct xfs_bmbt_irec *bmap)
  5846. {
  5847. int error;
  5848. struct xfs_bmap_intent *bi;
  5849. trace_xfs_bmap_defer(mp,
  5850. XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
  5851. type,
  5852. XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
  5853. ip->i_ino, whichfork,
  5854. bmap->br_startoff,
  5855. bmap->br_blockcount,
  5856. bmap->br_state);
  5857. bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
  5858. INIT_LIST_HEAD(&bi->bi_list);
  5859. bi->bi_type = type;
  5860. bi->bi_owner = ip;
  5861. bi->bi_whichfork = whichfork;
  5862. bi->bi_bmap = *bmap;
  5863. error = xfs_defer_ijoin(dfops, bi->bi_owner);
  5864. if (error) {
  5865. kmem_free(bi);
  5866. return error;
  5867. }
  5868. xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
  5869. return 0;
  5870. }
  5871. /* Map an extent into a file. */
  5872. int
  5873. xfs_bmap_map_extent(
  5874. struct xfs_mount *mp,
  5875. struct xfs_defer_ops *dfops,
  5876. struct xfs_inode *ip,
  5877. struct xfs_bmbt_irec *PREV)
  5878. {
  5879. if (!xfs_bmap_is_update_needed(PREV))
  5880. return 0;
  5881. return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
  5882. XFS_DATA_FORK, PREV);
  5883. }
  5884. /* Unmap an extent out of a file. */
  5885. int
  5886. xfs_bmap_unmap_extent(
  5887. struct xfs_mount *mp,
  5888. struct xfs_defer_ops *dfops,
  5889. struct xfs_inode *ip,
  5890. struct xfs_bmbt_irec *PREV)
  5891. {
  5892. if (!xfs_bmap_is_update_needed(PREV))
  5893. return 0;
  5894. return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
  5895. XFS_DATA_FORK, PREV);
  5896. }
  5897. /*
  5898. * Process one of the deferred bmap operations. We pass back the
  5899. * btree cursor to maintain our lock on the bmapbt between calls.
  5900. */
  5901. int
  5902. xfs_bmap_finish_one(
  5903. struct xfs_trans *tp,
  5904. struct xfs_defer_ops *dfops,
  5905. struct xfs_inode *ip,
  5906. enum xfs_bmap_intent_type type,
  5907. int whichfork,
  5908. xfs_fileoff_t startoff,
  5909. xfs_fsblock_t startblock,
  5910. xfs_filblks_t *blockcount,
  5911. xfs_exntst_t state)
  5912. {
  5913. xfs_fsblock_t firstfsb;
  5914. int error = 0;
  5915. /*
  5916. * firstfsb is tied to the transaction lifetime and is used to
  5917. * ensure correct AG locking order and schedule work item
  5918. * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
  5919. * to only making one bmap call per transaction, so it should
  5920. * be safe to have it as a local variable here.
  5921. */
  5922. firstfsb = NULLFSBLOCK;
  5923. trace_xfs_bmap_deferred(tp->t_mountp,
  5924. XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
  5925. XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
  5926. ip->i_ino, whichfork, startoff, *blockcount, state);
  5927. if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
  5928. return -EFSCORRUPTED;
  5929. if (XFS_TEST_ERROR(false, tp->t_mountp,
  5930. XFS_ERRTAG_BMAP_FINISH_ONE))
  5931. return -EIO;
  5932. switch (type) {
  5933. case XFS_BMAP_MAP:
  5934. error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
  5935. startblock, dfops);
  5936. *blockcount = 0;
  5937. break;
  5938. case XFS_BMAP_UNMAP:
  5939. error = __xfs_bunmapi(tp, ip, startoff, blockcount,
  5940. XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
  5941. break;
  5942. default:
  5943. ASSERT(0);
  5944. error = -EFSCORRUPTED;
  5945. }
  5946. return error;
  5947. }