xfs_bmap.c 160 KB

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