xfs_bmap.c 154 KB

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