xfs_bmap.c 178 KB

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