xfs_bmap.c 187 KB

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