xfs_bmap.c 178 KB

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