xfs_bmap.c 170 KB

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