xfs_bmap.c 171 KB

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