xfs_bmap.c 165 KB

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