xfs_bmap.c 178 KB

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