xfs_bmap.c 191 KB

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