xfs_bmap.c 173 KB

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