xfs_btree.c 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_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_mount.h"
  26. #include "xfs_defer.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_trans.h"
  29. #include "xfs_inode_item.h"
  30. #include "xfs_buf_item.h"
  31. #include "xfs_btree.h"
  32. #include "xfs_error.h"
  33. #include "xfs_trace.h"
  34. #include "xfs_cksum.h"
  35. #include "xfs_alloc.h"
  36. #include "xfs_log.h"
  37. /*
  38. * Cursor allocation zone.
  39. */
  40. kmem_zone_t *xfs_btree_cur_zone;
  41. /*
  42. * Btree magic numbers.
  43. */
  44. static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
  45. { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
  46. XFS_FIBT_MAGIC, 0 },
  47. { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
  48. XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC,
  49. XFS_REFC_CRC_MAGIC }
  50. };
  51. #define xfs_btree_magic(cur) \
  52. xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
  53. STATIC int /* error (0 or EFSCORRUPTED) */
  54. xfs_btree_check_lblock(
  55. struct xfs_btree_cur *cur, /* btree cursor */
  56. struct xfs_btree_block *block, /* btree long form block pointer */
  57. int level, /* level of the btree block */
  58. struct xfs_buf *bp) /* buffer for block, if any */
  59. {
  60. int lblock_ok = 1; /* block passes checks */
  61. struct xfs_mount *mp; /* file system mount point */
  62. mp = cur->bc_mp;
  63. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  64. lblock_ok = lblock_ok &&
  65. uuid_equal(&block->bb_u.l.bb_uuid,
  66. &mp->m_sb.sb_meta_uuid) &&
  67. block->bb_u.l.bb_blkno == cpu_to_be64(
  68. bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
  69. }
  70. lblock_ok = lblock_ok &&
  71. be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
  72. be16_to_cpu(block->bb_level) == level &&
  73. be16_to_cpu(block->bb_numrecs) <=
  74. cur->bc_ops->get_maxrecs(cur, level) &&
  75. block->bb_u.l.bb_leftsib &&
  76. (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK) ||
  77. XFS_FSB_SANITY_CHECK(mp,
  78. be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
  79. block->bb_u.l.bb_rightsib &&
  80. (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK) ||
  81. XFS_FSB_SANITY_CHECK(mp,
  82. be64_to_cpu(block->bb_u.l.bb_rightsib)));
  83. if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
  84. XFS_ERRTAG_BTREE_CHECK_LBLOCK,
  85. XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
  86. if (bp)
  87. trace_xfs_btree_corrupt(bp, _RET_IP_);
  88. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
  89. return -EFSCORRUPTED;
  90. }
  91. return 0;
  92. }
  93. STATIC int /* error (0 or EFSCORRUPTED) */
  94. xfs_btree_check_sblock(
  95. struct xfs_btree_cur *cur, /* btree cursor */
  96. struct xfs_btree_block *block, /* btree short form block pointer */
  97. int level, /* level of the btree block */
  98. struct xfs_buf *bp) /* buffer containing block */
  99. {
  100. struct xfs_mount *mp; /* file system mount point */
  101. struct xfs_buf *agbp; /* buffer for ag. freespace struct */
  102. struct xfs_agf *agf; /* ag. freespace structure */
  103. xfs_agblock_t agflen; /* native ag. freespace length */
  104. int sblock_ok = 1; /* block passes checks */
  105. mp = cur->bc_mp;
  106. agbp = cur->bc_private.a.agbp;
  107. agf = XFS_BUF_TO_AGF(agbp);
  108. agflen = be32_to_cpu(agf->agf_length);
  109. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  110. sblock_ok = sblock_ok &&
  111. uuid_equal(&block->bb_u.s.bb_uuid,
  112. &mp->m_sb.sb_meta_uuid) &&
  113. block->bb_u.s.bb_blkno == cpu_to_be64(
  114. bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
  115. }
  116. sblock_ok = sblock_ok &&
  117. be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
  118. be16_to_cpu(block->bb_level) == level &&
  119. be16_to_cpu(block->bb_numrecs) <=
  120. cur->bc_ops->get_maxrecs(cur, level) &&
  121. (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
  122. be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
  123. block->bb_u.s.bb_leftsib &&
  124. (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
  125. be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
  126. block->bb_u.s.bb_rightsib;
  127. if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
  128. XFS_ERRTAG_BTREE_CHECK_SBLOCK,
  129. XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
  130. if (bp)
  131. trace_xfs_btree_corrupt(bp, _RET_IP_);
  132. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
  133. return -EFSCORRUPTED;
  134. }
  135. return 0;
  136. }
  137. /*
  138. * Debug routine: check that block header is ok.
  139. */
  140. int
  141. xfs_btree_check_block(
  142. struct xfs_btree_cur *cur, /* btree cursor */
  143. struct xfs_btree_block *block, /* generic btree block pointer */
  144. int level, /* level of the btree block */
  145. struct xfs_buf *bp) /* buffer containing block, if any */
  146. {
  147. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  148. return xfs_btree_check_lblock(cur, block, level, bp);
  149. else
  150. return xfs_btree_check_sblock(cur, block, level, bp);
  151. }
  152. /*
  153. * Check that (long) pointer is ok.
  154. */
  155. int /* error (0 or EFSCORRUPTED) */
  156. xfs_btree_check_lptr(
  157. struct xfs_btree_cur *cur, /* btree cursor */
  158. xfs_fsblock_t bno, /* btree block disk address */
  159. int level) /* btree block level */
  160. {
  161. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
  162. level > 0 &&
  163. bno != NULLFSBLOCK &&
  164. XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
  165. return 0;
  166. }
  167. #ifdef DEBUG
  168. /*
  169. * Check that (short) pointer is ok.
  170. */
  171. STATIC int /* error (0 or EFSCORRUPTED) */
  172. xfs_btree_check_sptr(
  173. struct xfs_btree_cur *cur, /* btree cursor */
  174. xfs_agblock_t bno, /* btree block disk address */
  175. int level) /* btree block level */
  176. {
  177. xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
  178. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
  179. level > 0 &&
  180. bno != NULLAGBLOCK &&
  181. bno != 0 &&
  182. bno < agblocks);
  183. return 0;
  184. }
  185. /*
  186. * Check that block ptr is ok.
  187. */
  188. STATIC int /* error (0 or EFSCORRUPTED) */
  189. xfs_btree_check_ptr(
  190. struct xfs_btree_cur *cur, /* btree cursor */
  191. union xfs_btree_ptr *ptr, /* btree block disk address */
  192. int index, /* offset from ptr to check */
  193. int level) /* btree block level */
  194. {
  195. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  196. return xfs_btree_check_lptr(cur,
  197. be64_to_cpu((&ptr->l)[index]), level);
  198. } else {
  199. return xfs_btree_check_sptr(cur,
  200. be32_to_cpu((&ptr->s)[index]), level);
  201. }
  202. }
  203. #endif
  204. /*
  205. * Calculate CRC on the whole btree block and stuff it into the
  206. * long-form btree header.
  207. *
  208. * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
  209. * it into the buffer so recovery knows what the last modification was that made
  210. * it to disk.
  211. */
  212. void
  213. xfs_btree_lblock_calc_crc(
  214. struct xfs_buf *bp)
  215. {
  216. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  217. struct xfs_buf_log_item *bip = bp->b_fspriv;
  218. if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
  219. return;
  220. if (bip)
  221. block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  222. xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
  223. }
  224. bool
  225. xfs_btree_lblock_verify_crc(
  226. struct xfs_buf *bp)
  227. {
  228. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  229. struct xfs_mount *mp = bp->b_target->bt_mount;
  230. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  231. if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
  232. return false;
  233. return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
  234. }
  235. return true;
  236. }
  237. /*
  238. * Calculate CRC on the whole btree block and stuff it into the
  239. * short-form btree header.
  240. *
  241. * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
  242. * it into the buffer so recovery knows what the last modification was that made
  243. * it to disk.
  244. */
  245. void
  246. xfs_btree_sblock_calc_crc(
  247. struct xfs_buf *bp)
  248. {
  249. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  250. struct xfs_buf_log_item *bip = bp->b_fspriv;
  251. if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
  252. return;
  253. if (bip)
  254. block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  255. xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
  256. }
  257. bool
  258. xfs_btree_sblock_verify_crc(
  259. struct xfs_buf *bp)
  260. {
  261. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  262. struct xfs_mount *mp = bp->b_target->bt_mount;
  263. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  264. if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
  265. return false;
  266. return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
  267. }
  268. return true;
  269. }
  270. static int
  271. xfs_btree_free_block(
  272. struct xfs_btree_cur *cur,
  273. struct xfs_buf *bp)
  274. {
  275. int error;
  276. error = cur->bc_ops->free_block(cur, bp);
  277. if (!error) {
  278. xfs_trans_binval(cur->bc_tp, bp);
  279. XFS_BTREE_STATS_INC(cur, free);
  280. }
  281. return error;
  282. }
  283. /*
  284. * Delete the btree cursor.
  285. */
  286. void
  287. xfs_btree_del_cursor(
  288. xfs_btree_cur_t *cur, /* btree cursor */
  289. int error) /* del because of error */
  290. {
  291. int i; /* btree level */
  292. /*
  293. * Clear the buffer pointers, and release the buffers.
  294. * If we're doing this in the face of an error, we
  295. * need to make sure to inspect all of the entries
  296. * in the bc_bufs array for buffers to be unlocked.
  297. * This is because some of the btree code works from
  298. * level n down to 0, and if we get an error along
  299. * the way we won't have initialized all the entries
  300. * down to 0.
  301. */
  302. for (i = 0; i < cur->bc_nlevels; i++) {
  303. if (cur->bc_bufs[i])
  304. xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
  305. else if (!error)
  306. break;
  307. }
  308. /*
  309. * Can't free a bmap cursor without having dealt with the
  310. * allocated indirect blocks' accounting.
  311. */
  312. ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
  313. cur->bc_private.b.allocated == 0);
  314. /*
  315. * Free the cursor.
  316. */
  317. kmem_zone_free(xfs_btree_cur_zone, cur);
  318. }
  319. /*
  320. * Duplicate the btree cursor.
  321. * Allocate a new one, copy the record, re-get the buffers.
  322. */
  323. int /* error */
  324. xfs_btree_dup_cursor(
  325. xfs_btree_cur_t *cur, /* input cursor */
  326. xfs_btree_cur_t **ncur) /* output cursor */
  327. {
  328. xfs_buf_t *bp; /* btree block's buffer pointer */
  329. int error; /* error return value */
  330. int i; /* level number of btree block */
  331. xfs_mount_t *mp; /* mount structure for filesystem */
  332. xfs_btree_cur_t *new; /* new cursor value */
  333. xfs_trans_t *tp; /* transaction pointer, can be NULL */
  334. tp = cur->bc_tp;
  335. mp = cur->bc_mp;
  336. /*
  337. * Allocate a new cursor like the old one.
  338. */
  339. new = cur->bc_ops->dup_cursor(cur);
  340. /*
  341. * Copy the record currently in the cursor.
  342. */
  343. new->bc_rec = cur->bc_rec;
  344. /*
  345. * For each level current, re-get the buffer and copy the ptr value.
  346. */
  347. for (i = 0; i < new->bc_nlevels; i++) {
  348. new->bc_ptrs[i] = cur->bc_ptrs[i];
  349. new->bc_ra[i] = cur->bc_ra[i];
  350. bp = cur->bc_bufs[i];
  351. if (bp) {
  352. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  353. XFS_BUF_ADDR(bp), mp->m_bsize,
  354. 0, &bp,
  355. cur->bc_ops->buf_ops);
  356. if (error) {
  357. xfs_btree_del_cursor(new, error);
  358. *ncur = NULL;
  359. return error;
  360. }
  361. }
  362. new->bc_bufs[i] = bp;
  363. }
  364. *ncur = new;
  365. return 0;
  366. }
  367. /*
  368. * XFS btree block layout and addressing:
  369. *
  370. * There are two types of blocks in the btree: leaf and non-leaf blocks.
  371. *
  372. * The leaf record start with a header then followed by records containing
  373. * the values. A non-leaf block also starts with the same header, and
  374. * then first contains lookup keys followed by an equal number of pointers
  375. * to the btree blocks at the previous level.
  376. *
  377. * +--------+-------+-------+-------+-------+-------+-------+
  378. * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
  379. * +--------+-------+-------+-------+-------+-------+-------+
  380. *
  381. * +--------+-------+-------+-------+-------+-------+-------+
  382. * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
  383. * +--------+-------+-------+-------+-------+-------+-------+
  384. *
  385. * The header is called struct xfs_btree_block for reasons better left unknown
  386. * and comes in different versions for short (32bit) and long (64bit) block
  387. * pointers. The record and key structures are defined by the btree instances
  388. * and opaque to the btree core. The block pointers are simple disk endian
  389. * integers, available in a short (32bit) and long (64bit) variant.
  390. *
  391. * The helpers below calculate the offset of a given record, key or pointer
  392. * into a btree block (xfs_btree_*_offset) or return a pointer to the given
  393. * record, key or pointer (xfs_btree_*_addr). Note that all addressing
  394. * inside the btree block is done using indices starting at one, not zero!
  395. *
  396. * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
  397. * overlapping intervals. In such a tree, records are still sorted lowest to
  398. * highest and indexed by the smallest key value that refers to the record.
  399. * However, nodes are different: each pointer has two associated keys -- one
  400. * indexing the lowest key available in the block(s) below (the same behavior
  401. * as the key in a regular btree) and another indexing the highest key
  402. * available in the block(s) below. Because records are /not/ sorted by the
  403. * highest key, all leaf block updates require us to compute the highest key
  404. * that matches any record in the leaf and to recursively update the high keys
  405. * in the nodes going further up in the tree, if necessary. Nodes look like
  406. * this:
  407. *
  408. * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
  409. * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
  410. * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
  411. *
  412. * To perform an interval query on an overlapped tree, perform the usual
  413. * depth-first search and use the low and high keys to decide if we can skip
  414. * that particular node. If a leaf node is reached, return the records that
  415. * intersect the interval. Note that an interval query may return numerous
  416. * entries. For a non-overlapped tree, simply search for the record associated
  417. * with the lowest key and iterate forward until a non-matching record is
  418. * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
  419. * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
  420. * more detail.
  421. *
  422. * Why do we care about overlapping intervals? Let's say you have a bunch of
  423. * reverse mapping records on a reflink filesystem:
  424. *
  425. * 1: +- file A startblock B offset C length D -----------+
  426. * 2: +- file E startblock F offset G length H --------------+
  427. * 3: +- file I startblock F offset J length K --+
  428. * 4: +- file L... --+
  429. *
  430. * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
  431. * we'd simply increment the length of record 1. But how do we find the record
  432. * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
  433. * record 3 because the keys are ordered first by startblock. An interval
  434. * query would return records 1 and 2 because they both overlap (B+D-1), and
  435. * from that we can pick out record 1 as the appropriate left neighbor.
  436. *
  437. * In the non-overlapped case you can do a LE lookup and decrement the cursor
  438. * because a record's interval must end before the next record.
  439. */
  440. /*
  441. * Return size of the btree block header for this btree instance.
  442. */
  443. static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
  444. {
  445. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  446. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
  447. return XFS_BTREE_LBLOCK_CRC_LEN;
  448. return XFS_BTREE_LBLOCK_LEN;
  449. }
  450. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
  451. return XFS_BTREE_SBLOCK_CRC_LEN;
  452. return XFS_BTREE_SBLOCK_LEN;
  453. }
  454. /*
  455. * Return size of btree block pointers for this btree instance.
  456. */
  457. static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
  458. {
  459. return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  460. sizeof(__be64) : sizeof(__be32);
  461. }
  462. /*
  463. * Calculate offset of the n-th record in a btree block.
  464. */
  465. STATIC size_t
  466. xfs_btree_rec_offset(
  467. struct xfs_btree_cur *cur,
  468. int n)
  469. {
  470. return xfs_btree_block_len(cur) +
  471. (n - 1) * cur->bc_ops->rec_len;
  472. }
  473. /*
  474. * Calculate offset of the n-th key in a btree block.
  475. */
  476. STATIC size_t
  477. xfs_btree_key_offset(
  478. struct xfs_btree_cur *cur,
  479. int n)
  480. {
  481. return xfs_btree_block_len(cur) +
  482. (n - 1) * cur->bc_ops->key_len;
  483. }
  484. /*
  485. * Calculate offset of the n-th high key in a btree block.
  486. */
  487. STATIC size_t
  488. xfs_btree_high_key_offset(
  489. struct xfs_btree_cur *cur,
  490. int n)
  491. {
  492. return xfs_btree_block_len(cur) +
  493. (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
  494. }
  495. /*
  496. * Calculate offset of the n-th block pointer in a btree block.
  497. */
  498. STATIC size_t
  499. xfs_btree_ptr_offset(
  500. struct xfs_btree_cur *cur,
  501. int n,
  502. int level)
  503. {
  504. return xfs_btree_block_len(cur) +
  505. cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
  506. (n - 1) * xfs_btree_ptr_len(cur);
  507. }
  508. /*
  509. * Return a pointer to the n-th record in the btree block.
  510. */
  511. STATIC union xfs_btree_rec *
  512. xfs_btree_rec_addr(
  513. struct xfs_btree_cur *cur,
  514. int n,
  515. struct xfs_btree_block *block)
  516. {
  517. return (union xfs_btree_rec *)
  518. ((char *)block + xfs_btree_rec_offset(cur, n));
  519. }
  520. /*
  521. * Return a pointer to the n-th key in the btree block.
  522. */
  523. STATIC union xfs_btree_key *
  524. xfs_btree_key_addr(
  525. struct xfs_btree_cur *cur,
  526. int n,
  527. struct xfs_btree_block *block)
  528. {
  529. return (union xfs_btree_key *)
  530. ((char *)block + xfs_btree_key_offset(cur, n));
  531. }
  532. /*
  533. * Return a pointer to the n-th high key in the btree block.
  534. */
  535. STATIC union xfs_btree_key *
  536. xfs_btree_high_key_addr(
  537. struct xfs_btree_cur *cur,
  538. int n,
  539. struct xfs_btree_block *block)
  540. {
  541. return (union xfs_btree_key *)
  542. ((char *)block + xfs_btree_high_key_offset(cur, n));
  543. }
  544. /*
  545. * Return a pointer to the n-th block pointer in the btree block.
  546. */
  547. STATIC union xfs_btree_ptr *
  548. xfs_btree_ptr_addr(
  549. struct xfs_btree_cur *cur,
  550. int n,
  551. struct xfs_btree_block *block)
  552. {
  553. int level = xfs_btree_get_level(block);
  554. ASSERT(block->bb_level != 0);
  555. return (union xfs_btree_ptr *)
  556. ((char *)block + xfs_btree_ptr_offset(cur, n, level));
  557. }
  558. /*
  559. * Get the root block which is stored in the inode.
  560. *
  561. * For now this btree implementation assumes the btree root is always
  562. * stored in the if_broot field of an inode fork.
  563. */
  564. STATIC struct xfs_btree_block *
  565. xfs_btree_get_iroot(
  566. struct xfs_btree_cur *cur)
  567. {
  568. struct xfs_ifork *ifp;
  569. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
  570. return (struct xfs_btree_block *)ifp->if_broot;
  571. }
  572. /*
  573. * Retrieve the block pointer from the cursor at the given level.
  574. * This may be an inode btree root or from a buffer.
  575. */
  576. STATIC struct xfs_btree_block * /* generic btree block pointer */
  577. xfs_btree_get_block(
  578. struct xfs_btree_cur *cur, /* btree cursor */
  579. int level, /* level in btree */
  580. struct xfs_buf **bpp) /* buffer containing the block */
  581. {
  582. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  583. (level == cur->bc_nlevels - 1)) {
  584. *bpp = NULL;
  585. return xfs_btree_get_iroot(cur);
  586. }
  587. *bpp = cur->bc_bufs[level];
  588. return XFS_BUF_TO_BLOCK(*bpp);
  589. }
  590. /*
  591. * Get a buffer for the block, return it with no data read.
  592. * Long-form addressing.
  593. */
  594. xfs_buf_t * /* buffer for fsbno */
  595. xfs_btree_get_bufl(
  596. xfs_mount_t *mp, /* file system mount point */
  597. xfs_trans_t *tp, /* transaction pointer */
  598. xfs_fsblock_t fsbno, /* file system block number */
  599. uint lock) /* lock flags for get_buf */
  600. {
  601. xfs_daddr_t d; /* real disk block address */
  602. ASSERT(fsbno != NULLFSBLOCK);
  603. d = XFS_FSB_TO_DADDR(mp, fsbno);
  604. return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  605. }
  606. /*
  607. * Get a buffer for the block, return it with no data read.
  608. * Short-form addressing.
  609. */
  610. xfs_buf_t * /* buffer for agno/agbno */
  611. xfs_btree_get_bufs(
  612. xfs_mount_t *mp, /* file system mount point */
  613. xfs_trans_t *tp, /* transaction pointer */
  614. xfs_agnumber_t agno, /* allocation group number */
  615. xfs_agblock_t agbno, /* allocation group block number */
  616. uint lock) /* lock flags for get_buf */
  617. {
  618. xfs_daddr_t d; /* real disk block address */
  619. ASSERT(agno != NULLAGNUMBER);
  620. ASSERT(agbno != NULLAGBLOCK);
  621. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  622. return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  623. }
  624. /*
  625. * Check for the cursor referring to the last block at the given level.
  626. */
  627. int /* 1=is last block, 0=not last block */
  628. xfs_btree_islastblock(
  629. xfs_btree_cur_t *cur, /* btree cursor */
  630. int level) /* level to check */
  631. {
  632. struct xfs_btree_block *block; /* generic btree block pointer */
  633. xfs_buf_t *bp; /* buffer containing block */
  634. block = xfs_btree_get_block(cur, level, &bp);
  635. xfs_btree_check_block(cur, block, level, bp);
  636. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  637. return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
  638. else
  639. return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
  640. }
  641. /*
  642. * Change the cursor to point to the first record at the given level.
  643. * Other levels are unaffected.
  644. */
  645. STATIC int /* success=1, failure=0 */
  646. xfs_btree_firstrec(
  647. xfs_btree_cur_t *cur, /* btree cursor */
  648. int level) /* level to change */
  649. {
  650. struct xfs_btree_block *block; /* generic btree block pointer */
  651. xfs_buf_t *bp; /* buffer containing block */
  652. /*
  653. * Get the block pointer for this level.
  654. */
  655. block = xfs_btree_get_block(cur, level, &bp);
  656. if (xfs_btree_check_block(cur, block, level, bp))
  657. return 0;
  658. /*
  659. * It's empty, there is no such record.
  660. */
  661. if (!block->bb_numrecs)
  662. return 0;
  663. /*
  664. * Set the ptr value to 1, that's the first record/key.
  665. */
  666. cur->bc_ptrs[level] = 1;
  667. return 1;
  668. }
  669. /*
  670. * Change the cursor to point to the last record in the current block
  671. * at the given level. Other levels are unaffected.
  672. */
  673. STATIC int /* success=1, failure=0 */
  674. xfs_btree_lastrec(
  675. xfs_btree_cur_t *cur, /* btree cursor */
  676. int level) /* level to change */
  677. {
  678. struct xfs_btree_block *block; /* generic btree block pointer */
  679. xfs_buf_t *bp; /* buffer containing block */
  680. /*
  681. * Get the block pointer for this level.
  682. */
  683. block = xfs_btree_get_block(cur, level, &bp);
  684. if (xfs_btree_check_block(cur, block, level, bp))
  685. return 0;
  686. /*
  687. * It's empty, there is no such record.
  688. */
  689. if (!block->bb_numrecs)
  690. return 0;
  691. /*
  692. * Set the ptr value to numrecs, that's the last record/key.
  693. */
  694. cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
  695. return 1;
  696. }
  697. /*
  698. * Compute first and last byte offsets for the fields given.
  699. * Interprets the offsets table, which contains struct field offsets.
  700. */
  701. void
  702. xfs_btree_offsets(
  703. __int64_t fields, /* bitmask of fields */
  704. const short *offsets, /* table of field offsets */
  705. int nbits, /* number of bits to inspect */
  706. int *first, /* output: first byte offset */
  707. int *last) /* output: last byte offset */
  708. {
  709. int i; /* current bit number */
  710. __int64_t imask; /* mask for current bit number */
  711. ASSERT(fields != 0);
  712. /*
  713. * Find the lowest bit, so the first byte offset.
  714. */
  715. for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
  716. if (imask & fields) {
  717. *first = offsets[i];
  718. break;
  719. }
  720. }
  721. /*
  722. * Find the highest bit, so the last byte offset.
  723. */
  724. for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
  725. if (imask & fields) {
  726. *last = offsets[i + 1] - 1;
  727. break;
  728. }
  729. }
  730. }
  731. /*
  732. * Get a buffer for the block, return it read in.
  733. * Long-form addressing.
  734. */
  735. int
  736. xfs_btree_read_bufl(
  737. struct xfs_mount *mp, /* file system mount point */
  738. struct xfs_trans *tp, /* transaction pointer */
  739. xfs_fsblock_t fsbno, /* file system block number */
  740. uint lock, /* lock flags for read_buf */
  741. struct xfs_buf **bpp, /* buffer for fsbno */
  742. int refval, /* ref count value for buffer */
  743. const struct xfs_buf_ops *ops)
  744. {
  745. struct xfs_buf *bp; /* return value */
  746. xfs_daddr_t d; /* real disk block address */
  747. int error;
  748. if (!XFS_FSB_SANITY_CHECK(mp, fsbno))
  749. return -EFSCORRUPTED;
  750. d = XFS_FSB_TO_DADDR(mp, fsbno);
  751. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  752. mp->m_bsize, lock, &bp, ops);
  753. if (error)
  754. return error;
  755. if (bp)
  756. xfs_buf_set_ref(bp, refval);
  757. *bpp = bp;
  758. return 0;
  759. }
  760. /*
  761. * Read-ahead the block, don't wait for it, don't return a buffer.
  762. * Long-form addressing.
  763. */
  764. /* ARGSUSED */
  765. void
  766. xfs_btree_reada_bufl(
  767. struct xfs_mount *mp, /* file system mount point */
  768. xfs_fsblock_t fsbno, /* file system block number */
  769. xfs_extlen_t count, /* count of filesystem blocks */
  770. const struct xfs_buf_ops *ops)
  771. {
  772. xfs_daddr_t d;
  773. ASSERT(fsbno != NULLFSBLOCK);
  774. d = XFS_FSB_TO_DADDR(mp, fsbno);
  775. xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
  776. }
  777. /*
  778. * Read-ahead the block, don't wait for it, don't return a buffer.
  779. * Short-form addressing.
  780. */
  781. /* ARGSUSED */
  782. void
  783. xfs_btree_reada_bufs(
  784. struct xfs_mount *mp, /* file system mount point */
  785. xfs_agnumber_t agno, /* allocation group number */
  786. xfs_agblock_t agbno, /* allocation group block number */
  787. xfs_extlen_t count, /* count of filesystem blocks */
  788. const struct xfs_buf_ops *ops)
  789. {
  790. xfs_daddr_t d;
  791. ASSERT(agno != NULLAGNUMBER);
  792. ASSERT(agbno != NULLAGBLOCK);
  793. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  794. xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
  795. }
  796. STATIC int
  797. xfs_btree_readahead_lblock(
  798. struct xfs_btree_cur *cur,
  799. int lr,
  800. struct xfs_btree_block *block)
  801. {
  802. int rval = 0;
  803. xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
  804. xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
  805. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
  806. xfs_btree_reada_bufl(cur->bc_mp, left, 1,
  807. cur->bc_ops->buf_ops);
  808. rval++;
  809. }
  810. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
  811. xfs_btree_reada_bufl(cur->bc_mp, right, 1,
  812. cur->bc_ops->buf_ops);
  813. rval++;
  814. }
  815. return rval;
  816. }
  817. STATIC int
  818. xfs_btree_readahead_sblock(
  819. struct xfs_btree_cur *cur,
  820. int lr,
  821. struct xfs_btree_block *block)
  822. {
  823. int rval = 0;
  824. xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
  825. xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
  826. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
  827. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  828. left, 1, cur->bc_ops->buf_ops);
  829. rval++;
  830. }
  831. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
  832. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  833. right, 1, cur->bc_ops->buf_ops);
  834. rval++;
  835. }
  836. return rval;
  837. }
  838. /*
  839. * Read-ahead btree blocks, at the given level.
  840. * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
  841. */
  842. STATIC int
  843. xfs_btree_readahead(
  844. struct xfs_btree_cur *cur, /* btree cursor */
  845. int lev, /* level in btree */
  846. int lr) /* left/right bits */
  847. {
  848. struct xfs_btree_block *block;
  849. /*
  850. * No readahead needed if we are at the root level and the
  851. * btree root is stored in the inode.
  852. */
  853. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  854. (lev == cur->bc_nlevels - 1))
  855. return 0;
  856. if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
  857. return 0;
  858. cur->bc_ra[lev] |= lr;
  859. block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
  860. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  861. return xfs_btree_readahead_lblock(cur, lr, block);
  862. return xfs_btree_readahead_sblock(cur, lr, block);
  863. }
  864. STATIC xfs_daddr_t
  865. xfs_btree_ptr_to_daddr(
  866. struct xfs_btree_cur *cur,
  867. union xfs_btree_ptr *ptr)
  868. {
  869. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  870. ASSERT(ptr->l != cpu_to_be64(NULLFSBLOCK));
  871. return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
  872. } else {
  873. ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
  874. ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
  875. return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
  876. be32_to_cpu(ptr->s));
  877. }
  878. }
  879. /*
  880. * Readahead @count btree blocks at the given @ptr location.
  881. *
  882. * We don't need to care about long or short form btrees here as we have a
  883. * method of converting the ptr directly to a daddr available to us.
  884. */
  885. STATIC void
  886. xfs_btree_readahead_ptr(
  887. struct xfs_btree_cur *cur,
  888. union xfs_btree_ptr *ptr,
  889. xfs_extlen_t count)
  890. {
  891. xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
  892. xfs_btree_ptr_to_daddr(cur, ptr),
  893. cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
  894. }
  895. /*
  896. * Set the buffer for level "lev" in the cursor to bp, releasing
  897. * any previous buffer.
  898. */
  899. STATIC void
  900. xfs_btree_setbuf(
  901. xfs_btree_cur_t *cur, /* btree cursor */
  902. int lev, /* level in btree */
  903. xfs_buf_t *bp) /* new buffer to set */
  904. {
  905. struct xfs_btree_block *b; /* btree block */
  906. if (cur->bc_bufs[lev])
  907. xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
  908. cur->bc_bufs[lev] = bp;
  909. cur->bc_ra[lev] = 0;
  910. b = XFS_BUF_TO_BLOCK(bp);
  911. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  912. if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
  913. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  914. if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
  915. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  916. } else {
  917. if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
  918. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  919. if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
  920. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  921. }
  922. }
  923. STATIC int
  924. xfs_btree_ptr_is_null(
  925. struct xfs_btree_cur *cur,
  926. union xfs_btree_ptr *ptr)
  927. {
  928. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  929. return ptr->l == cpu_to_be64(NULLFSBLOCK);
  930. else
  931. return ptr->s == cpu_to_be32(NULLAGBLOCK);
  932. }
  933. STATIC void
  934. xfs_btree_set_ptr_null(
  935. struct xfs_btree_cur *cur,
  936. union xfs_btree_ptr *ptr)
  937. {
  938. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  939. ptr->l = cpu_to_be64(NULLFSBLOCK);
  940. else
  941. ptr->s = cpu_to_be32(NULLAGBLOCK);
  942. }
  943. /*
  944. * Get/set/init sibling pointers
  945. */
  946. STATIC void
  947. xfs_btree_get_sibling(
  948. struct xfs_btree_cur *cur,
  949. struct xfs_btree_block *block,
  950. union xfs_btree_ptr *ptr,
  951. int lr)
  952. {
  953. ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
  954. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  955. if (lr == XFS_BB_RIGHTSIB)
  956. ptr->l = block->bb_u.l.bb_rightsib;
  957. else
  958. ptr->l = block->bb_u.l.bb_leftsib;
  959. } else {
  960. if (lr == XFS_BB_RIGHTSIB)
  961. ptr->s = block->bb_u.s.bb_rightsib;
  962. else
  963. ptr->s = block->bb_u.s.bb_leftsib;
  964. }
  965. }
  966. STATIC void
  967. xfs_btree_set_sibling(
  968. struct xfs_btree_cur *cur,
  969. struct xfs_btree_block *block,
  970. union xfs_btree_ptr *ptr,
  971. int lr)
  972. {
  973. ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
  974. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  975. if (lr == XFS_BB_RIGHTSIB)
  976. block->bb_u.l.bb_rightsib = ptr->l;
  977. else
  978. block->bb_u.l.bb_leftsib = ptr->l;
  979. } else {
  980. if (lr == XFS_BB_RIGHTSIB)
  981. block->bb_u.s.bb_rightsib = ptr->s;
  982. else
  983. block->bb_u.s.bb_leftsib = ptr->s;
  984. }
  985. }
  986. void
  987. xfs_btree_init_block_int(
  988. struct xfs_mount *mp,
  989. struct xfs_btree_block *buf,
  990. xfs_daddr_t blkno,
  991. __u32 magic,
  992. __u16 level,
  993. __u16 numrecs,
  994. __u64 owner,
  995. unsigned int flags)
  996. {
  997. buf->bb_magic = cpu_to_be32(magic);
  998. buf->bb_level = cpu_to_be16(level);
  999. buf->bb_numrecs = cpu_to_be16(numrecs);
  1000. if (flags & XFS_BTREE_LONG_PTRS) {
  1001. buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
  1002. buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
  1003. if (flags & XFS_BTREE_CRC_BLOCKS) {
  1004. buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
  1005. buf->bb_u.l.bb_owner = cpu_to_be64(owner);
  1006. uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
  1007. buf->bb_u.l.bb_pad = 0;
  1008. buf->bb_u.l.bb_lsn = 0;
  1009. }
  1010. } else {
  1011. /* owner is a 32 bit value on short blocks */
  1012. __u32 __owner = (__u32)owner;
  1013. buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  1014. buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  1015. if (flags & XFS_BTREE_CRC_BLOCKS) {
  1016. buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
  1017. buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
  1018. uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
  1019. buf->bb_u.s.bb_lsn = 0;
  1020. }
  1021. }
  1022. }
  1023. void
  1024. xfs_btree_init_block(
  1025. struct xfs_mount *mp,
  1026. struct xfs_buf *bp,
  1027. __u32 magic,
  1028. __u16 level,
  1029. __u16 numrecs,
  1030. __u64 owner,
  1031. unsigned int flags)
  1032. {
  1033. xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
  1034. magic, level, numrecs, owner, flags);
  1035. }
  1036. STATIC void
  1037. xfs_btree_init_block_cur(
  1038. struct xfs_btree_cur *cur,
  1039. struct xfs_buf *bp,
  1040. int level,
  1041. int numrecs)
  1042. {
  1043. __u64 owner;
  1044. /*
  1045. * we can pull the owner from the cursor right now as the different
  1046. * owners align directly with the pointer size of the btree. This may
  1047. * change in future, but is safe for current users of the generic btree
  1048. * code.
  1049. */
  1050. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  1051. owner = cur->bc_private.b.ip->i_ino;
  1052. else
  1053. owner = cur->bc_private.a.agno;
  1054. xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
  1055. xfs_btree_magic(cur), level, numrecs,
  1056. owner, cur->bc_flags);
  1057. }
  1058. /*
  1059. * Return true if ptr is the last record in the btree and
  1060. * we need to track updates to this record. The decision
  1061. * will be further refined in the update_lastrec method.
  1062. */
  1063. STATIC int
  1064. xfs_btree_is_lastrec(
  1065. struct xfs_btree_cur *cur,
  1066. struct xfs_btree_block *block,
  1067. int level)
  1068. {
  1069. union xfs_btree_ptr ptr;
  1070. if (level > 0)
  1071. return 0;
  1072. if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
  1073. return 0;
  1074. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1075. if (!xfs_btree_ptr_is_null(cur, &ptr))
  1076. return 0;
  1077. return 1;
  1078. }
  1079. STATIC void
  1080. xfs_btree_buf_to_ptr(
  1081. struct xfs_btree_cur *cur,
  1082. struct xfs_buf *bp,
  1083. union xfs_btree_ptr *ptr)
  1084. {
  1085. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  1086. ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
  1087. XFS_BUF_ADDR(bp)));
  1088. else {
  1089. ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
  1090. XFS_BUF_ADDR(bp)));
  1091. }
  1092. }
  1093. STATIC void
  1094. xfs_btree_set_refs(
  1095. struct xfs_btree_cur *cur,
  1096. struct xfs_buf *bp)
  1097. {
  1098. switch (cur->bc_btnum) {
  1099. case XFS_BTNUM_BNO:
  1100. case XFS_BTNUM_CNT:
  1101. xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
  1102. break;
  1103. case XFS_BTNUM_INO:
  1104. case XFS_BTNUM_FINO:
  1105. xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
  1106. break;
  1107. case XFS_BTNUM_BMAP:
  1108. xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
  1109. break;
  1110. case XFS_BTNUM_RMAP:
  1111. xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
  1112. break;
  1113. case XFS_BTNUM_REFC:
  1114. xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
  1115. break;
  1116. default:
  1117. ASSERT(0);
  1118. }
  1119. }
  1120. STATIC int
  1121. xfs_btree_get_buf_block(
  1122. struct xfs_btree_cur *cur,
  1123. union xfs_btree_ptr *ptr,
  1124. int flags,
  1125. struct xfs_btree_block **block,
  1126. struct xfs_buf **bpp)
  1127. {
  1128. struct xfs_mount *mp = cur->bc_mp;
  1129. xfs_daddr_t d;
  1130. /* need to sort out how callers deal with failures first */
  1131. ASSERT(!(flags & XBF_TRYLOCK));
  1132. d = xfs_btree_ptr_to_daddr(cur, ptr);
  1133. *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
  1134. mp->m_bsize, flags);
  1135. if (!*bpp)
  1136. return -ENOMEM;
  1137. (*bpp)->b_ops = cur->bc_ops->buf_ops;
  1138. *block = XFS_BUF_TO_BLOCK(*bpp);
  1139. return 0;
  1140. }
  1141. /*
  1142. * Read in the buffer at the given ptr and return the buffer and
  1143. * the block pointer within the buffer.
  1144. */
  1145. STATIC int
  1146. xfs_btree_read_buf_block(
  1147. struct xfs_btree_cur *cur,
  1148. union xfs_btree_ptr *ptr,
  1149. int flags,
  1150. struct xfs_btree_block **block,
  1151. struct xfs_buf **bpp)
  1152. {
  1153. struct xfs_mount *mp = cur->bc_mp;
  1154. xfs_daddr_t d;
  1155. int error;
  1156. /* need to sort out how callers deal with failures first */
  1157. ASSERT(!(flags & XBF_TRYLOCK));
  1158. d = xfs_btree_ptr_to_daddr(cur, ptr);
  1159. error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
  1160. mp->m_bsize, flags, bpp,
  1161. cur->bc_ops->buf_ops);
  1162. if (error)
  1163. return error;
  1164. xfs_btree_set_refs(cur, *bpp);
  1165. *block = XFS_BUF_TO_BLOCK(*bpp);
  1166. return 0;
  1167. }
  1168. /*
  1169. * Copy keys from one btree block to another.
  1170. */
  1171. STATIC void
  1172. xfs_btree_copy_keys(
  1173. struct xfs_btree_cur *cur,
  1174. union xfs_btree_key *dst_key,
  1175. union xfs_btree_key *src_key,
  1176. int numkeys)
  1177. {
  1178. ASSERT(numkeys >= 0);
  1179. memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
  1180. }
  1181. /*
  1182. * Copy records from one btree block to another.
  1183. */
  1184. STATIC void
  1185. xfs_btree_copy_recs(
  1186. struct xfs_btree_cur *cur,
  1187. union xfs_btree_rec *dst_rec,
  1188. union xfs_btree_rec *src_rec,
  1189. int numrecs)
  1190. {
  1191. ASSERT(numrecs >= 0);
  1192. memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
  1193. }
  1194. /*
  1195. * Copy block pointers from one btree block to another.
  1196. */
  1197. STATIC void
  1198. xfs_btree_copy_ptrs(
  1199. struct xfs_btree_cur *cur,
  1200. union xfs_btree_ptr *dst_ptr,
  1201. union xfs_btree_ptr *src_ptr,
  1202. int numptrs)
  1203. {
  1204. ASSERT(numptrs >= 0);
  1205. memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
  1206. }
  1207. /*
  1208. * Shift keys one index left/right inside a single btree block.
  1209. */
  1210. STATIC void
  1211. xfs_btree_shift_keys(
  1212. struct xfs_btree_cur *cur,
  1213. union xfs_btree_key *key,
  1214. int dir,
  1215. int numkeys)
  1216. {
  1217. char *dst_key;
  1218. ASSERT(numkeys >= 0);
  1219. ASSERT(dir == 1 || dir == -1);
  1220. dst_key = (char *)key + (dir * cur->bc_ops->key_len);
  1221. memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
  1222. }
  1223. /*
  1224. * Shift records one index left/right inside a single btree block.
  1225. */
  1226. STATIC void
  1227. xfs_btree_shift_recs(
  1228. struct xfs_btree_cur *cur,
  1229. union xfs_btree_rec *rec,
  1230. int dir,
  1231. int numrecs)
  1232. {
  1233. char *dst_rec;
  1234. ASSERT(numrecs >= 0);
  1235. ASSERT(dir == 1 || dir == -1);
  1236. dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
  1237. memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
  1238. }
  1239. /*
  1240. * Shift block pointers one index left/right inside a single btree block.
  1241. */
  1242. STATIC void
  1243. xfs_btree_shift_ptrs(
  1244. struct xfs_btree_cur *cur,
  1245. union xfs_btree_ptr *ptr,
  1246. int dir,
  1247. int numptrs)
  1248. {
  1249. char *dst_ptr;
  1250. ASSERT(numptrs >= 0);
  1251. ASSERT(dir == 1 || dir == -1);
  1252. dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
  1253. memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
  1254. }
  1255. /*
  1256. * Log key values from the btree block.
  1257. */
  1258. STATIC void
  1259. xfs_btree_log_keys(
  1260. struct xfs_btree_cur *cur,
  1261. struct xfs_buf *bp,
  1262. int first,
  1263. int last)
  1264. {
  1265. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1266. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1267. if (bp) {
  1268. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1269. xfs_trans_log_buf(cur->bc_tp, bp,
  1270. xfs_btree_key_offset(cur, first),
  1271. xfs_btree_key_offset(cur, last + 1) - 1);
  1272. } else {
  1273. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1274. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1275. }
  1276. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1277. }
  1278. /*
  1279. * Log record values from the btree block.
  1280. */
  1281. void
  1282. xfs_btree_log_recs(
  1283. struct xfs_btree_cur *cur,
  1284. struct xfs_buf *bp,
  1285. int first,
  1286. int last)
  1287. {
  1288. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1289. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1290. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1291. xfs_trans_log_buf(cur->bc_tp, bp,
  1292. xfs_btree_rec_offset(cur, first),
  1293. xfs_btree_rec_offset(cur, last + 1) - 1);
  1294. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1295. }
  1296. /*
  1297. * Log block pointer fields from a btree block (nonleaf).
  1298. */
  1299. STATIC void
  1300. xfs_btree_log_ptrs(
  1301. struct xfs_btree_cur *cur, /* btree cursor */
  1302. struct xfs_buf *bp, /* buffer containing btree block */
  1303. int first, /* index of first pointer to log */
  1304. int last) /* index of last pointer to log */
  1305. {
  1306. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1307. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1308. if (bp) {
  1309. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  1310. int level = xfs_btree_get_level(block);
  1311. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1312. xfs_trans_log_buf(cur->bc_tp, bp,
  1313. xfs_btree_ptr_offset(cur, first, level),
  1314. xfs_btree_ptr_offset(cur, last + 1, level) - 1);
  1315. } else {
  1316. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1317. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1318. }
  1319. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1320. }
  1321. /*
  1322. * Log fields from a btree block header.
  1323. */
  1324. void
  1325. xfs_btree_log_block(
  1326. struct xfs_btree_cur *cur, /* btree cursor */
  1327. struct xfs_buf *bp, /* buffer containing btree block */
  1328. int fields) /* mask of fields: XFS_BB_... */
  1329. {
  1330. int first; /* first byte offset logged */
  1331. int last; /* last byte offset logged */
  1332. static const short soffsets[] = { /* table of offsets (short) */
  1333. offsetof(struct xfs_btree_block, bb_magic),
  1334. offsetof(struct xfs_btree_block, bb_level),
  1335. offsetof(struct xfs_btree_block, bb_numrecs),
  1336. offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
  1337. offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
  1338. offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
  1339. offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
  1340. offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
  1341. offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
  1342. offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
  1343. XFS_BTREE_SBLOCK_CRC_LEN
  1344. };
  1345. static const short loffsets[] = { /* table of offsets (long) */
  1346. offsetof(struct xfs_btree_block, bb_magic),
  1347. offsetof(struct xfs_btree_block, bb_level),
  1348. offsetof(struct xfs_btree_block, bb_numrecs),
  1349. offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
  1350. offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
  1351. offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
  1352. offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
  1353. offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
  1354. offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
  1355. offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
  1356. offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
  1357. XFS_BTREE_LBLOCK_CRC_LEN
  1358. };
  1359. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1360. XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
  1361. if (bp) {
  1362. int nbits;
  1363. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
  1364. /*
  1365. * We don't log the CRC when updating a btree
  1366. * block but instead recreate it during log
  1367. * recovery. As the log buffers have checksums
  1368. * of their own this is safe and avoids logging a crc
  1369. * update in a lot of places.
  1370. */
  1371. if (fields == XFS_BB_ALL_BITS)
  1372. fields = XFS_BB_ALL_BITS_CRC;
  1373. nbits = XFS_BB_NUM_BITS_CRC;
  1374. } else {
  1375. nbits = XFS_BB_NUM_BITS;
  1376. }
  1377. xfs_btree_offsets(fields,
  1378. (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  1379. loffsets : soffsets,
  1380. nbits, &first, &last);
  1381. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1382. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  1383. } else {
  1384. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1385. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1386. }
  1387. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1388. }
  1389. /*
  1390. * Increment cursor by one record at the level.
  1391. * For nonzero levels the leaf-ward information is untouched.
  1392. */
  1393. int /* error */
  1394. xfs_btree_increment(
  1395. struct xfs_btree_cur *cur,
  1396. int level,
  1397. int *stat) /* success/failure */
  1398. {
  1399. struct xfs_btree_block *block;
  1400. union xfs_btree_ptr ptr;
  1401. struct xfs_buf *bp;
  1402. int error; /* error return value */
  1403. int lev;
  1404. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1405. XFS_BTREE_TRACE_ARGI(cur, level);
  1406. ASSERT(level < cur->bc_nlevels);
  1407. /* Read-ahead to the right at this level. */
  1408. xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
  1409. /* Get a pointer to the btree block. */
  1410. block = xfs_btree_get_block(cur, level, &bp);
  1411. #ifdef DEBUG
  1412. error = xfs_btree_check_block(cur, block, level, bp);
  1413. if (error)
  1414. goto error0;
  1415. #endif
  1416. /* We're done if we remain in the block after the increment. */
  1417. if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
  1418. goto out1;
  1419. /* Fail if we just went off the right edge of the tree. */
  1420. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1421. if (xfs_btree_ptr_is_null(cur, &ptr))
  1422. goto out0;
  1423. XFS_BTREE_STATS_INC(cur, increment);
  1424. /*
  1425. * March up the tree incrementing pointers.
  1426. * Stop when we don't go off the right edge of a block.
  1427. */
  1428. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1429. block = xfs_btree_get_block(cur, lev, &bp);
  1430. #ifdef DEBUG
  1431. error = xfs_btree_check_block(cur, block, lev, bp);
  1432. if (error)
  1433. goto error0;
  1434. #endif
  1435. if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
  1436. break;
  1437. /* Read-ahead the right block for the next loop. */
  1438. xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
  1439. }
  1440. /*
  1441. * If we went off the root then we are either seriously
  1442. * confused or have the tree root in an inode.
  1443. */
  1444. if (lev == cur->bc_nlevels) {
  1445. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1446. goto out0;
  1447. ASSERT(0);
  1448. error = -EFSCORRUPTED;
  1449. goto error0;
  1450. }
  1451. ASSERT(lev < cur->bc_nlevels);
  1452. /*
  1453. * Now walk back down the tree, fixing up the cursor's buffer
  1454. * pointers and key numbers.
  1455. */
  1456. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1457. union xfs_btree_ptr *ptrp;
  1458. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1459. --lev;
  1460. error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
  1461. if (error)
  1462. goto error0;
  1463. xfs_btree_setbuf(cur, lev, bp);
  1464. cur->bc_ptrs[lev] = 1;
  1465. }
  1466. out1:
  1467. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1468. *stat = 1;
  1469. return 0;
  1470. out0:
  1471. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1472. *stat = 0;
  1473. return 0;
  1474. error0:
  1475. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1476. return error;
  1477. }
  1478. /*
  1479. * Decrement cursor by one record at the level.
  1480. * For nonzero levels the leaf-ward information is untouched.
  1481. */
  1482. int /* error */
  1483. xfs_btree_decrement(
  1484. struct xfs_btree_cur *cur,
  1485. int level,
  1486. int *stat) /* success/failure */
  1487. {
  1488. struct xfs_btree_block *block;
  1489. xfs_buf_t *bp;
  1490. int error; /* error return value */
  1491. int lev;
  1492. union xfs_btree_ptr ptr;
  1493. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1494. XFS_BTREE_TRACE_ARGI(cur, level);
  1495. ASSERT(level < cur->bc_nlevels);
  1496. /* Read-ahead to the left at this level. */
  1497. xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
  1498. /* We're done if we remain in the block after the decrement. */
  1499. if (--cur->bc_ptrs[level] > 0)
  1500. goto out1;
  1501. /* Get a pointer to the btree block. */
  1502. block = xfs_btree_get_block(cur, level, &bp);
  1503. #ifdef DEBUG
  1504. error = xfs_btree_check_block(cur, block, level, bp);
  1505. if (error)
  1506. goto error0;
  1507. #endif
  1508. /* Fail if we just went off the left edge of the tree. */
  1509. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
  1510. if (xfs_btree_ptr_is_null(cur, &ptr))
  1511. goto out0;
  1512. XFS_BTREE_STATS_INC(cur, decrement);
  1513. /*
  1514. * March up the tree decrementing pointers.
  1515. * Stop when we don't go off the left edge of a block.
  1516. */
  1517. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1518. if (--cur->bc_ptrs[lev] > 0)
  1519. break;
  1520. /* Read-ahead the left block for the next loop. */
  1521. xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
  1522. }
  1523. /*
  1524. * If we went off the root then we are seriously confused.
  1525. * or the root of the tree is in an inode.
  1526. */
  1527. if (lev == cur->bc_nlevels) {
  1528. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1529. goto out0;
  1530. ASSERT(0);
  1531. error = -EFSCORRUPTED;
  1532. goto error0;
  1533. }
  1534. ASSERT(lev < cur->bc_nlevels);
  1535. /*
  1536. * Now walk back down the tree, fixing up the cursor's buffer
  1537. * pointers and key numbers.
  1538. */
  1539. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1540. union xfs_btree_ptr *ptrp;
  1541. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1542. --lev;
  1543. error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
  1544. if (error)
  1545. goto error0;
  1546. xfs_btree_setbuf(cur, lev, bp);
  1547. cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
  1548. }
  1549. out1:
  1550. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1551. *stat = 1;
  1552. return 0;
  1553. out0:
  1554. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1555. *stat = 0;
  1556. return 0;
  1557. error0:
  1558. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1559. return error;
  1560. }
  1561. STATIC int
  1562. xfs_btree_lookup_get_block(
  1563. struct xfs_btree_cur *cur, /* btree cursor */
  1564. int level, /* level in the btree */
  1565. union xfs_btree_ptr *pp, /* ptr to btree block */
  1566. struct xfs_btree_block **blkp) /* return btree block */
  1567. {
  1568. struct xfs_buf *bp; /* buffer pointer for btree block */
  1569. int error = 0;
  1570. /* special case the root block if in an inode */
  1571. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1572. (level == cur->bc_nlevels - 1)) {
  1573. *blkp = xfs_btree_get_iroot(cur);
  1574. return 0;
  1575. }
  1576. /*
  1577. * If the old buffer at this level for the disk address we are
  1578. * looking for re-use it.
  1579. *
  1580. * Otherwise throw it away and get a new one.
  1581. */
  1582. bp = cur->bc_bufs[level];
  1583. if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
  1584. *blkp = XFS_BUF_TO_BLOCK(bp);
  1585. return 0;
  1586. }
  1587. error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
  1588. if (error)
  1589. return error;
  1590. /* Check the inode owner since the verifiers don't. */
  1591. if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
  1592. !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_INVALID_OWNER) &&
  1593. (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
  1594. be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
  1595. cur->bc_private.b.ip->i_ino)
  1596. goto out_bad;
  1597. /* Did we get the level we were looking for? */
  1598. if (be16_to_cpu((*blkp)->bb_level) != level)
  1599. goto out_bad;
  1600. /* Check that internal nodes have at least one record. */
  1601. if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
  1602. goto out_bad;
  1603. xfs_btree_setbuf(cur, level, bp);
  1604. return 0;
  1605. out_bad:
  1606. *blkp = NULL;
  1607. xfs_trans_brelse(cur->bc_tp, bp);
  1608. return -EFSCORRUPTED;
  1609. }
  1610. /*
  1611. * Get current search key. For level 0 we don't actually have a key
  1612. * structure so we make one up from the record. For all other levels
  1613. * we just return the right key.
  1614. */
  1615. STATIC union xfs_btree_key *
  1616. xfs_lookup_get_search_key(
  1617. struct xfs_btree_cur *cur,
  1618. int level,
  1619. int keyno,
  1620. struct xfs_btree_block *block,
  1621. union xfs_btree_key *kp)
  1622. {
  1623. if (level == 0) {
  1624. cur->bc_ops->init_key_from_rec(kp,
  1625. xfs_btree_rec_addr(cur, keyno, block));
  1626. return kp;
  1627. }
  1628. return xfs_btree_key_addr(cur, keyno, block);
  1629. }
  1630. /*
  1631. * Lookup the record. The cursor is made to point to it, based on dir.
  1632. * stat is set to 0 if can't find any such record, 1 for success.
  1633. */
  1634. int /* error */
  1635. xfs_btree_lookup(
  1636. struct xfs_btree_cur *cur, /* btree cursor */
  1637. xfs_lookup_t dir, /* <=, ==, or >= */
  1638. int *stat) /* success/failure */
  1639. {
  1640. struct xfs_btree_block *block; /* current btree block */
  1641. __int64_t diff; /* difference for the current key */
  1642. int error; /* error return value */
  1643. int keyno; /* current key number */
  1644. int level; /* level in the btree */
  1645. union xfs_btree_ptr *pp; /* ptr to btree block */
  1646. union xfs_btree_ptr ptr; /* ptr to btree block */
  1647. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1648. XFS_BTREE_TRACE_ARGI(cur, dir);
  1649. XFS_BTREE_STATS_INC(cur, lookup);
  1650. /* No such thing as a zero-level tree. */
  1651. if (cur->bc_nlevels == 0)
  1652. return -EFSCORRUPTED;
  1653. block = NULL;
  1654. keyno = 0;
  1655. /* initialise start pointer from cursor */
  1656. cur->bc_ops->init_ptr_from_cur(cur, &ptr);
  1657. pp = &ptr;
  1658. /*
  1659. * Iterate over each level in the btree, starting at the root.
  1660. * For each level above the leaves, find the key we need, based
  1661. * on the lookup record, then follow the corresponding block
  1662. * pointer down to the next level.
  1663. */
  1664. for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
  1665. /* Get the block we need to do the lookup on. */
  1666. error = xfs_btree_lookup_get_block(cur, level, pp, &block);
  1667. if (error)
  1668. goto error0;
  1669. if (diff == 0) {
  1670. /*
  1671. * If we already had a key match at a higher level, we
  1672. * know we need to use the first entry in this block.
  1673. */
  1674. keyno = 1;
  1675. } else {
  1676. /* Otherwise search this block. Do a binary search. */
  1677. int high; /* high entry number */
  1678. int low; /* low entry number */
  1679. /* Set low and high entry numbers, 1-based. */
  1680. low = 1;
  1681. high = xfs_btree_get_numrecs(block);
  1682. if (!high) {
  1683. /* Block is empty, must be an empty leaf. */
  1684. ASSERT(level == 0 && cur->bc_nlevels == 1);
  1685. cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
  1686. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1687. *stat = 0;
  1688. return 0;
  1689. }
  1690. /* Binary search the block. */
  1691. while (low <= high) {
  1692. union xfs_btree_key key;
  1693. union xfs_btree_key *kp;
  1694. XFS_BTREE_STATS_INC(cur, compare);
  1695. /* keyno is average of low and high. */
  1696. keyno = (low + high) >> 1;
  1697. /* Get current search key */
  1698. kp = xfs_lookup_get_search_key(cur, level,
  1699. keyno, block, &key);
  1700. /*
  1701. * Compute difference to get next direction:
  1702. * - less than, move right
  1703. * - greater than, move left
  1704. * - equal, we're done
  1705. */
  1706. diff = cur->bc_ops->key_diff(cur, kp);
  1707. if (diff < 0)
  1708. low = keyno + 1;
  1709. else if (diff > 0)
  1710. high = keyno - 1;
  1711. else
  1712. break;
  1713. }
  1714. }
  1715. /*
  1716. * If there are more levels, set up for the next level
  1717. * by getting the block number and filling in the cursor.
  1718. */
  1719. if (level > 0) {
  1720. /*
  1721. * If we moved left, need the previous key number,
  1722. * unless there isn't one.
  1723. */
  1724. if (diff > 0 && --keyno < 1)
  1725. keyno = 1;
  1726. pp = xfs_btree_ptr_addr(cur, keyno, block);
  1727. #ifdef DEBUG
  1728. error = xfs_btree_check_ptr(cur, pp, 0, level);
  1729. if (error)
  1730. goto error0;
  1731. #endif
  1732. cur->bc_ptrs[level] = keyno;
  1733. }
  1734. }
  1735. /* Done with the search. See if we need to adjust the results. */
  1736. if (dir != XFS_LOOKUP_LE && diff < 0) {
  1737. keyno++;
  1738. /*
  1739. * If ge search and we went off the end of the block, but it's
  1740. * not the last block, we're in the wrong block.
  1741. */
  1742. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1743. if (dir == XFS_LOOKUP_GE &&
  1744. keyno > xfs_btree_get_numrecs(block) &&
  1745. !xfs_btree_ptr_is_null(cur, &ptr)) {
  1746. int i;
  1747. cur->bc_ptrs[0] = keyno;
  1748. error = xfs_btree_increment(cur, 0, &i);
  1749. if (error)
  1750. goto error0;
  1751. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1752. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1753. *stat = 1;
  1754. return 0;
  1755. }
  1756. } else if (dir == XFS_LOOKUP_LE && diff > 0)
  1757. keyno--;
  1758. cur->bc_ptrs[0] = keyno;
  1759. /* Return if we succeeded or not. */
  1760. if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
  1761. *stat = 0;
  1762. else if (dir != XFS_LOOKUP_EQ || diff == 0)
  1763. *stat = 1;
  1764. else
  1765. *stat = 0;
  1766. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1767. return 0;
  1768. error0:
  1769. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1770. return error;
  1771. }
  1772. /* Find the high key storage area from a regular key. */
  1773. STATIC union xfs_btree_key *
  1774. xfs_btree_high_key_from_key(
  1775. struct xfs_btree_cur *cur,
  1776. union xfs_btree_key *key)
  1777. {
  1778. ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
  1779. return (union xfs_btree_key *)((char *)key +
  1780. (cur->bc_ops->key_len / 2));
  1781. }
  1782. /* Determine the low (and high if overlapped) keys of a leaf block */
  1783. STATIC void
  1784. xfs_btree_get_leaf_keys(
  1785. struct xfs_btree_cur *cur,
  1786. struct xfs_btree_block *block,
  1787. union xfs_btree_key *key)
  1788. {
  1789. union xfs_btree_key max_hkey;
  1790. union xfs_btree_key hkey;
  1791. union xfs_btree_rec *rec;
  1792. union xfs_btree_key *high;
  1793. int n;
  1794. rec = xfs_btree_rec_addr(cur, 1, block);
  1795. cur->bc_ops->init_key_from_rec(key, rec);
  1796. if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
  1797. cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
  1798. for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
  1799. rec = xfs_btree_rec_addr(cur, n, block);
  1800. cur->bc_ops->init_high_key_from_rec(&hkey, rec);
  1801. if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
  1802. > 0)
  1803. max_hkey = hkey;
  1804. }
  1805. high = xfs_btree_high_key_from_key(cur, key);
  1806. memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
  1807. }
  1808. }
  1809. /* Determine the low (and high if overlapped) keys of a node block */
  1810. STATIC void
  1811. xfs_btree_get_node_keys(
  1812. struct xfs_btree_cur *cur,
  1813. struct xfs_btree_block *block,
  1814. union xfs_btree_key *key)
  1815. {
  1816. union xfs_btree_key *hkey;
  1817. union xfs_btree_key *max_hkey;
  1818. union xfs_btree_key *high;
  1819. int n;
  1820. if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
  1821. memcpy(key, xfs_btree_key_addr(cur, 1, block),
  1822. cur->bc_ops->key_len / 2);
  1823. max_hkey = xfs_btree_high_key_addr(cur, 1, block);
  1824. for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
  1825. hkey = xfs_btree_high_key_addr(cur, n, block);
  1826. if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
  1827. max_hkey = hkey;
  1828. }
  1829. high = xfs_btree_high_key_from_key(cur, key);
  1830. memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
  1831. } else {
  1832. memcpy(key, xfs_btree_key_addr(cur, 1, block),
  1833. cur->bc_ops->key_len);
  1834. }
  1835. }
  1836. /* Derive the keys for any btree block. */
  1837. STATIC void
  1838. xfs_btree_get_keys(
  1839. struct xfs_btree_cur *cur,
  1840. struct xfs_btree_block *block,
  1841. union xfs_btree_key *key)
  1842. {
  1843. if (be16_to_cpu(block->bb_level) == 0)
  1844. xfs_btree_get_leaf_keys(cur, block, key);
  1845. else
  1846. xfs_btree_get_node_keys(cur, block, key);
  1847. }
  1848. /*
  1849. * Decide if we need to update the parent keys of a btree block. For
  1850. * a standard btree this is only necessary if we're updating the first
  1851. * record/key. For an overlapping btree, we must always update the
  1852. * keys because the highest key can be in any of the records or keys
  1853. * in the block.
  1854. */
  1855. static inline bool
  1856. xfs_btree_needs_key_update(
  1857. struct xfs_btree_cur *cur,
  1858. int ptr)
  1859. {
  1860. return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
  1861. }
  1862. /*
  1863. * Update the low and high parent keys of the given level, progressing
  1864. * towards the root. If force_all is false, stop if the keys for a given
  1865. * level do not need updating.
  1866. */
  1867. STATIC int
  1868. __xfs_btree_updkeys(
  1869. struct xfs_btree_cur *cur,
  1870. int level,
  1871. struct xfs_btree_block *block,
  1872. struct xfs_buf *bp0,
  1873. bool force_all)
  1874. {
  1875. union xfs_btree_key key; /* keys from current level */
  1876. union xfs_btree_key *lkey; /* keys from the next level up */
  1877. union xfs_btree_key *hkey;
  1878. union xfs_btree_key *nlkey; /* keys from the next level up */
  1879. union xfs_btree_key *nhkey;
  1880. struct xfs_buf *bp;
  1881. int ptr;
  1882. ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
  1883. /* Exit if there aren't any parent levels to update. */
  1884. if (level + 1 >= cur->bc_nlevels)
  1885. return 0;
  1886. trace_xfs_btree_updkeys(cur, level, bp0);
  1887. lkey = &key;
  1888. hkey = xfs_btree_high_key_from_key(cur, lkey);
  1889. xfs_btree_get_keys(cur, block, lkey);
  1890. for (level++; level < cur->bc_nlevels; level++) {
  1891. #ifdef DEBUG
  1892. int error;
  1893. #endif
  1894. block = xfs_btree_get_block(cur, level, &bp);
  1895. trace_xfs_btree_updkeys(cur, level, bp);
  1896. #ifdef DEBUG
  1897. error = xfs_btree_check_block(cur, block, level, bp);
  1898. if (error) {
  1899. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1900. return error;
  1901. }
  1902. #endif
  1903. ptr = cur->bc_ptrs[level];
  1904. nlkey = xfs_btree_key_addr(cur, ptr, block);
  1905. nhkey = xfs_btree_high_key_addr(cur, ptr, block);
  1906. if (!force_all &&
  1907. !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
  1908. cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
  1909. break;
  1910. xfs_btree_copy_keys(cur, nlkey, lkey, 1);
  1911. xfs_btree_log_keys(cur, bp, ptr, ptr);
  1912. if (level + 1 >= cur->bc_nlevels)
  1913. break;
  1914. xfs_btree_get_node_keys(cur, block, lkey);
  1915. }
  1916. return 0;
  1917. }
  1918. /* Update all the keys from some level in cursor back to the root. */
  1919. STATIC int
  1920. xfs_btree_updkeys_force(
  1921. struct xfs_btree_cur *cur,
  1922. int level)
  1923. {
  1924. struct xfs_buf *bp;
  1925. struct xfs_btree_block *block;
  1926. block = xfs_btree_get_block(cur, level, &bp);
  1927. return __xfs_btree_updkeys(cur, level, block, bp, true);
  1928. }
  1929. /*
  1930. * Update the parent keys of the given level, progressing towards the root.
  1931. */
  1932. STATIC int
  1933. xfs_btree_update_keys(
  1934. struct xfs_btree_cur *cur,
  1935. int level)
  1936. {
  1937. struct xfs_btree_block *block;
  1938. struct xfs_buf *bp;
  1939. union xfs_btree_key *kp;
  1940. union xfs_btree_key key;
  1941. int ptr;
  1942. ASSERT(level >= 0);
  1943. block = xfs_btree_get_block(cur, level, &bp);
  1944. if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
  1945. return __xfs_btree_updkeys(cur, level, block, bp, false);
  1946. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1947. XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
  1948. /*
  1949. * Go up the tree from this level toward the root.
  1950. * At each level, update the key value to the value input.
  1951. * Stop when we reach a level where the cursor isn't pointing
  1952. * at the first entry in the block.
  1953. */
  1954. xfs_btree_get_keys(cur, block, &key);
  1955. for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
  1956. #ifdef DEBUG
  1957. int error;
  1958. #endif
  1959. block = xfs_btree_get_block(cur, level, &bp);
  1960. #ifdef DEBUG
  1961. error = xfs_btree_check_block(cur, block, level, bp);
  1962. if (error) {
  1963. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1964. return error;
  1965. }
  1966. #endif
  1967. ptr = cur->bc_ptrs[level];
  1968. kp = xfs_btree_key_addr(cur, ptr, block);
  1969. xfs_btree_copy_keys(cur, kp, &key, 1);
  1970. xfs_btree_log_keys(cur, bp, ptr, ptr);
  1971. }
  1972. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1973. return 0;
  1974. }
  1975. /*
  1976. * Update the record referred to by cur to the value in the
  1977. * given record. This either works (return 0) or gets an
  1978. * EFSCORRUPTED error.
  1979. */
  1980. int
  1981. xfs_btree_update(
  1982. struct xfs_btree_cur *cur,
  1983. union xfs_btree_rec *rec)
  1984. {
  1985. struct xfs_btree_block *block;
  1986. struct xfs_buf *bp;
  1987. int error;
  1988. int ptr;
  1989. union xfs_btree_rec *rp;
  1990. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1991. XFS_BTREE_TRACE_ARGR(cur, rec);
  1992. /* Pick up the current block. */
  1993. block = xfs_btree_get_block(cur, 0, &bp);
  1994. #ifdef DEBUG
  1995. error = xfs_btree_check_block(cur, block, 0, bp);
  1996. if (error)
  1997. goto error0;
  1998. #endif
  1999. /* Get the address of the rec to be updated. */
  2000. ptr = cur->bc_ptrs[0];
  2001. rp = xfs_btree_rec_addr(cur, ptr, block);
  2002. /* Fill in the new contents and log them. */
  2003. xfs_btree_copy_recs(cur, rp, rec, 1);
  2004. xfs_btree_log_recs(cur, bp, ptr, ptr);
  2005. /*
  2006. * If we are tracking the last record in the tree and
  2007. * we are at the far right edge of the tree, update it.
  2008. */
  2009. if (xfs_btree_is_lastrec(cur, block, 0)) {
  2010. cur->bc_ops->update_lastrec(cur, block, rec,
  2011. ptr, LASTREC_UPDATE);
  2012. }
  2013. /* Pass new key value up to our parent. */
  2014. if (xfs_btree_needs_key_update(cur, ptr)) {
  2015. error = xfs_btree_update_keys(cur, 0);
  2016. if (error)
  2017. goto error0;
  2018. }
  2019. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2020. return 0;
  2021. error0:
  2022. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2023. return error;
  2024. }
  2025. /*
  2026. * Move 1 record left from cur/level if possible.
  2027. * Update cur to reflect the new path.
  2028. */
  2029. STATIC int /* error */
  2030. xfs_btree_lshift(
  2031. struct xfs_btree_cur *cur,
  2032. int level,
  2033. int *stat) /* success/failure */
  2034. {
  2035. struct xfs_buf *lbp; /* left buffer pointer */
  2036. struct xfs_btree_block *left; /* left btree block */
  2037. int lrecs; /* left record count */
  2038. struct xfs_buf *rbp; /* right buffer pointer */
  2039. struct xfs_btree_block *right; /* right btree block */
  2040. struct xfs_btree_cur *tcur; /* temporary btree cursor */
  2041. int rrecs; /* right record count */
  2042. union xfs_btree_ptr lptr; /* left btree pointer */
  2043. union xfs_btree_key *rkp = NULL; /* right btree key */
  2044. union xfs_btree_ptr *rpp = NULL; /* right address pointer */
  2045. union xfs_btree_rec *rrp = NULL; /* right record pointer */
  2046. int error; /* error return value */
  2047. int i;
  2048. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2049. XFS_BTREE_TRACE_ARGI(cur, level);
  2050. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2051. level == cur->bc_nlevels - 1)
  2052. goto out0;
  2053. /* Set up variables for this block as "right". */
  2054. right = xfs_btree_get_block(cur, level, &rbp);
  2055. #ifdef DEBUG
  2056. error = xfs_btree_check_block(cur, right, level, rbp);
  2057. if (error)
  2058. goto error0;
  2059. #endif
  2060. /* If we've got no left sibling then we can't shift an entry left. */
  2061. xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  2062. if (xfs_btree_ptr_is_null(cur, &lptr))
  2063. goto out0;
  2064. /*
  2065. * If the cursor entry is the one that would be moved, don't
  2066. * do it... it's too complicated.
  2067. */
  2068. if (cur->bc_ptrs[level] <= 1)
  2069. goto out0;
  2070. /* Set up the left neighbor as "left". */
  2071. error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
  2072. if (error)
  2073. goto error0;
  2074. /* If it's full, it can't take another entry. */
  2075. lrecs = xfs_btree_get_numrecs(left);
  2076. if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
  2077. goto out0;
  2078. rrecs = xfs_btree_get_numrecs(right);
  2079. /*
  2080. * We add one entry to the left side and remove one for the right side.
  2081. * Account for it here, the changes will be updated on disk and logged
  2082. * later.
  2083. */
  2084. lrecs++;
  2085. rrecs--;
  2086. XFS_BTREE_STATS_INC(cur, lshift);
  2087. XFS_BTREE_STATS_ADD(cur, moves, 1);
  2088. /*
  2089. * If non-leaf, copy a key and a ptr to the left block.
  2090. * Log the changes to the left block.
  2091. */
  2092. if (level > 0) {
  2093. /* It's a non-leaf. Move keys and pointers. */
  2094. union xfs_btree_key *lkp; /* left btree key */
  2095. union xfs_btree_ptr *lpp; /* left address pointer */
  2096. lkp = xfs_btree_key_addr(cur, lrecs, left);
  2097. rkp = xfs_btree_key_addr(cur, 1, right);
  2098. lpp = xfs_btree_ptr_addr(cur, lrecs, left);
  2099. rpp = xfs_btree_ptr_addr(cur, 1, right);
  2100. #ifdef DEBUG
  2101. error = xfs_btree_check_ptr(cur, rpp, 0, level);
  2102. if (error)
  2103. goto error0;
  2104. #endif
  2105. xfs_btree_copy_keys(cur, lkp, rkp, 1);
  2106. xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
  2107. xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
  2108. xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
  2109. ASSERT(cur->bc_ops->keys_inorder(cur,
  2110. xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
  2111. } else {
  2112. /* It's a leaf. Move records. */
  2113. union xfs_btree_rec *lrp; /* left record pointer */
  2114. lrp = xfs_btree_rec_addr(cur, lrecs, left);
  2115. rrp = xfs_btree_rec_addr(cur, 1, right);
  2116. xfs_btree_copy_recs(cur, lrp, rrp, 1);
  2117. xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
  2118. ASSERT(cur->bc_ops->recs_inorder(cur,
  2119. xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
  2120. }
  2121. xfs_btree_set_numrecs(left, lrecs);
  2122. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
  2123. xfs_btree_set_numrecs(right, rrecs);
  2124. xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
  2125. /*
  2126. * Slide the contents of right down one entry.
  2127. */
  2128. XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
  2129. if (level > 0) {
  2130. /* It's a nonleaf. operate on keys and ptrs */
  2131. #ifdef DEBUG
  2132. int i; /* loop index */
  2133. for (i = 0; i < rrecs; i++) {
  2134. error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
  2135. if (error)
  2136. goto error0;
  2137. }
  2138. #endif
  2139. xfs_btree_shift_keys(cur,
  2140. xfs_btree_key_addr(cur, 2, right),
  2141. -1, rrecs);
  2142. xfs_btree_shift_ptrs(cur,
  2143. xfs_btree_ptr_addr(cur, 2, right),
  2144. -1, rrecs);
  2145. xfs_btree_log_keys(cur, rbp, 1, rrecs);
  2146. xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
  2147. } else {
  2148. /* It's a leaf. operate on records */
  2149. xfs_btree_shift_recs(cur,
  2150. xfs_btree_rec_addr(cur, 2, right),
  2151. -1, rrecs);
  2152. xfs_btree_log_recs(cur, rbp, 1, rrecs);
  2153. }
  2154. /*
  2155. * Using a temporary cursor, update the parent key values of the
  2156. * block on the left.
  2157. */
  2158. if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
  2159. error = xfs_btree_dup_cursor(cur, &tcur);
  2160. if (error)
  2161. goto error0;
  2162. i = xfs_btree_firstrec(tcur, level);
  2163. XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
  2164. error = xfs_btree_decrement(tcur, level, &i);
  2165. if (error)
  2166. goto error1;
  2167. /* Update the parent high keys of the left block, if needed. */
  2168. error = xfs_btree_update_keys(tcur, level);
  2169. if (error)
  2170. goto error1;
  2171. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  2172. }
  2173. /* Update the parent keys of the right block. */
  2174. error = xfs_btree_update_keys(cur, level);
  2175. if (error)
  2176. goto error0;
  2177. /* Slide the cursor value left one. */
  2178. cur->bc_ptrs[level]--;
  2179. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2180. *stat = 1;
  2181. return 0;
  2182. out0:
  2183. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2184. *stat = 0;
  2185. return 0;
  2186. error0:
  2187. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2188. return error;
  2189. error1:
  2190. XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
  2191. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  2192. return error;
  2193. }
  2194. /*
  2195. * Move 1 record right from cur/level if possible.
  2196. * Update cur to reflect the new path.
  2197. */
  2198. STATIC int /* error */
  2199. xfs_btree_rshift(
  2200. struct xfs_btree_cur *cur,
  2201. int level,
  2202. int *stat) /* success/failure */
  2203. {
  2204. struct xfs_buf *lbp; /* left buffer pointer */
  2205. struct xfs_btree_block *left; /* left btree block */
  2206. struct xfs_buf *rbp; /* right buffer pointer */
  2207. struct xfs_btree_block *right; /* right btree block */
  2208. struct xfs_btree_cur *tcur; /* temporary btree cursor */
  2209. union xfs_btree_ptr rptr; /* right block pointer */
  2210. union xfs_btree_key *rkp; /* right btree key */
  2211. int rrecs; /* right record count */
  2212. int lrecs; /* left record count */
  2213. int error; /* error return value */
  2214. int i; /* loop counter */
  2215. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2216. XFS_BTREE_TRACE_ARGI(cur, level);
  2217. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2218. (level == cur->bc_nlevels - 1))
  2219. goto out0;
  2220. /* Set up variables for this block as "left". */
  2221. left = xfs_btree_get_block(cur, level, &lbp);
  2222. #ifdef DEBUG
  2223. error = xfs_btree_check_block(cur, left, level, lbp);
  2224. if (error)
  2225. goto error0;
  2226. #endif
  2227. /* If we've got no right sibling then we can't shift an entry right. */
  2228. xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
  2229. if (xfs_btree_ptr_is_null(cur, &rptr))
  2230. goto out0;
  2231. /*
  2232. * If the cursor entry is the one that would be moved, don't
  2233. * do it... it's too complicated.
  2234. */
  2235. lrecs = xfs_btree_get_numrecs(left);
  2236. if (cur->bc_ptrs[level] >= lrecs)
  2237. goto out0;
  2238. /* Set up the right neighbor as "right". */
  2239. error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
  2240. if (error)
  2241. goto error0;
  2242. /* If it's full, it can't take another entry. */
  2243. rrecs = xfs_btree_get_numrecs(right);
  2244. if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
  2245. goto out0;
  2246. XFS_BTREE_STATS_INC(cur, rshift);
  2247. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  2248. /*
  2249. * Make a hole at the start of the right neighbor block, then
  2250. * copy the last left block entry to the hole.
  2251. */
  2252. if (level > 0) {
  2253. /* It's a nonleaf. make a hole in the keys and ptrs */
  2254. union xfs_btree_key *lkp;
  2255. union xfs_btree_ptr *lpp;
  2256. union xfs_btree_ptr *rpp;
  2257. lkp = xfs_btree_key_addr(cur, lrecs, left);
  2258. lpp = xfs_btree_ptr_addr(cur, lrecs, left);
  2259. rkp = xfs_btree_key_addr(cur, 1, right);
  2260. rpp = xfs_btree_ptr_addr(cur, 1, right);
  2261. #ifdef DEBUG
  2262. for (i = rrecs - 1; i >= 0; i--) {
  2263. error = xfs_btree_check_ptr(cur, rpp, i, level);
  2264. if (error)
  2265. goto error0;
  2266. }
  2267. #endif
  2268. xfs_btree_shift_keys(cur, rkp, 1, rrecs);
  2269. xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
  2270. #ifdef DEBUG
  2271. error = xfs_btree_check_ptr(cur, lpp, 0, level);
  2272. if (error)
  2273. goto error0;
  2274. #endif
  2275. /* Now put the new data in, and log it. */
  2276. xfs_btree_copy_keys(cur, rkp, lkp, 1);
  2277. xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
  2278. xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
  2279. xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
  2280. ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
  2281. xfs_btree_key_addr(cur, 2, right)));
  2282. } else {
  2283. /* It's a leaf. make a hole in the records */
  2284. union xfs_btree_rec *lrp;
  2285. union xfs_btree_rec *rrp;
  2286. lrp = xfs_btree_rec_addr(cur, lrecs, left);
  2287. rrp = xfs_btree_rec_addr(cur, 1, right);
  2288. xfs_btree_shift_recs(cur, rrp, 1, rrecs);
  2289. /* Now put the new data in, and log it. */
  2290. xfs_btree_copy_recs(cur, rrp, lrp, 1);
  2291. xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
  2292. }
  2293. /*
  2294. * Decrement and log left's numrecs, bump and log right's numrecs.
  2295. */
  2296. xfs_btree_set_numrecs(left, --lrecs);
  2297. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
  2298. xfs_btree_set_numrecs(right, ++rrecs);
  2299. xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
  2300. /*
  2301. * Using a temporary cursor, update the parent key values of the
  2302. * block on the right.
  2303. */
  2304. error = xfs_btree_dup_cursor(cur, &tcur);
  2305. if (error)
  2306. goto error0;
  2307. i = xfs_btree_lastrec(tcur, level);
  2308. XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
  2309. error = xfs_btree_increment(tcur, level, &i);
  2310. if (error)
  2311. goto error1;
  2312. /* Update the parent high keys of the left block, if needed. */
  2313. if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
  2314. error = xfs_btree_update_keys(cur, level);
  2315. if (error)
  2316. goto error1;
  2317. }
  2318. /* Update the parent keys of the right block. */
  2319. error = xfs_btree_update_keys(tcur, level);
  2320. if (error)
  2321. goto error1;
  2322. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  2323. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2324. *stat = 1;
  2325. return 0;
  2326. out0:
  2327. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2328. *stat = 0;
  2329. return 0;
  2330. error0:
  2331. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2332. return error;
  2333. error1:
  2334. XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
  2335. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  2336. return error;
  2337. }
  2338. /*
  2339. * Split cur/level block in half.
  2340. * Return new block number and the key to its first
  2341. * record (to be inserted into parent).
  2342. */
  2343. STATIC int /* error */
  2344. __xfs_btree_split(
  2345. struct xfs_btree_cur *cur,
  2346. int level,
  2347. union xfs_btree_ptr *ptrp,
  2348. union xfs_btree_key *key,
  2349. struct xfs_btree_cur **curp,
  2350. int *stat) /* success/failure */
  2351. {
  2352. union xfs_btree_ptr lptr; /* left sibling block ptr */
  2353. struct xfs_buf *lbp; /* left buffer pointer */
  2354. struct xfs_btree_block *left; /* left btree block */
  2355. union xfs_btree_ptr rptr; /* right sibling block ptr */
  2356. struct xfs_buf *rbp; /* right buffer pointer */
  2357. struct xfs_btree_block *right; /* right btree block */
  2358. union xfs_btree_ptr rrptr; /* right-right sibling ptr */
  2359. struct xfs_buf *rrbp; /* right-right buffer pointer */
  2360. struct xfs_btree_block *rrblock; /* right-right btree block */
  2361. int lrecs;
  2362. int rrecs;
  2363. int src_index;
  2364. int error; /* error return value */
  2365. #ifdef DEBUG
  2366. int i;
  2367. #endif
  2368. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2369. XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
  2370. XFS_BTREE_STATS_INC(cur, split);
  2371. /* Set up left block (current one). */
  2372. left = xfs_btree_get_block(cur, level, &lbp);
  2373. #ifdef DEBUG
  2374. error = xfs_btree_check_block(cur, left, level, lbp);
  2375. if (error)
  2376. goto error0;
  2377. #endif
  2378. xfs_btree_buf_to_ptr(cur, lbp, &lptr);
  2379. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2380. error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
  2381. if (error)
  2382. goto error0;
  2383. if (*stat == 0)
  2384. goto out0;
  2385. XFS_BTREE_STATS_INC(cur, alloc);
  2386. /* Set up the new block as "right". */
  2387. error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
  2388. if (error)
  2389. goto error0;
  2390. /* Fill in the btree header for the new right block. */
  2391. xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
  2392. /*
  2393. * Split the entries between the old and the new block evenly.
  2394. * Make sure that if there's an odd number of entries now, that
  2395. * each new block will have the same number of entries.
  2396. */
  2397. lrecs = xfs_btree_get_numrecs(left);
  2398. rrecs = lrecs / 2;
  2399. if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
  2400. rrecs++;
  2401. src_index = (lrecs - rrecs + 1);
  2402. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  2403. /* Adjust numrecs for the later get_*_keys() calls. */
  2404. lrecs -= rrecs;
  2405. xfs_btree_set_numrecs(left, lrecs);
  2406. xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
  2407. /*
  2408. * Copy btree block entries from the left block over to the
  2409. * new block, the right. Update the right block and log the
  2410. * changes.
  2411. */
  2412. if (level > 0) {
  2413. /* It's a non-leaf. Move keys and pointers. */
  2414. union xfs_btree_key *lkp; /* left btree key */
  2415. union xfs_btree_ptr *lpp; /* left address pointer */
  2416. union xfs_btree_key *rkp; /* right btree key */
  2417. union xfs_btree_ptr *rpp; /* right address pointer */
  2418. lkp = xfs_btree_key_addr(cur, src_index, left);
  2419. lpp = xfs_btree_ptr_addr(cur, src_index, left);
  2420. rkp = xfs_btree_key_addr(cur, 1, right);
  2421. rpp = xfs_btree_ptr_addr(cur, 1, right);
  2422. #ifdef DEBUG
  2423. for (i = src_index; i < rrecs; i++) {
  2424. error = xfs_btree_check_ptr(cur, lpp, i, level);
  2425. if (error)
  2426. goto error0;
  2427. }
  2428. #endif
  2429. /* Copy the keys & pointers to the new block. */
  2430. xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
  2431. xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
  2432. xfs_btree_log_keys(cur, rbp, 1, rrecs);
  2433. xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
  2434. /* Stash the keys of the new block for later insertion. */
  2435. xfs_btree_get_node_keys(cur, right, key);
  2436. } else {
  2437. /* It's a leaf. Move records. */
  2438. union xfs_btree_rec *lrp; /* left record pointer */
  2439. union xfs_btree_rec *rrp; /* right record pointer */
  2440. lrp = xfs_btree_rec_addr(cur, src_index, left);
  2441. rrp = xfs_btree_rec_addr(cur, 1, right);
  2442. /* Copy records to the new block. */
  2443. xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
  2444. xfs_btree_log_recs(cur, rbp, 1, rrecs);
  2445. /* Stash the keys of the new block for later insertion. */
  2446. xfs_btree_get_leaf_keys(cur, right, key);
  2447. }
  2448. /*
  2449. * Find the left block number by looking in the buffer.
  2450. * Adjust sibling pointers.
  2451. */
  2452. xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
  2453. xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
  2454. xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  2455. xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
  2456. xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
  2457. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  2458. /*
  2459. * If there's a block to the new block's right, make that block
  2460. * point back to right instead of to left.
  2461. */
  2462. if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
  2463. error = xfs_btree_read_buf_block(cur, &rrptr,
  2464. 0, &rrblock, &rrbp);
  2465. if (error)
  2466. goto error0;
  2467. xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
  2468. xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
  2469. }
  2470. /* Update the parent high keys of the left block, if needed. */
  2471. if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
  2472. error = xfs_btree_update_keys(cur, level);
  2473. if (error)
  2474. goto error0;
  2475. }
  2476. /*
  2477. * If the cursor is really in the right block, move it there.
  2478. * If it's just pointing past the last entry in left, then we'll
  2479. * insert there, so don't change anything in that case.
  2480. */
  2481. if (cur->bc_ptrs[level] > lrecs + 1) {
  2482. xfs_btree_setbuf(cur, level, rbp);
  2483. cur->bc_ptrs[level] -= lrecs;
  2484. }
  2485. /*
  2486. * If there are more levels, we'll need another cursor which refers
  2487. * the right block, no matter where this cursor was.
  2488. */
  2489. if (level + 1 < cur->bc_nlevels) {
  2490. error = xfs_btree_dup_cursor(cur, curp);
  2491. if (error)
  2492. goto error0;
  2493. (*curp)->bc_ptrs[level + 1]++;
  2494. }
  2495. *ptrp = rptr;
  2496. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2497. *stat = 1;
  2498. return 0;
  2499. out0:
  2500. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2501. *stat = 0;
  2502. return 0;
  2503. error0:
  2504. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2505. return error;
  2506. }
  2507. struct xfs_btree_split_args {
  2508. struct xfs_btree_cur *cur;
  2509. int level;
  2510. union xfs_btree_ptr *ptrp;
  2511. union xfs_btree_key *key;
  2512. struct xfs_btree_cur **curp;
  2513. int *stat; /* success/failure */
  2514. int result;
  2515. bool kswapd; /* allocation in kswapd context */
  2516. struct completion *done;
  2517. struct work_struct work;
  2518. };
  2519. /*
  2520. * Stack switching interfaces for allocation
  2521. */
  2522. static void
  2523. xfs_btree_split_worker(
  2524. struct work_struct *work)
  2525. {
  2526. struct xfs_btree_split_args *args = container_of(work,
  2527. struct xfs_btree_split_args, work);
  2528. unsigned long pflags;
  2529. unsigned long new_pflags = PF_FSTRANS;
  2530. /*
  2531. * we are in a transaction context here, but may also be doing work
  2532. * in kswapd context, and hence we may need to inherit that state
  2533. * temporarily to ensure that we don't block waiting for memory reclaim
  2534. * in any way.
  2535. */
  2536. if (args->kswapd)
  2537. new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
  2538. current_set_flags_nested(&pflags, new_pflags);
  2539. args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
  2540. args->key, args->curp, args->stat);
  2541. complete(args->done);
  2542. current_restore_flags_nested(&pflags, new_pflags);
  2543. }
  2544. /*
  2545. * BMBT split requests often come in with little stack to work on. Push
  2546. * them off to a worker thread so there is lots of stack to use. For the other
  2547. * btree types, just call directly to avoid the context switch overhead here.
  2548. */
  2549. STATIC int /* error */
  2550. xfs_btree_split(
  2551. struct xfs_btree_cur *cur,
  2552. int level,
  2553. union xfs_btree_ptr *ptrp,
  2554. union xfs_btree_key *key,
  2555. struct xfs_btree_cur **curp,
  2556. int *stat) /* success/failure */
  2557. {
  2558. struct xfs_btree_split_args args;
  2559. DECLARE_COMPLETION_ONSTACK(done);
  2560. if (cur->bc_btnum != XFS_BTNUM_BMAP)
  2561. return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
  2562. args.cur = cur;
  2563. args.level = level;
  2564. args.ptrp = ptrp;
  2565. args.key = key;
  2566. args.curp = curp;
  2567. args.stat = stat;
  2568. args.done = &done;
  2569. args.kswapd = current_is_kswapd();
  2570. INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
  2571. queue_work(xfs_alloc_wq, &args.work);
  2572. wait_for_completion(&done);
  2573. destroy_work_on_stack(&args.work);
  2574. return args.result;
  2575. }
  2576. /*
  2577. * Copy the old inode root contents into a real block and make the
  2578. * broot point to it.
  2579. */
  2580. int /* error */
  2581. xfs_btree_new_iroot(
  2582. struct xfs_btree_cur *cur, /* btree cursor */
  2583. int *logflags, /* logging flags for inode */
  2584. int *stat) /* return status - 0 fail */
  2585. {
  2586. struct xfs_buf *cbp; /* buffer for cblock */
  2587. struct xfs_btree_block *block; /* btree block */
  2588. struct xfs_btree_block *cblock; /* child btree block */
  2589. union xfs_btree_key *ckp; /* child key pointer */
  2590. union xfs_btree_ptr *cpp; /* child ptr pointer */
  2591. union xfs_btree_key *kp; /* pointer to btree key */
  2592. union xfs_btree_ptr *pp; /* pointer to block addr */
  2593. union xfs_btree_ptr nptr; /* new block addr */
  2594. int level; /* btree level */
  2595. int error; /* error return code */
  2596. #ifdef DEBUG
  2597. int i; /* loop counter */
  2598. #endif
  2599. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2600. XFS_BTREE_STATS_INC(cur, newroot);
  2601. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  2602. level = cur->bc_nlevels - 1;
  2603. block = xfs_btree_get_iroot(cur);
  2604. pp = xfs_btree_ptr_addr(cur, 1, block);
  2605. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2606. error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
  2607. if (error)
  2608. goto error0;
  2609. if (*stat == 0) {
  2610. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2611. return 0;
  2612. }
  2613. XFS_BTREE_STATS_INC(cur, alloc);
  2614. /* Copy the root into a real block. */
  2615. error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
  2616. if (error)
  2617. goto error0;
  2618. /*
  2619. * we can't just memcpy() the root in for CRC enabled btree blocks.
  2620. * In that case have to also ensure the blkno remains correct
  2621. */
  2622. memcpy(cblock, block, xfs_btree_block_len(cur));
  2623. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
  2624. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  2625. cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
  2626. else
  2627. cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
  2628. }
  2629. be16_add_cpu(&block->bb_level, 1);
  2630. xfs_btree_set_numrecs(block, 1);
  2631. cur->bc_nlevels++;
  2632. cur->bc_ptrs[level + 1] = 1;
  2633. kp = xfs_btree_key_addr(cur, 1, block);
  2634. ckp = xfs_btree_key_addr(cur, 1, cblock);
  2635. xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
  2636. cpp = xfs_btree_ptr_addr(cur, 1, cblock);
  2637. #ifdef DEBUG
  2638. for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
  2639. error = xfs_btree_check_ptr(cur, pp, i, level);
  2640. if (error)
  2641. goto error0;
  2642. }
  2643. #endif
  2644. xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
  2645. #ifdef DEBUG
  2646. error = xfs_btree_check_ptr(cur, &nptr, 0, level);
  2647. if (error)
  2648. goto error0;
  2649. #endif
  2650. xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
  2651. xfs_iroot_realloc(cur->bc_private.b.ip,
  2652. 1 - xfs_btree_get_numrecs(cblock),
  2653. cur->bc_private.b.whichfork);
  2654. xfs_btree_setbuf(cur, level, cbp);
  2655. /*
  2656. * Do all this logging at the end so that
  2657. * the root is at the right level.
  2658. */
  2659. xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
  2660. xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
  2661. xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
  2662. *logflags |=
  2663. XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
  2664. *stat = 1;
  2665. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2666. return 0;
  2667. error0:
  2668. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2669. return error;
  2670. }
  2671. /*
  2672. * Allocate a new root block, fill it in.
  2673. */
  2674. STATIC int /* error */
  2675. xfs_btree_new_root(
  2676. struct xfs_btree_cur *cur, /* btree cursor */
  2677. int *stat) /* success/failure */
  2678. {
  2679. struct xfs_btree_block *block; /* one half of the old root block */
  2680. struct xfs_buf *bp; /* buffer containing block */
  2681. int error; /* error return value */
  2682. struct xfs_buf *lbp; /* left buffer pointer */
  2683. struct xfs_btree_block *left; /* left btree block */
  2684. struct xfs_buf *nbp; /* new (root) buffer */
  2685. struct xfs_btree_block *new; /* new (root) btree block */
  2686. int nptr; /* new value for key index, 1 or 2 */
  2687. struct xfs_buf *rbp; /* right buffer pointer */
  2688. struct xfs_btree_block *right; /* right btree block */
  2689. union xfs_btree_ptr rptr;
  2690. union xfs_btree_ptr lptr;
  2691. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2692. XFS_BTREE_STATS_INC(cur, newroot);
  2693. /* initialise our start point from the cursor */
  2694. cur->bc_ops->init_ptr_from_cur(cur, &rptr);
  2695. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2696. error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
  2697. if (error)
  2698. goto error0;
  2699. if (*stat == 0)
  2700. goto out0;
  2701. XFS_BTREE_STATS_INC(cur, alloc);
  2702. /* Set up the new block. */
  2703. error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
  2704. if (error)
  2705. goto error0;
  2706. /* Set the root in the holding structure increasing the level by 1. */
  2707. cur->bc_ops->set_root(cur, &lptr, 1);
  2708. /*
  2709. * At the previous root level there are now two blocks: the old root,
  2710. * and the new block generated when it was split. We don't know which
  2711. * one the cursor is pointing at, so we set up variables "left" and
  2712. * "right" for each case.
  2713. */
  2714. block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
  2715. #ifdef DEBUG
  2716. error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
  2717. if (error)
  2718. goto error0;
  2719. #endif
  2720. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  2721. if (!xfs_btree_ptr_is_null(cur, &rptr)) {
  2722. /* Our block is left, pick up the right block. */
  2723. lbp = bp;
  2724. xfs_btree_buf_to_ptr(cur, lbp, &lptr);
  2725. left = block;
  2726. error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
  2727. if (error)
  2728. goto error0;
  2729. bp = rbp;
  2730. nptr = 1;
  2731. } else {
  2732. /* Our block is right, pick up the left block. */
  2733. rbp = bp;
  2734. xfs_btree_buf_to_ptr(cur, rbp, &rptr);
  2735. right = block;
  2736. xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  2737. error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
  2738. if (error)
  2739. goto error0;
  2740. bp = lbp;
  2741. nptr = 2;
  2742. }
  2743. /* Fill in the new block's btree header and log it. */
  2744. xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
  2745. xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
  2746. ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
  2747. !xfs_btree_ptr_is_null(cur, &rptr));
  2748. /* Fill in the key data in the new root. */
  2749. if (xfs_btree_get_level(left) > 0) {
  2750. /*
  2751. * Get the keys for the left block's keys and put them directly
  2752. * in the parent block. Do the same for the right block.
  2753. */
  2754. xfs_btree_get_node_keys(cur, left,
  2755. xfs_btree_key_addr(cur, 1, new));
  2756. xfs_btree_get_node_keys(cur, right,
  2757. xfs_btree_key_addr(cur, 2, new));
  2758. } else {
  2759. /*
  2760. * Get the keys for the left block's records and put them
  2761. * directly in the parent block. Do the same for the right
  2762. * block.
  2763. */
  2764. xfs_btree_get_leaf_keys(cur, left,
  2765. xfs_btree_key_addr(cur, 1, new));
  2766. xfs_btree_get_leaf_keys(cur, right,
  2767. xfs_btree_key_addr(cur, 2, new));
  2768. }
  2769. xfs_btree_log_keys(cur, nbp, 1, 2);
  2770. /* Fill in the pointer data in the new root. */
  2771. xfs_btree_copy_ptrs(cur,
  2772. xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
  2773. xfs_btree_copy_ptrs(cur,
  2774. xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
  2775. xfs_btree_log_ptrs(cur, nbp, 1, 2);
  2776. /* Fix up the cursor. */
  2777. xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
  2778. cur->bc_ptrs[cur->bc_nlevels] = nptr;
  2779. cur->bc_nlevels++;
  2780. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2781. *stat = 1;
  2782. return 0;
  2783. error0:
  2784. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2785. return error;
  2786. out0:
  2787. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2788. *stat = 0;
  2789. return 0;
  2790. }
  2791. STATIC int
  2792. xfs_btree_make_block_unfull(
  2793. struct xfs_btree_cur *cur, /* btree cursor */
  2794. int level, /* btree level */
  2795. int numrecs,/* # of recs in block */
  2796. int *oindex,/* old tree index */
  2797. int *index, /* new tree index */
  2798. union xfs_btree_ptr *nptr, /* new btree ptr */
  2799. struct xfs_btree_cur **ncur, /* new btree cursor */
  2800. union xfs_btree_key *key, /* key of new block */
  2801. int *stat)
  2802. {
  2803. int error = 0;
  2804. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2805. level == cur->bc_nlevels - 1) {
  2806. struct xfs_inode *ip = cur->bc_private.b.ip;
  2807. if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
  2808. /* A root block that can be made bigger. */
  2809. xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
  2810. *stat = 1;
  2811. } else {
  2812. /* A root block that needs replacing */
  2813. int logflags = 0;
  2814. error = xfs_btree_new_iroot(cur, &logflags, stat);
  2815. if (error || *stat == 0)
  2816. return error;
  2817. xfs_trans_log_inode(cur->bc_tp, ip, logflags);
  2818. }
  2819. return 0;
  2820. }
  2821. /* First, try shifting an entry to the right neighbor. */
  2822. error = xfs_btree_rshift(cur, level, stat);
  2823. if (error || *stat)
  2824. return error;
  2825. /* Next, try shifting an entry to the left neighbor. */
  2826. error = xfs_btree_lshift(cur, level, stat);
  2827. if (error)
  2828. return error;
  2829. if (*stat) {
  2830. *oindex = *index = cur->bc_ptrs[level];
  2831. return 0;
  2832. }
  2833. /*
  2834. * Next, try splitting the current block in half.
  2835. *
  2836. * If this works we have to re-set our variables because we
  2837. * could be in a different block now.
  2838. */
  2839. error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
  2840. if (error || *stat == 0)
  2841. return error;
  2842. *index = cur->bc_ptrs[level];
  2843. return 0;
  2844. }
  2845. /*
  2846. * Insert one record/level. Return information to the caller
  2847. * allowing the next level up to proceed if necessary.
  2848. */
  2849. STATIC int
  2850. xfs_btree_insrec(
  2851. struct xfs_btree_cur *cur, /* btree cursor */
  2852. int level, /* level to insert record at */
  2853. union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
  2854. union xfs_btree_rec *rec, /* record to insert */
  2855. union xfs_btree_key *key, /* i/o: block key for ptrp */
  2856. struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
  2857. int *stat) /* success/failure */
  2858. {
  2859. struct xfs_btree_block *block; /* btree block */
  2860. struct xfs_buf *bp; /* buffer for block */
  2861. union xfs_btree_ptr nptr; /* new block ptr */
  2862. struct xfs_btree_cur *ncur; /* new btree cursor */
  2863. union xfs_btree_key nkey; /* new block key */
  2864. union xfs_btree_key *lkey;
  2865. int optr; /* old key/record index */
  2866. int ptr; /* key/record index */
  2867. int numrecs;/* number of records */
  2868. int error; /* error return value */
  2869. #ifdef DEBUG
  2870. int i;
  2871. #endif
  2872. xfs_daddr_t old_bn;
  2873. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2874. XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, &rec);
  2875. ncur = NULL;
  2876. lkey = &nkey;
  2877. /*
  2878. * If we have an external root pointer, and we've made it to the
  2879. * root level, allocate a new root block and we're done.
  2880. */
  2881. if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2882. (level >= cur->bc_nlevels)) {
  2883. error = xfs_btree_new_root(cur, stat);
  2884. xfs_btree_set_ptr_null(cur, ptrp);
  2885. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2886. return error;
  2887. }
  2888. /* If we're off the left edge, return failure. */
  2889. ptr = cur->bc_ptrs[level];
  2890. if (ptr == 0) {
  2891. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2892. *stat = 0;
  2893. return 0;
  2894. }
  2895. optr = ptr;
  2896. XFS_BTREE_STATS_INC(cur, insrec);
  2897. /* Get pointers to the btree buffer and block. */
  2898. block = xfs_btree_get_block(cur, level, &bp);
  2899. old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
  2900. numrecs = xfs_btree_get_numrecs(block);
  2901. #ifdef DEBUG
  2902. error = xfs_btree_check_block(cur, block, level, bp);
  2903. if (error)
  2904. goto error0;
  2905. /* Check that the new entry is being inserted in the right place. */
  2906. if (ptr <= numrecs) {
  2907. if (level == 0) {
  2908. ASSERT(cur->bc_ops->recs_inorder(cur, rec,
  2909. xfs_btree_rec_addr(cur, ptr, block)));
  2910. } else {
  2911. ASSERT(cur->bc_ops->keys_inorder(cur, key,
  2912. xfs_btree_key_addr(cur, ptr, block)));
  2913. }
  2914. }
  2915. #endif
  2916. /*
  2917. * If the block is full, we can't insert the new entry until we
  2918. * make the block un-full.
  2919. */
  2920. xfs_btree_set_ptr_null(cur, &nptr);
  2921. if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
  2922. error = xfs_btree_make_block_unfull(cur, level, numrecs,
  2923. &optr, &ptr, &nptr, &ncur, lkey, stat);
  2924. if (error || *stat == 0)
  2925. goto error0;
  2926. }
  2927. /*
  2928. * The current block may have changed if the block was
  2929. * previously full and we have just made space in it.
  2930. */
  2931. block = xfs_btree_get_block(cur, level, &bp);
  2932. numrecs = xfs_btree_get_numrecs(block);
  2933. #ifdef DEBUG
  2934. error = xfs_btree_check_block(cur, block, level, bp);
  2935. if (error)
  2936. return error;
  2937. #endif
  2938. /*
  2939. * At this point we know there's room for our new entry in the block
  2940. * we're pointing at.
  2941. */
  2942. XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
  2943. if (level > 0) {
  2944. /* It's a nonleaf. make a hole in the keys and ptrs */
  2945. union xfs_btree_key *kp;
  2946. union xfs_btree_ptr *pp;
  2947. kp = xfs_btree_key_addr(cur, ptr, block);
  2948. pp = xfs_btree_ptr_addr(cur, ptr, block);
  2949. #ifdef DEBUG
  2950. for (i = numrecs - ptr; i >= 0; i--) {
  2951. error = xfs_btree_check_ptr(cur, pp, i, level);
  2952. if (error)
  2953. return error;
  2954. }
  2955. #endif
  2956. xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
  2957. xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
  2958. #ifdef DEBUG
  2959. error = xfs_btree_check_ptr(cur, ptrp, 0, level);
  2960. if (error)
  2961. goto error0;
  2962. #endif
  2963. /* Now put the new data in, bump numrecs and log it. */
  2964. xfs_btree_copy_keys(cur, kp, key, 1);
  2965. xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
  2966. numrecs++;
  2967. xfs_btree_set_numrecs(block, numrecs);
  2968. xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
  2969. xfs_btree_log_keys(cur, bp, ptr, numrecs);
  2970. #ifdef DEBUG
  2971. if (ptr < numrecs) {
  2972. ASSERT(cur->bc_ops->keys_inorder(cur, kp,
  2973. xfs_btree_key_addr(cur, ptr + 1, block)));
  2974. }
  2975. #endif
  2976. } else {
  2977. /* It's a leaf. make a hole in the records */
  2978. union xfs_btree_rec *rp;
  2979. rp = xfs_btree_rec_addr(cur, ptr, block);
  2980. xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
  2981. /* Now put the new data in, bump numrecs and log it. */
  2982. xfs_btree_copy_recs(cur, rp, rec, 1);
  2983. xfs_btree_set_numrecs(block, ++numrecs);
  2984. xfs_btree_log_recs(cur, bp, ptr, numrecs);
  2985. #ifdef DEBUG
  2986. if (ptr < numrecs) {
  2987. ASSERT(cur->bc_ops->recs_inorder(cur, rp,
  2988. xfs_btree_rec_addr(cur, ptr + 1, block)));
  2989. }
  2990. #endif
  2991. }
  2992. /* Log the new number of records in the btree header. */
  2993. xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
  2994. /*
  2995. * If we just inserted into a new tree block, we have to
  2996. * recalculate nkey here because nkey is out of date.
  2997. *
  2998. * Otherwise we're just updating an existing block (having shoved
  2999. * some records into the new tree block), so use the regular key
  3000. * update mechanism.
  3001. */
  3002. if (bp && bp->b_bn != old_bn) {
  3003. xfs_btree_get_keys(cur, block, lkey);
  3004. } else if (xfs_btree_needs_key_update(cur, optr)) {
  3005. error = xfs_btree_update_keys(cur, level);
  3006. if (error)
  3007. goto error0;
  3008. }
  3009. /*
  3010. * If we are tracking the last record in the tree and
  3011. * we are at the far right edge of the tree, update it.
  3012. */
  3013. if (xfs_btree_is_lastrec(cur, block, level)) {
  3014. cur->bc_ops->update_lastrec(cur, block, rec,
  3015. ptr, LASTREC_INSREC);
  3016. }
  3017. /*
  3018. * Return the new block number, if any.
  3019. * If there is one, give back a record value and a cursor too.
  3020. */
  3021. *ptrp = nptr;
  3022. if (!xfs_btree_ptr_is_null(cur, &nptr)) {
  3023. xfs_btree_copy_keys(cur, key, lkey, 1);
  3024. *curp = ncur;
  3025. }
  3026. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3027. *stat = 1;
  3028. return 0;
  3029. error0:
  3030. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3031. return error;
  3032. }
  3033. /*
  3034. * Insert the record at the point referenced by cur.
  3035. *
  3036. * A multi-level split of the tree on insert will invalidate the original
  3037. * cursor. All callers of this function should assume that the cursor is
  3038. * no longer valid and revalidate it.
  3039. */
  3040. int
  3041. xfs_btree_insert(
  3042. struct xfs_btree_cur *cur,
  3043. int *stat)
  3044. {
  3045. int error; /* error return value */
  3046. int i; /* result value, 0 for failure */
  3047. int level; /* current level number in btree */
  3048. union xfs_btree_ptr nptr; /* new block number (split result) */
  3049. struct xfs_btree_cur *ncur; /* new cursor (split result) */
  3050. struct xfs_btree_cur *pcur; /* previous level's cursor */
  3051. union xfs_btree_key bkey; /* key of block to insert */
  3052. union xfs_btree_key *key;
  3053. union xfs_btree_rec rec; /* record to insert */
  3054. level = 0;
  3055. ncur = NULL;
  3056. pcur = cur;
  3057. key = &bkey;
  3058. xfs_btree_set_ptr_null(cur, &nptr);
  3059. /* Make a key out of the record data to be inserted, and save it. */
  3060. cur->bc_ops->init_rec_from_cur(cur, &rec);
  3061. cur->bc_ops->init_key_from_rec(key, &rec);
  3062. /*
  3063. * Loop going up the tree, starting at the leaf level.
  3064. * Stop when we don't get a split block, that must mean that
  3065. * the insert is finished with this level.
  3066. */
  3067. do {
  3068. /*
  3069. * Insert nrec/nptr into this level of the tree.
  3070. * Note if we fail, nptr will be null.
  3071. */
  3072. error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
  3073. &ncur, &i);
  3074. if (error) {
  3075. if (pcur != cur)
  3076. xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
  3077. goto error0;
  3078. }
  3079. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
  3080. level++;
  3081. /*
  3082. * See if the cursor we just used is trash.
  3083. * Can't trash the caller's cursor, but otherwise we should
  3084. * if ncur is a new cursor or we're about to be done.
  3085. */
  3086. if (pcur != cur &&
  3087. (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
  3088. /* Save the state from the cursor before we trash it */
  3089. if (cur->bc_ops->update_cursor)
  3090. cur->bc_ops->update_cursor(pcur, cur);
  3091. cur->bc_nlevels = pcur->bc_nlevels;
  3092. xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
  3093. }
  3094. /* If we got a new cursor, switch to it. */
  3095. if (ncur) {
  3096. pcur = ncur;
  3097. ncur = NULL;
  3098. }
  3099. } while (!xfs_btree_ptr_is_null(cur, &nptr));
  3100. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3101. *stat = i;
  3102. return 0;
  3103. error0:
  3104. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3105. return error;
  3106. }
  3107. /*
  3108. * Try to merge a non-leaf block back into the inode root.
  3109. *
  3110. * Note: the killroot names comes from the fact that we're effectively
  3111. * killing the old root block. But because we can't just delete the
  3112. * inode we have to copy the single block it was pointing to into the
  3113. * inode.
  3114. */
  3115. STATIC int
  3116. xfs_btree_kill_iroot(
  3117. struct xfs_btree_cur *cur)
  3118. {
  3119. int whichfork = cur->bc_private.b.whichfork;
  3120. struct xfs_inode *ip = cur->bc_private.b.ip;
  3121. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  3122. struct xfs_btree_block *block;
  3123. struct xfs_btree_block *cblock;
  3124. union xfs_btree_key *kp;
  3125. union xfs_btree_key *ckp;
  3126. union xfs_btree_ptr *pp;
  3127. union xfs_btree_ptr *cpp;
  3128. struct xfs_buf *cbp;
  3129. int level;
  3130. int index;
  3131. int numrecs;
  3132. int error;
  3133. #ifdef DEBUG
  3134. union xfs_btree_ptr ptr;
  3135. int i;
  3136. #endif
  3137. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  3138. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  3139. ASSERT(cur->bc_nlevels > 1);
  3140. /*
  3141. * Don't deal with the root block needs to be a leaf case.
  3142. * We're just going to turn the thing back into extents anyway.
  3143. */
  3144. level = cur->bc_nlevels - 1;
  3145. if (level == 1)
  3146. goto out0;
  3147. /*
  3148. * Give up if the root has multiple children.
  3149. */
  3150. block = xfs_btree_get_iroot(cur);
  3151. if (xfs_btree_get_numrecs(block) != 1)
  3152. goto out0;
  3153. cblock = xfs_btree_get_block(cur, level - 1, &cbp);
  3154. numrecs = xfs_btree_get_numrecs(cblock);
  3155. /*
  3156. * Only do this if the next level will fit.
  3157. * Then the data must be copied up to the inode,
  3158. * instead of freeing the root you free the next level.
  3159. */
  3160. if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
  3161. goto out0;
  3162. XFS_BTREE_STATS_INC(cur, killroot);
  3163. #ifdef DEBUG
  3164. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
  3165. ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
  3166. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  3167. ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
  3168. #endif
  3169. index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
  3170. if (index) {
  3171. xfs_iroot_realloc(cur->bc_private.b.ip, index,
  3172. cur->bc_private.b.whichfork);
  3173. block = ifp->if_broot;
  3174. }
  3175. be16_add_cpu(&block->bb_numrecs, index);
  3176. ASSERT(block->bb_numrecs == cblock->bb_numrecs);
  3177. kp = xfs_btree_key_addr(cur, 1, block);
  3178. ckp = xfs_btree_key_addr(cur, 1, cblock);
  3179. xfs_btree_copy_keys(cur, kp, ckp, numrecs);
  3180. pp = xfs_btree_ptr_addr(cur, 1, block);
  3181. cpp = xfs_btree_ptr_addr(cur, 1, cblock);
  3182. #ifdef DEBUG
  3183. for (i = 0; i < numrecs; i++) {
  3184. error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
  3185. if (error) {
  3186. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3187. return error;
  3188. }
  3189. }
  3190. #endif
  3191. xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
  3192. error = xfs_btree_free_block(cur, cbp);
  3193. if (error) {
  3194. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3195. return error;
  3196. }
  3197. cur->bc_bufs[level - 1] = NULL;
  3198. be16_add_cpu(&block->bb_level, -1);
  3199. xfs_trans_log_inode(cur->bc_tp, ip,
  3200. XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  3201. cur->bc_nlevels--;
  3202. out0:
  3203. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3204. return 0;
  3205. }
  3206. /*
  3207. * Kill the current root node, and replace it with it's only child node.
  3208. */
  3209. STATIC int
  3210. xfs_btree_kill_root(
  3211. struct xfs_btree_cur *cur,
  3212. struct xfs_buf *bp,
  3213. int level,
  3214. union xfs_btree_ptr *newroot)
  3215. {
  3216. int error;
  3217. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  3218. XFS_BTREE_STATS_INC(cur, killroot);
  3219. /*
  3220. * Update the root pointer, decreasing the level by 1 and then
  3221. * free the old root.
  3222. */
  3223. cur->bc_ops->set_root(cur, newroot, -1);
  3224. error = xfs_btree_free_block(cur, bp);
  3225. if (error) {
  3226. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3227. return error;
  3228. }
  3229. cur->bc_bufs[level] = NULL;
  3230. cur->bc_ra[level] = 0;
  3231. cur->bc_nlevels--;
  3232. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3233. return 0;
  3234. }
  3235. STATIC int
  3236. xfs_btree_dec_cursor(
  3237. struct xfs_btree_cur *cur,
  3238. int level,
  3239. int *stat)
  3240. {
  3241. int error;
  3242. int i;
  3243. if (level > 0) {
  3244. error = xfs_btree_decrement(cur, level, &i);
  3245. if (error)
  3246. return error;
  3247. }
  3248. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3249. *stat = 1;
  3250. return 0;
  3251. }
  3252. /*
  3253. * Single level of the btree record deletion routine.
  3254. * Delete record pointed to by cur/level.
  3255. * Remove the record from its block then rebalance the tree.
  3256. * Return 0 for error, 1 for done, 2 to go on to the next level.
  3257. */
  3258. STATIC int /* error */
  3259. xfs_btree_delrec(
  3260. struct xfs_btree_cur *cur, /* btree cursor */
  3261. int level, /* level removing record from */
  3262. int *stat) /* fail/done/go-on */
  3263. {
  3264. struct xfs_btree_block *block; /* btree block */
  3265. union xfs_btree_ptr cptr; /* current block ptr */
  3266. struct xfs_buf *bp; /* buffer for block */
  3267. int error; /* error return value */
  3268. int i; /* loop counter */
  3269. union xfs_btree_ptr lptr; /* left sibling block ptr */
  3270. struct xfs_buf *lbp; /* left buffer pointer */
  3271. struct xfs_btree_block *left; /* left btree block */
  3272. int lrecs = 0; /* left record count */
  3273. int ptr; /* key/record index */
  3274. union xfs_btree_ptr rptr; /* right sibling block ptr */
  3275. struct xfs_buf *rbp; /* right buffer pointer */
  3276. struct xfs_btree_block *right; /* right btree block */
  3277. struct xfs_btree_block *rrblock; /* right-right btree block */
  3278. struct xfs_buf *rrbp; /* right-right buffer pointer */
  3279. int rrecs = 0; /* right record count */
  3280. struct xfs_btree_cur *tcur; /* temporary btree cursor */
  3281. int numrecs; /* temporary numrec count */
  3282. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  3283. XFS_BTREE_TRACE_ARGI(cur, level);
  3284. tcur = NULL;
  3285. /* Get the index of the entry being deleted, check for nothing there. */
  3286. ptr = cur->bc_ptrs[level];
  3287. if (ptr == 0) {
  3288. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3289. *stat = 0;
  3290. return 0;
  3291. }
  3292. /* Get the buffer & block containing the record or key/ptr. */
  3293. block = xfs_btree_get_block(cur, level, &bp);
  3294. numrecs = xfs_btree_get_numrecs(block);
  3295. #ifdef DEBUG
  3296. error = xfs_btree_check_block(cur, block, level, bp);
  3297. if (error)
  3298. goto error0;
  3299. #endif
  3300. /* Fail if we're off the end of the block. */
  3301. if (ptr > numrecs) {
  3302. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3303. *stat = 0;
  3304. return 0;
  3305. }
  3306. XFS_BTREE_STATS_INC(cur, delrec);
  3307. XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
  3308. /* Excise the entries being deleted. */
  3309. if (level > 0) {
  3310. /* It's a nonleaf. operate on keys and ptrs */
  3311. union xfs_btree_key *lkp;
  3312. union xfs_btree_ptr *lpp;
  3313. lkp = xfs_btree_key_addr(cur, ptr + 1, block);
  3314. lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
  3315. #ifdef DEBUG
  3316. for (i = 0; i < numrecs - ptr; i++) {
  3317. error = xfs_btree_check_ptr(cur, lpp, i, level);
  3318. if (error)
  3319. goto error0;
  3320. }
  3321. #endif
  3322. if (ptr < numrecs) {
  3323. xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
  3324. xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
  3325. xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
  3326. xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
  3327. }
  3328. } else {
  3329. /* It's a leaf. operate on records */
  3330. if (ptr < numrecs) {
  3331. xfs_btree_shift_recs(cur,
  3332. xfs_btree_rec_addr(cur, ptr + 1, block),
  3333. -1, numrecs - ptr);
  3334. xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
  3335. }
  3336. }
  3337. /*
  3338. * Decrement and log the number of entries in the block.
  3339. */
  3340. xfs_btree_set_numrecs(block, --numrecs);
  3341. xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
  3342. /*
  3343. * If we are tracking the last record in the tree and
  3344. * we are at the far right edge of the tree, update it.
  3345. */
  3346. if (xfs_btree_is_lastrec(cur, block, level)) {
  3347. cur->bc_ops->update_lastrec(cur, block, NULL,
  3348. ptr, LASTREC_DELREC);
  3349. }
  3350. /*
  3351. * We're at the root level. First, shrink the root block in-memory.
  3352. * Try to get rid of the next level down. If we can't then there's
  3353. * nothing left to do.
  3354. */
  3355. if (level == cur->bc_nlevels - 1) {
  3356. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
  3357. xfs_iroot_realloc(cur->bc_private.b.ip, -1,
  3358. cur->bc_private.b.whichfork);
  3359. error = xfs_btree_kill_iroot(cur);
  3360. if (error)
  3361. goto error0;
  3362. error = xfs_btree_dec_cursor(cur, level, stat);
  3363. if (error)
  3364. goto error0;
  3365. *stat = 1;
  3366. return 0;
  3367. }
  3368. /*
  3369. * If this is the root level, and there's only one entry left,
  3370. * and it's NOT the leaf level, then we can get rid of this
  3371. * level.
  3372. */
  3373. if (numrecs == 1 && level > 0) {
  3374. union xfs_btree_ptr *pp;
  3375. /*
  3376. * pp is still set to the first pointer in the block.
  3377. * Make it the new root of the btree.
  3378. */
  3379. pp = xfs_btree_ptr_addr(cur, 1, block);
  3380. error = xfs_btree_kill_root(cur, bp, level, pp);
  3381. if (error)
  3382. goto error0;
  3383. } else if (level > 0) {
  3384. error = xfs_btree_dec_cursor(cur, level, stat);
  3385. if (error)
  3386. goto error0;
  3387. }
  3388. *stat = 1;
  3389. return 0;
  3390. }
  3391. /*
  3392. * If we deleted the leftmost entry in the block, update the
  3393. * key values above us in the tree.
  3394. */
  3395. if (xfs_btree_needs_key_update(cur, ptr)) {
  3396. error = xfs_btree_update_keys(cur, level);
  3397. if (error)
  3398. goto error0;
  3399. }
  3400. /*
  3401. * If the number of records remaining in the block is at least
  3402. * the minimum, we're done.
  3403. */
  3404. if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
  3405. error = xfs_btree_dec_cursor(cur, level, stat);
  3406. if (error)
  3407. goto error0;
  3408. return 0;
  3409. }
  3410. /*
  3411. * Otherwise, we have to move some records around to keep the
  3412. * tree balanced. Look at the left and right sibling blocks to
  3413. * see if we can re-balance by moving only one record.
  3414. */
  3415. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  3416. xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
  3417. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
  3418. /*
  3419. * One child of root, need to get a chance to copy its contents
  3420. * into the root and delete it. Can't go up to next level,
  3421. * there's nothing to delete there.
  3422. */
  3423. if (xfs_btree_ptr_is_null(cur, &rptr) &&
  3424. xfs_btree_ptr_is_null(cur, &lptr) &&
  3425. level == cur->bc_nlevels - 2) {
  3426. error = xfs_btree_kill_iroot(cur);
  3427. if (!error)
  3428. error = xfs_btree_dec_cursor(cur, level, stat);
  3429. if (error)
  3430. goto error0;
  3431. return 0;
  3432. }
  3433. }
  3434. ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
  3435. !xfs_btree_ptr_is_null(cur, &lptr));
  3436. /*
  3437. * Duplicate the cursor so our btree manipulations here won't
  3438. * disrupt the next level up.
  3439. */
  3440. error = xfs_btree_dup_cursor(cur, &tcur);
  3441. if (error)
  3442. goto error0;
  3443. /*
  3444. * If there's a right sibling, see if it's ok to shift an entry
  3445. * out of it.
  3446. */
  3447. if (!xfs_btree_ptr_is_null(cur, &rptr)) {
  3448. /*
  3449. * Move the temp cursor to the last entry in the next block.
  3450. * Actually any entry but the first would suffice.
  3451. */
  3452. i = xfs_btree_lastrec(tcur, level);
  3453. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
  3454. error = xfs_btree_increment(tcur, level, &i);
  3455. if (error)
  3456. goto error0;
  3457. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
  3458. i = xfs_btree_lastrec(tcur, level);
  3459. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
  3460. /* Grab a pointer to the block. */
  3461. right = xfs_btree_get_block(tcur, level, &rbp);
  3462. #ifdef DEBUG
  3463. error = xfs_btree_check_block(tcur, right, level, rbp);
  3464. if (error)
  3465. goto error0;
  3466. #endif
  3467. /* Grab the current block number, for future use. */
  3468. xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
  3469. /*
  3470. * If right block is full enough so that removing one entry
  3471. * won't make it too empty, and left-shifting an entry out
  3472. * of right to us works, we're done.
  3473. */
  3474. if (xfs_btree_get_numrecs(right) - 1 >=
  3475. cur->bc_ops->get_minrecs(tcur, level)) {
  3476. error = xfs_btree_lshift(tcur, level, &i);
  3477. if (error)
  3478. goto error0;
  3479. if (i) {
  3480. ASSERT(xfs_btree_get_numrecs(block) >=
  3481. cur->bc_ops->get_minrecs(tcur, level));
  3482. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  3483. tcur = NULL;
  3484. error = xfs_btree_dec_cursor(cur, level, stat);
  3485. if (error)
  3486. goto error0;
  3487. return 0;
  3488. }
  3489. }
  3490. /*
  3491. * Otherwise, grab the number of records in right for
  3492. * future reference, and fix up the temp cursor to point
  3493. * to our block again (last record).
  3494. */
  3495. rrecs = xfs_btree_get_numrecs(right);
  3496. if (!xfs_btree_ptr_is_null(cur, &lptr)) {
  3497. i = xfs_btree_firstrec(tcur, level);
  3498. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
  3499. error = xfs_btree_decrement(tcur, level, &i);
  3500. if (error)
  3501. goto error0;
  3502. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
  3503. }
  3504. }
  3505. /*
  3506. * If there's a left sibling, see if it's ok to shift an entry
  3507. * out of it.
  3508. */
  3509. if (!xfs_btree_ptr_is_null(cur, &lptr)) {
  3510. /*
  3511. * Move the temp cursor to the first entry in the
  3512. * previous block.
  3513. */
  3514. i = xfs_btree_firstrec(tcur, level);
  3515. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
  3516. error = xfs_btree_decrement(tcur, level, &i);
  3517. if (error)
  3518. goto error0;
  3519. i = xfs_btree_firstrec(tcur, level);
  3520. XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
  3521. /* Grab a pointer to the block. */
  3522. left = xfs_btree_get_block(tcur, level, &lbp);
  3523. #ifdef DEBUG
  3524. error = xfs_btree_check_block(cur, left, level, lbp);
  3525. if (error)
  3526. goto error0;
  3527. #endif
  3528. /* Grab the current block number, for future use. */
  3529. xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
  3530. /*
  3531. * If left block is full enough so that removing one entry
  3532. * won't make it too empty, and right-shifting an entry out
  3533. * of left to us works, we're done.
  3534. */
  3535. if (xfs_btree_get_numrecs(left) - 1 >=
  3536. cur->bc_ops->get_minrecs(tcur, level)) {
  3537. error = xfs_btree_rshift(tcur, level, &i);
  3538. if (error)
  3539. goto error0;
  3540. if (i) {
  3541. ASSERT(xfs_btree_get_numrecs(block) >=
  3542. cur->bc_ops->get_minrecs(tcur, level));
  3543. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  3544. tcur = NULL;
  3545. if (level == 0)
  3546. cur->bc_ptrs[0]++;
  3547. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3548. *stat = 1;
  3549. return 0;
  3550. }
  3551. }
  3552. /*
  3553. * Otherwise, grab the number of records in right for
  3554. * future reference.
  3555. */
  3556. lrecs = xfs_btree_get_numrecs(left);
  3557. }
  3558. /* Delete the temp cursor, we're done with it. */
  3559. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  3560. tcur = NULL;
  3561. /* If here, we need to do a join to keep the tree balanced. */
  3562. ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
  3563. if (!xfs_btree_ptr_is_null(cur, &lptr) &&
  3564. lrecs + xfs_btree_get_numrecs(block) <=
  3565. cur->bc_ops->get_maxrecs(cur, level)) {
  3566. /*
  3567. * Set "right" to be the starting block,
  3568. * "left" to be the left neighbor.
  3569. */
  3570. rptr = cptr;
  3571. right = block;
  3572. rbp = bp;
  3573. error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
  3574. if (error)
  3575. goto error0;
  3576. /*
  3577. * If that won't work, see if we can join with the right neighbor block.
  3578. */
  3579. } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
  3580. rrecs + xfs_btree_get_numrecs(block) <=
  3581. cur->bc_ops->get_maxrecs(cur, level)) {
  3582. /*
  3583. * Set "left" to be the starting block,
  3584. * "right" to be the right neighbor.
  3585. */
  3586. lptr = cptr;
  3587. left = block;
  3588. lbp = bp;
  3589. error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
  3590. if (error)
  3591. goto error0;
  3592. /*
  3593. * Otherwise, we can't fix the imbalance.
  3594. * Just return. This is probably a logic error, but it's not fatal.
  3595. */
  3596. } else {
  3597. error = xfs_btree_dec_cursor(cur, level, stat);
  3598. if (error)
  3599. goto error0;
  3600. return 0;
  3601. }
  3602. rrecs = xfs_btree_get_numrecs(right);
  3603. lrecs = xfs_btree_get_numrecs(left);
  3604. /*
  3605. * We're now going to join "left" and "right" by moving all the stuff
  3606. * in "right" to "left" and deleting "right".
  3607. */
  3608. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  3609. if (level > 0) {
  3610. /* It's a non-leaf. Move keys and pointers. */
  3611. union xfs_btree_key *lkp; /* left btree key */
  3612. union xfs_btree_ptr *lpp; /* left address pointer */
  3613. union xfs_btree_key *rkp; /* right btree key */
  3614. union xfs_btree_ptr *rpp; /* right address pointer */
  3615. lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
  3616. lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
  3617. rkp = xfs_btree_key_addr(cur, 1, right);
  3618. rpp = xfs_btree_ptr_addr(cur, 1, right);
  3619. #ifdef DEBUG
  3620. for (i = 1; i < rrecs; i++) {
  3621. error = xfs_btree_check_ptr(cur, rpp, i, level);
  3622. if (error)
  3623. goto error0;
  3624. }
  3625. #endif
  3626. xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
  3627. xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
  3628. xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
  3629. xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
  3630. } else {
  3631. /* It's a leaf. Move records. */
  3632. union xfs_btree_rec *lrp; /* left record pointer */
  3633. union xfs_btree_rec *rrp; /* right record pointer */
  3634. lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
  3635. rrp = xfs_btree_rec_addr(cur, 1, right);
  3636. xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
  3637. xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
  3638. }
  3639. XFS_BTREE_STATS_INC(cur, join);
  3640. /*
  3641. * Fix up the number of records and right block pointer in the
  3642. * surviving block, and log it.
  3643. */
  3644. xfs_btree_set_numrecs(left, lrecs + rrecs);
  3645. xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
  3646. xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
  3647. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  3648. /* If there is a right sibling, point it to the remaining block. */
  3649. xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
  3650. if (!xfs_btree_ptr_is_null(cur, &cptr)) {
  3651. error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
  3652. if (error)
  3653. goto error0;
  3654. xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
  3655. xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
  3656. }
  3657. /* Free the deleted block. */
  3658. error = xfs_btree_free_block(cur, rbp);
  3659. if (error)
  3660. goto error0;
  3661. /*
  3662. * If we joined with the left neighbor, set the buffer in the
  3663. * cursor to the left block, and fix up the index.
  3664. */
  3665. if (bp != lbp) {
  3666. cur->bc_bufs[level] = lbp;
  3667. cur->bc_ptrs[level] += lrecs;
  3668. cur->bc_ra[level] = 0;
  3669. }
  3670. /*
  3671. * If we joined with the right neighbor and there's a level above
  3672. * us, increment the cursor at that level.
  3673. */
  3674. else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
  3675. (level + 1 < cur->bc_nlevels)) {
  3676. error = xfs_btree_increment(cur, level + 1, &i);
  3677. if (error)
  3678. goto error0;
  3679. }
  3680. /*
  3681. * Readjust the ptr at this level if it's not a leaf, since it's
  3682. * still pointing at the deletion point, which makes the cursor
  3683. * inconsistent. If this makes the ptr 0, the caller fixes it up.
  3684. * We can't use decrement because it would change the next level up.
  3685. */
  3686. if (level > 0)
  3687. cur->bc_ptrs[level]--;
  3688. /*
  3689. * We combined blocks, so we have to update the parent keys if the
  3690. * btree supports overlapped intervals. However, bc_ptrs[level + 1]
  3691. * points to the old block so that the caller knows which record to
  3692. * delete. Therefore, the caller must be savvy enough to call updkeys
  3693. * for us if we return stat == 2. The other exit points from this
  3694. * function don't require deletions further up the tree, so they can
  3695. * call updkeys directly.
  3696. */
  3697. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3698. /* Return value means the next level up has something to do. */
  3699. *stat = 2;
  3700. return 0;
  3701. error0:
  3702. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3703. if (tcur)
  3704. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  3705. return error;
  3706. }
  3707. /*
  3708. * Delete the record pointed to by cur.
  3709. * The cursor refers to the place where the record was (could be inserted)
  3710. * when the operation returns.
  3711. */
  3712. int /* error */
  3713. xfs_btree_delete(
  3714. struct xfs_btree_cur *cur,
  3715. int *stat) /* success/failure */
  3716. {
  3717. int error; /* error return value */
  3718. int level;
  3719. int i;
  3720. bool joined = false;
  3721. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  3722. /*
  3723. * Go up the tree, starting at leaf level.
  3724. *
  3725. * If 2 is returned then a join was done; go to the next level.
  3726. * Otherwise we are done.
  3727. */
  3728. for (level = 0, i = 2; i == 2; level++) {
  3729. error = xfs_btree_delrec(cur, level, &i);
  3730. if (error)
  3731. goto error0;
  3732. if (i == 2)
  3733. joined = true;
  3734. }
  3735. /*
  3736. * If we combined blocks as part of deleting the record, delrec won't
  3737. * have updated the parent high keys so we have to do that here.
  3738. */
  3739. if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
  3740. error = xfs_btree_updkeys_force(cur, 0);
  3741. if (error)
  3742. goto error0;
  3743. }
  3744. if (i == 0) {
  3745. for (level = 1; level < cur->bc_nlevels; level++) {
  3746. if (cur->bc_ptrs[level] == 0) {
  3747. error = xfs_btree_decrement(cur, level, &i);
  3748. if (error)
  3749. goto error0;
  3750. break;
  3751. }
  3752. }
  3753. }
  3754. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3755. *stat = i;
  3756. return 0;
  3757. error0:
  3758. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3759. return error;
  3760. }
  3761. /*
  3762. * Get the data from the pointed-to record.
  3763. */
  3764. int /* error */
  3765. xfs_btree_get_rec(
  3766. struct xfs_btree_cur *cur, /* btree cursor */
  3767. union xfs_btree_rec **recp, /* output: btree record */
  3768. int *stat) /* output: success/failure */
  3769. {
  3770. struct xfs_btree_block *block; /* btree block */
  3771. struct xfs_buf *bp; /* buffer pointer */
  3772. int ptr; /* record number */
  3773. #ifdef DEBUG
  3774. int error; /* error return value */
  3775. #endif
  3776. ptr = cur->bc_ptrs[0];
  3777. block = xfs_btree_get_block(cur, 0, &bp);
  3778. #ifdef DEBUG
  3779. error = xfs_btree_check_block(cur, block, 0, bp);
  3780. if (error)
  3781. return error;
  3782. #endif
  3783. /*
  3784. * Off the right end or left end, return failure.
  3785. */
  3786. if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
  3787. *stat = 0;
  3788. return 0;
  3789. }
  3790. /*
  3791. * Point to the record and extract its data.
  3792. */
  3793. *recp = xfs_btree_rec_addr(cur, ptr, block);
  3794. *stat = 1;
  3795. return 0;
  3796. }
  3797. /* Visit a block in a btree. */
  3798. STATIC int
  3799. xfs_btree_visit_block(
  3800. struct xfs_btree_cur *cur,
  3801. int level,
  3802. xfs_btree_visit_blocks_fn fn,
  3803. void *data)
  3804. {
  3805. struct xfs_btree_block *block;
  3806. struct xfs_buf *bp;
  3807. union xfs_btree_ptr rptr;
  3808. int error;
  3809. /* do right sibling readahead */
  3810. xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
  3811. block = xfs_btree_get_block(cur, level, &bp);
  3812. /* process the block */
  3813. error = fn(cur, level, data);
  3814. if (error)
  3815. return error;
  3816. /* now read rh sibling block for next iteration */
  3817. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  3818. if (xfs_btree_ptr_is_null(cur, &rptr))
  3819. return -ENOENT;
  3820. return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
  3821. }
  3822. /* Visit every block in a btree. */
  3823. int
  3824. xfs_btree_visit_blocks(
  3825. struct xfs_btree_cur *cur,
  3826. xfs_btree_visit_blocks_fn fn,
  3827. void *data)
  3828. {
  3829. union xfs_btree_ptr lptr;
  3830. int level;
  3831. struct xfs_btree_block *block = NULL;
  3832. int error = 0;
  3833. cur->bc_ops->init_ptr_from_cur(cur, &lptr);
  3834. /* for each level */
  3835. for (level = cur->bc_nlevels - 1; level >= 0; level--) {
  3836. /* grab the left hand block */
  3837. error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
  3838. if (error)
  3839. return error;
  3840. /* readahead the left most block for the next level down */
  3841. if (level > 0) {
  3842. union xfs_btree_ptr *ptr;
  3843. ptr = xfs_btree_ptr_addr(cur, 1, block);
  3844. xfs_btree_readahead_ptr(cur, ptr, 1);
  3845. /* save for the next iteration of the loop */
  3846. xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
  3847. }
  3848. /* for each buffer in the level */
  3849. do {
  3850. error = xfs_btree_visit_block(cur, level, fn, data);
  3851. } while (!error);
  3852. if (error != -ENOENT)
  3853. return error;
  3854. }
  3855. return 0;
  3856. }
  3857. /*
  3858. * Change the owner of a btree.
  3859. *
  3860. * The mechanism we use here is ordered buffer logging. Because we don't know
  3861. * how many buffers were are going to need to modify, we don't really want to
  3862. * have to make transaction reservations for the worst case of every buffer in a
  3863. * full size btree as that may be more space that we can fit in the log....
  3864. *
  3865. * We do the btree walk in the most optimal manner possible - we have sibling
  3866. * pointers so we can just walk all the blocks on each level from left to right
  3867. * in a single pass, and then move to the next level and do the same. We can
  3868. * also do readahead on the sibling pointers to get IO moving more quickly,
  3869. * though for slow disks this is unlikely to make much difference to performance
  3870. * as the amount of CPU work we have to do before moving to the next block is
  3871. * relatively small.
  3872. *
  3873. * For each btree block that we load, modify the owner appropriately, set the
  3874. * buffer as an ordered buffer and log it appropriately. We need to ensure that
  3875. * we mark the region we change dirty so that if the buffer is relogged in
  3876. * a subsequent transaction the changes we make here as an ordered buffer are
  3877. * correctly relogged in that transaction. If we are in recovery context, then
  3878. * just queue the modified buffer as delayed write buffer so the transaction
  3879. * recovery completion writes the changes to disk.
  3880. */
  3881. struct xfs_btree_block_change_owner_info {
  3882. __uint64_t new_owner;
  3883. struct list_head *buffer_list;
  3884. };
  3885. static int
  3886. xfs_btree_block_change_owner(
  3887. struct xfs_btree_cur *cur,
  3888. int level,
  3889. void *data)
  3890. {
  3891. struct xfs_btree_block_change_owner_info *bbcoi = data;
  3892. struct xfs_btree_block *block;
  3893. struct xfs_buf *bp;
  3894. /* modify the owner */
  3895. block = xfs_btree_get_block(cur, level, &bp);
  3896. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  3897. if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner))
  3898. return 0;
  3899. block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
  3900. } else {
  3901. if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner))
  3902. return 0;
  3903. block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
  3904. }
  3905. /*
  3906. * If the block is a root block hosted in an inode, we might not have a
  3907. * buffer pointer here and we shouldn't attempt to log the change as the
  3908. * information is already held in the inode and discarded when the root
  3909. * block is formatted into the on-disk inode fork. We still change it,
  3910. * though, so everything is consistent in memory.
  3911. */
  3912. if (!bp) {
  3913. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  3914. ASSERT(level == cur->bc_nlevels - 1);
  3915. return 0;
  3916. }
  3917. if (cur->bc_tp) {
  3918. if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) {
  3919. xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
  3920. return -EAGAIN;
  3921. }
  3922. } else {
  3923. xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
  3924. }
  3925. return 0;
  3926. }
  3927. int
  3928. xfs_btree_change_owner(
  3929. struct xfs_btree_cur *cur,
  3930. __uint64_t new_owner,
  3931. struct list_head *buffer_list)
  3932. {
  3933. struct xfs_btree_block_change_owner_info bbcoi;
  3934. bbcoi.new_owner = new_owner;
  3935. bbcoi.buffer_list = buffer_list;
  3936. return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
  3937. &bbcoi);
  3938. }
  3939. /**
  3940. * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
  3941. * btree block
  3942. *
  3943. * @bp: buffer containing the btree block
  3944. * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
  3945. * @pag_max_level: pointer to the per-ag max level field
  3946. */
  3947. bool
  3948. xfs_btree_sblock_v5hdr_verify(
  3949. struct xfs_buf *bp)
  3950. {
  3951. struct xfs_mount *mp = bp->b_target->bt_mount;
  3952. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  3953. struct xfs_perag *pag = bp->b_pag;
  3954. if (!xfs_sb_version_hascrc(&mp->m_sb))
  3955. return false;
  3956. if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
  3957. return false;
  3958. if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
  3959. return false;
  3960. if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
  3961. return false;
  3962. return true;
  3963. }
  3964. /**
  3965. * xfs_btree_sblock_verify() -- verify a short-format btree block
  3966. *
  3967. * @bp: buffer containing the btree block
  3968. * @max_recs: maximum records allowed in this btree node
  3969. */
  3970. bool
  3971. xfs_btree_sblock_verify(
  3972. struct xfs_buf *bp,
  3973. unsigned int max_recs)
  3974. {
  3975. struct xfs_mount *mp = bp->b_target->bt_mount;
  3976. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  3977. /* numrecs verification */
  3978. if (be16_to_cpu(block->bb_numrecs) > max_recs)
  3979. return false;
  3980. /* sibling pointer verification */
  3981. if (!block->bb_u.s.bb_leftsib ||
  3982. (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
  3983. block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
  3984. return false;
  3985. if (!block->bb_u.s.bb_rightsib ||
  3986. (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
  3987. block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
  3988. return false;
  3989. return true;
  3990. }
  3991. /*
  3992. * Calculate the number of btree levels needed to store a given number of
  3993. * records in a short-format btree.
  3994. */
  3995. uint
  3996. xfs_btree_compute_maxlevels(
  3997. struct xfs_mount *mp,
  3998. uint *limits,
  3999. unsigned long len)
  4000. {
  4001. uint level;
  4002. unsigned long maxblocks;
  4003. maxblocks = (len + limits[0] - 1) / limits[0];
  4004. for (level = 1; maxblocks > 1; level++)
  4005. maxblocks = (maxblocks + limits[1] - 1) / limits[1];
  4006. return level;
  4007. }
  4008. /*
  4009. * Query a regular btree for all records overlapping a given interval.
  4010. * Start with a LE lookup of the key of low_rec and return all records
  4011. * until we find a record with a key greater than the key of high_rec.
  4012. */
  4013. STATIC int
  4014. xfs_btree_simple_query_range(
  4015. struct xfs_btree_cur *cur,
  4016. union xfs_btree_key *low_key,
  4017. union xfs_btree_key *high_key,
  4018. xfs_btree_query_range_fn fn,
  4019. void *priv)
  4020. {
  4021. union xfs_btree_rec *recp;
  4022. union xfs_btree_key rec_key;
  4023. __int64_t diff;
  4024. int stat;
  4025. bool firstrec = true;
  4026. int error;
  4027. ASSERT(cur->bc_ops->init_high_key_from_rec);
  4028. ASSERT(cur->bc_ops->diff_two_keys);
  4029. /*
  4030. * Find the leftmost record. The btree cursor must be set
  4031. * to the low record used to generate low_key.
  4032. */
  4033. stat = 0;
  4034. error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
  4035. if (error)
  4036. goto out;
  4037. /* Nothing? See if there's anything to the right. */
  4038. if (!stat) {
  4039. error = xfs_btree_increment(cur, 0, &stat);
  4040. if (error)
  4041. goto out;
  4042. }
  4043. while (stat) {
  4044. /* Find the record. */
  4045. error = xfs_btree_get_rec(cur, &recp, &stat);
  4046. if (error || !stat)
  4047. break;
  4048. /* Skip if high_key(rec) < low_key. */
  4049. if (firstrec) {
  4050. cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
  4051. firstrec = false;
  4052. diff = cur->bc_ops->diff_two_keys(cur, low_key,
  4053. &rec_key);
  4054. if (diff > 0)
  4055. goto advloop;
  4056. }
  4057. /* Stop if high_key < low_key(rec). */
  4058. cur->bc_ops->init_key_from_rec(&rec_key, recp);
  4059. diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
  4060. if (diff > 0)
  4061. break;
  4062. /* Callback */
  4063. error = fn(cur, recp, priv);
  4064. if (error < 0 || error == XFS_BTREE_QUERY_RANGE_ABORT)
  4065. break;
  4066. advloop:
  4067. /* Move on to the next record. */
  4068. error = xfs_btree_increment(cur, 0, &stat);
  4069. if (error)
  4070. break;
  4071. }
  4072. out:
  4073. return error;
  4074. }
  4075. /*
  4076. * Query an overlapped interval btree for all records overlapping a given
  4077. * interval. This function roughly follows the algorithm given in
  4078. * "Interval Trees" of _Introduction to Algorithms_, which is section
  4079. * 14.3 in the 2nd and 3rd editions.
  4080. *
  4081. * First, generate keys for the low and high records passed in.
  4082. *
  4083. * For any leaf node, generate the high and low keys for the record.
  4084. * If the record keys overlap with the query low/high keys, pass the
  4085. * record to the function iterator.
  4086. *
  4087. * For any internal node, compare the low and high keys of each
  4088. * pointer against the query low/high keys. If there's an overlap,
  4089. * follow the pointer.
  4090. *
  4091. * As an optimization, we stop scanning a block when we find a low key
  4092. * that is greater than the query's high key.
  4093. */
  4094. STATIC int
  4095. xfs_btree_overlapped_query_range(
  4096. struct xfs_btree_cur *cur,
  4097. union xfs_btree_key *low_key,
  4098. union xfs_btree_key *high_key,
  4099. xfs_btree_query_range_fn fn,
  4100. void *priv)
  4101. {
  4102. union xfs_btree_ptr ptr;
  4103. union xfs_btree_ptr *pp;
  4104. union xfs_btree_key rec_key;
  4105. union xfs_btree_key rec_hkey;
  4106. union xfs_btree_key *lkp;
  4107. union xfs_btree_key *hkp;
  4108. union xfs_btree_rec *recp;
  4109. struct xfs_btree_block *block;
  4110. __int64_t ldiff;
  4111. __int64_t hdiff;
  4112. int level;
  4113. struct xfs_buf *bp;
  4114. int i;
  4115. int error;
  4116. /* Load the root of the btree. */
  4117. level = cur->bc_nlevels - 1;
  4118. cur->bc_ops->init_ptr_from_cur(cur, &ptr);
  4119. error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
  4120. if (error)
  4121. return error;
  4122. xfs_btree_get_block(cur, level, &bp);
  4123. trace_xfs_btree_overlapped_query_range(cur, level, bp);
  4124. #ifdef DEBUG
  4125. error = xfs_btree_check_block(cur, block, level, bp);
  4126. if (error)
  4127. goto out;
  4128. #endif
  4129. cur->bc_ptrs[level] = 1;
  4130. while (level < cur->bc_nlevels) {
  4131. block = xfs_btree_get_block(cur, level, &bp);
  4132. /* End of node, pop back towards the root. */
  4133. if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
  4134. pop_up:
  4135. if (level < cur->bc_nlevels - 1)
  4136. cur->bc_ptrs[level + 1]++;
  4137. level++;
  4138. continue;
  4139. }
  4140. if (level == 0) {
  4141. /* Handle a leaf node. */
  4142. recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
  4143. cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
  4144. ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
  4145. low_key);
  4146. cur->bc_ops->init_key_from_rec(&rec_key, recp);
  4147. hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
  4148. &rec_key);
  4149. /*
  4150. * If (record's high key >= query's low key) and
  4151. * (query's high key >= record's low key), then
  4152. * this record overlaps the query range; callback.
  4153. */
  4154. if (ldiff >= 0 && hdiff >= 0) {
  4155. error = fn(cur, recp, priv);
  4156. if (error < 0 ||
  4157. error == XFS_BTREE_QUERY_RANGE_ABORT)
  4158. break;
  4159. } else if (hdiff < 0) {
  4160. /* Record is larger than high key; pop. */
  4161. goto pop_up;
  4162. }
  4163. cur->bc_ptrs[level]++;
  4164. continue;
  4165. }
  4166. /* Handle an internal node. */
  4167. lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
  4168. hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
  4169. pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
  4170. ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
  4171. hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
  4172. /*
  4173. * If (pointer's high key >= query's low key) and
  4174. * (query's high key >= pointer's low key), then
  4175. * this record overlaps the query range; follow pointer.
  4176. */
  4177. if (ldiff >= 0 && hdiff >= 0) {
  4178. level--;
  4179. error = xfs_btree_lookup_get_block(cur, level, pp,
  4180. &block);
  4181. if (error)
  4182. goto out;
  4183. xfs_btree_get_block(cur, level, &bp);
  4184. trace_xfs_btree_overlapped_query_range(cur, level, bp);
  4185. #ifdef DEBUG
  4186. error = xfs_btree_check_block(cur, block, level, bp);
  4187. if (error)
  4188. goto out;
  4189. #endif
  4190. cur->bc_ptrs[level] = 1;
  4191. continue;
  4192. } else if (hdiff < 0) {
  4193. /* The low key is larger than the upper range; pop. */
  4194. goto pop_up;
  4195. }
  4196. cur->bc_ptrs[level]++;
  4197. }
  4198. out:
  4199. /*
  4200. * If we don't end this function with the cursor pointing at a record
  4201. * block, a subsequent non-error cursor deletion will not release
  4202. * node-level buffers, causing a buffer leak. This is quite possible
  4203. * with a zero-results range query, so release the buffers if we
  4204. * failed to return any results.
  4205. */
  4206. if (cur->bc_bufs[0] == NULL) {
  4207. for (i = 0; i < cur->bc_nlevels; i++) {
  4208. if (cur->bc_bufs[i]) {
  4209. xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
  4210. cur->bc_bufs[i] = NULL;
  4211. cur->bc_ptrs[i] = 0;
  4212. cur->bc_ra[i] = 0;
  4213. }
  4214. }
  4215. }
  4216. return error;
  4217. }
  4218. /*
  4219. * Query a btree for all records overlapping a given interval of keys. The
  4220. * supplied function will be called with each record found; return one of the
  4221. * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
  4222. * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a
  4223. * negative error code.
  4224. */
  4225. int
  4226. xfs_btree_query_range(
  4227. struct xfs_btree_cur *cur,
  4228. union xfs_btree_irec *low_rec,
  4229. union xfs_btree_irec *high_rec,
  4230. xfs_btree_query_range_fn fn,
  4231. void *priv)
  4232. {
  4233. union xfs_btree_rec rec;
  4234. union xfs_btree_key low_key;
  4235. union xfs_btree_key high_key;
  4236. /* Find the keys of both ends of the interval. */
  4237. cur->bc_rec = *high_rec;
  4238. cur->bc_ops->init_rec_from_cur(cur, &rec);
  4239. cur->bc_ops->init_key_from_rec(&high_key, &rec);
  4240. cur->bc_rec = *low_rec;
  4241. cur->bc_ops->init_rec_from_cur(cur, &rec);
  4242. cur->bc_ops->init_key_from_rec(&low_key, &rec);
  4243. /* Enforce low key < high key. */
  4244. if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
  4245. return -EINVAL;
  4246. if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
  4247. return xfs_btree_simple_query_range(cur, &low_key,
  4248. &high_key, fn, priv);
  4249. return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
  4250. fn, priv);
  4251. }
  4252. /*
  4253. * Calculate the number of blocks needed to store a given number of records
  4254. * in a short-format (per-AG metadata) btree.
  4255. */
  4256. xfs_extlen_t
  4257. xfs_btree_calc_size(
  4258. struct xfs_mount *mp,
  4259. uint *limits,
  4260. unsigned long long len)
  4261. {
  4262. int level;
  4263. int maxrecs;
  4264. xfs_extlen_t rval;
  4265. maxrecs = limits[0];
  4266. for (level = 0, rval = 0; len > 1; level++) {
  4267. len += maxrecs - 1;
  4268. do_div(len, maxrecs);
  4269. maxrecs = limits[1];
  4270. rval += len;
  4271. }
  4272. return rval;
  4273. }
  4274. static int
  4275. xfs_btree_count_blocks_helper(
  4276. struct xfs_btree_cur *cur,
  4277. int level,
  4278. void *data)
  4279. {
  4280. xfs_extlen_t *blocks = data;
  4281. (*blocks)++;
  4282. return 0;
  4283. }
  4284. /* Count the blocks in a btree and return the result in *blocks. */
  4285. int
  4286. xfs_btree_count_blocks(
  4287. struct xfs_btree_cur *cur,
  4288. xfs_extlen_t *blocks)
  4289. {
  4290. *blocks = 0;
  4291. return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
  4292. blocks);
  4293. }