xfs_btree.c 128 KB

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