zfs.c 110 KB

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