xfs_bmap.c 185 KB

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