xfs_bmap.c 181 KB

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