xfs_btree.c 128 KB

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