xfs_bmap.c 167 KB

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