xfs_bmap.c 172 KB

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