xfs_bmap.c 169 KB

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