xfs_bmap.c 184 KB

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