xfs_bmap.c 185 KB

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