xfs_bmap.c 163 KB

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