xfs_bmap.c 174 KB

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