xfs_bmap.c 181 KB

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