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