xfs_bmap.c 170 KB

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