xfs_bmap.c 160 KB

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