zfs.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2009,2010,2011 Free Software Foundation, Inc.
  4. * Copyright 2010 Sun Microsystems, Inc.
  5. * Copyright (c) 2012 by Delphix. All rights reserved.
  6. *
  7. * GRUB is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GRUB is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. * The zfs plug-in routines for GRUB are:
  22. *
  23. * zfs_mount() - locates a valid uberblock of the root pool and reads
  24. * in its MOS at the memory address MOS.
  25. *
  26. * zfs_open() - locates a plain file object by following the MOS
  27. * and places its dnode at the memory address DNODE.
  28. *
  29. * zfs_read() - read in the data blocks pointed by the DNODE.
  30. *
  31. */
  32. #include <grub/err.h>
  33. #include <grub/file.h>
  34. #include <grub/mm.h>
  35. #include <grub/misc.h>
  36. #include <grub/disk.h>
  37. #include <grub/partition.h>
  38. #include <grub/dl.h>
  39. #include <grub/types.h>
  40. #include <grub/zfs/zfs.h>
  41. #include <grub/zfs/zio.h>
  42. #include <grub/zfs/dnode.h>
  43. #include <grub/zfs/uberblock_impl.h>
  44. #include <grub/zfs/vdev_impl.h>
  45. #include <grub/zfs/zio_checksum.h>
  46. #include <grub/zfs/zap_impl.h>
  47. #include <grub/zfs/zap_leaf.h>
  48. #include <grub/zfs/zfs_znode.h>
  49. #include <grub/zfs/dmu.h>
  50. #include <grub/zfs/dmu_objset.h>
  51. #include <grub/zfs/sa_impl.h>
  52. #include <grub/zfs/dsl_dir.h>
  53. #include <grub/zfs/dsl_dataset.h>
  54. #include <grub/deflate.h>
  55. #include <grub/crypto.h>
  56. #include <grub/i18n.h>
  57. #include <grub/safemath.h>
  58. #include <zstd.h>
  59. GRUB_MOD_LICENSE ("GPLv3+");
  60. #define ZPOOL_PROP_BOOTFS "bootfs"
  61. /*
  62. * For nvlist manipulation. (from nvpair.h)
  63. */
  64. #define NV_ENCODE_NATIVE 0
  65. #define NV_ENCODE_XDR 1
  66. #define NV_BIG_ENDIAN 0
  67. #define NV_LITTLE_ENDIAN 1
  68. #define DATA_TYPE_UINT64 8
  69. #define DATA_TYPE_STRING 9
  70. #define DATA_TYPE_NVLIST 19
  71. #define DATA_TYPE_NVLIST_ARRAY 20
  72. #define DNODE_NUM_MASK 0xffffffffffffULL
  73. #ifndef GRUB_UTIL
  74. static grub_dl_t my_mod;
  75. #endif
  76. #define P2PHASE(x, align) ((x) & ((align) - 1))
  77. static inline grub_disk_addr_t
  78. DVA_OFFSET_TO_PHYS_SECTOR (grub_disk_addr_t offset)
  79. {
  80. return ((offset + VDEV_LABEL_START_SIZE) >> SPA_MINBLOCKSHIFT);
  81. }
  82. /*
  83. * FAT ZAP data structures
  84. */
  85. #define ZFS_CRC64_POLY 0xC96C5795D7870F42ULL /* ECMA-182, reflected form */
  86. static inline grub_uint64_t
  87. ZAP_HASH_IDX (grub_uint64_t hash, grub_uint64_t n)
  88. {
  89. return (((n) == 0) ? 0 : ((hash) >> (64 - (n))));
  90. }
  91. #define CHAIN_END 0xffff /* end of the chunk chain */
  92. /*
  93. * The amount of space within the chunk available for the array is:
  94. * chunk size - space for type (1) - space for next pointer (2)
  95. */
  96. #define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
  97. static inline int
  98. ZAP_LEAF_HASH_SHIFT (int bs)
  99. {
  100. return bs - 5;
  101. }
  102. static inline int
  103. ZAP_LEAF_HASH_NUMENTRIES (int bs)
  104. {
  105. return 1 << ZAP_LEAF_HASH_SHIFT(bs);
  106. }
  107. static inline grub_size_t
  108. LEAF_HASH (int bs, grub_uint64_t h, zap_leaf_phys_t *l)
  109. {
  110. return ((ZAP_LEAF_HASH_NUMENTRIES (bs)-1)
  111. & ((h) >> (64 - ZAP_LEAF_HASH_SHIFT (bs) - l->l_hdr.lh_prefix_len)));
  112. }
  113. /*
  114. * The amount of space available for chunks is:
  115. * block size shift - hash entry size (2) * number of hash
  116. * entries - header space (2*chunksize)
  117. */
  118. static inline int
  119. ZAP_LEAF_NUMCHUNKS (int bs)
  120. {
  121. return (((1U << bs) - 2 * ZAP_LEAF_HASH_NUMENTRIES (bs)) /
  122. ZAP_LEAF_CHUNKSIZE - 2);
  123. }
  124. /*
  125. * The chunks start immediately after the hash table. The end of the
  126. * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
  127. * chunk_t.
  128. */
  129. static inline zap_leaf_chunk_t *
  130. ZAP_LEAF_CHUNK (zap_leaf_phys_t *l, int bs, int idx)
  131. {
  132. grub_properly_aligned_t *l_entries;
  133. l_entries = (grub_properly_aligned_t *) ALIGN_UP((grub_addr_t)l->l_hash, sizeof (grub_properly_aligned_t));
  134. return &((zap_leaf_chunk_t *) (l_entries
  135. + (ZAP_LEAF_HASH_NUMENTRIES(bs) * 2)
  136. / sizeof (grub_properly_aligned_t)))[idx];
  137. }
  138. static inline struct zap_leaf_entry *
  139. ZAP_LEAF_ENTRY(zap_leaf_phys_t *l, int bs, int idx)
  140. {
  141. return &ZAP_LEAF_CHUNK(l, bs, idx)->l_entry;
  142. }
  143. /*
  144. * Decompression Entry - lzjb & lz4
  145. */
  146. extern grub_err_t lzjb_decompress (void *, void *, grub_size_t, grub_size_t);
  147. extern grub_err_t lz4_decompress (void *, void *, grub_size_t, grub_size_t);
  148. typedef grub_err_t zfs_decomp_func_t (void *s_start, void *d_start,
  149. grub_size_t s_len, grub_size_t d_len);
  150. typedef struct decomp_entry
  151. {
  152. const char *name;
  153. zfs_decomp_func_t *decomp_func;
  154. } decomp_entry_t;
  155. /*
  156. * Signature for checksum functions.
  157. */
  158. typedef void zio_checksum_t(const void *data, grub_uint64_t size,
  159. grub_zfs_endian_t endian, zio_cksum_t *zcp);
  160. /*
  161. * Information about each checksum function.
  162. */
  163. typedef struct zio_checksum_info {
  164. zio_checksum_t *ci_func; /* checksum function for each byteorder */
  165. int ci_correctable; /* number of correctable bits */
  166. int ci_eck; /* uses zio embedded checksum? */
  167. const char *ci_name; /* descriptive name */
  168. } zio_checksum_info_t;
  169. typedef struct dnode_end
  170. {
  171. dnode_phys_t dn;
  172. grub_zfs_endian_t endian;
  173. } dnode_end_t;
  174. struct grub_zfs_device_desc
  175. {
  176. enum { DEVICE_LEAF, DEVICE_MIRROR, DEVICE_RAIDZ } type;
  177. grub_uint64_t id;
  178. grub_uint64_t guid;
  179. unsigned ashift;
  180. unsigned max_children_ashift;
  181. /* Valid only for non-leafs. */
  182. unsigned n_children;
  183. struct grub_zfs_device_desc *children;
  184. /* Valid only for RAIDZ. */
  185. unsigned nparity;
  186. /* Valid only for leaf devices. */
  187. grub_device_t dev;
  188. grub_disk_addr_t vdev_phys_sector;
  189. uberblock_t current_uberblock;
  190. int original;
  191. };
  192. struct subvolume
  193. {
  194. dnode_end_t mdn;
  195. grub_uint64_t obj;
  196. grub_uint64_t case_insensitive;
  197. grub_size_t nkeys;
  198. struct
  199. {
  200. grub_crypto_cipher_handle_t cipher;
  201. grub_uint64_t txg;
  202. grub_uint64_t algo;
  203. } *keyring;
  204. };
  205. struct grub_zfs_data
  206. {
  207. /* cache for a file block of the currently zfs_open()-ed file */
  208. char *file_buf;
  209. grub_uint64_t file_start;
  210. grub_uint64_t file_end;
  211. /* cache for a dnode block */
  212. dnode_phys_t *dnode_buf;
  213. dnode_phys_t *dnode_mdn;
  214. grub_uint64_t dnode_start;
  215. grub_uint64_t dnode_end;
  216. grub_zfs_endian_t dnode_endian;
  217. dnode_end_t mos;
  218. dnode_end_t dnode;
  219. struct subvolume subvol;
  220. struct grub_zfs_device_desc *devices_attached;
  221. unsigned n_devices_attached;
  222. unsigned n_devices_allocated;
  223. struct grub_zfs_device_desc *device_original;
  224. uberblock_t current_uberblock;
  225. grub_uint64_t guid;
  226. };
  227. /* Context for grub_zfs_dir. */
  228. struct grub_zfs_dir_ctx
  229. {
  230. grub_fs_dir_hook_t hook;
  231. void *hook_data;
  232. struct grub_zfs_data *data;
  233. };
  234. grub_err_t (*grub_zfs_decrypt) (grub_crypto_cipher_handle_t cipher,
  235. grub_uint64_t algo,
  236. void *nonce,
  237. char *buf, grub_size_t size,
  238. const grub_uint32_t *expected_mac,
  239. grub_zfs_endian_t endian) = NULL;
  240. grub_crypto_cipher_handle_t (*grub_zfs_load_key) (const struct grub_zfs_key *key,
  241. grub_size_t keysize,
  242. grub_uint64_t salt,
  243. grub_uint64_t algo) = NULL;
  244. /*
  245. * List of pool features that the grub implementation of ZFS supports for
  246. * read. Note that features that are only required for write do not need
  247. * to be listed here since grub opens pools in read-only mode.
  248. */
  249. #define MAX_SUPPORTED_FEATURE_STRLEN 50
  250. static const char *spa_feature_names[] = {
  251. "org.illumos:lz4_compress",
  252. "com.delphix:hole_birth",
  253. "com.delphix:embedded_data",
  254. "com.delphix:extensible_dataset",
  255. "org.open-zfs:large_blocks",
  256. "com.klarasystems:vdev_zaps_v2",
  257. "com.delphix:head_errlog",
  258. "org.freebsd:zstd_compress",
  259. NULL
  260. };
  261. static int
  262. check_feature(const char *name, grub_uint64_t val, struct grub_zfs_dir_ctx *ctx);
  263. static grub_err_t
  264. check_mos_features(dnode_phys_t *mosmdn_phys,grub_zfs_endian_t endian,struct grub_zfs_data* data );
  265. static grub_err_t
  266. zlib_decompress (void *s, void *d,
  267. grub_size_t slen, grub_size_t dlen)
  268. {
  269. if (grub_zlib_decompress (s, slen, 0, d, dlen) == (grub_ssize_t) dlen)
  270. return GRUB_ERR_NONE;
  271. if (!grub_errno)
  272. grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
  273. "premature end of compressed");
  274. return grub_errno;
  275. }
  276. static grub_err_t
  277. zstd_decompress (void *ibuf, void *obuf, grub_size_t isize,
  278. grub_size_t osize)
  279. {
  280. grub_size_t zstd_ret;
  281. grub_uint32_t c_len;
  282. grub_uint8_t *byte_buf = (grub_uint8_t *) ibuf;
  283. if (isize < 8)
  284. return grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "zstd data too short");
  285. c_len = grub_be_to_cpu32 (grub_get_unaligned32 (byte_buf));
  286. if (c_len > isize - 8)
  287. return grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
  288. "zstd data announced size overflow");
  289. /*
  290. * ZFS uses non-stadard magic for zstd streams. Rather than adjusting
  291. * library functions, replace non-standard magic with standard one.
  292. */
  293. byte_buf[4] = 0x28;
  294. byte_buf[5] = 0xb5;
  295. byte_buf[6] = 0x2f;
  296. byte_buf[7] = 0xfd;
  297. zstd_ret = ZSTD_decompress (obuf, osize, byte_buf + 4, c_len + 4);
  298. if (ZSTD_isError (zstd_ret))
  299. return grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
  300. "zstd data corrupted (error %d)", (int) zstd_ret);
  301. return GRUB_ERR_NONE;
  302. }
  303. static grub_err_t
  304. zle_decompress (void *s, void *d,
  305. grub_size_t slen, grub_size_t dlen)
  306. {
  307. grub_uint8_t *iptr, *optr;
  308. grub_size_t clen;
  309. for (iptr = s, optr = d; iptr < (grub_uint8_t *) s + slen
  310. && optr < (grub_uint8_t *) d + dlen;)
  311. {
  312. if (*iptr & 0x80)
  313. clen = ((*iptr) & 0x7f) + 0x41;
  314. else
  315. clen = ((*iptr) & 0x3f) + 1;
  316. if ((grub_ssize_t) clen > (grub_uint8_t *) d + dlen - optr)
  317. clen = (grub_uint8_t *) d + dlen - optr;
  318. if (*iptr & 0x40 || *iptr & 0x80)
  319. {
  320. grub_memset (optr, 0, clen);
  321. iptr++;
  322. optr += clen;
  323. continue;
  324. }
  325. if ((grub_ssize_t) clen > (grub_uint8_t *) s + slen - iptr - 1)
  326. clen = (grub_uint8_t *) s + slen - iptr - 1;
  327. grub_memcpy (optr, iptr + 1, clen);
  328. optr += clen;
  329. iptr += clen + 1;
  330. }
  331. if (optr < (grub_uint8_t *) d + dlen)
  332. grub_memset (optr, 0, (grub_uint8_t *) d + dlen - optr);
  333. return GRUB_ERR_NONE;
  334. }
  335. static decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] = {
  336. {"inherit", NULL}, /* ZIO_COMPRESS_INHERIT */
  337. {"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */
  338. {"off", NULL}, /* ZIO_COMPRESS_OFF */
  339. {"lzjb", lzjb_decompress}, /* ZIO_COMPRESS_LZJB */
  340. {"empty", NULL}, /* ZIO_COMPRESS_EMPTY */
  341. {"gzip-1", zlib_decompress}, /* ZIO_COMPRESS_GZIP1 */
  342. {"gzip-2", zlib_decompress}, /* ZIO_COMPRESS_GZIP2 */
  343. {"gzip-3", zlib_decompress}, /* ZIO_COMPRESS_GZIP3 */
  344. {"gzip-4", zlib_decompress}, /* ZIO_COMPRESS_GZIP4 */
  345. {"gzip-5", zlib_decompress}, /* ZIO_COMPRESS_GZIP5 */
  346. {"gzip-6", zlib_decompress}, /* ZIO_COMPRESS_GZIP6 */
  347. {"gzip-7", zlib_decompress}, /* ZIO_COMPRESS_GZIP7 */
  348. {"gzip-8", zlib_decompress}, /* ZIO_COMPRESS_GZIP8 */
  349. {"gzip-9", zlib_decompress}, /* ZIO_COMPRESS_GZIP9 */
  350. {"zle", zle_decompress}, /* ZIO_COMPRESS_ZLE */
  351. {"lz4", lz4_decompress}, /* ZIO_COMPRESS_LZ4 */
  352. {"zstd", zstd_decompress}, /* ZIO_COMPRESS_ZSTD */
  353. };
  354. static grub_err_t zio_read_data (blkptr_t * bp, grub_zfs_endian_t endian,
  355. void *buf, struct grub_zfs_data *data);
  356. /*
  357. * Our own version of log2(). Same thing as highbit()-1.
  358. */
  359. static int
  360. zfs_log2 (grub_uint64_t num)
  361. {
  362. int i = 0;
  363. while (num > 1)
  364. {
  365. i++;
  366. num = num >> 1;
  367. }
  368. return i;
  369. }
  370. /* Checksum Functions */
  371. static void
  372. zio_checksum_off (const void *buf __attribute__ ((unused)),
  373. grub_uint64_t size __attribute__ ((unused)),
  374. grub_zfs_endian_t endian __attribute__ ((unused)),
  375. zio_cksum_t * zcp)
  376. {
  377. ZIO_SET_CHECKSUM (zcp, 0, 0, 0, 0);
  378. }
  379. /* Checksum Table and Values */
  380. static zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
  381. {NULL, 0, 0, "inherit"},
  382. {NULL, 0, 0, "on"},
  383. {zio_checksum_off, 0, 0, "off"},
  384. {zio_checksum_SHA256, 1, 1, "label"},
  385. {zio_checksum_SHA256, 1, 1, "gang_header"},
  386. {NULL, 0, 0, "zilog"},
  387. {fletcher_2, 0, 0, "fletcher2"},
  388. {fletcher_4, 1, 0, "fletcher4"},
  389. {zio_checksum_SHA256, 1, 0, "SHA256"},
  390. {NULL, 0, 0, "zilog2"},
  391. {zio_checksum_SHA256, 1, 0, "SHA256+MAC"},
  392. };
  393. /*
  394. * zio_checksum_verify: Provides support for checksum verification.
  395. *
  396. * Fletcher2, Fletcher4, and SHA256 are supported.
  397. *
  398. */
  399. static grub_err_t
  400. zio_checksum_verify (zio_cksum_t zc, grub_uint32_t checksum,
  401. grub_zfs_endian_t endian,
  402. char *buf, grub_size_t size)
  403. {
  404. zio_eck_t *zec = (zio_eck_t *) (buf + size) - 1;
  405. zio_checksum_info_t *ci = &zio_checksum_table[checksum];
  406. zio_cksum_t actual_cksum, expected_cksum;
  407. if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func == NULL)
  408. {
  409. grub_dprintf ("zfs", "unknown checksum function %d\n", checksum);
  410. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  411. "unknown checksum function %d", checksum);
  412. }
  413. if (ci->ci_eck)
  414. {
  415. expected_cksum = zec->zec_cksum;
  416. zec->zec_cksum = zc;
  417. ci->ci_func (buf, size, endian, &actual_cksum);
  418. zec->zec_cksum = expected_cksum;
  419. zc = expected_cksum;
  420. }
  421. else
  422. ci->ci_func (buf, size, endian, &actual_cksum);
  423. if (grub_memcmp (&actual_cksum, &zc,
  424. checksum != ZIO_CHECKSUM_SHA256_MAC ? 32 : 20) != 0)
  425. {
  426. grub_dprintf ("zfs", "checksum %s verification failed\n", ci->ci_name);
  427. grub_dprintf ("zfs", "actual checksum %016llx %016llx %016llx %016llx\n",
  428. (unsigned long long) actual_cksum.zc_word[0],
  429. (unsigned long long) actual_cksum.zc_word[1],
  430. (unsigned long long) actual_cksum.zc_word[2],
  431. (unsigned long long) actual_cksum.zc_word[3]);
  432. grub_dprintf ("zfs", "expected checksum %016llx %016llx %016llx %016llx\n",
  433. (unsigned long long) zc.zc_word[0],
  434. (unsigned long long) zc.zc_word[1],
  435. (unsigned long long) zc.zc_word[2],
  436. (unsigned long long) zc.zc_word[3]);
  437. return grub_error (GRUB_ERR_BAD_FS, N_("checksum verification failed"));
  438. }
  439. return GRUB_ERR_NONE;
  440. }
  441. /*
  442. * vdev_uberblock_compare takes two uberblock structures and returns an integer
  443. * indicating the more recent of the two.
  444. * Return Value = 1 if ub2 is more recent
  445. * Return Value = -1 if ub1 is more recent
  446. * The most recent uberblock is determined using its transaction number and
  447. * timestamp. The uberblock with the highest transaction number is
  448. * considered "newer". If the transaction numbers of the two blocks match, the
  449. * timestamps are compared to determine the "newer" of the two.
  450. */
  451. static int
  452. vdev_uberblock_compare (uberblock_t * ub1, uberblock_t * ub2)
  453. {
  454. grub_zfs_endian_t ub1_endian, ub2_endian;
  455. if (grub_zfs_to_cpu64 (ub1->ub_magic, GRUB_ZFS_LITTLE_ENDIAN)
  456. == UBERBLOCK_MAGIC)
  457. ub1_endian = GRUB_ZFS_LITTLE_ENDIAN;
  458. else
  459. ub1_endian = GRUB_ZFS_BIG_ENDIAN;
  460. if (grub_zfs_to_cpu64 (ub2->ub_magic, GRUB_ZFS_LITTLE_ENDIAN)
  461. == UBERBLOCK_MAGIC)
  462. ub2_endian = GRUB_ZFS_LITTLE_ENDIAN;
  463. else
  464. ub2_endian = GRUB_ZFS_BIG_ENDIAN;
  465. if (grub_zfs_to_cpu64 (ub1->ub_txg, ub1_endian)
  466. < grub_zfs_to_cpu64 (ub2->ub_txg, ub2_endian))
  467. return -1;
  468. if (grub_zfs_to_cpu64 (ub1->ub_txg, ub1_endian)
  469. > grub_zfs_to_cpu64 (ub2->ub_txg, ub2_endian))
  470. return 1;
  471. if (grub_zfs_to_cpu64 (ub1->ub_timestamp, ub1_endian)
  472. < grub_zfs_to_cpu64 (ub2->ub_timestamp, ub2_endian))
  473. return -1;
  474. if (grub_zfs_to_cpu64 (ub1->ub_timestamp, ub1_endian)
  475. > grub_zfs_to_cpu64 (ub2->ub_timestamp, ub2_endian))
  476. return 1;
  477. return 0;
  478. }
  479. /*
  480. * Three pieces of information are needed to verify an uberblock: the magic
  481. * number, the version number, and the checksum.
  482. *
  483. * Currently Implemented: version number, magic number, checksum
  484. *
  485. */
  486. static grub_err_t
  487. uberblock_verify (uberblock_phys_t * ub, grub_uint64_t offset,
  488. grub_size_t s)
  489. {
  490. uberblock_t *uber = &ub->ubp_uberblock;
  491. grub_err_t err;
  492. grub_zfs_endian_t endian = GRUB_ZFS_UNKNOWN_ENDIAN;
  493. zio_cksum_t zc;
  494. if (grub_zfs_to_cpu64 (uber->ub_magic, GRUB_ZFS_LITTLE_ENDIAN)
  495. == UBERBLOCK_MAGIC
  496. && SPA_VERSION_IS_SUPPORTED(grub_zfs_to_cpu64 (uber->ub_version, GRUB_ZFS_LITTLE_ENDIAN)))
  497. endian = GRUB_ZFS_LITTLE_ENDIAN;
  498. if (grub_zfs_to_cpu64 (uber->ub_magic, GRUB_ZFS_BIG_ENDIAN) == UBERBLOCK_MAGIC
  499. && SPA_VERSION_IS_SUPPORTED(grub_zfs_to_cpu64 (uber->ub_version, GRUB_ZFS_BIG_ENDIAN)))
  500. endian = GRUB_ZFS_BIG_ENDIAN;
  501. if (endian == GRUB_ZFS_UNKNOWN_ENDIAN)
  502. return grub_error (GRUB_ERR_BAD_FS, "invalid uberblock magic");
  503. grub_memset (&zc, 0, sizeof (zc));
  504. zc.zc_word[0] = grub_cpu_to_zfs64 (offset, endian);
  505. err = zio_checksum_verify (zc, ZIO_CHECKSUM_LABEL, endian,
  506. (char *) ub, s);
  507. return err;
  508. }
  509. /*
  510. * Find the best uberblock.
  511. * Return:
  512. * Success - Pointer to the best uberblock.
  513. * Failure - NULL
  514. */
  515. static uberblock_phys_t *
  516. find_bestub (uberblock_phys_t * ub_array,
  517. const struct grub_zfs_device_desc *desc)
  518. {
  519. uberblock_phys_t *ubbest = NULL, *ubptr;
  520. int i;
  521. grub_disk_addr_t offset;
  522. grub_err_t err = GRUB_ERR_NONE;
  523. int ub_shift;
  524. ub_shift = desc->ashift;
  525. if (ub_shift < VDEV_UBERBLOCK_SHIFT)
  526. ub_shift = VDEV_UBERBLOCK_SHIFT;
  527. for (i = 0; i < (VDEV_UBERBLOCK_RING >> ub_shift); i++)
  528. {
  529. offset = (desc->vdev_phys_sector << SPA_MINBLOCKSHIFT) + VDEV_PHYS_SIZE
  530. + (i << ub_shift);
  531. ubptr = (uberblock_phys_t *) ((grub_properly_aligned_t *) ub_array
  532. + ((i << ub_shift)
  533. / sizeof (grub_properly_aligned_t)));
  534. err = uberblock_verify (ubptr, offset, (grub_size_t) 1 << ub_shift);
  535. if (err)
  536. {
  537. grub_errno = GRUB_ERR_NONE;
  538. continue;
  539. }
  540. if (ubbest == NULL
  541. || vdev_uberblock_compare (&(ubptr->ubp_uberblock),
  542. &(ubbest->ubp_uberblock)) > 0)
  543. ubbest = ubptr;
  544. }
  545. if (!ubbest)
  546. grub_errno = err;
  547. return ubbest;
  548. }
  549. static inline grub_size_t
  550. get_psize (blkptr_t * bp, grub_zfs_endian_t endian)
  551. {
  552. return ((((grub_zfs_to_cpu64 ((bp)->blk_prop, endian) >> 16) & 0xffff) + 1)
  553. << SPA_MINBLOCKSHIFT);
  554. }
  555. static grub_uint64_t
  556. dva_get_offset (const dva_t *dva, grub_zfs_endian_t endian)
  557. {
  558. grub_dprintf ("zfs", "dva=%llx, %llx\n",
  559. (unsigned long long) dva->dva_word[0],
  560. (unsigned long long) dva->dva_word[1]);
  561. return grub_zfs_to_cpu64 ((dva)->dva_word[1],
  562. endian) << SPA_MINBLOCKSHIFT;
  563. }
  564. static grub_err_t
  565. zfs_fetch_nvlist (struct grub_zfs_device_desc *diskdesc, char **nvlist)
  566. {
  567. grub_err_t err;
  568. *nvlist = 0;
  569. if (!diskdesc->dev)
  570. return grub_error (GRUB_ERR_BUG, "member drive unknown");
  571. *nvlist = grub_malloc (VDEV_PHYS_SIZE);
  572. /* Read in the vdev name-value pair list (112K). */
  573. err = grub_disk_read (diskdesc->dev->disk, diskdesc->vdev_phys_sector, 0,
  574. VDEV_PHYS_SIZE, *nvlist);
  575. if (err)
  576. {
  577. grub_free (*nvlist);
  578. *nvlist = 0;
  579. return err;
  580. }
  581. return GRUB_ERR_NONE;
  582. }
  583. static grub_err_t
  584. fill_vdev_info_real (struct grub_zfs_data *data,
  585. const char *nvlist,
  586. struct grub_zfs_device_desc *fill,
  587. struct grub_zfs_device_desc *insert,
  588. int *inserted,
  589. unsigned ashift)
  590. {
  591. char *type;
  592. type = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_TYPE);
  593. if (!type)
  594. return grub_errno;
  595. if (!grub_zfs_nvlist_lookup_uint64 (nvlist, "id", &(fill->id)))
  596. {
  597. grub_free (type);
  598. return grub_error (GRUB_ERR_BAD_FS, "couldn't find vdev id");
  599. }
  600. if (!grub_zfs_nvlist_lookup_uint64 (nvlist, "guid", &(fill->guid)))
  601. {
  602. grub_free (type);
  603. return grub_error (GRUB_ERR_BAD_FS, "couldn't find vdev id");
  604. }
  605. {
  606. grub_uint64_t par;
  607. if (grub_zfs_nvlist_lookup_uint64 (nvlist, "ashift", &par))
  608. fill->ashift = par;
  609. else if (ashift != 0xffffffff)
  610. fill->ashift = ashift;
  611. else
  612. {
  613. grub_free (type);
  614. return grub_error (GRUB_ERR_BAD_FS, "couldn't find ashift");
  615. }
  616. }
  617. fill->max_children_ashift = 0;
  618. if (grub_strcmp (type, VDEV_TYPE_DISK) == 0
  619. || grub_strcmp (type, VDEV_TYPE_FILE) == 0)
  620. {
  621. fill->type = DEVICE_LEAF;
  622. if (!fill->dev && fill->guid == insert->guid)
  623. {
  624. fill->dev = insert->dev;
  625. fill->vdev_phys_sector = insert->vdev_phys_sector;
  626. fill->current_uberblock = insert->current_uberblock;
  627. fill->original = insert->original;
  628. if (!data->device_original)
  629. data->device_original = fill;
  630. insert->ashift = fill->ashift;
  631. *inserted = 1;
  632. }
  633. grub_free (type);
  634. return GRUB_ERR_NONE;
  635. }
  636. if (grub_strcmp (type, VDEV_TYPE_MIRROR) == 0
  637. || grub_strcmp (type, VDEV_TYPE_RAIDZ) == 0)
  638. {
  639. int nelm, i;
  640. if (grub_strcmp (type, VDEV_TYPE_MIRROR) == 0)
  641. fill->type = DEVICE_MIRROR;
  642. else
  643. {
  644. grub_uint64_t par;
  645. fill->type = DEVICE_RAIDZ;
  646. if (!grub_zfs_nvlist_lookup_uint64 (nvlist, "nparity", &par))
  647. {
  648. grub_free (type);
  649. return grub_error (GRUB_ERR_BAD_FS, "couldn't find raidz parity");
  650. }
  651. fill->nparity = par;
  652. }
  653. nelm = grub_zfs_nvlist_lookup_nvlist_array_get_nelm (nvlist,
  654. ZPOOL_CONFIG_CHILDREN);
  655. if (nelm <= 0)
  656. {
  657. grub_free (type);
  658. return grub_error (GRUB_ERR_BAD_FS, "incorrect mirror VDEV");
  659. }
  660. if (!fill->children)
  661. {
  662. fill->n_children = nelm;
  663. fill->children = grub_zalloc (fill->n_children
  664. * sizeof (fill->children[0]));
  665. }
  666. for (i = 0; i < nelm; i++)
  667. {
  668. char *child;
  669. grub_err_t err;
  670. child = grub_zfs_nvlist_lookup_nvlist_array
  671. (nvlist, ZPOOL_CONFIG_CHILDREN, i);
  672. err = fill_vdev_info_real (data, child, &fill->children[i], insert,
  673. inserted, fill->ashift);
  674. grub_free (child);
  675. if (err)
  676. {
  677. grub_free (type);
  678. return err;
  679. }
  680. if (fill->children[i].ashift > fill->max_children_ashift)
  681. fill->max_children_ashift = fill->children[i].ashift;
  682. }
  683. grub_free (type);
  684. return GRUB_ERR_NONE;
  685. }
  686. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "vdev %s isn't supported", type);
  687. grub_free (type);
  688. return grub_errno;
  689. }
  690. static grub_err_t
  691. fill_vdev_info (struct grub_zfs_data *data,
  692. char *nvlist, struct grub_zfs_device_desc *diskdesc,
  693. int *inserted)
  694. {
  695. grub_uint64_t id;
  696. unsigned i;
  697. *inserted = 0;
  698. if (!grub_zfs_nvlist_lookup_uint64 (nvlist, "id", &id))
  699. return grub_error (GRUB_ERR_BAD_FS, "couldn't find vdev id");
  700. for (i = 0; i < data->n_devices_attached; i++)
  701. if (data->devices_attached[i].id == id)
  702. return fill_vdev_info_real (data, nvlist, &data->devices_attached[i],
  703. diskdesc, inserted, 0xffffffff);
  704. data->n_devices_attached++;
  705. if (data->n_devices_attached > data->n_devices_allocated)
  706. {
  707. void *tmp;
  708. grub_size_t sz;
  709. if (grub_mul (data->n_devices_attached, 2, &data->n_devices_allocated) ||
  710. grub_add (data->n_devices_allocated, 1, &data->n_devices_allocated) ||
  711. grub_mul (data->n_devices_allocated, sizeof (data->devices_attached[0]), &sz))
  712. return GRUB_ERR_OUT_OF_RANGE;
  713. data->devices_attached = grub_realloc (tmp = data->devices_attached, sz);
  714. if (!data->devices_attached)
  715. {
  716. data->devices_attached = tmp;
  717. return grub_errno;
  718. }
  719. }
  720. grub_memset (&data->devices_attached[data->n_devices_attached - 1],
  721. 0, sizeof (data->devices_attached[data->n_devices_attached - 1]));
  722. return fill_vdev_info_real (data, nvlist,
  723. &data->devices_attached[data->n_devices_attached - 1],
  724. diskdesc, inserted, 0xffffffff);
  725. }
  726. /*
  727. * For a given XDR packed nvlist, verify the first 4 bytes and move on.
  728. *
  729. * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
  730. *
  731. * encoding method/host endian (4 bytes)
  732. * nvl_version (4 bytes)
  733. * nvl_nvflag (4 bytes)
  734. * encoded nvpairs:
  735. * encoded size of the nvpair (4 bytes)
  736. * decoded size of the nvpair (4 bytes)
  737. * name string size (4 bytes)
  738. * name string data (sizeof(NV_ALIGN4(string))
  739. * data type (4 bytes)
  740. * # of elements in the nvpair (4 bytes)
  741. * data
  742. * 2 zero's for the last nvpair
  743. * (end of the entire list) (8 bytes)
  744. *
  745. */
  746. /*
  747. * The nvlist_next_nvpair() function returns a handle to the next nvpair in the
  748. * list following nvpair. If nvpair is NULL, the first pair is returned. If
  749. * nvpair is the last pair in the nvlist, NULL is returned.
  750. */
  751. static const char *
  752. nvlist_next_nvpair (const char *nvl, const char *nvpair)
  753. {
  754. const char *nvp;
  755. int encode_size;
  756. int name_len;
  757. if (nvl == NULL)
  758. return NULL;
  759. if (nvpair == NULL)
  760. {
  761. /* skip over header, nvl_version and nvl_nvflag */
  762. nvpair = nvl + 4 * 3;
  763. }
  764. else
  765. {
  766. /* skip to the next nvpair */
  767. encode_size = grub_be_to_cpu32 (grub_get_unaligned32(nvpair));
  768. nvpair += encode_size;
  769. /*If encode_size equals 0 nvlist_next_nvpair would return
  770. * the same pair received in input, leading to an infinite loop.
  771. * If encode_size is less than 0, this will move the pointer
  772. * backwards, *possibly* examinining two times the same nvpair
  773. * and potentially getting into an infinite loop. */
  774. if(encode_size <= 0)
  775. {
  776. grub_dprintf ("zfs", "nvpair with size <= 0\n");
  777. grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist");
  778. return NULL;
  779. }
  780. }
  781. /* 8 bytes of 0 marks the end of the list */
  782. if (grub_get_unaligned64 (nvpair) == 0)
  783. return NULL;
  784. /*consistency checks*/
  785. if (nvpair + 4 * 3 >= nvl + VDEV_PHYS_SIZE)
  786. {
  787. grub_dprintf ("zfs", "nvlist overflow\n");
  788. grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist");
  789. return NULL;
  790. }
  791. encode_size = grub_be_to_cpu32 (grub_get_unaligned32(nvpair));
  792. nvp = nvpair + 4*2;
  793. name_len = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
  794. nvp += 4;
  795. nvp = nvp + ((name_len + 3) & ~3); // align
  796. if (nvp + 4 >= nvl + VDEV_PHYS_SIZE
  797. || encode_size < 0
  798. || nvp + 4 + encode_size > nvl + VDEV_PHYS_SIZE)
  799. {
  800. grub_dprintf ("zfs", "nvlist overflow\n");
  801. grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist");
  802. return NULL;
  803. }
  804. /* end consistency checks */
  805. return nvpair;
  806. }
  807. /*
  808. * This function returns 0 on success and 1 on failure. On success, a string
  809. * containing the name of nvpair is saved in buf.
  810. */
  811. static int
  812. nvpair_name (const char *nvp, char **buf, grub_size_t *buflen)
  813. {
  814. /* skip over encode/decode size */
  815. nvp += 4 * 2;
  816. *buf = (char *) (nvp + 4);
  817. *buflen = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
  818. return 0;
  819. }
  820. /*
  821. * This function retrieves the value of the nvpair in the form of enumerated
  822. * type data_type_t.
  823. */
  824. static int
  825. nvpair_type (const char *nvp)
  826. {
  827. int name_len, type;
  828. /* skip over encode/decode size */
  829. nvp += 4 * 2;
  830. /* skip over name_len */
  831. name_len = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
  832. nvp += 4;
  833. /* skip over name */
  834. nvp = nvp + ((name_len + 3) & ~3); /* align */
  835. type = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
  836. return type;
  837. }
  838. static int
  839. nvpair_value (const char *nvp,char **val,
  840. grub_size_t *size_out, grub_size_t *nelm_out)
  841. {
  842. int name_len,nelm,encode_size;
  843. /* skip over encode/decode size */
  844. encode_size = grub_be_to_cpu32 (grub_get_unaligned32(nvp));
  845. nvp += 8;
  846. /* skip over name_len */
  847. name_len = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
  848. nvp += 4;
  849. /* skip over name */
  850. nvp = nvp + ((name_len + 3) & ~3); /* align */
  851. /* skip over type */
  852. nvp += 4;
  853. nelm = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
  854. nvp +=4;
  855. if (nelm < 1)
  856. {
  857. grub_error (GRUB_ERR_BAD_FS, "empty nvpair");
  858. return 0;
  859. }
  860. *val = (char *) nvp;
  861. *size_out = encode_size;
  862. if (nelm_out)
  863. *nelm_out = nelm;
  864. return 1;
  865. }
  866. /*
  867. * Check the disk label information and retrieve needed vdev name-value pairs.
  868. *
  869. */
  870. static grub_err_t
  871. check_pool_label (struct grub_zfs_data *data,
  872. struct grub_zfs_device_desc *diskdesc,
  873. int *inserted, int original)
  874. {
  875. grub_uint64_t pool_state, txg = 0;
  876. char *nvlist,*features;
  877. #if 0
  878. char *nv;
  879. #endif
  880. grub_uint64_t poolguid;
  881. grub_uint64_t version;
  882. int found;
  883. grub_err_t err;
  884. grub_zfs_endian_t endian;
  885. vdev_phys_t *phys;
  886. zio_cksum_t emptycksum;
  887. *inserted = 0;
  888. err = zfs_fetch_nvlist (diskdesc, &nvlist);
  889. if (err)
  890. return err;
  891. phys = (vdev_phys_t*) nvlist;
  892. if (grub_zfs_to_cpu64 (phys->vp_zbt.zec_magic,
  893. GRUB_ZFS_LITTLE_ENDIAN)
  894. == ZEC_MAGIC)
  895. endian = GRUB_ZFS_LITTLE_ENDIAN;
  896. else if (grub_zfs_to_cpu64 (phys->vp_zbt.zec_magic,
  897. GRUB_ZFS_BIG_ENDIAN)
  898. == ZEC_MAGIC)
  899. endian = GRUB_ZFS_BIG_ENDIAN;
  900. else
  901. {
  902. grub_free (nvlist);
  903. return grub_error (GRUB_ERR_BAD_FS,
  904. "bad vdev_phys_t.vp_zbt.zec_magic number");
  905. }
  906. /* Now check the integrity of the vdev_phys_t structure though checksum. */
  907. ZIO_SET_CHECKSUM(&emptycksum, diskdesc->vdev_phys_sector << 9, 0, 0, 0);
  908. err = zio_checksum_verify (emptycksum, ZIO_CHECKSUM_LABEL, endian,
  909. nvlist, VDEV_PHYS_SIZE);
  910. if (err)
  911. return err;
  912. grub_dprintf ("zfs", "check 2 passed\n");
  913. found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_STATE,
  914. &pool_state);
  915. if (! found)
  916. {
  917. grub_free (nvlist);
  918. if (! grub_errno)
  919. grub_error (GRUB_ERR_BAD_FS, ZPOOL_CONFIG_POOL_STATE " not found");
  920. return grub_errno;
  921. }
  922. grub_dprintf ("zfs", "check 3 passed\n");
  923. if (pool_state == POOL_STATE_DESTROYED)
  924. {
  925. grub_free (nvlist);
  926. return grub_error (GRUB_ERR_BAD_FS, "zpool is marked as destroyed");
  927. }
  928. grub_dprintf ("zfs", "check 4 passed\n");
  929. found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_TXG, &txg);
  930. if (!found)
  931. {
  932. grub_free (nvlist);
  933. if (! grub_errno)
  934. grub_error (GRUB_ERR_BAD_FS, ZPOOL_CONFIG_POOL_TXG " not found");
  935. return grub_errno;
  936. }
  937. grub_dprintf ("zfs", "check 6 passed\n");
  938. /* not an active device */
  939. if (txg == 0)
  940. {
  941. grub_free (nvlist);
  942. return grub_error (GRUB_ERR_BAD_FS, "zpool isn't active");
  943. }
  944. grub_dprintf ("zfs", "check 7 passed\n");
  945. found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_VERSION,
  946. &version);
  947. if (! found)
  948. {
  949. grub_free (nvlist);
  950. if (! grub_errno)
  951. grub_error (GRUB_ERR_BAD_FS, ZPOOL_CONFIG_VERSION " not found");
  952. return grub_errno;
  953. }
  954. grub_dprintf ("zfs", "check 8 passed\n");
  955. if (!SPA_VERSION_IS_SUPPORTED(version))
  956. {
  957. grub_free (nvlist);
  958. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  959. "too new version %llu > %llu",
  960. (unsigned long long) version,
  961. (unsigned long long) SPA_VERSION_BEFORE_FEATURES);
  962. }
  963. grub_dprintf ("zfs", "check 9 passed\n");
  964. found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_GUID,
  965. &(diskdesc->guid));
  966. if (! found)
  967. {
  968. grub_free (nvlist);
  969. if (! grub_errno)
  970. grub_error (GRUB_ERR_BAD_FS, ZPOOL_CONFIG_GUID " not found");
  971. return grub_errno;
  972. }
  973. found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_GUID,
  974. &poolguid);
  975. if (! found)
  976. {
  977. grub_free (nvlist);
  978. if (! grub_errno)
  979. grub_error (GRUB_ERR_BAD_FS, ZPOOL_CONFIG_POOL_GUID " not found");
  980. return grub_errno;
  981. }
  982. grub_dprintf ("zfs", "check 11 passed\n");
  983. if (original)
  984. data->guid = poolguid;
  985. if (data->guid != poolguid)
  986. return grub_error (GRUB_ERR_BAD_FS, "another zpool");
  987. {
  988. char *nv;
  989. nv = grub_zfs_nvlist_lookup_nvlist (nvlist, ZPOOL_CONFIG_VDEV_TREE);
  990. if (!nv)
  991. {
  992. grub_free (nvlist);
  993. return grub_error (GRUB_ERR_BAD_FS, "couldn't find vdev tree");
  994. }
  995. err = fill_vdev_info (data, nv, diskdesc, inserted);
  996. if (err)
  997. {
  998. grub_free (nv);
  999. grub_free (nvlist);
  1000. return err;
  1001. }
  1002. grub_free (nv);
  1003. }
  1004. grub_dprintf ("zfs", "check 10 passed\n");
  1005. features = grub_zfs_nvlist_lookup_nvlist(nvlist,
  1006. ZPOOL_CONFIG_FEATURES_FOR_READ);
  1007. if (features)
  1008. {
  1009. const char *nvp=NULL;
  1010. char name[MAX_SUPPORTED_FEATURE_STRLEN + 1];
  1011. char *nameptr;
  1012. grub_size_t namelen;
  1013. while ((nvp = nvlist_next_nvpair(features, nvp)) != NULL)
  1014. {
  1015. nvpair_name (nvp, &nameptr, &namelen);
  1016. if(namelen > MAX_SUPPORTED_FEATURE_STRLEN)
  1017. namelen = MAX_SUPPORTED_FEATURE_STRLEN;
  1018. grub_memcpy (name, nameptr, namelen);
  1019. name[namelen] = '\0';
  1020. grub_dprintf("zfs","str=%s\n",name);
  1021. if (check_feature(name,1, NULL) != 0)
  1022. {
  1023. grub_dprintf("zfs","feature missing in check_pool_label:%s\n",name);
  1024. err= grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET," check_pool_label missing feature '%s' for read",name);
  1025. return err;
  1026. }
  1027. }
  1028. }
  1029. grub_dprintf ("zfs", "check 12 passed (feature flags)\n");
  1030. grub_free (nvlist);
  1031. return GRUB_ERR_NONE;
  1032. }
  1033. static grub_err_t
  1034. scan_disk (grub_device_t dev, struct grub_zfs_data *data,
  1035. int original, int *inserted)
  1036. {
  1037. int label = 0;
  1038. uberblock_phys_t *ub_array, *ubbest = NULL;
  1039. vdev_boot_header_t *bh;
  1040. grub_err_t err;
  1041. int vdevnum;
  1042. struct grub_zfs_device_desc desc;
  1043. ub_array = grub_malloc (VDEV_UBERBLOCK_RING);
  1044. if (!ub_array)
  1045. return grub_errno;
  1046. bh = grub_malloc (VDEV_BOOT_HEADER_SIZE);
  1047. if (!bh)
  1048. {
  1049. grub_free (ub_array);
  1050. return grub_errno;
  1051. }
  1052. vdevnum = VDEV_LABELS;
  1053. desc.dev = dev;
  1054. desc.original = original;
  1055. /* Don't check back labels on CDROM. */
  1056. if (grub_disk_native_sectors (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
  1057. vdevnum = VDEV_LABELS / 2;
  1058. for (label = 0; ubbest == NULL && label < vdevnum; label++)
  1059. {
  1060. desc.vdev_phys_sector
  1061. = label * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT)
  1062. + ((VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE) >> SPA_MINBLOCKSHIFT)
  1063. + (label < VDEV_LABELS / 2 ? 0 :
  1064. ALIGN_DOWN (grub_disk_native_sectors (dev->disk), sizeof (vdev_label_t))
  1065. - VDEV_LABELS * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT));
  1066. /* Read in the uberblock ring (128K). */
  1067. err = grub_disk_read (dev->disk, desc.vdev_phys_sector
  1068. + (VDEV_PHYS_SIZE >> SPA_MINBLOCKSHIFT),
  1069. 0, VDEV_UBERBLOCK_RING, (char *) ub_array);
  1070. if (err)
  1071. {
  1072. grub_errno = GRUB_ERR_NONE;
  1073. continue;
  1074. }
  1075. grub_dprintf ("zfs", "label ok %d\n", label);
  1076. err = check_pool_label (data, &desc, inserted, original);
  1077. if (err || !*inserted)
  1078. {
  1079. grub_errno = GRUB_ERR_NONE;
  1080. continue;
  1081. }
  1082. ubbest = find_bestub (ub_array, &desc);
  1083. if (!ubbest)
  1084. {
  1085. grub_dprintf ("zfs", "No uberblock found\n");
  1086. grub_errno = GRUB_ERR_NONE;
  1087. continue;
  1088. }
  1089. grub_memmove (&(desc.current_uberblock),
  1090. &ubbest->ubp_uberblock, sizeof (uberblock_t));
  1091. if (original)
  1092. grub_memmove (&(data->current_uberblock),
  1093. &ubbest->ubp_uberblock, sizeof (uberblock_t));
  1094. #if 0
  1095. if (find_best_root &&
  1096. vdev_uberblock_compare (&ubbest->ubp_uberblock,
  1097. &(current_uberblock)) <= 0)
  1098. continue;
  1099. #endif
  1100. grub_free (ub_array);
  1101. grub_free (bh);
  1102. return GRUB_ERR_NONE;
  1103. }
  1104. grub_free (ub_array);
  1105. grub_free (bh);
  1106. return grub_error (GRUB_ERR_BAD_FS, "couldn't find a valid label");
  1107. }
  1108. /* Helper for scan_devices. */
  1109. static int
  1110. scan_devices_iter (const char *name, void *hook_data)
  1111. {
  1112. struct grub_zfs_data *data = hook_data;
  1113. grub_device_t dev;
  1114. grub_err_t err;
  1115. int inserted;
  1116. dev = grub_device_open (name);
  1117. if (!dev)
  1118. return 0;
  1119. if (!dev->disk)
  1120. {
  1121. grub_device_close (dev);
  1122. return 0;
  1123. }
  1124. err = scan_disk (dev, data, 0, &inserted);
  1125. if (err == GRUB_ERR_BAD_FS)
  1126. {
  1127. grub_device_close (dev);
  1128. grub_errno = GRUB_ERR_NONE;
  1129. return 0;
  1130. }
  1131. if (err)
  1132. {
  1133. grub_device_close (dev);
  1134. grub_print_error ();
  1135. return 0;
  1136. }
  1137. if (!inserted)
  1138. grub_device_close (dev);
  1139. return 0;
  1140. }
  1141. static grub_err_t
  1142. scan_devices (struct grub_zfs_data *data)
  1143. {
  1144. grub_device_iterate (scan_devices_iter, data);
  1145. return GRUB_ERR_NONE;
  1146. }
  1147. /* x**y. */
  1148. static grub_uint8_t powx[255 * 2];
  1149. /* Such an s that x**s = y */
  1150. static int powx_inv[256];
  1151. static const grub_uint8_t poly = 0x1d;
  1152. /* perform the operation a ^= b * (x ** (known_idx * recovery_pow) ) */
  1153. static inline void
  1154. xor_out (grub_uint8_t *a, const grub_uint8_t *b, grub_size_t s,
  1155. unsigned known_idx, unsigned recovery_pow)
  1156. {
  1157. unsigned add;
  1158. /* Simple xor. */
  1159. if (known_idx == 0 || recovery_pow == 0)
  1160. {
  1161. grub_crypto_xor (a, a, b, s);
  1162. return;
  1163. }
  1164. add = (known_idx * recovery_pow) % 255;
  1165. for (;s--; b++, a++)
  1166. if (*b)
  1167. *a ^= powx[powx_inv[*b] + add];
  1168. }
  1169. static inline grub_uint8_t
  1170. gf_mul (grub_uint8_t a, grub_uint8_t b)
  1171. {
  1172. if (a == 0 || b == 0)
  1173. return 0;
  1174. return powx[powx_inv[a] + powx_inv[b]];
  1175. }
  1176. #define MAX_NBUFS 4
  1177. static grub_err_t
  1178. recovery (grub_uint8_t *bufs[4], grub_size_t s, const int nbufs,
  1179. const unsigned *powers,
  1180. const unsigned *idx)
  1181. {
  1182. grub_dprintf ("zfs", "recovering %u buffers\n", nbufs);
  1183. /* Now we have */
  1184. /* b_i = sum (r_j* (x ** (powers[i] * idx[j])))*/
  1185. /* Let's invert the matrix in question. */
  1186. switch (nbufs)
  1187. {
  1188. /* Easy: r_0 = bufs[0] / (x << (powers[i] * idx[j])). */
  1189. case 1:
  1190. {
  1191. int add;
  1192. grub_uint8_t *a;
  1193. if (powers[0] == 0 || idx[0] == 0)
  1194. return GRUB_ERR_NONE;
  1195. add = 255 - ((powers[0] * idx[0]) % 255);
  1196. for (a = bufs[0]; s--; a++)
  1197. if (*a)
  1198. *a = powx[powx_inv[*a] + add];
  1199. return GRUB_ERR_NONE;
  1200. }
  1201. /* Case 2x2: Let's use the determinant formula. */
  1202. case 2:
  1203. {
  1204. grub_uint8_t det, det_inv;
  1205. grub_uint8_t matrixinv[2][2];
  1206. unsigned i;
  1207. /* The determinant is: */
  1208. det = (powx[(powers[0] * idx[0] + powers[1] * idx[1]) % 255]
  1209. ^ powx[(powers[0] * idx[1] + powers[1] * idx[0]) % 255]);
  1210. if (det == 0)
  1211. return grub_error (GRUB_ERR_BAD_FS, "singular recovery matrix");
  1212. det_inv = powx[255 - powx_inv[det]];
  1213. matrixinv[0][0] = gf_mul (powx[(powers[1] * idx[1]) % 255], det_inv);
  1214. matrixinv[1][1] = gf_mul (powx[(powers[0] * idx[0]) % 255], det_inv);
  1215. matrixinv[0][1] = gf_mul (powx[(powers[0] * idx[1]) % 255], det_inv);
  1216. matrixinv[1][0] = gf_mul (powx[(powers[1] * idx[0]) % 255], det_inv);
  1217. for (i = 0; i < s; i++)
  1218. {
  1219. grub_uint8_t b0, b1;
  1220. b0 = bufs[0][i];
  1221. b1 = bufs[1][i];
  1222. bufs[0][i] = (gf_mul (b0, matrixinv[0][0])
  1223. ^ gf_mul (b1, matrixinv[0][1]));
  1224. bufs[1][i] = (gf_mul (b0, matrixinv[1][0])
  1225. ^ gf_mul (b1, matrixinv[1][1]));
  1226. }
  1227. return GRUB_ERR_NONE;
  1228. }
  1229. /* Otherwise use Gauss. */
  1230. case 3:
  1231. {
  1232. grub_uint8_t matrix1[MAX_NBUFS][MAX_NBUFS], matrix2[MAX_NBUFS][MAX_NBUFS];
  1233. int i, j, k;
  1234. for (i = 0; i < nbufs; i++)
  1235. for (j = 0; j < nbufs; j++)
  1236. matrix1[i][j] = powx[(powers[i] * idx[j]) % 255];
  1237. for (i = 0; i < nbufs; i++)
  1238. for (j = 0; j < nbufs; j++)
  1239. matrix2[i][j] = 0;
  1240. for (i = 0; i < nbufs; i++)
  1241. matrix2[i][i] = 1;
  1242. for (i = 0; i < nbufs; i++)
  1243. {
  1244. grub_uint8_t mul;
  1245. for (j = i; j < nbufs; j++)
  1246. if (matrix1[i][j])
  1247. break;
  1248. if (j == nbufs)
  1249. return grub_error (GRUB_ERR_BAD_FS, "singular recovery matrix");
  1250. if (j != i)
  1251. {
  1252. int xchng;
  1253. xchng = j;
  1254. for (j = 0; j < nbufs; j++)
  1255. {
  1256. grub_uint8_t t;
  1257. t = matrix1[xchng][j];
  1258. matrix1[xchng][j] = matrix1[i][j];
  1259. matrix1[i][j] = t;
  1260. }
  1261. for (j = 0; j < nbufs; j++)
  1262. {
  1263. grub_uint8_t t;
  1264. t = matrix2[xchng][j];
  1265. matrix2[xchng][j] = matrix2[i][j];
  1266. matrix2[i][j] = t;
  1267. }
  1268. }
  1269. mul = powx[255 - powx_inv[matrix1[i][i]]];
  1270. for (j = 0; j < nbufs; j++)
  1271. matrix1[i][j] = gf_mul (matrix1[i][j], mul);
  1272. for (j = 0; j < nbufs; j++)
  1273. matrix2[i][j] = gf_mul (matrix2[i][j], mul);
  1274. for (j = i + 1; j < nbufs; j++)
  1275. {
  1276. mul = matrix1[j][i];
  1277. for (k = 0; k < nbufs; k++)
  1278. matrix1[j][k] ^= gf_mul (matrix1[i][k], mul);
  1279. for (k = 0; k < nbufs; k++)
  1280. matrix2[j][k] ^= gf_mul (matrix2[i][k], mul);
  1281. }
  1282. }
  1283. for (i = nbufs - 1; i >= 0; i--)
  1284. {
  1285. for (j = 0; j < i; j++)
  1286. {
  1287. grub_uint8_t mul;
  1288. mul = matrix1[j][i];
  1289. for (k = 0; k < nbufs; k++)
  1290. matrix1[j][k] ^= gf_mul (matrix1[i][k], mul);
  1291. for (k = 0; k < nbufs; k++)
  1292. matrix2[j][k] ^= gf_mul (matrix2[i][k], mul);
  1293. }
  1294. }
  1295. for (i = 0; i < (int) s; i++)
  1296. {
  1297. grub_uint8_t b[MAX_NBUFS];
  1298. for (j = 0; j < nbufs; j++)
  1299. b[j] = bufs[j][i];
  1300. for (j = 0; j < nbufs; j++)
  1301. {
  1302. bufs[j][i] = 0;
  1303. for (k = 0; k < nbufs; k++)
  1304. bufs[j][i] ^= gf_mul (matrix2[j][k], b[k]);
  1305. }
  1306. }
  1307. return GRUB_ERR_NONE;
  1308. }
  1309. default:
  1310. return grub_error (GRUB_ERR_BUG, "too big matrix");
  1311. }
  1312. }
  1313. static grub_err_t
  1314. read_device (grub_uint64_t offset, struct grub_zfs_device_desc *desc,
  1315. grub_size_t len, void *buf)
  1316. {
  1317. switch (desc->type)
  1318. {
  1319. case DEVICE_LEAF:
  1320. {
  1321. grub_uint64_t sector;
  1322. sector = DVA_OFFSET_TO_PHYS_SECTOR (offset);
  1323. if (!desc->dev)
  1324. {
  1325. return grub_error (GRUB_ERR_BAD_FS,
  1326. N_("couldn't find a necessary member device "
  1327. "of multi-device filesystem"));
  1328. }
  1329. /* read in a data block */
  1330. return grub_disk_read (desc->dev->disk, sector, 0, len, buf);
  1331. }
  1332. case DEVICE_MIRROR:
  1333. {
  1334. grub_err_t err = GRUB_ERR_NONE;
  1335. unsigned i;
  1336. if (desc->n_children <= 0)
  1337. return grub_error (GRUB_ERR_BAD_FS,
  1338. "non-positive number of mirror children");
  1339. for (i = 0; i < desc->n_children; i++)
  1340. {
  1341. err = read_device (offset, &desc->children[i],
  1342. len, buf);
  1343. if (!err)
  1344. break;
  1345. grub_errno = GRUB_ERR_NONE;
  1346. }
  1347. grub_errno = err;
  1348. return err;
  1349. }
  1350. case DEVICE_RAIDZ:
  1351. {
  1352. unsigned c = 0;
  1353. grub_uint64_t high;
  1354. grub_uint64_t devn;
  1355. grub_uint64_t m;
  1356. grub_uint32_t s, orig_s;
  1357. void *orig_buf = buf;
  1358. grub_size_t orig_len = len;
  1359. grub_uint8_t *recovery_buf[4];
  1360. grub_size_t recovery_len[4];
  1361. unsigned recovery_idx[4];
  1362. unsigned failed_devices = 0;
  1363. int idx, orig_idx;
  1364. if (desc->nparity < 1 || desc->nparity > 3)
  1365. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  1366. "raidz%d is not supported", desc->nparity);
  1367. if (desc->n_children <= desc->nparity || desc->n_children < 1)
  1368. return grub_error(GRUB_ERR_BAD_FS, "too little devices for given parity");
  1369. orig_s = (((len + (1 << desc->ashift) - 1) >> desc->ashift)
  1370. + (desc->n_children - desc->nparity) - 1);
  1371. s = orig_s;
  1372. high = grub_divmod64 ((offset >> desc->ashift),
  1373. desc->n_children, &m);
  1374. if (desc->nparity == 2)
  1375. c = 2;
  1376. if (desc->nparity == 3)
  1377. c = 3;
  1378. if (((len + (1 << desc->ashift) - 1) >> desc->ashift)
  1379. >= (desc->n_children - desc->nparity))
  1380. idx = (desc->n_children - desc->nparity - 1);
  1381. else
  1382. idx = ((len + (1 << desc->ashift) - 1) >> desc->ashift) - 1;
  1383. orig_idx = idx;
  1384. while (len > 0)
  1385. {
  1386. grub_size_t csize;
  1387. grub_uint32_t bsize;
  1388. grub_err_t err;
  1389. bsize = s / (desc->n_children - desc->nparity);
  1390. if (desc->nparity == 1
  1391. && ((offset >> (desc->ashift + 20 - desc->max_children_ashift))
  1392. & 1) == c)
  1393. c++;
  1394. high = grub_divmod64 ((offset >> desc->ashift) + c,
  1395. desc->n_children, &devn);
  1396. csize = (grub_size_t) bsize << desc->ashift;
  1397. if (csize > len)
  1398. csize = len;
  1399. grub_dprintf ("zfs", "RAIDZ mapping 0x%" PRIxGRUB_UINT64_T
  1400. "+%u (%" PRIxGRUB_SIZE ", %" PRIxGRUB_UINT32_T
  1401. ") -> (0x%" PRIxGRUB_UINT64_T ", 0x%"
  1402. PRIxGRUB_UINT64_T ")\n",
  1403. offset >> desc->ashift, c, len, bsize, high,
  1404. devn);
  1405. err = read_device ((high << desc->ashift)
  1406. | (offset & ((1 << desc->ashift) - 1)),
  1407. &desc->children[devn],
  1408. csize, buf);
  1409. if (err && failed_devices < desc->nparity)
  1410. {
  1411. recovery_buf[failed_devices] = buf;
  1412. recovery_len[failed_devices] = csize;
  1413. recovery_idx[failed_devices] = idx;
  1414. failed_devices++;
  1415. grub_errno = err = 0;
  1416. }
  1417. if (err)
  1418. return err;
  1419. c++;
  1420. idx--;
  1421. s--;
  1422. buf = (char *) buf + csize;
  1423. len -= csize;
  1424. }
  1425. if (failed_devices)
  1426. {
  1427. unsigned redundancy_pow[4];
  1428. unsigned cur_redundancy_pow = 0;
  1429. unsigned n_redundancy = 0;
  1430. unsigned i, j;
  1431. grub_err_t err;
  1432. /* Compute mul. x**s has a period of 255. */
  1433. if (powx[0] == 0)
  1434. {
  1435. grub_uint8_t cur = 1;
  1436. for (i = 0; i < 255; i++)
  1437. {
  1438. powx[i] = cur;
  1439. powx[i + 255] = cur;
  1440. powx_inv[cur] = i;
  1441. if (cur & 0x80)
  1442. cur = (cur << 1) ^ poly;
  1443. else
  1444. cur <<= 1;
  1445. }
  1446. }
  1447. /* Read redundancy data. */
  1448. for (n_redundancy = 0, cur_redundancy_pow = 0;
  1449. n_redundancy < failed_devices;
  1450. cur_redundancy_pow++)
  1451. {
  1452. high = grub_divmod64 ((offset >> desc->ashift)
  1453. + cur_redundancy_pow
  1454. + ((desc->nparity == 1)
  1455. && ((offset >> (desc->ashift + 20
  1456. - desc->max_children_ashift))
  1457. & 1)),
  1458. desc->n_children, &devn);
  1459. err = read_device ((high << desc->ashift)
  1460. | (offset & ((1 << desc->ashift) - 1)),
  1461. &desc->children[devn],
  1462. recovery_len[n_redundancy],
  1463. recovery_buf[n_redundancy]);
  1464. /* Ignore error if we may still have enough devices. */
  1465. if (err && n_redundancy + desc->nparity - cur_redundancy_pow - 1
  1466. >= failed_devices)
  1467. {
  1468. grub_errno = GRUB_ERR_NONE;
  1469. continue;
  1470. }
  1471. if (err)
  1472. return err;
  1473. redundancy_pow[n_redundancy] = cur_redundancy_pow;
  1474. n_redundancy++;
  1475. }
  1476. /* Now xor-our the parts we already know. */
  1477. buf = orig_buf;
  1478. len = orig_len;
  1479. s = orig_s;
  1480. idx = orig_idx;
  1481. while (len > 0)
  1482. {
  1483. grub_size_t csize = s;
  1484. csize = ((csize / (desc->n_children - desc->nparity))
  1485. << desc->ashift);
  1486. if (csize > len)
  1487. csize = len;
  1488. for (j = 0; j < failed_devices; j++)
  1489. if (buf == recovery_buf[j])
  1490. break;
  1491. if (j == failed_devices)
  1492. for (j = 0; j < failed_devices; j++)
  1493. xor_out (recovery_buf[j], buf,
  1494. csize < recovery_len[j] ? csize : recovery_len[j],
  1495. idx, redundancy_pow[j]);
  1496. s--;
  1497. buf = (char *) buf + csize;
  1498. len -= csize;
  1499. idx--;
  1500. }
  1501. for (i = 0; i < failed_devices
  1502. && recovery_len[i] == recovery_len[0];
  1503. i++);
  1504. /* Since the chunks have variable length handle the last block
  1505. separately. */
  1506. if (i != failed_devices)
  1507. {
  1508. grub_uint8_t *tmp_recovery_buf[4];
  1509. for (j = 0; j < i; j++)
  1510. tmp_recovery_buf[j] = recovery_buf[j] + recovery_len[failed_devices - 1];
  1511. err = recovery (tmp_recovery_buf, recovery_len[0] - recovery_len[failed_devices - 1], i, redundancy_pow,
  1512. recovery_idx);
  1513. if (err)
  1514. return err;
  1515. }
  1516. err = recovery (recovery_buf, recovery_len[failed_devices - 1],
  1517. failed_devices, redundancy_pow, recovery_idx);
  1518. if (err)
  1519. return err;
  1520. }
  1521. return GRUB_ERR_NONE;
  1522. }
  1523. }
  1524. return grub_error (GRUB_ERR_BAD_FS, "unsupported device type");
  1525. }
  1526. static grub_err_t
  1527. read_dva (const dva_t *dva,
  1528. grub_zfs_endian_t endian, struct grub_zfs_data *data,
  1529. void *buf, grub_size_t len)
  1530. {
  1531. grub_uint64_t offset;
  1532. unsigned i;
  1533. grub_err_t err = 0;
  1534. int try = 0;
  1535. offset = dva_get_offset (dva, endian);
  1536. for (try = 0; try < 2; try++)
  1537. {
  1538. for (i = 0; i < data->n_devices_attached; i++)
  1539. if (data->devices_attached[i].id == DVA_GET_VDEV (dva))
  1540. {
  1541. err = read_device (offset, &data->devices_attached[i], len, buf);
  1542. if (!err)
  1543. return GRUB_ERR_NONE;
  1544. break;
  1545. }
  1546. if (try == 1)
  1547. break;
  1548. err = scan_devices (data);
  1549. if (err)
  1550. return err;
  1551. }
  1552. if (!err)
  1553. return grub_error (GRUB_ERR_BAD_FS, "unknown device %d",
  1554. (int) DVA_GET_VDEV (dva));
  1555. return err;
  1556. }
  1557. /*
  1558. * Read a block of data based on the gang block address dva,
  1559. * and put its data in buf.
  1560. *
  1561. */
  1562. static grub_err_t
  1563. zio_read_gang (blkptr_t * bp, grub_zfs_endian_t endian, dva_t * dva, void *buf,
  1564. struct grub_zfs_data *data)
  1565. {
  1566. zio_gbh_phys_t *zio_gb;
  1567. unsigned i;
  1568. grub_err_t err;
  1569. zio_cksum_t zc;
  1570. grub_memset (&zc, 0, sizeof (zc));
  1571. zio_gb = grub_malloc (SPA_GANGBLOCKSIZE);
  1572. if (!zio_gb)
  1573. return grub_errno;
  1574. grub_dprintf ("zfs", endian == GRUB_ZFS_LITTLE_ENDIAN ? "little-endian gang\n"
  1575. :"big-endian gang\n");
  1576. err = read_dva (dva, endian, data, zio_gb, SPA_GANGBLOCKSIZE);
  1577. if (err)
  1578. {
  1579. grub_free (zio_gb);
  1580. return err;
  1581. }
  1582. /* XXX */
  1583. /* self checksuming the gang block header */
  1584. ZIO_SET_CHECKSUM (&zc, DVA_GET_VDEV (dva),
  1585. dva_get_offset (dva, endian), bp->blk_birth, 0);
  1586. err = zio_checksum_verify (zc, ZIO_CHECKSUM_GANG_HEADER, endian,
  1587. (char *) zio_gb, SPA_GANGBLOCKSIZE);
  1588. if (err)
  1589. {
  1590. grub_free (zio_gb);
  1591. return err;
  1592. }
  1593. endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1;
  1594. for (i = 0; i < SPA_GBH_NBLKPTRS; i++)
  1595. {
  1596. if (BP_IS_HOLE(&zio_gb->zg_blkptr[i]))
  1597. continue;
  1598. err = zio_read_data (&zio_gb->zg_blkptr[i], endian, buf, data);
  1599. if (err)
  1600. {
  1601. grub_free (zio_gb);
  1602. return err;
  1603. }
  1604. buf = (char *) buf + get_psize (&zio_gb->zg_blkptr[i], endian);
  1605. }
  1606. grub_free (zio_gb);
  1607. return GRUB_ERR_NONE;
  1608. }
  1609. /*
  1610. * Read in a block of raw data to buf.
  1611. */
  1612. static grub_err_t
  1613. zio_read_data (blkptr_t * bp, grub_zfs_endian_t endian, void *buf,
  1614. struct grub_zfs_data *data)
  1615. {
  1616. int i, psize;
  1617. grub_err_t err = GRUB_ERR_NONE;
  1618. psize = get_psize (bp, endian);
  1619. /* pick a good dva from the block pointer */
  1620. for (i = 0; i < SPA_DVAS_PER_BP; i++)
  1621. {
  1622. if (bp->blk_dva[i].dva_word[0] == 0 && bp->blk_dva[i].dva_word[1] == 0)
  1623. continue;
  1624. if ((grub_zfs_to_cpu64 (bp->blk_dva[i].dva_word[1], endian)>>63) & 1)
  1625. err = zio_read_gang (bp, endian, &bp->blk_dva[i], buf, data);
  1626. else
  1627. err = read_dva (&bp->blk_dva[i], endian, data, buf, psize);
  1628. if (!err)
  1629. return GRUB_ERR_NONE;
  1630. grub_errno = GRUB_ERR_NONE;
  1631. }
  1632. if (!err)
  1633. err = grub_error (GRUB_ERR_BAD_FS, "couldn't find a valid DVA");
  1634. grub_errno = err;
  1635. return err;
  1636. }
  1637. /*
  1638. * buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be
  1639. * more than BPE_PAYLOAD_SIZE bytes).
  1640. */
  1641. static grub_err_t
  1642. decode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
  1643. {
  1644. grub_size_t psize, i;
  1645. grub_uint8_t *buf8 = buf;
  1646. grub_uint64_t w = 0;
  1647. const grub_uint64_t *bp64 = (const grub_uint64_t *)bp;
  1648. psize = BPE_GET_PSIZE(bp);
  1649. /*
  1650. * Decode the words of the block pointer into the byte array.
  1651. * Low bits of first word are the first byte (little endian).
  1652. */
  1653. for (i = 0; i < psize; i++)
  1654. {
  1655. if (i % sizeof (w) == 0)
  1656. {
  1657. /* beginning of a word */
  1658. w = *bp64;
  1659. bp64++;
  1660. if (!BPE_IS_PAYLOADWORD(bp, bp64))
  1661. bp64++;
  1662. }
  1663. buf8[i] = BF64_GET(w, (i % sizeof (w)) * 8, 8);
  1664. }
  1665. return GRUB_ERR_NONE;
  1666. }
  1667. /*
  1668. * Read in a block of data, verify its checksum, decompress if needed,
  1669. * and put the uncompressed data in buf.
  1670. */
  1671. static grub_err_t
  1672. zio_read (blkptr_t *bp, grub_zfs_endian_t endian, void **buf,
  1673. grub_size_t *size, struct grub_zfs_data *data)
  1674. {
  1675. grub_size_t lsize, psize;
  1676. unsigned int comp, encrypted;
  1677. char *compbuf = NULL;
  1678. grub_err_t err;
  1679. zio_cksum_t zc = bp->blk_cksum;
  1680. grub_uint32_t checksum;
  1681. *buf = NULL;
  1682. checksum = (grub_zfs_to_cpu64((bp)->blk_prop, endian) >> 40) & 0xff;
  1683. comp = (grub_zfs_to_cpu64((bp)->blk_prop, endian)>>32) & 0x7f;
  1684. encrypted = ((grub_zfs_to_cpu64((bp)->blk_prop, endian) >> 60) & 3);
  1685. if (BP_IS_EMBEDDED(bp))
  1686. {
  1687. if (BPE_GET_ETYPE(bp) != BP_EMBEDDED_TYPE_DATA)
  1688. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  1689. "unsupported embedded BP (type=%"
  1690. PRIuGRUB_UINT64_T ")\n",
  1691. BPE_GET_ETYPE (bp));
  1692. lsize = BPE_GET_LSIZE(bp);
  1693. psize = BF64_GET_SB(grub_zfs_to_cpu64 ((bp)->blk_prop, endian), 25, 7, 0, 1);
  1694. }
  1695. else
  1696. {
  1697. lsize = (BP_IS_HOLE(bp) ? 0 :
  1698. (((grub_zfs_to_cpu64 ((bp)->blk_prop, endian) & 0xffff) + 1)
  1699. << SPA_MINBLOCKSHIFT));
  1700. psize = get_psize (bp, endian);
  1701. }
  1702. grub_dprintf("zfs", "zio_read: E %d: size %" PRIdGRUB_SSIZE "/%"
  1703. PRIdGRUB_SSIZE "\n", (int)BP_IS_EMBEDDED(bp), lsize, psize);
  1704. if (size)
  1705. *size = lsize;
  1706. if (comp >= ZIO_COMPRESS_FUNCTIONS)
  1707. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  1708. "compression algorithm %u not supported\n", (unsigned int) comp);
  1709. if (comp != ZIO_COMPRESS_OFF && decomp_table[comp].decomp_func == NULL)
  1710. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  1711. "compression algorithm %s not supported\n", decomp_table[comp].name);
  1712. if (comp != ZIO_COMPRESS_OFF)
  1713. /* It's not really necessary to align to 16, just for safety. */
  1714. compbuf = grub_malloc (ALIGN_UP (psize, 16));
  1715. else
  1716. compbuf = *buf = grub_malloc (lsize);
  1717. if (! compbuf)
  1718. return grub_errno;
  1719. grub_dprintf ("zfs", "endian = %d\n", endian);
  1720. if (BP_IS_EMBEDDED(bp))
  1721. err = decode_embedded_bp_compressed(bp, compbuf);
  1722. else
  1723. {
  1724. err = zio_read_data (bp, endian, compbuf, data);
  1725. /* FIXME is it really necessary? */
  1726. if (comp != ZIO_COMPRESS_OFF)
  1727. grub_memset (compbuf + psize, 0, ALIGN_UP (psize, 16) - psize);
  1728. }
  1729. if (err)
  1730. {
  1731. grub_free (compbuf);
  1732. *buf = NULL;
  1733. return err;
  1734. }
  1735. if (!BP_IS_EMBEDDED(bp))
  1736. {
  1737. err = zio_checksum_verify (zc, checksum, endian,
  1738. compbuf, psize);
  1739. if (err)
  1740. {
  1741. grub_dprintf ("zfs", "incorrect checksum\n");
  1742. grub_free (compbuf);
  1743. *buf = NULL;
  1744. return err;
  1745. }
  1746. }
  1747. if (encrypted)
  1748. {
  1749. if (!grub_zfs_decrypt)
  1750. err = grub_error (GRUB_ERR_BAD_FS,
  1751. N_("module `%s' isn't loaded"),
  1752. "zfscrypt");
  1753. else
  1754. {
  1755. unsigned i, besti = 0;
  1756. grub_uint64_t bestval = 0;
  1757. for (i = 0; i < data->subvol.nkeys; i++)
  1758. if (data->subvol.keyring[i].txg <= grub_zfs_to_cpu64 (bp->blk_birth,
  1759. endian)
  1760. && data->subvol.keyring[i].txg > bestval)
  1761. {
  1762. besti = i;
  1763. bestval = data->subvol.keyring[i].txg;
  1764. }
  1765. if (bestval == 0)
  1766. {
  1767. grub_free (compbuf);
  1768. *buf = NULL;
  1769. grub_dprintf ("zfs", "no key for txg %" PRIxGRUB_UINT64_T "\n",
  1770. grub_zfs_to_cpu64 (bp->blk_birth,
  1771. endian));
  1772. return grub_error (GRUB_ERR_BAD_FS, "no key found in keychain");
  1773. }
  1774. grub_dprintf ("zfs", "using key %u (%" PRIxGRUB_UINT64_T
  1775. ", %p) for txg %" PRIxGRUB_UINT64_T "\n",
  1776. besti, data->subvol.keyring[besti].txg,
  1777. data->subvol.keyring[besti].cipher,
  1778. grub_zfs_to_cpu64 (bp->blk_birth,
  1779. endian));
  1780. err = grub_zfs_decrypt (data->subvol.keyring[besti].cipher,
  1781. data->subvol.keyring[besti].algo,
  1782. &(bp)->blk_dva[encrypted],
  1783. compbuf, psize, zc.zc_mac,
  1784. endian);
  1785. }
  1786. if (err)
  1787. {
  1788. grub_free (compbuf);
  1789. *buf = NULL;
  1790. return err;
  1791. }
  1792. }
  1793. if (comp != ZIO_COMPRESS_OFF)
  1794. {
  1795. *buf = grub_malloc (lsize);
  1796. if (!*buf)
  1797. {
  1798. grub_free (compbuf);
  1799. return grub_errno;
  1800. }
  1801. err = decomp_table[comp].decomp_func (compbuf, *buf, psize, lsize);
  1802. grub_free (compbuf);
  1803. if (err)
  1804. {
  1805. grub_free (*buf);
  1806. *buf = NULL;
  1807. return err;
  1808. }
  1809. }
  1810. return GRUB_ERR_NONE;
  1811. }
  1812. /*
  1813. * Get the block from a block id.
  1814. * push the block onto the stack.
  1815. *
  1816. */
  1817. static grub_err_t
  1818. dmu_read (dnode_end_t * dn, grub_uint64_t blkid, void **buf,
  1819. grub_zfs_endian_t *endian_out, struct grub_zfs_data *data)
  1820. {
  1821. int level;
  1822. grub_off_t idx;
  1823. blkptr_t *bp_array = dn->dn.dn_blkptr;
  1824. int epbs = dn->dn.dn_indblkshift - SPA_BLKPTRSHIFT;
  1825. blkptr_t *bp;
  1826. void *tmpbuf = 0;
  1827. grub_zfs_endian_t endian;
  1828. grub_err_t err = GRUB_ERR_NONE;
  1829. bp = grub_malloc (sizeof (blkptr_t));
  1830. if (!bp)
  1831. return grub_errno;
  1832. endian = dn->endian;
  1833. for (level = dn->dn.dn_nlevels - 1; level >= 0; level--)
  1834. {
  1835. grub_dprintf ("zfs", "endian = %d\n", endian);
  1836. idx = (blkid >> (epbs * level)) & ((1 << epbs) - 1);
  1837. *bp = bp_array[idx];
  1838. if (bp_array != dn->dn.dn_blkptr)
  1839. {
  1840. grub_free (bp_array);
  1841. bp_array = 0;
  1842. }
  1843. if (BP_IS_HOLE (bp))
  1844. {
  1845. grub_size_t size = grub_zfs_to_cpu16 (dn->dn.dn_datablkszsec,
  1846. dn->endian)
  1847. << SPA_MINBLOCKSHIFT;
  1848. *buf = grub_malloc (size);
  1849. if (!*buf)
  1850. {
  1851. err = grub_errno;
  1852. break;
  1853. }
  1854. grub_memset (*buf, 0, size);
  1855. endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1;
  1856. break;
  1857. }
  1858. if (level == 0)
  1859. {
  1860. grub_dprintf ("zfs", "endian = %d\n", endian);
  1861. err = zio_read (bp, endian, buf, 0, data);
  1862. endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1;
  1863. break;
  1864. }
  1865. grub_dprintf ("zfs", "endian = %d\n", endian);
  1866. err = zio_read (bp, endian, &tmpbuf, 0, data);
  1867. endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1;
  1868. if (err)
  1869. break;
  1870. bp_array = tmpbuf;
  1871. }
  1872. if (bp_array != dn->dn.dn_blkptr)
  1873. grub_free (bp_array);
  1874. if (endian_out)
  1875. *endian_out = endian;
  1876. grub_free (bp);
  1877. return err;
  1878. }
  1879. /*
  1880. * mzap_lookup: Looks up property described by "name" and returns the value
  1881. * in "value".
  1882. */
  1883. static grub_err_t
  1884. mzap_lookup (mzap_phys_t * zapobj, grub_zfs_endian_t endian,
  1885. grub_uint32_t objsize, const char *name, grub_uint64_t * value,
  1886. int case_insensitive)
  1887. {
  1888. grub_uint32_t i, chunks;
  1889. mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
  1890. if (objsize < MZAP_ENT_LEN)
  1891. return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), name);
  1892. chunks = objsize / MZAP_ENT_LEN - 1;
  1893. for (i = 0; i < chunks; i++)
  1894. {
  1895. if (case_insensitive ? (grub_strcasecmp (mzap_ent[i].mze_name, name) == 0)
  1896. : (grub_strcmp (mzap_ent[i].mze_name, name) == 0))
  1897. {
  1898. *value = grub_zfs_to_cpu64 (mzap_ent[i].mze_value, endian);
  1899. return GRUB_ERR_NONE;
  1900. }
  1901. }
  1902. return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), name);
  1903. }
  1904. static int
  1905. mzap_iterate (mzap_phys_t * zapobj, grub_zfs_endian_t endian, int objsize,
  1906. int (*hook) (const char *name, grub_uint64_t val,
  1907. struct grub_zfs_dir_ctx *ctx),
  1908. struct grub_zfs_dir_ctx *ctx)
  1909. {
  1910. int i, chunks;
  1911. mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
  1912. chunks = objsize / MZAP_ENT_LEN - 1;
  1913. for (i = 0; i < chunks; i++)
  1914. {
  1915. grub_dprintf ("zfs", "zap: name = %s, value = %llx, cd = %x\n",
  1916. mzap_ent[i].mze_name, (long long)mzap_ent[i].mze_value,
  1917. (int)mzap_ent[i].mze_cd);
  1918. if (hook (mzap_ent[i].mze_name,
  1919. grub_zfs_to_cpu64 (mzap_ent[i].mze_value, endian), ctx))
  1920. return 1;
  1921. }
  1922. return 0;
  1923. }
  1924. static grub_uint64_t
  1925. zap_hash (grub_uint64_t salt, const char *name,
  1926. int case_insensitive)
  1927. {
  1928. static grub_uint64_t table[256];
  1929. const grub_uint8_t *cp;
  1930. grub_uint8_t c;
  1931. grub_uint64_t crc = salt;
  1932. if (table[128] == 0)
  1933. {
  1934. grub_uint64_t *ct;
  1935. int i, j;
  1936. for (i = 0; i < 256; i++)
  1937. {
  1938. for (ct = table + i, *ct = i, j = 8; j > 0; j--)
  1939. *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
  1940. }
  1941. }
  1942. if (case_insensitive)
  1943. for (cp = (const grub_uint8_t *) name; (c = *cp) != '\0'; cp++)
  1944. crc = (crc >> 8) ^ table[(crc ^ grub_toupper (c)) & 0xFF];
  1945. else
  1946. for (cp = (const grub_uint8_t *) name; (c = *cp) != '\0'; cp++)
  1947. crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
  1948. /*
  1949. * Only use 28 bits, since we need 4 bits in the cookie for the
  1950. * collision differentiator. We MUST use the high bits, since
  1951. * those are the onces that we first pay attention to when
  1952. * chosing the bucket.
  1953. */
  1954. crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
  1955. return crc;
  1956. }
  1957. /*
  1958. * Only to be used on 8-bit arrays.
  1959. * array_len is actual len in bytes (not encoded le_value_length).
  1960. * buf is null-terminated.
  1961. */
  1962. static inline int
  1963. name_cmp (const char *s1, const char *s2, grub_size_t n,
  1964. int case_insensitive)
  1965. {
  1966. const char *t1 = (const char *) s1;
  1967. const char *t2 = (const char *) s2;
  1968. if (!case_insensitive)
  1969. return grub_memcmp (t1, t2, n);
  1970. while (n--)
  1971. {
  1972. if (grub_toupper (*t1) != grub_toupper (*t2))
  1973. return (int) grub_toupper (*t1) - (int) grub_toupper (*t2);
  1974. t1++;
  1975. t2++;
  1976. }
  1977. return 0;
  1978. }
  1979. /* XXX */
  1980. static int
  1981. zap_leaf_array_equal (zap_leaf_phys_t * l, grub_zfs_endian_t endian,
  1982. int blksft, int chunk, grub_size_t array_len,
  1983. const char *buf, int case_insensitive)
  1984. {
  1985. grub_size_t bseen = 0;
  1986. while (bseen < array_len)
  1987. {
  1988. struct zap_leaf_array *la = &ZAP_LEAF_CHUNK (l, blksft, chunk)->l_array;
  1989. grub_size_t toread = array_len - bseen;
  1990. if (toread > ZAP_LEAF_ARRAY_BYTES)
  1991. toread = ZAP_LEAF_ARRAY_BYTES;
  1992. if (chunk >= ZAP_LEAF_NUMCHUNKS (blksft))
  1993. return 0;
  1994. if (name_cmp ((char *) la->la_array, buf + bseen, toread,
  1995. case_insensitive) != 0)
  1996. break;
  1997. chunk = grub_zfs_to_cpu16 (la->la_next, endian);
  1998. bseen += toread;
  1999. }
  2000. return (bseen == array_len);
  2001. }
  2002. /* XXX */
  2003. static grub_err_t
  2004. zap_leaf_array_get (zap_leaf_phys_t * l, grub_zfs_endian_t endian, int blksft,
  2005. int chunk, grub_size_t array_len, char *buf)
  2006. {
  2007. grub_size_t bseen = 0;
  2008. while (bseen < array_len)
  2009. {
  2010. struct zap_leaf_array *la;
  2011. grub_size_t toread = array_len - bseen;
  2012. if (toread > ZAP_LEAF_ARRAY_BYTES)
  2013. toread = ZAP_LEAF_ARRAY_BYTES;
  2014. if (chunk >= ZAP_LEAF_NUMCHUNKS (blksft))
  2015. /* Don't use grub_error because this error is to be ignored. */
  2016. return GRUB_ERR_BAD_FS;
  2017. la = &ZAP_LEAF_CHUNK (l, blksft, chunk)->l_array;
  2018. grub_memcpy (buf + bseen,la->la_array, toread);
  2019. chunk = grub_zfs_to_cpu16 (la->la_next, endian);
  2020. bseen += toread;
  2021. }
  2022. return GRUB_ERR_NONE;
  2023. }
  2024. /*
  2025. * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
  2026. * value for the property "name".
  2027. *
  2028. */
  2029. /* XXX */
  2030. static grub_err_t
  2031. zap_leaf_lookup (zap_leaf_phys_t * l, grub_zfs_endian_t endian,
  2032. int blksft, grub_uint64_t h,
  2033. const char *name, grub_uint64_t * value,
  2034. int case_insensitive)
  2035. {
  2036. grub_uint16_t chunk;
  2037. struct zap_leaf_entry *le;
  2038. /* Verify if this is a valid leaf block */
  2039. if (grub_zfs_to_cpu64 (l->l_hdr.lh_block_type, endian) != ZBT_LEAF)
  2040. return grub_error (GRUB_ERR_BAD_FS, "invalid leaf type");
  2041. if (grub_zfs_to_cpu32 (l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC)
  2042. return grub_error (GRUB_ERR_BAD_FS, "invalid leaf magic");
  2043. for (chunk = grub_zfs_to_cpu16 (l->l_hash[LEAF_HASH (blksft, h, l)], endian);
  2044. chunk != CHAIN_END; chunk = grub_zfs_to_cpu16 (le->le_next, endian))
  2045. {
  2046. if (chunk >= ZAP_LEAF_NUMCHUNKS (blksft))
  2047. return grub_error (GRUB_ERR_BAD_FS, "invalid chunk number");
  2048. le = ZAP_LEAF_ENTRY (l, blksft, chunk);
  2049. /* Verify the chunk entry */
  2050. if (le->le_type != ZAP_CHUNK_ENTRY)
  2051. return grub_error (GRUB_ERR_BAD_FS, "invalid chunk entry");
  2052. if (grub_zfs_to_cpu64 (le->le_hash,endian) != h)
  2053. continue;
  2054. grub_dprintf ("zfs", "fzap: length %d\n", (int) le->le_name_length);
  2055. if (zap_leaf_array_equal (l, endian, blksft,
  2056. grub_zfs_to_cpu16 (le->le_name_chunk,endian),
  2057. grub_zfs_to_cpu16 (le->le_name_length, endian),
  2058. name, case_insensitive))
  2059. {
  2060. struct zap_leaf_array *la;
  2061. if (le->le_int_size != 8 || grub_zfs_to_cpu16 (le->le_value_length,
  2062. endian) != 1)
  2063. return grub_error (GRUB_ERR_BAD_FS, "invalid leaf chunk entry");
  2064. /* get the uint64_t property value */
  2065. la = &ZAP_LEAF_CHUNK (l, blksft, le->le_value_chunk)->l_array;
  2066. *value = grub_be_to_cpu64 (la->la_array64);
  2067. return GRUB_ERR_NONE;
  2068. }
  2069. }
  2070. return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"), name);
  2071. }
  2072. /* Verify if this is a fat zap header block */
  2073. static grub_err_t
  2074. zap_verify (zap_phys_t *zap, grub_zfs_endian_t endian)
  2075. {
  2076. if (grub_zfs_to_cpu64 (zap->zap_magic, endian) != (grub_uint64_t) ZAP_MAGIC)
  2077. return grub_error (GRUB_ERR_BAD_FS, "bad ZAP magic");
  2078. if (zap->zap_salt == 0)
  2079. return grub_error (GRUB_ERR_BAD_FS, "bad ZAP salt");
  2080. return GRUB_ERR_NONE;
  2081. }
  2082. /*
  2083. * Fat ZAP lookup
  2084. *
  2085. */
  2086. /* XXX */
  2087. static grub_err_t
  2088. fzap_lookup (dnode_end_t * zap_dnode, zap_phys_t * zap,
  2089. const char *name, grub_uint64_t * value,
  2090. struct grub_zfs_data *data, int case_insensitive)
  2091. {
  2092. void *l;
  2093. grub_uint64_t hash, idx, blkid;
  2094. int blksft = zfs_log2 (grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec,
  2095. zap_dnode->endian) << DNODE_SHIFT);
  2096. grub_err_t err;
  2097. grub_zfs_endian_t leafendian;
  2098. err = zap_verify (zap, zap_dnode->endian);
  2099. if (err)
  2100. return err;
  2101. hash = zap_hash (zap->zap_salt, name, case_insensitive);
  2102. /* get block id from index */
  2103. if (zap->zap_ptrtbl.zt_numblks != 0)
  2104. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  2105. "external pointer tables not supported");
  2106. idx = ZAP_HASH_IDX (hash, zap->zap_ptrtbl.zt_shift);
  2107. blkid = grub_zfs_to_cpu64 (((grub_uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))], zap_dnode->endian);
  2108. /* Get the leaf block */
  2109. if ((1U << blksft) < sizeof (zap_leaf_phys_t))
  2110. return grub_error (GRUB_ERR_BAD_FS, "ZAP leaf is too small");
  2111. err = dmu_read (zap_dnode, blkid, &l, &leafendian, data);
  2112. if (err)
  2113. return err;
  2114. err = zap_leaf_lookup (l, leafendian, blksft, hash, name, value,
  2115. case_insensitive);
  2116. grub_free (l);
  2117. return err;
  2118. }
  2119. /* XXX */
  2120. static int
  2121. fzap_iterate (dnode_end_t * zap_dnode, zap_phys_t * zap,
  2122. grub_size_t name_elem_length,
  2123. int (*hook) (const void *name, grub_size_t name_length,
  2124. const void *val_in,
  2125. grub_size_t nelem, grub_size_t elemsize,
  2126. void *data),
  2127. void *hook_data, struct grub_zfs_data *data)
  2128. {
  2129. zap_leaf_phys_t *l;
  2130. void *l_in;
  2131. grub_uint64_t idx, idx2, blkid;
  2132. grub_uint16_t chunk;
  2133. int blksft = zfs_log2 (grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec,
  2134. zap_dnode->endian) << DNODE_SHIFT);
  2135. grub_err_t err;
  2136. grub_zfs_endian_t endian;
  2137. if (zap_verify (zap, zap_dnode->endian))
  2138. return 0;
  2139. /* get block id from index */
  2140. if (zap->zap_ptrtbl.zt_numblks != 0)
  2141. {
  2142. grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  2143. "external pointer tables not supported");
  2144. return 0;
  2145. }
  2146. /* Get the leaf block */
  2147. if ((1U << blksft) < sizeof (zap_leaf_phys_t))
  2148. {
  2149. grub_error (GRUB_ERR_BAD_FS, "ZAP leaf is too small");
  2150. return 0;
  2151. }
  2152. for (idx = 0; idx < (1ULL << zap->zap_ptrtbl.zt_shift); idx++)
  2153. {
  2154. blkid = grub_zfs_to_cpu64 (((grub_uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))],
  2155. zap_dnode->endian);
  2156. for (idx2 = 0; idx2 < idx; idx2++)
  2157. if (blkid == grub_zfs_to_cpu64 (((grub_uint64_t *) zap)[idx2 + (1 << (blksft - 3 - 1))],
  2158. zap_dnode->endian))
  2159. break;
  2160. if (idx2 != idx)
  2161. continue;
  2162. err = dmu_read (zap_dnode, blkid, &l_in, &endian, data);
  2163. l = l_in;
  2164. if (err)
  2165. {
  2166. grub_errno = GRUB_ERR_NONE;
  2167. continue;
  2168. }
  2169. /* Verify if this is a valid leaf block */
  2170. if (grub_zfs_to_cpu64 (l->l_hdr.lh_block_type, endian) != ZBT_LEAF)
  2171. {
  2172. grub_free (l);
  2173. continue;
  2174. }
  2175. if (grub_zfs_to_cpu32 (l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC)
  2176. {
  2177. grub_free (l);
  2178. continue;
  2179. }
  2180. for (chunk = 0; chunk < ZAP_LEAF_NUMCHUNKS (blksft); chunk++)
  2181. {
  2182. char *buf;
  2183. struct zap_leaf_entry *le;
  2184. char *val;
  2185. grub_size_t val_length;
  2186. le = ZAP_LEAF_ENTRY (l, blksft, chunk);
  2187. /* Verify the chunk entry */
  2188. if (le->le_type != ZAP_CHUNK_ENTRY)
  2189. continue;
  2190. buf = grub_malloc (grub_zfs_to_cpu16 (le->le_name_length, endian)
  2191. * name_elem_length + 1);
  2192. if (zap_leaf_array_get (l, endian, blksft,
  2193. grub_zfs_to_cpu16 (le->le_name_chunk,
  2194. endian),
  2195. grub_zfs_to_cpu16 (le->le_name_length,
  2196. endian)
  2197. * name_elem_length, buf))
  2198. {
  2199. grub_free (buf);
  2200. continue;
  2201. }
  2202. buf[le->le_name_length * name_elem_length] = 0;
  2203. val_length = ((int) le->le_value_length
  2204. * (int) le->le_int_size);
  2205. val = grub_malloc (grub_zfs_to_cpu16 (val_length, endian));
  2206. if (zap_leaf_array_get (l, endian, blksft,
  2207. grub_zfs_to_cpu16 (le->le_value_chunk,
  2208. endian),
  2209. val_length, val))
  2210. {
  2211. grub_free (buf);
  2212. grub_free (val);
  2213. continue;
  2214. }
  2215. if (hook (buf, le->le_name_length,
  2216. val, le->le_value_length, le->le_int_size, hook_data))
  2217. {
  2218. grub_free (l);
  2219. return 1;
  2220. }
  2221. grub_free (buf);
  2222. grub_free (val);
  2223. }
  2224. grub_free (l);
  2225. }
  2226. return 0;
  2227. }
  2228. /*
  2229. * Read in the data of a zap object and find the value for a matching
  2230. * property name.
  2231. *
  2232. */
  2233. static grub_err_t
  2234. zap_lookup (dnode_end_t * zap_dnode, const char *name, grub_uint64_t *val,
  2235. struct grub_zfs_data *data, int case_insensitive)
  2236. {
  2237. grub_uint64_t block_type;
  2238. grub_uint32_t size;
  2239. void *zapbuf;
  2240. grub_err_t err;
  2241. grub_zfs_endian_t endian;
  2242. grub_dprintf ("zfs", "looking for '%s'\n", name);
  2243. /* Read in the first block of the zap object data. */
  2244. size = (grub_uint32_t) grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec,
  2245. zap_dnode->endian) << SPA_MINBLOCKSHIFT;
  2246. err = dmu_read (zap_dnode, 0, &zapbuf, &endian, data);
  2247. if (err)
  2248. return err;
  2249. block_type = grub_zfs_to_cpu64 (*((grub_uint64_t *) zapbuf), endian);
  2250. grub_dprintf ("zfs", "zap read\n");
  2251. if (block_type == ZBT_MICRO)
  2252. {
  2253. grub_dprintf ("zfs", "micro zap\n");
  2254. err = mzap_lookup (zapbuf, endian, size, name, val,
  2255. case_insensitive);
  2256. grub_dprintf ("zfs", "returned %d\n", err);
  2257. grub_free (zapbuf);
  2258. return err;
  2259. }
  2260. else if (block_type == ZBT_HEADER)
  2261. {
  2262. grub_dprintf ("zfs", "fat zap\n");
  2263. /* this is a fat zap */
  2264. err = fzap_lookup (zap_dnode, zapbuf, name, val, data,
  2265. case_insensitive);
  2266. grub_dprintf ("zfs", "returned %d\n", err);
  2267. grub_free (zapbuf);
  2268. return err;
  2269. }
  2270. return grub_error (GRUB_ERR_BAD_FS, "unknown ZAP type");
  2271. }
  2272. /* Context for zap_iterate_u64. */
  2273. struct zap_iterate_u64_ctx
  2274. {
  2275. int (*hook) (const char *, grub_uint64_t, struct grub_zfs_dir_ctx *);
  2276. struct grub_zfs_dir_ctx *dir_ctx;
  2277. };
  2278. /* Helper for zap_iterate_u64. */
  2279. static int
  2280. zap_iterate_u64_transform (const void *name,
  2281. grub_size_t namelen __attribute__ ((unused)),
  2282. const void *val_in,
  2283. grub_size_t nelem,
  2284. grub_size_t elemsize,
  2285. void *data)
  2286. {
  2287. struct zap_iterate_u64_ctx *ctx = data;
  2288. if (elemsize != sizeof (grub_uint64_t) || nelem != 1)
  2289. return 0;
  2290. return ctx->hook (name, grub_be_to_cpu64 (*(const grub_uint64_t *) val_in),
  2291. ctx->dir_ctx);
  2292. }
  2293. static int
  2294. zap_iterate_u64 (dnode_end_t * zap_dnode,
  2295. int (*hook) (const char *name, grub_uint64_t val,
  2296. struct grub_zfs_dir_ctx *ctx),
  2297. struct grub_zfs_data *data, struct grub_zfs_dir_ctx *ctx)
  2298. {
  2299. grub_uint64_t block_type;
  2300. int size;
  2301. void *zapbuf;
  2302. grub_err_t err;
  2303. int ret;
  2304. grub_zfs_endian_t endian;
  2305. /* Read in the first block of the zap object data. */
  2306. size = grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec, zap_dnode->endian) << SPA_MINBLOCKSHIFT;
  2307. err = dmu_read (zap_dnode, 0, &zapbuf, &endian, data);
  2308. if (err)
  2309. return 0;
  2310. block_type = grub_zfs_to_cpu64 (*((grub_uint64_t *) zapbuf), endian);
  2311. grub_dprintf ("zfs", "zap iterate\n");
  2312. if (block_type == ZBT_MICRO)
  2313. {
  2314. grub_dprintf ("zfs", "micro zap\n");
  2315. ret = mzap_iterate (zapbuf, endian, size, hook, ctx);
  2316. grub_free (zapbuf);
  2317. return ret;
  2318. }
  2319. else if (block_type == ZBT_HEADER)
  2320. {
  2321. struct zap_iterate_u64_ctx transform_ctx = {
  2322. .hook = hook,
  2323. .dir_ctx = ctx
  2324. };
  2325. grub_dprintf ("zfs", "fat zap\n");
  2326. /* this is a fat zap */
  2327. ret = fzap_iterate (zap_dnode, zapbuf, 1,
  2328. zap_iterate_u64_transform, &transform_ctx, data);
  2329. grub_free (zapbuf);
  2330. return ret;
  2331. }
  2332. grub_error (GRUB_ERR_BAD_FS, "unknown ZAP type");
  2333. return 0;
  2334. }
  2335. static int
  2336. zap_iterate (dnode_end_t * zap_dnode,
  2337. grub_size_t nameelemlen,
  2338. int (*hook) (const void *name, grub_size_t namelen,
  2339. const void *val_in,
  2340. grub_size_t nelem, grub_size_t elemsize,
  2341. void *data),
  2342. void *hook_data, struct grub_zfs_data *data)
  2343. {
  2344. grub_uint64_t block_type;
  2345. void *zapbuf;
  2346. grub_err_t err;
  2347. int ret;
  2348. grub_zfs_endian_t endian;
  2349. /* Read in the first block of the zap object data. */
  2350. err = dmu_read (zap_dnode, 0, &zapbuf, &endian, data);
  2351. if (err)
  2352. return 0;
  2353. block_type = grub_zfs_to_cpu64 (*((grub_uint64_t *) zapbuf), endian);
  2354. grub_dprintf ("zfs", "zap iterate\n");
  2355. if (block_type == ZBT_MICRO)
  2356. {
  2357. grub_error (GRUB_ERR_BAD_FS, "micro ZAP where FAT ZAP expected");
  2358. return 0;
  2359. }
  2360. if (block_type == ZBT_HEADER)
  2361. {
  2362. grub_dprintf ("zfs", "fat zap\n");
  2363. /* this is a fat zap */
  2364. ret = fzap_iterate (zap_dnode, zapbuf, nameelemlen, hook, hook_data,
  2365. data);
  2366. grub_free (zapbuf);
  2367. return ret;
  2368. }
  2369. grub_error (GRUB_ERR_BAD_FS, "unknown ZAP type");
  2370. return 0;
  2371. }
  2372. /*
  2373. * Get the dnode of an object number from the metadnode of an object set.
  2374. *
  2375. * Input
  2376. * mdn - metadnode to get the object dnode
  2377. * objnum - object number for the object dnode
  2378. * buf - data buffer that holds the returning dnode
  2379. */
  2380. static grub_err_t
  2381. dnode_get (dnode_end_t * mdn, grub_uint64_t objnum, grub_uint8_t type,
  2382. dnode_end_t * buf, struct grub_zfs_data *data)
  2383. {
  2384. grub_uint64_t blkid, blksz; /* the block id this object dnode is in */
  2385. int epbs; /* shift of number of dnodes in a block */
  2386. int idx; /* index within a block */
  2387. void *dnbuf;
  2388. grub_err_t err;
  2389. grub_zfs_endian_t endian;
  2390. objnum &= DNODE_NUM_MASK;
  2391. blksz = grub_zfs_to_cpu16 (mdn->dn.dn_datablkszsec,
  2392. mdn->endian) << SPA_MINBLOCKSHIFT;
  2393. epbs = zfs_log2 (blksz) - DNODE_SHIFT;
  2394. /* While this should never happen, we should check that epbs is not negative. */
  2395. if (epbs < 0)
  2396. epbs = 0;
  2397. blkid = objnum >> epbs;
  2398. idx = objnum & ((1 << epbs) - 1);
  2399. if (data->dnode_buf != NULL && grub_memcmp (data->dnode_mdn, &mdn->dn,
  2400. sizeof (mdn->dn)) == 0
  2401. && objnum >= data->dnode_start && objnum < data->dnode_end)
  2402. {
  2403. grub_memmove (&(buf->dn), &(data->dnode_buf)[idx], DNODE_SIZE);
  2404. buf->endian = data->dnode_endian;
  2405. if (type && buf->dn.dn_type != type)
  2406. return grub_error(GRUB_ERR_BAD_FS, "incorrect dnode type");
  2407. return GRUB_ERR_NONE;
  2408. }
  2409. grub_dprintf ("zfs", "endian = %d, blkid=%llx\n", mdn->endian,
  2410. (unsigned long long) blkid);
  2411. err = dmu_read (mdn, blkid, &dnbuf, &endian, data);
  2412. if (err)
  2413. return err;
  2414. grub_dprintf ("zfs", "alive\n");
  2415. grub_free (data->dnode_buf);
  2416. grub_free (data->dnode_mdn);
  2417. data->dnode_mdn = grub_malloc (sizeof (*mdn));
  2418. if (! data->dnode_mdn)
  2419. {
  2420. grub_errno = GRUB_ERR_NONE;
  2421. data->dnode_buf = 0;
  2422. }
  2423. else
  2424. {
  2425. grub_memcpy (data->dnode_mdn, mdn, sizeof (*mdn));
  2426. data->dnode_buf = dnbuf;
  2427. data->dnode_start = blkid << epbs;
  2428. data->dnode_end = (blkid + 1) << epbs;
  2429. data->dnode_endian = endian;
  2430. }
  2431. grub_memmove (&(buf->dn), (dnode_phys_t *) dnbuf + idx, DNODE_SIZE);
  2432. buf->endian = endian;
  2433. if (type && buf->dn.dn_type != type)
  2434. return grub_error(GRUB_ERR_BAD_FS, "incorrect dnode type");
  2435. return GRUB_ERR_NONE;
  2436. }
  2437. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  2438. /*
  2439. * Get the file dnode for a given file name where mdn is the meta dnode
  2440. * for this ZFS object set. When found, place the file dnode in dn.
  2441. * The 'path' argument will be mangled.
  2442. *
  2443. */
  2444. static grub_err_t
  2445. dnode_get_path (struct subvolume *subvol, const char *path_in, dnode_end_t *dn,
  2446. struct grub_zfs_data *data)
  2447. {
  2448. grub_uint64_t objnum, version;
  2449. char *cname, ch;
  2450. grub_err_t err = GRUB_ERR_NONE;
  2451. char *path, *path_buf;
  2452. struct dnode_chain
  2453. {
  2454. struct dnode_chain *next;
  2455. dnode_end_t dn;
  2456. };
  2457. struct dnode_chain *dnode_path = 0, *dn_new, *root;
  2458. dn_new = grub_malloc (sizeof (*dn_new));
  2459. if (! dn_new)
  2460. return grub_errno;
  2461. dn_new->next = 0;
  2462. dnode_path = root = dn_new;
  2463. err = dnode_get (&subvol->mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
  2464. &(dnode_path->dn), data);
  2465. if (err)
  2466. {
  2467. grub_free (dn_new);
  2468. return err;
  2469. }
  2470. err = zap_lookup (&(dnode_path->dn), ZPL_VERSION_STR, &version,
  2471. data, 0);
  2472. if (err)
  2473. {
  2474. grub_free (dn_new);
  2475. return err;
  2476. }
  2477. if (version > ZPL_VERSION)
  2478. {
  2479. grub_free (dn_new);
  2480. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "too new ZPL version");
  2481. }
  2482. err = zap_lookup (&(dnode_path->dn), "casesensitivity",
  2483. &subvol->case_insensitive,
  2484. data, 0);
  2485. if (err == GRUB_ERR_FILE_NOT_FOUND)
  2486. {
  2487. grub_errno = GRUB_ERR_NONE;
  2488. subvol->case_insensitive = 0;
  2489. }
  2490. err = zap_lookup (&(dnode_path->dn), ZFS_ROOT_OBJ, &objnum, data, 0);
  2491. if (err)
  2492. {
  2493. grub_free (dn_new);
  2494. return err;
  2495. }
  2496. err = dnode_get (&subvol->mdn, objnum, 0, &(dnode_path->dn), data);
  2497. if (err)
  2498. {
  2499. grub_free (dn_new);
  2500. return err;
  2501. }
  2502. path = path_buf = grub_strdup (path_in);
  2503. if (!path_buf)
  2504. {
  2505. grub_free (dn_new);
  2506. return grub_errno;
  2507. }
  2508. while (1)
  2509. {
  2510. /* skip leading slashes */
  2511. while (*path == '/')
  2512. path++;
  2513. if (!*path)
  2514. break;
  2515. /* get the next component name */
  2516. cname = path;
  2517. while (*path && *path != '/')
  2518. path++;
  2519. /* Skip dot. */
  2520. if (cname + 1 == path && cname[0] == '.')
  2521. continue;
  2522. /* Handle double dot. */
  2523. if (cname + 2 == path && cname[0] == '.' && cname[1] == '.')
  2524. {
  2525. if (dn_new->next)
  2526. {
  2527. dn_new = dnode_path;
  2528. dnode_path = dn_new->next;
  2529. grub_free (dn_new);
  2530. }
  2531. else
  2532. {
  2533. err = grub_error (GRUB_ERR_FILE_NOT_FOUND,
  2534. "can't resolve ..");
  2535. break;
  2536. }
  2537. continue;
  2538. }
  2539. ch = *path;
  2540. *path = 0; /* ensure null termination */
  2541. if (dnode_path->dn.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS)
  2542. {
  2543. err = grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  2544. break;
  2545. }
  2546. err = zap_lookup (&(dnode_path->dn), cname, &objnum,
  2547. data, subvol->case_insensitive);
  2548. if (err)
  2549. break;
  2550. dn_new = grub_malloc (sizeof (*dn_new));
  2551. if (! dn_new)
  2552. {
  2553. err = grub_errno;
  2554. break;
  2555. }
  2556. dn_new->next = dnode_path;
  2557. dnode_path = dn_new;
  2558. objnum = ZFS_DIRENT_OBJ (objnum);
  2559. err = dnode_get (&subvol->mdn, objnum, 0, &(dnode_path->dn), data);
  2560. if (err)
  2561. break;
  2562. *path = ch;
  2563. if (dnode_path->dn.dn.dn_bonustype == DMU_OT_ZNODE
  2564. && ((grub_zfs_to_cpu64(((znode_phys_t *) DN_BONUS (&dnode_path->dn.dn))->zp_mode, dnode_path->dn.endian) >> 12) & 0xf) == 0xa)
  2565. {
  2566. char *sym_value;
  2567. grub_size_t sym_sz;
  2568. int free_symval = 0;
  2569. char *oldpath = path, *oldpathbuf = path_buf;
  2570. sym_value = ((char *) DN_BONUS (&dnode_path->dn.dn) + sizeof (struct znode_phys));
  2571. sym_sz = grub_zfs_to_cpu64 (((znode_phys_t *) DN_BONUS (&dnode_path->dn.dn))->zp_size, dnode_path->dn.endian);
  2572. if (dnode_path->dn.dn.dn_flags & 1)
  2573. {
  2574. grub_size_t block;
  2575. grub_size_t blksz;
  2576. blksz = (grub_zfs_to_cpu16 (dnode_path->dn.dn.dn_datablkszsec,
  2577. dnode_path->dn.endian)
  2578. << SPA_MINBLOCKSHIFT);
  2579. if (blksz == 0)
  2580. {
  2581. err = grub_error (GRUB_ERR_BAD_FS, "0-sized block");
  2582. break;
  2583. }
  2584. sym_value = grub_malloc (sym_sz);
  2585. if (!sym_value)
  2586. {
  2587. err = grub_errno;
  2588. break;
  2589. }
  2590. for (block = 0; block < (sym_sz + blksz - 1) / blksz; block++)
  2591. {
  2592. void *t;
  2593. grub_size_t movesize;
  2594. err = dmu_read (&(dnode_path->dn), block, &t, 0, data);
  2595. if (err)
  2596. {
  2597. grub_free (sym_value);
  2598. break;
  2599. }
  2600. movesize = sym_sz - block * blksz;
  2601. if (movesize > blksz)
  2602. movesize = blksz;
  2603. grub_memcpy (sym_value + block * blksz, t, movesize);
  2604. grub_free (t);
  2605. }
  2606. if (err)
  2607. break;
  2608. free_symval = 1;
  2609. }
  2610. path = path_buf = grub_malloc (sym_sz + grub_strlen (oldpath) + 1);
  2611. if (!path_buf)
  2612. {
  2613. grub_free (oldpathbuf);
  2614. if (free_symval)
  2615. grub_free (sym_value);
  2616. err = grub_errno;
  2617. break;
  2618. }
  2619. grub_memcpy (path, sym_value, sym_sz);
  2620. if (free_symval)
  2621. grub_free (sym_value);
  2622. path [sym_sz] = 0;
  2623. grub_memcpy (path + grub_strlen (path), oldpath,
  2624. grub_strlen (oldpath) + 1);
  2625. grub_free (oldpathbuf);
  2626. /* Restart dnode walk using path of symlink. */
  2627. if (path[0] != '/')
  2628. {
  2629. dn_new = dnode_path;
  2630. dnode_path = dn_new->next;
  2631. grub_free (dn_new);
  2632. }
  2633. else while (dnode_path != root)
  2634. {
  2635. dn_new = dnode_path;
  2636. dnode_path = dn_new->next;
  2637. grub_free (dn_new);
  2638. }
  2639. dn_new = dnode_path;
  2640. }
  2641. if (dnode_path->dn.dn.dn_bonustype == DMU_OT_SA)
  2642. {
  2643. void *sahdrp;
  2644. int hdrsize;
  2645. if (dnode_path->dn.dn.dn_bonuslen != 0)
  2646. {
  2647. sahdrp = DN_BONUS (&dnode_path->dn.dn);
  2648. }
  2649. else if (dnode_path->dn.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR)
  2650. {
  2651. blkptr_t *bp = &dnode_path->dn.dn.dn_spill;
  2652. err = zio_read (bp, dnode_path->dn.endian, &sahdrp, NULL, data);
  2653. if (err)
  2654. break;
  2655. }
  2656. else
  2657. {
  2658. err = grub_error (GRUB_ERR_BAD_FS, "filesystem is corrupt");
  2659. break;
  2660. }
  2661. hdrsize = SA_HDR_SIZE (((sa_hdr_phys_t *) sahdrp));
  2662. if (((grub_zfs_to_cpu64 (grub_get_unaligned64 ((char *) sahdrp
  2663. + hdrsize
  2664. + SA_TYPE_OFFSET),
  2665. dnode_path->dn.endian) >> 12) & 0xf) == 0xa)
  2666. {
  2667. char *sym_value = (char *) sahdrp + hdrsize + SA_SYMLINK_OFFSET;
  2668. grub_size_t sym_sz =
  2669. grub_zfs_to_cpu64 (grub_get_unaligned64 ((char *) sahdrp
  2670. + hdrsize
  2671. + SA_SIZE_OFFSET),
  2672. dnode_path->dn.endian);
  2673. char *oldpath = path, *oldpathbuf = path_buf;
  2674. path = path_buf = grub_malloc (sym_sz + grub_strlen (oldpath) + 1);
  2675. if (!path_buf)
  2676. {
  2677. grub_free (oldpathbuf);
  2678. err = grub_errno;
  2679. break;
  2680. }
  2681. grub_memcpy (path, sym_value, sym_sz);
  2682. path [sym_sz] = 0;
  2683. grub_memcpy (path + grub_strlen (path), oldpath,
  2684. grub_strlen (oldpath) + 1);
  2685. grub_free (oldpathbuf);
  2686. /* Restart dnode walk using path of symlink. */
  2687. if (path[0] != '/')
  2688. {
  2689. dn_new = dnode_path;
  2690. dnode_path = dn_new->next;
  2691. grub_free (dn_new);
  2692. }
  2693. else while (dnode_path != root)
  2694. {
  2695. dn_new = dnode_path;
  2696. dnode_path = dn_new->next;
  2697. grub_free (dn_new);
  2698. }
  2699. dn_new = dnode_path;
  2700. }
  2701. }
  2702. }
  2703. if (!err)
  2704. grub_memcpy (dn, &(dnode_path->dn), sizeof (*dn));
  2705. while (dnode_path)
  2706. {
  2707. dn_new = dnode_path->next;
  2708. grub_free (dnode_path);
  2709. dnode_path = dn_new;
  2710. }
  2711. grub_free (path_buf);
  2712. return err;
  2713. }
  2714. #if 0
  2715. /*
  2716. * Get the default 'bootfs' property value from the rootpool.
  2717. *
  2718. */
  2719. static grub_err_t
  2720. get_default_bootfsobj (dnode_phys_t * mosmdn, grub_uint64_t * obj,
  2721. struct grub_zfs_data *data)
  2722. {
  2723. grub_uint64_t objnum = 0;
  2724. dnode_phys_t *dn;
  2725. if (!dn)
  2726. return grub_errno;
  2727. if ((grub_errno = dnode_get (mosmdn, DMU_POOL_DIRECTORY_OBJECT,
  2728. DMU_OT_OBJECT_DIRECTORY, dn, data)))
  2729. {
  2730. grub_free (dn);
  2731. return (grub_errno);
  2732. }
  2733. /*
  2734. * find the object number for 'pool_props', and get the dnode
  2735. * of the 'pool_props'.
  2736. */
  2737. if (zap_lookup (dn, DMU_POOL_PROPS, &objnum, data))
  2738. {
  2739. grub_free (dn);
  2740. return (GRUB_ERR_BAD_FS);
  2741. }
  2742. if ((grub_errno = dnode_get (mosmdn, objnum, DMU_OT_POOL_PROPS, dn, data)))
  2743. {
  2744. grub_free (dn);
  2745. return (grub_errno);
  2746. }
  2747. if (zap_lookup (dn, ZPOOL_PROP_BOOTFS, &objnum, data))
  2748. {
  2749. grub_free (dn);
  2750. return (GRUB_ERR_BAD_FS);
  2751. }
  2752. if (!objnum)
  2753. {
  2754. grub_free (dn);
  2755. return (GRUB_ERR_BAD_FS);
  2756. }
  2757. *obj = objnum;
  2758. return (0);
  2759. }
  2760. #endif
  2761. /*
  2762. * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
  2763. * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
  2764. * of pool/rootfs.
  2765. *
  2766. * If no fsname and no obj are given, return the DSL_DIR metadnode.
  2767. * If fsname is given, return its metadnode and its matching object number.
  2768. * If only obj is given, return the metadnode for this object number.
  2769. *
  2770. */
  2771. static grub_err_t
  2772. get_filesystem_dnode (dnode_end_t * mosmdn, char *fsname,
  2773. dnode_end_t * mdn, struct grub_zfs_data *data)
  2774. {
  2775. grub_uint64_t objnum;
  2776. grub_err_t err;
  2777. grub_dprintf ("zfs", "endian = %d\n", mosmdn->endian);
  2778. err = dnode_get (mosmdn, DMU_POOL_DIRECTORY_OBJECT,
  2779. DMU_OT_OBJECT_DIRECTORY, mdn, data);
  2780. if (err)
  2781. return err;
  2782. grub_dprintf ("zfs", "alive\n");
  2783. err = zap_lookup (mdn, DMU_POOL_ROOT_DATASET, &objnum, data, 0);
  2784. if (err)
  2785. return err;
  2786. grub_dprintf ("zfs", "alive\n");
  2787. err = dnode_get (mosmdn, objnum, 0, mdn, data);
  2788. if (err)
  2789. return err;
  2790. grub_dprintf ("zfs", "alive\n");
  2791. while (*fsname)
  2792. {
  2793. grub_uint64_t childobj;
  2794. char *cname, ch;
  2795. while (*fsname == '/')
  2796. fsname++;
  2797. if (! *fsname || *fsname == '@')
  2798. break;
  2799. cname = fsname;
  2800. while (*fsname && *fsname != '/')
  2801. fsname++;
  2802. ch = *fsname;
  2803. *fsname = 0;
  2804. childobj = grub_zfs_to_cpu64 ((((dsl_dir_phys_t *) DN_BONUS (&mdn->dn)))->dd_child_dir_zapobj, mdn->endian);
  2805. err = dnode_get (mosmdn, childobj,
  2806. DMU_OT_DSL_DIR_CHILD_MAP, mdn, data);
  2807. if (err)
  2808. return err;
  2809. err = zap_lookup (mdn, cname, &objnum, data, 0);
  2810. if (err)
  2811. return err;
  2812. err = dnode_get (mosmdn, objnum, 0, mdn, data);
  2813. if (err)
  2814. return err;
  2815. *fsname = ch;
  2816. }
  2817. return GRUB_ERR_NONE;
  2818. }
  2819. static grub_err_t
  2820. make_mdn (dnode_end_t * mdn, struct grub_zfs_data *data)
  2821. {
  2822. objset_phys_t *osp;
  2823. blkptr_t *bp;
  2824. grub_size_t ospsize = 0;
  2825. grub_err_t err;
  2826. grub_dprintf ("zfs", "endian = %d\n", mdn->endian);
  2827. bp = &(((dsl_dataset_phys_t *) DN_BONUS (&mdn->dn))->ds_bp);
  2828. err = zio_read (bp, mdn->endian, (void **) &osp, &ospsize, data);
  2829. if (err)
  2830. return err;
  2831. if (ospsize < OBJSET_PHYS_SIZE_V14)
  2832. {
  2833. grub_free (osp);
  2834. return grub_error (GRUB_ERR_BAD_FS, "too small osp");
  2835. }
  2836. mdn->endian = (grub_zfs_to_cpu64 (bp->blk_prop, mdn->endian)>>63) & 1;
  2837. grub_memmove ((char *) &(mdn->dn),
  2838. (char *) &(osp)->os_meta_dnode, DNODE_SIZE);
  2839. grub_free (osp);
  2840. return GRUB_ERR_NONE;
  2841. }
  2842. /* Context for dnode_get_fullpath. */
  2843. struct dnode_get_fullpath_ctx
  2844. {
  2845. struct subvolume *subvol;
  2846. grub_uint64_t salt;
  2847. int keyn;
  2848. };
  2849. /* Helper for dnode_get_fullpath. */
  2850. static int
  2851. count_zap_keys (const void *name __attribute__ ((unused)),
  2852. grub_size_t namelen __attribute__ ((unused)),
  2853. const void *val_in __attribute__ ((unused)),
  2854. grub_size_t nelem __attribute__ ((unused)),
  2855. grub_size_t elemsize __attribute__ ((unused)),
  2856. void *data)
  2857. {
  2858. struct dnode_get_fullpath_ctx *ctx = data;
  2859. ctx->subvol->nkeys++;
  2860. return 0;
  2861. }
  2862. /* Helper for dnode_get_fullpath. */
  2863. static int
  2864. load_zap_key (const void *name, grub_size_t namelen, const void *val_in,
  2865. grub_size_t nelem, grub_size_t elemsize, void *data)
  2866. {
  2867. struct dnode_get_fullpath_ctx *ctx = data;
  2868. if (namelen != 1)
  2869. {
  2870. grub_dprintf ("zfs", "Unexpected key index size %" PRIuGRUB_SIZE "\n",
  2871. namelen);
  2872. return 0;
  2873. }
  2874. if (elemsize != 1)
  2875. {
  2876. grub_dprintf ("zfs", "Unexpected key element size %" PRIuGRUB_SIZE "\n",
  2877. elemsize);
  2878. return 0;
  2879. }
  2880. ctx->subvol->keyring[ctx->keyn].txg =
  2881. grub_be_to_cpu64 (*(grub_uint64_t *) name);
  2882. ctx->subvol->keyring[ctx->keyn].algo =
  2883. grub_le_to_cpu64 (*(grub_uint64_t *) val_in);
  2884. ctx->subvol->keyring[ctx->keyn].cipher =
  2885. grub_zfs_load_key (val_in, nelem, ctx->salt,
  2886. ctx->subvol->keyring[ctx->keyn].algo);
  2887. ctx->keyn++;
  2888. return 0;
  2889. }
  2890. static grub_err_t
  2891. dnode_get_fullpath (const char *fullpath, struct subvolume *subvol,
  2892. dnode_end_t * dn, int *isfs,
  2893. struct grub_zfs_data *data)
  2894. {
  2895. char *fsname, *snapname;
  2896. const char *ptr_at, *filename;
  2897. grub_uint64_t headobj;
  2898. grub_uint64_t keychainobj;
  2899. grub_err_t err;
  2900. ptr_at = grub_strchr (fullpath, '@');
  2901. if (! ptr_at)
  2902. {
  2903. *isfs = 1;
  2904. filename = 0;
  2905. snapname = 0;
  2906. fsname = grub_strdup (fullpath);
  2907. }
  2908. else
  2909. {
  2910. const char *ptr_slash = grub_strchr (ptr_at, '/');
  2911. *isfs = 0;
  2912. fsname = grub_malloc (ptr_at - fullpath + 1);
  2913. if (!fsname)
  2914. return grub_errno;
  2915. grub_memcpy (fsname, fullpath, ptr_at - fullpath);
  2916. fsname[ptr_at - fullpath] = 0;
  2917. if (ptr_at[1] && ptr_at[1] != '/')
  2918. {
  2919. snapname = grub_malloc (ptr_slash - ptr_at);
  2920. if (!snapname)
  2921. {
  2922. grub_free (fsname);
  2923. return grub_errno;
  2924. }
  2925. grub_memcpy (snapname, ptr_at + 1, ptr_slash - ptr_at - 1);
  2926. snapname[ptr_slash - ptr_at - 1] = 0;
  2927. }
  2928. else
  2929. snapname = 0;
  2930. if (ptr_slash)
  2931. filename = ptr_slash;
  2932. else
  2933. filename = "/";
  2934. grub_dprintf ("zfs", "fsname = '%s' snapname='%s' filename = '%s'\n",
  2935. fsname, snapname, filename);
  2936. }
  2937. grub_dprintf ("zfs", "alive\n");
  2938. err = get_filesystem_dnode (&(data->mos), fsname, dn, data);
  2939. if (err)
  2940. {
  2941. grub_free (fsname);
  2942. grub_free (snapname);
  2943. return err;
  2944. }
  2945. grub_dprintf ("zfs", "alive\n");
  2946. headobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&dn->dn))->dd_head_dataset_obj, dn->endian);
  2947. grub_dprintf ("zfs", "endian = %d\n", subvol->mdn.endian);
  2948. err = dnode_get (&(data->mos), headobj, 0, &subvol->mdn, data);
  2949. if (err)
  2950. {
  2951. grub_free (fsname);
  2952. grub_free (snapname);
  2953. return err;
  2954. }
  2955. grub_dprintf ("zfs", "endian = %d\n", subvol->mdn.endian);
  2956. keychainobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&dn->dn))->keychain, dn->endian);
  2957. if (grub_zfs_load_key && keychainobj)
  2958. {
  2959. struct dnode_get_fullpath_ctx ctx = {
  2960. .subvol = subvol,
  2961. .keyn = 0
  2962. };
  2963. dnode_end_t keychain_dn, props_dn;
  2964. grub_uint64_t propsobj;
  2965. propsobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&dn->dn))->dd_props_zapobj, dn->endian);
  2966. err = dnode_get (&(data->mos), propsobj, DMU_OT_DSL_PROPS,
  2967. &props_dn, data);
  2968. if (err)
  2969. {
  2970. grub_free (fsname);
  2971. grub_free (snapname);
  2972. return err;
  2973. }
  2974. err = zap_lookup (&props_dn, "salt", &ctx.salt, data, 0);
  2975. if (err == GRUB_ERR_FILE_NOT_FOUND)
  2976. {
  2977. err = 0;
  2978. grub_errno = 0;
  2979. ctx.salt = 0;
  2980. }
  2981. if (err)
  2982. {
  2983. grub_dprintf ("zfs", "failed here\n");
  2984. return err;
  2985. }
  2986. err = dnode_get (&(data->mos), keychainobj, DMU_OT_DSL_KEYCHAIN,
  2987. &keychain_dn, data);
  2988. if (err)
  2989. {
  2990. grub_free (fsname);
  2991. grub_free (snapname);
  2992. return err;
  2993. }
  2994. subvol->nkeys = 0;
  2995. zap_iterate (&keychain_dn, 8, count_zap_keys, &ctx, data);
  2996. subvol->keyring = grub_calloc (subvol->nkeys, sizeof (subvol->keyring[0]));
  2997. if (!subvol->keyring)
  2998. {
  2999. grub_free (fsname);
  3000. grub_free (snapname);
  3001. return err;
  3002. }
  3003. zap_iterate (&keychain_dn, 8, load_zap_key, &ctx, data);
  3004. }
  3005. if (snapname)
  3006. {
  3007. grub_uint64_t snapobj;
  3008. snapobj = grub_zfs_to_cpu64 (((dsl_dataset_phys_t *) DN_BONUS (&subvol->mdn.dn))->ds_snapnames_zapobj, subvol->mdn.endian);
  3009. err = dnode_get (&(data->mos), snapobj,
  3010. DMU_OT_DSL_DS_SNAP_MAP, &subvol->mdn, data);
  3011. if (!err)
  3012. err = zap_lookup (&subvol->mdn, snapname, &headobj, data, 0);
  3013. if (!err)
  3014. err = dnode_get (&(data->mos), headobj, 0,
  3015. &subvol->mdn, data);
  3016. if (!err && subvol->mdn.dn.dn_type != DMU_OT_DSL_DATASET && subvol->mdn.dn.dn_bonustype != DMU_OT_DSL_DATASET)
  3017. return grub_error(GRUB_ERR_BAD_FS, "incorrect dataset dnode type");
  3018. if (err)
  3019. {
  3020. grub_free (fsname);
  3021. grub_free (snapname);
  3022. return err;
  3023. }
  3024. }
  3025. subvol->obj = headobj;
  3026. make_mdn (&subvol->mdn, data);
  3027. grub_dprintf ("zfs", "endian = %d\n", subvol->mdn.endian);
  3028. if (*isfs)
  3029. {
  3030. grub_free (fsname);
  3031. grub_free (snapname);
  3032. return GRUB_ERR_NONE;
  3033. }
  3034. err = dnode_get_path (subvol, filename, dn, data);
  3035. grub_free (fsname);
  3036. grub_free (snapname);
  3037. return err;
  3038. }
  3039. static int
  3040. nvlist_find_value (const char *nvlist_in, const char *name,
  3041. int valtype, char **val,
  3042. grub_size_t *size_out, grub_size_t *nelm_out)
  3043. {
  3044. grub_size_t nvp_name_len, name_len = grub_strlen(name);
  3045. int type;
  3046. const char *nvpair=NULL,*nvlist=nvlist_in;
  3047. char *nvp_name;
  3048. /* Verify if the 1st and 2nd byte in the nvlist are valid. */
  3049. /* NOTE: independently of what endianness header announces all
  3050. subsequent values are big-endian. */
  3051. if (nvlist[0] != NV_ENCODE_XDR || (nvlist[1] != NV_LITTLE_ENDIAN
  3052. && nvlist[1] != NV_BIG_ENDIAN))
  3053. {
  3054. grub_dprintf ("zfs", "incorrect nvlist header\n");
  3055. grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist");
  3056. return 0;
  3057. }
  3058. /*
  3059. * Loop thru the nvpair list
  3060. * The XDR representation of an integer is in big-endian byte order.
  3061. */
  3062. while ((nvpair=nvlist_next_nvpair(nvlist,nvpair)))
  3063. {
  3064. nvpair_name(nvpair,&nvp_name, &nvp_name_len);
  3065. type = nvpair_type(nvpair);
  3066. if (type == valtype
  3067. && (nvp_name_len == name_len
  3068. || (nvp_name_len > name_len && nvp_name[name_len] == '\0'))
  3069. && grub_memcmp (nvp_name, name, name_len) == 0)
  3070. {
  3071. return nvpair_value(nvpair,val,size_out,nelm_out);
  3072. }
  3073. }
  3074. return 0;
  3075. }
  3076. int
  3077. grub_zfs_nvlist_lookup_uint64 (const char *nvlist, const char *name,
  3078. grub_uint64_t * out)
  3079. {
  3080. char *nvpair;
  3081. grub_size_t size;
  3082. int found;
  3083. found = nvlist_find_value (nvlist, name, DATA_TYPE_UINT64, &nvpair, &size, 0);
  3084. if (!found)
  3085. return 0;
  3086. if (size < sizeof (grub_uint64_t))
  3087. {
  3088. grub_error (GRUB_ERR_BAD_FS, "invalid uint64");
  3089. return 0;
  3090. }
  3091. *out = grub_be_to_cpu64 (grub_get_unaligned64 (nvpair));
  3092. return 1;
  3093. }
  3094. char *
  3095. grub_zfs_nvlist_lookup_string (const char *nvlist, const char *name)
  3096. {
  3097. char *nvpair;
  3098. char *ret;
  3099. grub_size_t slen;
  3100. grub_size_t size;
  3101. int found;
  3102. found = nvlist_find_value (nvlist, name, DATA_TYPE_STRING, &nvpair, &size, 0);
  3103. if (!found)
  3104. return 0;
  3105. if (size < 4)
  3106. {
  3107. grub_error (GRUB_ERR_BAD_FS, "invalid string");
  3108. return 0;
  3109. }
  3110. slen = grub_be_to_cpu32 (grub_get_unaligned32 (nvpair));
  3111. if (slen > size - 4)
  3112. slen = size - 4;
  3113. ret = grub_malloc (slen + 1);
  3114. if (!ret)
  3115. return 0;
  3116. grub_memcpy (ret, nvpair + 4, slen);
  3117. ret[slen] = 0;
  3118. return ret;
  3119. }
  3120. char *
  3121. grub_zfs_nvlist_lookup_nvlist (const char *nvlist, const char *name)
  3122. {
  3123. char *nvpair;
  3124. char *ret;
  3125. grub_size_t size, sz;
  3126. int found;
  3127. found = nvlist_find_value (nvlist, name, DATA_TYPE_NVLIST, &nvpair,
  3128. &size, 0);
  3129. if (!found)
  3130. return 0;
  3131. if (grub_add (size, 3 * sizeof (grub_uint32_t), &sz))
  3132. return 0;
  3133. ret = grub_zalloc (sz);
  3134. if (!ret)
  3135. return 0;
  3136. grub_memcpy (ret, nvlist, sizeof (grub_uint32_t));
  3137. grub_memcpy (ret + sizeof (grub_uint32_t), nvpair, size);
  3138. return ret;
  3139. }
  3140. int
  3141. grub_zfs_nvlist_lookup_nvlist_array_get_nelm (const char *nvlist,
  3142. const char *name)
  3143. {
  3144. char *nvpair;
  3145. grub_size_t nelm, size;
  3146. int found;
  3147. found = nvlist_find_value (nvlist, name, DATA_TYPE_NVLIST_ARRAY, &nvpair,
  3148. &size, &nelm);
  3149. if (! found)
  3150. return -1;
  3151. return nelm;
  3152. }
  3153. static int
  3154. get_nvlist_size (const char *beg, const char *limit)
  3155. {
  3156. const char *ptr;
  3157. grub_uint32_t encode_size;
  3158. ptr = beg + 8;
  3159. while (ptr < limit
  3160. && (encode_size = grub_be_to_cpu32 (grub_get_unaligned32 (ptr))))
  3161. ptr += encode_size; /* goto the next nvpair */
  3162. ptr += 8;
  3163. return (ptr > limit) ? -1 : (ptr - beg);
  3164. }
  3165. char *
  3166. grub_zfs_nvlist_lookup_nvlist_array (const char *nvlist, const char *name,
  3167. grub_size_t index)
  3168. {
  3169. char *nvpair, *nvpairptr;
  3170. int found;
  3171. char *ret;
  3172. grub_size_t size;
  3173. unsigned i;
  3174. grub_size_t nelm;
  3175. int elemsize = 0;
  3176. found = nvlist_find_value (nvlist, name, DATA_TYPE_NVLIST_ARRAY, &nvpair,
  3177. &size, &nelm);
  3178. if (!found)
  3179. return 0;
  3180. if (index >= nelm)
  3181. {
  3182. grub_error (GRUB_ERR_OUT_OF_RANGE, "trying to lookup past nvlist array");
  3183. return 0;
  3184. }
  3185. nvpairptr = nvpair;
  3186. for (i = 0; i < index; i++)
  3187. {
  3188. int r;
  3189. r = get_nvlist_size (nvpairptr, nvpair + size);
  3190. if (r < 0)
  3191. {
  3192. grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist array");
  3193. return NULL;
  3194. }
  3195. nvpairptr += r;
  3196. }
  3197. elemsize = get_nvlist_size (nvpairptr, nvpair + size);
  3198. if (elemsize < 0)
  3199. {
  3200. grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist array");
  3201. return 0;
  3202. }
  3203. ret = grub_zalloc (elemsize + sizeof (grub_uint32_t));
  3204. if (!ret)
  3205. return 0;
  3206. grub_memcpy (ret, nvlist, sizeof (grub_uint32_t));
  3207. grub_memcpy (ret + sizeof (grub_uint32_t), nvpairptr, elemsize);
  3208. return ret;
  3209. }
  3210. static void
  3211. unmount_device (struct grub_zfs_device_desc *desc)
  3212. {
  3213. unsigned i;
  3214. switch (desc->type)
  3215. {
  3216. case DEVICE_LEAF:
  3217. if (!desc->original && desc->dev)
  3218. grub_device_close (desc->dev);
  3219. return;
  3220. case DEVICE_RAIDZ:
  3221. case DEVICE_MIRROR:
  3222. for (i = 0; i < desc->n_children; i++)
  3223. unmount_device (&desc->children[i]);
  3224. grub_free (desc->children);
  3225. return;
  3226. }
  3227. }
  3228. static void
  3229. zfs_unmount (struct grub_zfs_data *data)
  3230. {
  3231. unsigned i;
  3232. for (i = 0; i < data->n_devices_attached; i++)
  3233. unmount_device (&data->devices_attached[i]);
  3234. grub_free (data->devices_attached);
  3235. grub_free (data->dnode_buf);
  3236. grub_free (data->dnode_mdn);
  3237. grub_free (data->file_buf);
  3238. for (i = 0; i < data->subvol.nkeys; i++)
  3239. grub_crypto_cipher_close (data->subvol.keyring[i].cipher);
  3240. grub_free (data->subvol.keyring);
  3241. grub_free (data);
  3242. }
  3243. /*
  3244. * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
  3245. * to the memory address MOS.
  3246. *
  3247. */
  3248. static struct grub_zfs_data *
  3249. zfs_mount (grub_device_t dev)
  3250. {
  3251. struct grub_zfs_data *data = 0;
  3252. grub_err_t err;
  3253. objset_phys_t *osp = 0;
  3254. grub_size_t ospsize;
  3255. grub_zfs_endian_t ub_endian = GRUB_ZFS_UNKNOWN_ENDIAN;
  3256. uberblock_t *ub;
  3257. int inserted;
  3258. if (! dev->disk)
  3259. {
  3260. grub_error (GRUB_ERR_BAD_DEVICE, "not a disk");
  3261. return 0;
  3262. }
  3263. data = grub_zalloc (sizeof (*data));
  3264. if (!data)
  3265. return 0;
  3266. #if 0
  3267. /* if it's our first time here, zero the best uberblock out */
  3268. if (data->best_drive == 0 && data->best_part == 0 && find_best_root)
  3269. grub_memset (&current_uberblock, 0, sizeof (uberblock_t));
  3270. #endif
  3271. data->n_devices_allocated = 16;
  3272. data->devices_attached = grub_malloc (sizeof (data->devices_attached[0])
  3273. * data->n_devices_allocated);
  3274. data->n_devices_attached = 0;
  3275. err = scan_disk (dev, data, 1, &inserted);
  3276. if (err)
  3277. {
  3278. zfs_unmount (data);
  3279. return NULL;
  3280. }
  3281. ub = &(data->current_uberblock);
  3282. ub_endian = (grub_zfs_to_cpu64 (ub->ub_magic,
  3283. GRUB_ZFS_LITTLE_ENDIAN) == UBERBLOCK_MAGIC
  3284. ? GRUB_ZFS_LITTLE_ENDIAN : GRUB_ZFS_BIG_ENDIAN);
  3285. err = zio_read (&ub->ub_rootbp, ub_endian,
  3286. (void **) &osp, &ospsize, data);
  3287. if (err)
  3288. {
  3289. zfs_unmount (data);
  3290. return NULL;
  3291. }
  3292. if (ospsize < OBJSET_PHYS_SIZE_V14)
  3293. {
  3294. grub_error (GRUB_ERR_BAD_FS, "OSP too small");
  3295. grub_free (osp);
  3296. zfs_unmount (data);
  3297. return NULL;
  3298. }
  3299. if (ub->ub_version >= SPA_VERSION_FEATURES &&
  3300. check_mos_features(&osp->os_meta_dnode, ub_endian, data) != 0)
  3301. {
  3302. grub_error (GRUB_ERR_BAD_FS, "Unsupported features in pool");
  3303. grub_free (osp);
  3304. zfs_unmount (data);
  3305. return NULL;
  3306. }
  3307. /* Got the MOS. Save it at the memory addr MOS. */
  3308. grub_memmove (&(data->mos.dn), &osp->os_meta_dnode, DNODE_SIZE);
  3309. data->mos.endian = (grub_zfs_to_cpu64 (ub->ub_rootbp.blk_prop,
  3310. ub_endian) >> 63) & 1;
  3311. grub_free (osp);
  3312. return data;
  3313. }
  3314. grub_err_t
  3315. grub_zfs_fetch_nvlist (grub_device_t dev, char **nvlist)
  3316. {
  3317. struct grub_zfs_data *zfs;
  3318. grub_err_t err;
  3319. zfs = zfs_mount (dev);
  3320. if (!zfs)
  3321. return grub_errno;
  3322. err = zfs_fetch_nvlist (zfs->device_original, nvlist);
  3323. zfs_unmount (zfs);
  3324. return err;
  3325. }
  3326. static grub_err_t
  3327. zfs_label (grub_device_t device, char **label)
  3328. {
  3329. char *nvlist;
  3330. grub_err_t err;
  3331. struct grub_zfs_data *data;
  3332. data = zfs_mount (device);
  3333. if (! data)
  3334. return grub_errno;
  3335. err = zfs_fetch_nvlist (data->device_original, &nvlist);
  3336. if (err)
  3337. {
  3338. zfs_unmount (data);
  3339. return err;
  3340. }
  3341. *label = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_POOL_NAME);
  3342. grub_free (nvlist);
  3343. zfs_unmount (data);
  3344. return grub_errno;
  3345. }
  3346. static grub_err_t
  3347. zfs_uuid (grub_device_t device, char **uuid)
  3348. {
  3349. struct grub_zfs_data *data;
  3350. *uuid = 0;
  3351. data = zfs_mount (device);
  3352. if (! data)
  3353. return grub_errno;
  3354. *uuid = grub_xasprintf ("%016llx", (long long unsigned) data->guid);
  3355. zfs_unmount (data);
  3356. if (! *uuid)
  3357. return grub_errno;
  3358. return GRUB_ERR_NONE;
  3359. }
  3360. static grub_err_t
  3361. zfs_mtime (grub_device_t device, grub_int64_t *mt)
  3362. {
  3363. struct grub_zfs_data *data;
  3364. grub_zfs_endian_t ub_endian = GRUB_ZFS_UNKNOWN_ENDIAN;
  3365. uberblock_t *ub;
  3366. *mt = 0;
  3367. data = zfs_mount (device);
  3368. if (! data)
  3369. return grub_errno;
  3370. ub = &(data->current_uberblock);
  3371. ub_endian = (grub_zfs_to_cpu64 (ub->ub_magic,
  3372. GRUB_ZFS_LITTLE_ENDIAN) == UBERBLOCK_MAGIC
  3373. ? GRUB_ZFS_LITTLE_ENDIAN : GRUB_ZFS_BIG_ENDIAN);
  3374. *mt = grub_zfs_to_cpu64 (ub->ub_timestamp, ub_endian);
  3375. zfs_unmount (data);
  3376. return GRUB_ERR_NONE;
  3377. }
  3378. /*
  3379. * zfs_open() locates a file in the rootpool by following the
  3380. * MOS and places the dnode of the file in the memory address DNODE.
  3381. */
  3382. static grub_err_t
  3383. grub_zfs_open (struct grub_file *file, const char *fsfilename)
  3384. {
  3385. struct grub_zfs_data *data;
  3386. grub_err_t err;
  3387. int isfs;
  3388. data = zfs_mount (file->device);
  3389. if (! data)
  3390. return grub_errno;
  3391. err = dnode_get_fullpath (fsfilename, &(data->subvol),
  3392. &(data->dnode), &isfs, data);
  3393. if (err)
  3394. {
  3395. zfs_unmount (data);
  3396. return err;
  3397. }
  3398. if (isfs)
  3399. {
  3400. zfs_unmount (data);
  3401. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("missing `%c' symbol"), '@');
  3402. }
  3403. /* We found the dnode for this file. Verify if it is a plain file. */
  3404. if (data->dnode.dn.dn_type != DMU_OT_PLAIN_FILE_CONTENTS)
  3405. {
  3406. zfs_unmount (data);
  3407. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a regular file"));
  3408. }
  3409. /* get the file size and set the file position to 0 */
  3410. /*
  3411. * For DMU_OT_SA we will need to locate the SIZE attribute
  3412. * attribute, which could be either in the bonus buffer
  3413. * or the "spill" block.
  3414. */
  3415. if (data->dnode.dn.dn_bonustype == DMU_OT_SA)
  3416. {
  3417. void *sahdrp;
  3418. int hdrsize;
  3419. if (data->dnode.dn.dn_bonuslen != 0)
  3420. {
  3421. sahdrp = (sa_hdr_phys_t *) DN_BONUS (&data->dnode.dn);
  3422. }
  3423. else if (data->dnode.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR)
  3424. {
  3425. blkptr_t *bp = &data->dnode.dn.dn_spill;
  3426. err = zio_read (bp, data->dnode.endian, &sahdrp, NULL, data);
  3427. if (err)
  3428. return err;
  3429. }
  3430. else
  3431. {
  3432. return grub_error (GRUB_ERR_BAD_FS, "filesystem is corrupt");
  3433. }
  3434. hdrsize = SA_HDR_SIZE (((sa_hdr_phys_t *) sahdrp));
  3435. file->size = grub_zfs_to_cpu64 (grub_get_unaligned64 ((char *) sahdrp + hdrsize + SA_SIZE_OFFSET), data->dnode.endian);
  3436. }
  3437. else if (data->dnode.dn.dn_bonustype == DMU_OT_ZNODE)
  3438. {
  3439. file->size = grub_zfs_to_cpu64 (((znode_phys_t *) DN_BONUS (&data->dnode.dn))->zp_size, data->dnode.endian);
  3440. }
  3441. else
  3442. return grub_error (GRUB_ERR_BAD_FS, "bad bonus type");
  3443. file->data = data;
  3444. file->offset = 0;
  3445. #ifndef GRUB_UTIL
  3446. grub_dl_ref (my_mod);
  3447. #endif
  3448. return GRUB_ERR_NONE;
  3449. }
  3450. static grub_ssize_t
  3451. grub_zfs_read (grub_file_t file, char *buf, grub_size_t len)
  3452. {
  3453. struct grub_zfs_data *data = (struct grub_zfs_data *) file->data;
  3454. grub_size_t blksz, movesize;
  3455. grub_size_t length;
  3456. grub_size_t read;
  3457. grub_err_t err;
  3458. /*
  3459. * If offset is in memory, move it into the buffer provided and return.
  3460. */
  3461. if (file->offset >= data->file_start
  3462. && file->offset + len <= data->file_end)
  3463. {
  3464. grub_memmove (buf, data->file_buf + file->offset - data->file_start,
  3465. len);
  3466. return len;
  3467. }
  3468. blksz = grub_zfs_to_cpu16 (data->dnode.dn.dn_datablkszsec,
  3469. data->dnode.endian) << SPA_MINBLOCKSHIFT;
  3470. if (blksz == 0)
  3471. {
  3472. grub_error (GRUB_ERR_BAD_FS, "0-sized block");
  3473. return -1;
  3474. }
  3475. /*
  3476. * Entire Dnode is too big to fit into the space available. We
  3477. * will need to read it in chunks. This could be optimized to
  3478. * read in as large a chunk as there is space available, but for
  3479. * now, this only reads in one data block at a time.
  3480. */
  3481. length = len;
  3482. read = 0;
  3483. while (length)
  3484. {
  3485. void *t;
  3486. /*
  3487. * Find requested blkid and the offset within that block.
  3488. */
  3489. grub_uint64_t blkid = grub_divmod64 (file->offset + read, blksz, 0);
  3490. grub_free (data->file_buf);
  3491. data->file_buf = 0;
  3492. err = dmu_read (&(data->dnode), blkid, &t,
  3493. 0, data);
  3494. data->file_buf = t;
  3495. if (err)
  3496. {
  3497. data->file_buf = NULL;
  3498. data->file_start = data->file_end = 0;
  3499. return -1;
  3500. }
  3501. data->file_start = blkid * blksz;
  3502. data->file_end = data->file_start + blksz;
  3503. movesize = data->file_end - file->offset - read;
  3504. if (movesize > length)
  3505. movesize = length;
  3506. grub_memmove (buf, data->file_buf + file->offset + read
  3507. - data->file_start, movesize);
  3508. buf += movesize;
  3509. length -= movesize;
  3510. read += movesize;
  3511. }
  3512. return len;
  3513. }
  3514. static grub_err_t
  3515. grub_zfs_close (grub_file_t file)
  3516. {
  3517. zfs_unmount ((struct grub_zfs_data *) file->data);
  3518. #ifndef GRUB_UTIL
  3519. grub_dl_unref (my_mod);
  3520. #endif
  3521. return GRUB_ERR_NONE;
  3522. }
  3523. grub_err_t
  3524. grub_zfs_getmdnobj (grub_device_t dev, const char *fsfilename,
  3525. grub_uint64_t *mdnobj)
  3526. {
  3527. struct grub_zfs_data *data;
  3528. grub_err_t err;
  3529. int isfs;
  3530. data = zfs_mount (dev);
  3531. if (! data)
  3532. return grub_errno;
  3533. err = dnode_get_fullpath (fsfilename, &(data->subvol),
  3534. &(data->dnode), &isfs, data);
  3535. *mdnobj = data->subvol.obj;
  3536. zfs_unmount (data);
  3537. return err;
  3538. }
  3539. /*
  3540. * Note: fill_fs_info() uses functions such as make_mdn() that modify
  3541. * the input dnode_end_t parameter. However, we should not allow it.
  3542. * Therefore, we are making mdn_in constant - fill_fs_info() makes
  3543. * a local copy of it.
  3544. */
  3545. static grub_err_t
  3546. fill_fs_info (struct grub_dirhook_info *info,
  3547. const dnode_end_t *mdn_in, struct grub_zfs_data *data)
  3548. {
  3549. grub_err_t err;
  3550. dnode_end_t dn;
  3551. grub_uint64_t objnum;
  3552. grub_uint64_t headobj;
  3553. dnode_end_t mdn;
  3554. grub_memcpy (&mdn, mdn_in, sizeof (*mdn_in));
  3555. grub_memset (info, 0, sizeof (*info));
  3556. info->dir = 1;
  3557. if (mdn.dn.dn_type == DMU_OT_DSL_DIR || mdn.dn.dn_bonustype == DMU_OT_DSL_DIR)
  3558. {
  3559. headobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&mdn.dn))->dd_head_dataset_obj, mdn.endian);
  3560. err = dnode_get (&(data->mos), headobj, 0, &mdn, data);
  3561. if (err)
  3562. {
  3563. grub_dprintf ("zfs", "failed here\n");
  3564. return err;
  3565. }
  3566. }
  3567. err = make_mdn (&mdn, data);
  3568. if (err)
  3569. return err;
  3570. err = dnode_get (&mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
  3571. &dn, data);
  3572. if (err)
  3573. {
  3574. grub_dprintf ("zfs", "failed here\n");
  3575. return err;
  3576. }
  3577. err = zap_lookup (&dn, ZFS_ROOT_OBJ, &objnum, data, 0);
  3578. if (err)
  3579. {
  3580. grub_dprintf ("zfs", "failed here\n");
  3581. return err;
  3582. }
  3583. err = dnode_get (&mdn, objnum, 0, &dn, data);
  3584. if (err)
  3585. {
  3586. grub_dprintf ("zfs", "failed here\n");
  3587. return err;
  3588. }
  3589. if (dn.dn.dn_bonustype == DMU_OT_SA)
  3590. {
  3591. void *sahdrp;
  3592. int hdrsize;
  3593. if (dn.dn.dn_bonuslen != 0)
  3594. {
  3595. sahdrp = (sa_hdr_phys_t *) DN_BONUS (&dn.dn);
  3596. }
  3597. else if (dn.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR)
  3598. {
  3599. blkptr_t *bp = &dn.dn.dn_spill;
  3600. err = zio_read (bp, dn.endian, &sahdrp, NULL, data);
  3601. if (err)
  3602. return err;
  3603. }
  3604. else
  3605. {
  3606. grub_error (GRUB_ERR_BAD_FS, "filesystem is corrupt");
  3607. return grub_errno;
  3608. }
  3609. hdrsize = SA_HDR_SIZE (((sa_hdr_phys_t *) sahdrp));
  3610. info->mtimeset = 1;
  3611. info->mtime = grub_zfs_to_cpu64 (grub_get_unaligned64 ((char *) sahdrp + hdrsize + SA_MTIME_OFFSET), dn.endian);
  3612. }
  3613. if (dn.dn.dn_bonustype == DMU_OT_ZNODE)
  3614. {
  3615. info->mtimeset = 1;
  3616. info->mtime = grub_zfs_to_cpu64 (((znode_phys_t *) DN_BONUS (&dn.dn))->zp_mtime[0], dn.endian);
  3617. }
  3618. return 0;
  3619. }
  3620. /* Helper for grub_zfs_dir. */
  3621. static int
  3622. iterate_zap (const char *name, grub_uint64_t val, struct grub_zfs_dir_ctx *ctx)
  3623. {
  3624. grub_err_t err;
  3625. struct grub_dirhook_info info;
  3626. dnode_end_t dn;
  3627. grub_memset (&info, 0, sizeof (info));
  3628. err = dnode_get (&(ctx->data->subvol.mdn), val, 0, &dn, ctx->data);
  3629. if (err)
  3630. {
  3631. grub_print_error ();
  3632. return 0;
  3633. }
  3634. if (dn.dn.dn_bonustype == DMU_OT_SA)
  3635. {
  3636. void *sahdrp;
  3637. int hdrsize;
  3638. if (dn.dn.dn_bonuslen != 0)
  3639. {
  3640. sahdrp = (sa_hdr_phys_t *) DN_BONUS (&ctx->data->dnode.dn);
  3641. }
  3642. else if (dn.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR)
  3643. {
  3644. blkptr_t *bp = &dn.dn.dn_spill;
  3645. err = zio_read (bp, dn.endian, &sahdrp, NULL, ctx->data);
  3646. if (err)
  3647. {
  3648. grub_print_error ();
  3649. return 0;
  3650. }
  3651. }
  3652. else
  3653. {
  3654. grub_error (GRUB_ERR_BAD_FS, "filesystem is corrupt");
  3655. grub_print_error ();
  3656. return 0;
  3657. }
  3658. hdrsize = SA_HDR_SIZE (((sa_hdr_phys_t *) sahdrp));
  3659. info.mtimeset = 1;
  3660. info.mtime = grub_zfs_to_cpu64 (grub_get_unaligned64 ((char *) sahdrp + hdrsize + SA_MTIME_OFFSET), dn.endian);
  3661. info.case_insensitive = ctx->data->subvol.case_insensitive;
  3662. }
  3663. if (dn.dn.dn_bonustype == DMU_OT_ZNODE)
  3664. {
  3665. info.mtimeset = 1;
  3666. info.mtime = grub_zfs_to_cpu64 (((znode_phys_t *) DN_BONUS (&dn.dn))->zp_mtime[0],
  3667. dn.endian);
  3668. }
  3669. info.dir = (dn.dn.dn_type == DMU_OT_DIRECTORY_CONTENTS);
  3670. grub_dprintf ("zfs", "type=%d, name=%s\n",
  3671. (int)dn.dn.dn_type, (char *)name);
  3672. return ctx->hook (name, &info, ctx->hook_data);
  3673. }
  3674. /* Helper for grub_zfs_dir. */
  3675. static int
  3676. iterate_zap_fs (const char *name, grub_uint64_t val,
  3677. struct grub_zfs_dir_ctx *ctx)
  3678. {
  3679. grub_err_t err;
  3680. struct grub_dirhook_info info;
  3681. if (name[0] == 0 && val == 0)
  3682. return 0;
  3683. dnode_end_t mdn;
  3684. err = dnode_get (&(ctx->data->mos), val, 0, &mdn, ctx->data);
  3685. if (err)
  3686. {
  3687. grub_errno = 0;
  3688. return 0;
  3689. }
  3690. if (mdn.dn.dn_type != DMU_OT_DSL_DIR && mdn.dn.dn_bonustype != DMU_OT_DSL_DIR) {
  3691. grub_dprintf ("zfs", "type = 0x%x, val = 0x%llx\n", mdn.dn.dn_type, (long long)val);
  3692. return 0;
  3693. }
  3694. err = fill_fs_info (&info, &mdn, ctx->data);
  3695. if (err)
  3696. {
  3697. grub_errno = 0;
  3698. return 0;
  3699. }
  3700. return ctx->hook (name, &info, ctx->hook_data);
  3701. }
  3702. /* Helper for grub_zfs_dir. */
  3703. static int
  3704. iterate_zap_snap (const char *name, grub_uint64_t val,
  3705. struct grub_zfs_dir_ctx *ctx)
  3706. {
  3707. grub_err_t err;
  3708. struct grub_dirhook_info info;
  3709. char *name2;
  3710. int ret;
  3711. dnode_end_t mdn;
  3712. err = dnode_get (&(ctx->data->mos), val, 0, &mdn, ctx->data);
  3713. if (err)
  3714. {
  3715. grub_errno = 0;
  3716. return 0;
  3717. }
  3718. if (mdn.dn.dn_type != DMU_OT_DSL_DATASET && mdn.dn.dn_bonustype != DMU_OT_DSL_DATASET)
  3719. return 0;
  3720. err = fill_fs_info (&info, &mdn, ctx->data);
  3721. if (err)
  3722. {
  3723. grub_errno = 0;
  3724. return 0;
  3725. }
  3726. name2 = grub_malloc (grub_strlen (name) + 2);
  3727. name2[0] = '@';
  3728. grub_memcpy (name2 + 1, name, grub_strlen (name) + 1);
  3729. ret = ctx->hook (name2, &info, ctx->hook_data);
  3730. grub_free (name2);
  3731. return ret;
  3732. }
  3733. static grub_err_t
  3734. grub_zfs_dir (grub_device_t device, const char *path,
  3735. grub_fs_dir_hook_t hook, void *hook_data)
  3736. {
  3737. struct grub_zfs_dir_ctx ctx = {
  3738. .hook = hook,
  3739. .hook_data = hook_data
  3740. };
  3741. struct grub_zfs_data *data;
  3742. grub_err_t err;
  3743. int isfs;
  3744. data = zfs_mount (device);
  3745. if (! data)
  3746. return grub_errno;
  3747. err = dnode_get_fullpath (path, &(data->subvol), &(data->dnode), &isfs, data);
  3748. if (err)
  3749. {
  3750. zfs_unmount (data);
  3751. return err;
  3752. }
  3753. ctx.data = data;
  3754. if (isfs)
  3755. {
  3756. grub_uint64_t childobj, headobj;
  3757. grub_uint64_t snapobj;
  3758. dnode_end_t dn;
  3759. struct grub_dirhook_info info;
  3760. err = fill_fs_info (&info, &data->dnode, data);
  3761. if (err)
  3762. {
  3763. zfs_unmount (data);
  3764. return err;
  3765. }
  3766. if (hook ("@", &info, hook_data))
  3767. {
  3768. zfs_unmount (data);
  3769. return GRUB_ERR_NONE;
  3770. }
  3771. childobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&data->dnode.dn))->dd_child_dir_zapobj, data->dnode.endian);
  3772. headobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&data->dnode.dn))->dd_head_dataset_obj, data->dnode.endian);
  3773. err = dnode_get (&(data->mos), childobj,
  3774. DMU_OT_DSL_DIR_CHILD_MAP, &dn, data);
  3775. if (err)
  3776. {
  3777. zfs_unmount (data);
  3778. return err;
  3779. }
  3780. zap_iterate_u64 (&dn, iterate_zap_fs, data, &ctx);
  3781. err = dnode_get (&(data->mos), headobj, 0, &dn, data);
  3782. if (!err && dn.dn.dn_type != DMU_OT_DSL_DATASET && dn.dn.dn_bonustype != DMU_OT_DSL_DATASET)
  3783. return grub_error(GRUB_ERR_BAD_FS, "incorrect dataset dnode type");
  3784. if (err)
  3785. {
  3786. zfs_unmount (data);
  3787. return err;
  3788. }
  3789. snapobj = grub_zfs_to_cpu64 (((dsl_dataset_phys_t *) DN_BONUS (&dn.dn))->ds_snapnames_zapobj, dn.endian);
  3790. err = dnode_get (&(data->mos), snapobj,
  3791. DMU_OT_DSL_DS_SNAP_MAP, &dn, data);
  3792. if (err)
  3793. {
  3794. zfs_unmount (data);
  3795. return err;
  3796. }
  3797. zap_iterate_u64 (&dn, iterate_zap_snap, data, &ctx);
  3798. }
  3799. else
  3800. {
  3801. if (data->dnode.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS)
  3802. {
  3803. zfs_unmount (data);
  3804. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
  3805. }
  3806. zap_iterate_u64 (&(data->dnode), iterate_zap, data, &ctx);
  3807. }
  3808. zfs_unmount (data);
  3809. return grub_errno;
  3810. }
  3811. static int
  3812. check_feature (const char *name, grub_uint64_t val,
  3813. struct grub_zfs_dir_ctx *ctx __attribute__((unused)))
  3814. {
  3815. int i;
  3816. if (val == 0)
  3817. return 0;
  3818. if (name[0] == 0)
  3819. return 0;
  3820. for (i = 0; spa_feature_names[i] != NULL; i++)
  3821. if (grub_strcmp (name, spa_feature_names[i]) == 0)
  3822. return 0;
  3823. return 1;
  3824. }
  3825. /*
  3826. * Checks whether the MOS features that are active are supported by this
  3827. * (GRUB's) implementation of ZFS.
  3828. *
  3829. * Return:
  3830. * 0: Success.
  3831. * errnum: Failure.
  3832. */
  3833. static grub_err_t
  3834. check_mos_features(dnode_phys_t *mosmdn_phys,grub_zfs_endian_t endian,struct grub_zfs_data* data )
  3835. {
  3836. grub_uint64_t objnum;
  3837. grub_err_t errnum = 0;
  3838. dnode_end_t dn,mosmdn;
  3839. mzap_phys_t* mzp;
  3840. grub_zfs_endian_t endianzap;
  3841. int size;
  3842. grub_memmove(&(mosmdn.dn),mosmdn_phys,sizeof(dnode_phys_t));
  3843. mosmdn.endian=endian;
  3844. errnum = dnode_get(&mosmdn, DMU_POOL_DIRECTORY_OBJECT,
  3845. DMU_OT_OBJECT_DIRECTORY, &dn,data);
  3846. if (errnum != 0)
  3847. return errnum;
  3848. /*
  3849. * Find the object number for 'features_for_read' and retrieve its
  3850. * corresponding dnode. Note that we don't check features_for_write
  3851. * because GRUB is not opening the pool for write.
  3852. */
  3853. errnum = zap_lookup(&dn, DMU_POOL_FEATURES_FOR_READ, &objnum, data,0);
  3854. if (errnum != 0)
  3855. return errnum;
  3856. errnum = dnode_get(&mosmdn, objnum, DMU_OTN_ZAP_METADATA, &dn, data);
  3857. if (errnum != 0)
  3858. return errnum;
  3859. errnum = dmu_read(&dn, 0, (void**)&mzp, &endianzap,data);
  3860. if (errnum != 0)
  3861. return errnum;
  3862. size = grub_zfs_to_cpu16 (dn.dn.dn_datablkszsec, dn.endian) << SPA_MINBLOCKSHIFT;
  3863. return mzap_iterate (mzp,endianzap, size, check_feature,NULL);
  3864. }
  3865. #ifdef GRUB_UTIL
  3866. static grub_err_t
  3867. grub_zfs_embed (grub_device_t device __attribute__ ((unused)),
  3868. unsigned int *nsectors,
  3869. unsigned int max_nsectors,
  3870. grub_embed_type_t embed_type,
  3871. grub_disk_addr_t **sectors)
  3872. {
  3873. unsigned i;
  3874. if (embed_type != GRUB_EMBED_PCBIOS)
  3875. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  3876. "ZFS currently supports only PC-BIOS embedding");
  3877. if ((VDEV_BOOT_SIZE >> GRUB_DISK_SECTOR_BITS) < *nsectors)
  3878. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  3879. N_("your core.img is unusually large. "
  3880. "It won't fit in the embedding area"));
  3881. *nsectors = (VDEV_BOOT_SIZE >> GRUB_DISK_SECTOR_BITS);
  3882. if (*nsectors > max_nsectors)
  3883. *nsectors = max_nsectors;
  3884. *sectors = grub_calloc (*nsectors, sizeof (**sectors));
  3885. if (!*sectors)
  3886. return grub_errno;
  3887. for (i = 0; i < *nsectors; i++)
  3888. (*sectors)[i] = i + (VDEV_BOOT_OFFSET >> GRUB_DISK_SECTOR_BITS);
  3889. return GRUB_ERR_NONE;
  3890. }
  3891. #endif
  3892. static struct grub_fs grub_zfs_fs = {
  3893. .name = "zfs",
  3894. .fs_dir = grub_zfs_dir,
  3895. .fs_open = grub_zfs_open,
  3896. .fs_read = grub_zfs_read,
  3897. .fs_close = grub_zfs_close,
  3898. .fs_label = zfs_label,
  3899. .fs_uuid = zfs_uuid,
  3900. .fs_mtime = zfs_mtime,
  3901. #ifdef GRUB_UTIL
  3902. .fs_embed = grub_zfs_embed,
  3903. .reserved_first_sector = 1,
  3904. .blocklist_install = 0,
  3905. #endif
  3906. .next = 0
  3907. };
  3908. GRUB_MOD_INIT (zfs)
  3909. {
  3910. COMPILE_TIME_ASSERT (sizeof (zap_leaf_chunk_t) == ZAP_LEAF_CHUNKSIZE);
  3911. grub_fs_register (&grub_zfs_fs);
  3912. #ifndef GRUB_UTIL
  3913. my_mod = mod;
  3914. #endif
  3915. }
  3916. GRUB_MOD_FINI (zfs)
  3917. {
  3918. grub_fs_unregister (&grub_zfs_fs);
  3919. }