xfs_bmap.c 163 KB

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