xfs_bmap.c 176 KB

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