xfs_bmap.c 170 KB

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