zfs.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * ZFS filesystem ported to u-boot by
  5. * Jorgen Lundman <lundman at lundman.net>
  6. *
  7. * GRUB -- GRand Unified Bootloader
  8. * Copyright (C) 1999,2000,2001,2002,2003,2004
  9. * Free Software Foundation, Inc.
  10. * Copyright 2004 Sun Microsystems, Inc.
  11. */
  12. #include <common.h>
  13. #include <malloc.h>
  14. #include <linux/stat.h>
  15. #include <linux/time.h>
  16. #include <linux/ctype.h>
  17. #include <asm/byteorder.h>
  18. #include "zfs_common.h"
  19. #include "div64.h"
  20. struct blk_desc *zfs_dev_desc;
  21. /*
  22. * The zfs plug-in routines for GRUB are:
  23. *
  24. * zfs_mount() - locates a valid uberblock of the root pool and reads
  25. * in its MOS at the memory address MOS.
  26. *
  27. * zfs_open() - locates a plain file object by following the MOS
  28. * and places its dnode at the memory address DNODE.
  29. *
  30. * zfs_read() - read in the data blocks pointed by the DNODE.
  31. *
  32. */
  33. #include <zfs/zfs.h>
  34. #include <zfs/zio.h>
  35. #include <zfs/dnode.h>
  36. #include <zfs/uberblock_impl.h>
  37. #include <zfs/vdev_impl.h>
  38. #include <zfs/zio_checksum.h>
  39. #include <zfs/zap_impl.h>
  40. #include <zfs/zap_leaf.h>
  41. #include <zfs/zfs_znode.h>
  42. #include <zfs/dmu.h>
  43. #include <zfs/dmu_objset.h>
  44. #include <zfs/sa_impl.h>
  45. #include <zfs/dsl_dir.h>
  46. #include <zfs/dsl_dataset.h>
  47. #define ZPOOL_PROP_BOOTFS "bootfs"
  48. /*
  49. * For nvlist manipulation. (from nvpair.h)
  50. */
  51. #define NV_ENCODE_NATIVE 0
  52. #define NV_ENCODE_XDR 1
  53. #define NV_BIG_ENDIAN 0
  54. #define NV_LITTLE_ENDIAN 1
  55. #define DATA_TYPE_UINT64 8
  56. #define DATA_TYPE_STRING 9
  57. #define DATA_TYPE_NVLIST 19
  58. #define DATA_TYPE_NVLIST_ARRAY 20
  59. /*
  60. * Macros to get fields in a bp or DVA.
  61. */
  62. #define P2PHASE(x, align) ((x) & ((align) - 1))
  63. #define DVA_OFFSET_TO_PHYS_SECTOR(offset) \
  64. ((offset + VDEV_LABEL_START_SIZE) >> SPA_MINBLOCKSHIFT)
  65. /*
  66. * return x rounded down to an align boundary
  67. * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
  68. * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
  69. * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
  70. * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
  71. */
  72. #define P2ALIGN(x, align) ((x) & -(align))
  73. /*
  74. * FAT ZAP data structures
  75. */
  76. #define ZFS_CRC64_POLY 0xC96C5795D7870F42ULL /* ECMA-182, reflected form */
  77. #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
  78. #define CHAIN_END 0xffff /* end of the chunk chain */
  79. /*
  80. * The amount of space within the chunk available for the array is:
  81. * chunk size - space for type (1) - space for next pointer (2)
  82. */
  83. #define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
  84. #define ZAP_LEAF_HASH_SHIFT(bs) (bs - 5)
  85. #define ZAP_LEAF_HASH_NUMENTRIES(bs) (1 << ZAP_LEAF_HASH_SHIFT(bs))
  86. #define LEAF_HASH(bs, h) \
  87. ((ZAP_LEAF_HASH_NUMENTRIES(bs)-1) & \
  88. ((h) >> (64 - ZAP_LEAF_HASH_SHIFT(bs)-l->l_hdr.lh_prefix_len)))
  89. /*
  90. * The amount of space available for chunks is:
  91. * block size shift - hash entry size (2) * number of hash
  92. * entries - header space (2*chunksize)
  93. */
  94. #define ZAP_LEAF_NUMCHUNKS(bs) \
  95. (((1<<bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(bs)) / \
  96. ZAP_LEAF_CHUNKSIZE - 2)
  97. /*
  98. * The chunks start immediately after the hash table. The end of the
  99. * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
  100. * chunk_t.
  101. */
  102. #define ZAP_LEAF_CHUNK(l, bs, idx) \
  103. ((zap_leaf_chunk_t *)(l->l_hash + ZAP_LEAF_HASH_NUMENTRIES(bs)))[idx]
  104. #define ZAP_LEAF_ENTRY(l, bs, idx) (&ZAP_LEAF_CHUNK(l, bs, idx).l_entry)
  105. /*
  106. * Decompression Entry - lzjb
  107. */
  108. #ifndef NBBY
  109. #define NBBY 8
  110. #endif
  111. typedef int zfs_decomp_func_t(void *s_start, void *d_start,
  112. uint32_t s_len, uint32_t d_len);
  113. typedef struct decomp_entry {
  114. char *name;
  115. zfs_decomp_func_t *decomp_func;
  116. } decomp_entry_t;
  117. typedef struct dnode_end {
  118. dnode_phys_t dn;
  119. zfs_endian_t endian;
  120. } dnode_end_t;
  121. struct zfs_data {
  122. /* cache for a file block of the currently zfs_open()-ed file */
  123. char *file_buf;
  124. uint64_t file_start;
  125. uint64_t file_end;
  126. /* XXX: ashift is per vdev, not per pool. We currently only ever touch
  127. * a single vdev, but when/if raid-z or stripes are supported, this
  128. * may need revision.
  129. */
  130. uint64_t vdev_ashift;
  131. uint64_t label_txg;
  132. uint64_t pool_guid;
  133. /* cache for a dnode block */
  134. dnode_phys_t *dnode_buf;
  135. dnode_phys_t *dnode_mdn;
  136. uint64_t dnode_start;
  137. uint64_t dnode_end;
  138. zfs_endian_t dnode_endian;
  139. uberblock_t current_uberblock;
  140. dnode_end_t mos;
  141. dnode_end_t mdn;
  142. dnode_end_t dnode;
  143. uint64_t vdev_phys_sector;
  144. int (*userhook)(const char *, const struct zfs_dirhook_info *);
  145. struct zfs_dirhook_info *dirinfo;
  146. };
  147. static int
  148. zlib_decompress(void *s, void *d,
  149. uint32_t slen, uint32_t dlen)
  150. {
  151. if (zlib_decompress(s, d, slen, dlen) < 0)
  152. return ZFS_ERR_BAD_FS;
  153. return ZFS_ERR_NONE;
  154. }
  155. static decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] = {
  156. {"inherit", NULL}, /* ZIO_COMPRESS_INHERIT */
  157. {"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */
  158. {"off", NULL}, /* ZIO_COMPRESS_OFF */
  159. {"lzjb", lzjb_decompress}, /* ZIO_COMPRESS_LZJB */
  160. {"empty", NULL}, /* ZIO_COMPRESS_EMPTY */
  161. {"gzip-1", zlib_decompress}, /* ZIO_COMPRESS_GZIP1 */
  162. {"gzip-2", zlib_decompress}, /* ZIO_COMPRESS_GZIP2 */
  163. {"gzip-3", zlib_decompress}, /* ZIO_COMPRESS_GZIP3 */
  164. {"gzip-4", zlib_decompress}, /* ZIO_COMPRESS_GZIP4 */
  165. {"gzip-5", zlib_decompress}, /* ZIO_COMPRESS_GZIP5 */
  166. {"gzip-6", zlib_decompress}, /* ZIO_COMPRESS_GZIP6 */
  167. {"gzip-7", zlib_decompress}, /* ZIO_COMPRESS_GZIP7 */
  168. {"gzip-8", zlib_decompress}, /* ZIO_COMPRESS_GZIP8 */
  169. {"gzip-9", zlib_decompress}, /* ZIO_COMPRESS_GZIP9 */
  170. };
  171. static int zio_read_data(blkptr_t *bp, zfs_endian_t endian,
  172. void *buf, struct zfs_data *data);
  173. static int
  174. zio_read(blkptr_t *bp, zfs_endian_t endian, void **buf,
  175. size_t *size, struct zfs_data *data);
  176. /*
  177. * Our own version of log2(). Same thing as highbit()-1.
  178. */
  179. static int
  180. zfs_log2(uint64_t num)
  181. {
  182. int i = 0;
  183. while (num > 1) {
  184. i++;
  185. num = num >> 1;
  186. }
  187. return i;
  188. }
  189. /* Checksum Functions */
  190. static void
  191. zio_checksum_off(const void *buf __attribute__ ((unused)),
  192. uint64_t size __attribute__ ((unused)),
  193. zfs_endian_t endian __attribute__ ((unused)),
  194. zio_cksum_t *zcp)
  195. {
  196. ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
  197. }
  198. /* Checksum Table and Values */
  199. static zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
  200. {NULL, 0, 0, "inherit"},
  201. {NULL, 0, 0, "on"},
  202. {zio_checksum_off, 0, 0, "off"},
  203. {zio_checksum_SHA256, 1, 1, "label"},
  204. {zio_checksum_SHA256, 1, 1, "gang_header"},
  205. {NULL, 0, 0, "zilog"},
  206. {fletcher_2_endian, 0, 0, "fletcher2"},
  207. {fletcher_4_endian, 1, 0, "fletcher4"},
  208. {zio_checksum_SHA256, 1, 0, "SHA256"},
  209. {NULL, 0, 0, "zilog2"},
  210. };
  211. /*
  212. * zio_checksum_verify: Provides support for checksum verification.
  213. *
  214. * Fletcher2, Fletcher4, and SHA256 are supported.
  215. *
  216. */
  217. static int
  218. zio_checksum_verify(zio_cksum_t zc, uint32_t checksum,
  219. zfs_endian_t endian, char *buf, int size)
  220. {
  221. zio_eck_t *zec = (zio_eck_t *) (buf + size) - 1;
  222. zio_checksum_info_t *ci = &zio_checksum_table[checksum];
  223. zio_cksum_t actual_cksum, expected_cksum;
  224. if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func == NULL) {
  225. printf("zfs unknown checksum function %d\n", checksum);
  226. return ZFS_ERR_NOT_IMPLEMENTED_YET;
  227. }
  228. if (ci->ci_eck) {
  229. expected_cksum = zec->zec_cksum;
  230. zec->zec_cksum = zc;
  231. ci->ci_func(buf, size, endian, &actual_cksum);
  232. zec->zec_cksum = expected_cksum;
  233. zc = expected_cksum;
  234. } else {
  235. ci->ci_func(buf, size, endian, &actual_cksum);
  236. }
  237. if ((actual_cksum.zc_word[0] != zc.zc_word[0])
  238. || (actual_cksum.zc_word[1] != zc.zc_word[1])
  239. || (actual_cksum.zc_word[2] != zc.zc_word[2])
  240. || (actual_cksum.zc_word[3] != zc.zc_word[3])) {
  241. return ZFS_ERR_BAD_FS;
  242. }
  243. return ZFS_ERR_NONE;
  244. }
  245. /*
  246. * vdev_uberblock_compare takes two uberblock structures and returns an integer
  247. * indicating the more recent of the two.
  248. * Return Value = 1 if ub2 is more recent
  249. * Return Value = -1 if ub1 is more recent
  250. * The most recent uberblock is determined using its transaction number and
  251. * timestamp. The uberblock with the highest transaction number is
  252. * considered "newer". If the transaction numbers of the two blocks match, the
  253. * timestamps are compared to determine the "newer" of the two.
  254. */
  255. static int
  256. vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
  257. {
  258. zfs_endian_t ub1_endian, ub2_endian;
  259. if (zfs_to_cpu64(ub1->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC)
  260. ub1_endian = LITTLE_ENDIAN;
  261. else
  262. ub1_endian = BIG_ENDIAN;
  263. if (zfs_to_cpu64(ub2->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC)
  264. ub2_endian = LITTLE_ENDIAN;
  265. else
  266. ub2_endian = BIG_ENDIAN;
  267. if (zfs_to_cpu64(ub1->ub_txg, ub1_endian)
  268. < zfs_to_cpu64(ub2->ub_txg, ub2_endian))
  269. return -1;
  270. if (zfs_to_cpu64(ub1->ub_txg, ub1_endian)
  271. > zfs_to_cpu64(ub2->ub_txg, ub2_endian))
  272. return 1;
  273. if (zfs_to_cpu64(ub1->ub_timestamp, ub1_endian)
  274. < zfs_to_cpu64(ub2->ub_timestamp, ub2_endian))
  275. return -1;
  276. if (zfs_to_cpu64(ub1->ub_timestamp, ub1_endian)
  277. > zfs_to_cpu64(ub2->ub_timestamp, ub2_endian))
  278. return 1;
  279. return 0;
  280. }
  281. /*
  282. * Three pieces of information are needed to verify an uberblock: the magic
  283. * number, the version number, and the checksum.
  284. *
  285. * Currently Implemented: version number, magic number, label txg
  286. * Need to Implement: checksum
  287. *
  288. */
  289. static int
  290. uberblock_verify(uberblock_t *uber, int offset, struct zfs_data *data)
  291. {
  292. int err;
  293. zfs_endian_t endian = UNKNOWN_ENDIAN;
  294. zio_cksum_t zc;
  295. if (uber->ub_txg < data->label_txg) {
  296. debug("ignoring partially written label: uber_txg < label_txg %llu %llu\n",
  297. uber->ub_txg, data->label_txg);
  298. return ZFS_ERR_BAD_FS;
  299. }
  300. if (zfs_to_cpu64(uber->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC
  301. && zfs_to_cpu64(uber->ub_version, LITTLE_ENDIAN) > 0
  302. && zfs_to_cpu64(uber->ub_version, LITTLE_ENDIAN) <= SPA_VERSION)
  303. endian = LITTLE_ENDIAN;
  304. if (zfs_to_cpu64(uber->ub_magic, BIG_ENDIAN) == UBERBLOCK_MAGIC
  305. && zfs_to_cpu64(uber->ub_version, BIG_ENDIAN) > 0
  306. && zfs_to_cpu64(uber->ub_version, BIG_ENDIAN) <= SPA_VERSION)
  307. endian = BIG_ENDIAN;
  308. if (endian == UNKNOWN_ENDIAN) {
  309. printf("invalid uberblock magic\n");
  310. return ZFS_ERR_BAD_FS;
  311. }
  312. memset(&zc, 0, sizeof(zc));
  313. zc.zc_word[0] = cpu_to_zfs64(offset, endian);
  314. err = zio_checksum_verify(zc, ZIO_CHECKSUM_LABEL, endian,
  315. (char *) uber, UBERBLOCK_SIZE(data->vdev_ashift));
  316. if (!err) {
  317. /* Check that the data pointed by the rootbp is usable. */
  318. void *osp = NULL;
  319. size_t ospsize;
  320. err = zio_read(&uber->ub_rootbp, endian, &osp, &ospsize, data);
  321. free(osp);
  322. if (!err && ospsize < OBJSET_PHYS_SIZE_V14) {
  323. printf("uberblock rootbp points to invalid data\n");
  324. return ZFS_ERR_BAD_FS;
  325. }
  326. }
  327. return err;
  328. }
  329. /*
  330. * Find the best uberblock.
  331. * Return:
  332. * Success - Pointer to the best uberblock.
  333. * Failure - NULL
  334. */
  335. static uberblock_t *find_bestub(char *ub_array, struct zfs_data *data)
  336. {
  337. const uint64_t sector = data->vdev_phys_sector;
  338. uberblock_t *ubbest = NULL;
  339. uberblock_t *ubnext;
  340. unsigned int i, offset, pickedub = 0;
  341. int err = ZFS_ERR_NONE;
  342. const unsigned int UBCOUNT = UBERBLOCK_COUNT(data->vdev_ashift);
  343. const uint64_t UBBYTES = UBERBLOCK_SIZE(data->vdev_ashift);
  344. for (i = 0; i < UBCOUNT; i++) {
  345. ubnext = (uberblock_t *) (i * UBBYTES + ub_array);
  346. offset = (sector << SPA_MINBLOCKSHIFT) + VDEV_PHYS_SIZE + (i * UBBYTES);
  347. err = uberblock_verify(ubnext, offset, data);
  348. if (err)
  349. continue;
  350. if (ubbest == NULL || vdev_uberblock_compare(ubnext, ubbest) > 0) {
  351. ubbest = ubnext;
  352. pickedub = i;
  353. }
  354. }
  355. if (ubbest)
  356. debug("zfs Found best uberblock at idx %d, txg %llu\n",
  357. pickedub, (unsigned long long) ubbest->ub_txg);
  358. return ubbest;
  359. }
  360. static inline size_t
  361. get_psize(blkptr_t *bp, zfs_endian_t endian)
  362. {
  363. return (((zfs_to_cpu64((bp)->blk_prop, endian) >> 16) & 0xffff) + 1)
  364. << SPA_MINBLOCKSHIFT;
  365. }
  366. static uint64_t
  367. dva_get_offset(dva_t *dva, zfs_endian_t endian)
  368. {
  369. return zfs_to_cpu64((dva)->dva_word[1],
  370. endian) << SPA_MINBLOCKSHIFT;
  371. }
  372. /*
  373. * Read a block of data based on the gang block address dva,
  374. * and put its data in buf.
  375. *
  376. */
  377. static int
  378. zio_read_gang(blkptr_t *bp, zfs_endian_t endian, dva_t *dva, void *buf,
  379. struct zfs_data *data)
  380. {
  381. zio_gbh_phys_t *zio_gb;
  382. uint64_t offset, sector;
  383. unsigned i;
  384. int err;
  385. zio_cksum_t zc;
  386. memset(&zc, 0, sizeof(zc));
  387. zio_gb = malloc(SPA_GANGBLOCKSIZE);
  388. if (!zio_gb)
  389. return ZFS_ERR_OUT_OF_MEMORY;
  390. offset = dva_get_offset(dva, endian);
  391. sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
  392. /* read in the gang block header */
  393. err = zfs_devread(sector, 0, SPA_GANGBLOCKSIZE, (char *) zio_gb);
  394. if (err) {
  395. free(zio_gb);
  396. return err;
  397. }
  398. /* XXX */
  399. /* self checksuming the gang block header */
  400. ZIO_SET_CHECKSUM(&zc, DVA_GET_VDEV(dva),
  401. dva_get_offset(dva, endian), bp->blk_birth, 0);
  402. err = zio_checksum_verify(zc, ZIO_CHECKSUM_GANG_HEADER, endian,
  403. (char *) zio_gb, SPA_GANGBLOCKSIZE);
  404. if (err) {
  405. free(zio_gb);
  406. return err;
  407. }
  408. endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1;
  409. for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
  410. if (zio_gb->zg_blkptr[i].blk_birth == 0)
  411. continue;
  412. err = zio_read_data(&zio_gb->zg_blkptr[i], endian, buf, data);
  413. if (err) {
  414. free(zio_gb);
  415. return err;
  416. }
  417. buf = (char *) buf + get_psize(&zio_gb->zg_blkptr[i], endian);
  418. }
  419. free(zio_gb);
  420. return ZFS_ERR_NONE;
  421. }
  422. /*
  423. * Read in a block of raw data to buf.
  424. */
  425. static int
  426. zio_read_data(blkptr_t *bp, zfs_endian_t endian, void *buf,
  427. struct zfs_data *data)
  428. {
  429. int i, psize;
  430. int err = ZFS_ERR_NONE;
  431. psize = get_psize(bp, endian);
  432. /* pick a good dva from the block pointer */
  433. for (i = 0; i < SPA_DVAS_PER_BP; i++) {
  434. uint64_t offset, sector;
  435. if (bp->blk_dva[i].dva_word[0] == 0 && bp->blk_dva[i].dva_word[1] == 0)
  436. continue;
  437. if ((zfs_to_cpu64(bp->blk_dva[i].dva_word[1], endian)>>63) & 1) {
  438. err = zio_read_gang(bp, endian, &bp->blk_dva[i], buf, data);
  439. } else {
  440. /* read in a data block */
  441. offset = dva_get_offset(&bp->blk_dva[i], endian);
  442. sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
  443. err = zfs_devread(sector, 0, psize, buf);
  444. }
  445. if (!err) {
  446. /*Check the underlying checksum before we rule this DVA as "good"*/
  447. uint32_t checkalgo = (zfs_to_cpu64((bp)->blk_prop, endian) >> 40) & 0xff;
  448. err = zio_checksum_verify(bp->blk_cksum, checkalgo, endian, buf, psize);
  449. if (!err)
  450. return ZFS_ERR_NONE;
  451. }
  452. /* If read failed or checksum bad, reset the error. Hopefully we've got some more DVA's to try.*/
  453. }
  454. if (!err) {
  455. printf("couldn't find a valid DVA\n");
  456. err = ZFS_ERR_BAD_FS;
  457. }
  458. return err;
  459. }
  460. /*
  461. * Read in a block of data, verify its checksum, decompress if needed,
  462. * and put the uncompressed data in buf.
  463. */
  464. static int
  465. zio_read(blkptr_t *bp, zfs_endian_t endian, void **buf,
  466. size_t *size, struct zfs_data *data)
  467. {
  468. size_t lsize, psize;
  469. unsigned int comp;
  470. char *compbuf = NULL;
  471. int err;
  472. *buf = NULL;
  473. comp = (zfs_to_cpu64((bp)->blk_prop, endian)>>32) & 0xff;
  474. lsize = (BP_IS_HOLE(bp) ? 0 :
  475. (((zfs_to_cpu64((bp)->blk_prop, endian) & 0xffff) + 1)
  476. << SPA_MINBLOCKSHIFT));
  477. psize = get_psize(bp, endian);
  478. if (size)
  479. *size = lsize;
  480. if (comp >= ZIO_COMPRESS_FUNCTIONS) {
  481. printf("compression algorithm %u not supported\n", (unsigned int) comp);
  482. return ZFS_ERR_NOT_IMPLEMENTED_YET;
  483. }
  484. if (comp != ZIO_COMPRESS_OFF && decomp_table[comp].decomp_func == NULL) {
  485. printf("compression algorithm %s not supported\n", decomp_table[comp].name);
  486. return ZFS_ERR_NOT_IMPLEMENTED_YET;
  487. }
  488. if (comp != ZIO_COMPRESS_OFF) {
  489. compbuf = malloc(psize);
  490. if (!compbuf)
  491. return ZFS_ERR_OUT_OF_MEMORY;
  492. } else {
  493. compbuf = *buf = malloc(lsize);
  494. }
  495. err = zio_read_data(bp, endian, compbuf, data);
  496. if (err) {
  497. free(compbuf);
  498. *buf = NULL;
  499. return err;
  500. }
  501. if (comp != ZIO_COMPRESS_OFF) {
  502. *buf = malloc(lsize);
  503. if (!*buf) {
  504. free(compbuf);
  505. return ZFS_ERR_OUT_OF_MEMORY;
  506. }
  507. err = decomp_table[comp].decomp_func(compbuf, *buf, psize, lsize);
  508. free(compbuf);
  509. if (err) {
  510. free(*buf);
  511. *buf = NULL;
  512. return err;
  513. }
  514. }
  515. return ZFS_ERR_NONE;
  516. }
  517. /*
  518. * Get the block from a block id.
  519. * push the block onto the stack.
  520. *
  521. */
  522. static int
  523. dmu_read(dnode_end_t *dn, uint64_t blkid, void **buf,
  524. zfs_endian_t *endian_out, struct zfs_data *data)
  525. {
  526. int idx, level;
  527. blkptr_t *bp_array = dn->dn.dn_blkptr;
  528. int epbs = dn->dn.dn_indblkshift - SPA_BLKPTRSHIFT;
  529. blkptr_t *bp;
  530. void *tmpbuf = 0;
  531. zfs_endian_t endian;
  532. int err = ZFS_ERR_NONE;
  533. bp = malloc(sizeof(blkptr_t));
  534. if (!bp)
  535. return ZFS_ERR_OUT_OF_MEMORY;
  536. endian = dn->endian;
  537. for (level = dn->dn.dn_nlevels - 1; level >= 0; level--) {
  538. idx = (blkid >> (epbs * level)) & ((1 << epbs) - 1);
  539. *bp = bp_array[idx];
  540. if (bp_array != dn->dn.dn_blkptr) {
  541. free(bp_array);
  542. bp_array = 0;
  543. }
  544. if (BP_IS_HOLE(bp)) {
  545. size_t size = zfs_to_cpu16(dn->dn.dn_datablkszsec,
  546. dn->endian)
  547. << SPA_MINBLOCKSHIFT;
  548. *buf = malloc(size);
  549. if (*buf) {
  550. err = ZFS_ERR_OUT_OF_MEMORY;
  551. break;
  552. }
  553. memset(*buf, 0, size);
  554. endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1;
  555. break;
  556. }
  557. if (level == 0) {
  558. err = zio_read(bp, endian, buf, 0, data);
  559. endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1;
  560. break;
  561. }
  562. err = zio_read(bp, endian, &tmpbuf, 0, data);
  563. endian = (zfs_to_cpu64(bp->blk_prop, endian) >> 63) & 1;
  564. if (err)
  565. break;
  566. bp_array = tmpbuf;
  567. }
  568. if (bp_array != dn->dn.dn_blkptr)
  569. free(bp_array);
  570. if (endian_out)
  571. *endian_out = endian;
  572. free(bp);
  573. return err;
  574. }
  575. /*
  576. * mzap_lookup: Looks up property described by "name" and returns the value
  577. * in "value".
  578. */
  579. static int
  580. mzap_lookup(mzap_phys_t *zapobj, zfs_endian_t endian,
  581. int objsize, char *name, uint64_t * value)
  582. {
  583. int i, chunks;
  584. mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
  585. chunks = objsize / MZAP_ENT_LEN - 1;
  586. for (i = 0; i < chunks; i++) {
  587. if (strcmp(mzap_ent[i].mze_name, name) == 0) {
  588. *value = zfs_to_cpu64(mzap_ent[i].mze_value, endian);
  589. return ZFS_ERR_NONE;
  590. }
  591. }
  592. printf("couldn't find '%s'\n", name);
  593. return ZFS_ERR_FILE_NOT_FOUND;
  594. }
  595. static int
  596. mzap_iterate(mzap_phys_t *zapobj, zfs_endian_t endian, int objsize,
  597. int (*hook)(const char *name,
  598. uint64_t val,
  599. struct zfs_data *data),
  600. struct zfs_data *data)
  601. {
  602. int i, chunks;
  603. mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
  604. chunks = objsize / MZAP_ENT_LEN - 1;
  605. for (i = 0; i < chunks; i++) {
  606. if (hook(mzap_ent[i].mze_name,
  607. zfs_to_cpu64(mzap_ent[i].mze_value, endian),
  608. data))
  609. return 1;
  610. }
  611. return 0;
  612. }
  613. static uint64_t
  614. zap_hash(uint64_t salt, const char *name)
  615. {
  616. static uint64_t table[256];
  617. const uint8_t *cp;
  618. uint8_t c;
  619. uint64_t crc = salt;
  620. if (table[128] == 0) {
  621. uint64_t *ct = NULL;
  622. int i, j;
  623. for (i = 0; i < 256; i++) {
  624. for (ct = table + i, *ct = i, j = 8; j > 0; j--)
  625. *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
  626. }
  627. }
  628. for (cp = (const uint8_t *) name; (c = *cp) != '\0'; cp++)
  629. crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
  630. /*
  631. * Only use 28 bits, since we need 4 bits in the cookie for the
  632. * collision differentiator. We MUST use the high bits, since
  633. * those are the onces that we first pay attention to when
  634. * chosing the bucket.
  635. */
  636. crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
  637. return crc;
  638. }
  639. /*
  640. * Only to be used on 8-bit arrays.
  641. * array_len is actual len in bytes (not encoded le_value_length).
  642. * buf is null-terminated.
  643. */
  644. /* XXX */
  645. static int
  646. zap_leaf_array_equal(zap_leaf_phys_t *l, zfs_endian_t endian,
  647. int blksft, int chunk, int array_len, const char *buf)
  648. {
  649. int bseen = 0;
  650. while (bseen < array_len) {
  651. struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
  652. int toread = min(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
  653. if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
  654. return 0;
  655. if (memcmp(la->la_array, buf + bseen, toread) != 0)
  656. break;
  657. chunk = zfs_to_cpu16(la->la_next, endian);
  658. bseen += toread;
  659. }
  660. return (bseen == array_len);
  661. }
  662. /* XXX */
  663. static int
  664. zap_leaf_array_get(zap_leaf_phys_t *l, zfs_endian_t endian, int blksft,
  665. int chunk, int array_len, char *buf)
  666. {
  667. int bseen = 0;
  668. while (bseen < array_len) {
  669. struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
  670. int toread = min(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
  671. if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
  672. /* Don't use errno because this error is to be ignored. */
  673. return ZFS_ERR_BAD_FS;
  674. memcpy(buf + bseen, la->la_array, toread);
  675. chunk = zfs_to_cpu16(la->la_next, endian);
  676. bseen += toread;
  677. }
  678. return ZFS_ERR_NONE;
  679. }
  680. /*
  681. * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
  682. * value for the property "name".
  683. *
  684. */
  685. /* XXX */
  686. static int
  687. zap_leaf_lookup(zap_leaf_phys_t *l, zfs_endian_t endian,
  688. int blksft, uint64_t h,
  689. const char *name, uint64_t *value)
  690. {
  691. uint16_t chunk;
  692. struct zap_leaf_entry *le;
  693. /* Verify if this is a valid leaf block */
  694. if (zfs_to_cpu64(l->l_hdr.lh_block_type, endian) != ZBT_LEAF) {
  695. printf("invalid leaf type\n");
  696. return ZFS_ERR_BAD_FS;
  697. }
  698. if (zfs_to_cpu32(l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC) {
  699. printf("invalid leaf magic\n");
  700. return ZFS_ERR_BAD_FS;
  701. }
  702. for (chunk = zfs_to_cpu16(l->l_hash[LEAF_HASH(blksft, h)], endian);
  703. chunk != CHAIN_END; chunk = le->le_next) {
  704. if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft)) {
  705. printf("invalid chunk number\n");
  706. return ZFS_ERR_BAD_FS;
  707. }
  708. le = ZAP_LEAF_ENTRY(l, blksft, chunk);
  709. /* Verify the chunk entry */
  710. if (le->le_type != ZAP_CHUNK_ENTRY) {
  711. printf("invalid chunk entry\n");
  712. return ZFS_ERR_BAD_FS;
  713. }
  714. if (zfs_to_cpu64(le->le_hash, endian) != h)
  715. continue;
  716. if (zap_leaf_array_equal(l, endian, blksft,
  717. zfs_to_cpu16(le->le_name_chunk, endian),
  718. zfs_to_cpu16(le->le_name_length, endian),
  719. name)) {
  720. struct zap_leaf_array *la;
  721. if (le->le_int_size != 8 || le->le_value_length != 1) {
  722. printf("invalid leaf chunk entry\n");
  723. return ZFS_ERR_BAD_FS;
  724. }
  725. /* get the uint64_t property value */
  726. la = &ZAP_LEAF_CHUNK(l, blksft, le->le_value_chunk).l_array;
  727. *value = be64_to_cpu(la->la_array64);
  728. return ZFS_ERR_NONE;
  729. }
  730. }
  731. printf("couldn't find '%s'\n", name);
  732. return ZFS_ERR_FILE_NOT_FOUND;
  733. }
  734. /* Verify if this is a fat zap header block */
  735. static int
  736. zap_verify(zap_phys_t *zap)
  737. {
  738. if (zap->zap_magic != (uint64_t) ZAP_MAGIC) {
  739. printf("bad ZAP magic\n");
  740. return ZFS_ERR_BAD_FS;
  741. }
  742. if (zap->zap_flags != 0) {
  743. printf("bad ZAP flags\n");
  744. return ZFS_ERR_BAD_FS;
  745. }
  746. if (zap->zap_salt == 0) {
  747. printf("bad ZAP salt\n");
  748. return ZFS_ERR_BAD_FS;
  749. }
  750. return ZFS_ERR_NONE;
  751. }
  752. /*
  753. * Fat ZAP lookup
  754. *
  755. */
  756. /* XXX */
  757. static int
  758. fzap_lookup(dnode_end_t *zap_dnode, zap_phys_t *zap,
  759. char *name, uint64_t *value, struct zfs_data *data)
  760. {
  761. void *l;
  762. uint64_t hash, idx, blkid;
  763. int blksft = zfs_log2(zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec,
  764. zap_dnode->endian) << DNODE_SHIFT);
  765. int err;
  766. zfs_endian_t leafendian;
  767. err = zap_verify(zap);
  768. if (err)
  769. return err;
  770. hash = zap_hash(zap->zap_salt, name);
  771. /* get block id from index */
  772. if (zap->zap_ptrtbl.zt_numblks != 0) {
  773. printf("external pointer tables not supported\n");
  774. return ZFS_ERR_NOT_IMPLEMENTED_YET;
  775. }
  776. idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
  777. blkid = ((uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))];
  778. /* Get the leaf block */
  779. if ((1U << blksft) < sizeof(zap_leaf_phys_t)) {
  780. printf("ZAP leaf is too small\n");
  781. return ZFS_ERR_BAD_FS;
  782. }
  783. err = dmu_read(zap_dnode, blkid, &l, &leafendian, data);
  784. if (err)
  785. return err;
  786. err = zap_leaf_lookup(l, leafendian, blksft, hash, name, value);
  787. free(l);
  788. return err;
  789. }
  790. /* XXX */
  791. static int
  792. fzap_iterate(dnode_end_t *zap_dnode, zap_phys_t *zap,
  793. int (*hook)(const char *name,
  794. uint64_t val,
  795. struct zfs_data *data),
  796. struct zfs_data *data)
  797. {
  798. zap_leaf_phys_t *l;
  799. void *l_in;
  800. uint64_t idx, blkid;
  801. uint16_t chunk;
  802. int blksft = zfs_log2(zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec,
  803. zap_dnode->endian) << DNODE_SHIFT);
  804. int err;
  805. zfs_endian_t endian;
  806. if (zap_verify(zap))
  807. return 0;
  808. /* get block id from index */
  809. if (zap->zap_ptrtbl.zt_numblks != 0) {
  810. printf("external pointer tables not supported\n");
  811. return 0;
  812. }
  813. /* Get the leaf block */
  814. if ((1U << blksft) < sizeof(zap_leaf_phys_t)) {
  815. printf("ZAP leaf is too small\n");
  816. return 0;
  817. }
  818. for (idx = 0; idx < zap->zap_ptrtbl.zt_numblks; idx++) {
  819. blkid = ((uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))];
  820. err = dmu_read(zap_dnode, blkid, &l_in, &endian, data);
  821. l = l_in;
  822. if (err)
  823. continue;
  824. /* Verify if this is a valid leaf block */
  825. if (zfs_to_cpu64(l->l_hdr.lh_block_type, endian) != ZBT_LEAF) {
  826. free(l);
  827. continue;
  828. }
  829. if (zfs_to_cpu32(l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC) {
  830. free(l);
  831. continue;
  832. }
  833. for (chunk = 0; chunk < ZAP_LEAF_NUMCHUNKS(blksft); chunk++) {
  834. char *buf;
  835. struct zap_leaf_array *la;
  836. struct zap_leaf_entry *le;
  837. uint64_t val;
  838. le = ZAP_LEAF_ENTRY(l, blksft, chunk);
  839. /* Verify the chunk entry */
  840. if (le->le_type != ZAP_CHUNK_ENTRY)
  841. continue;
  842. buf = malloc(zfs_to_cpu16(le->le_name_length, endian)
  843. + 1);
  844. if (zap_leaf_array_get(l, endian, blksft, le->le_name_chunk,
  845. le->le_name_length, buf)) {
  846. free(buf);
  847. continue;
  848. }
  849. buf[le->le_name_length] = 0;
  850. if (le->le_int_size != 8
  851. || zfs_to_cpu16(le->le_value_length, endian) != 1)
  852. continue;
  853. /* get the uint64_t property value */
  854. la = &ZAP_LEAF_CHUNK(l, blksft, le->le_value_chunk).l_array;
  855. val = be64_to_cpu(la->la_array64);
  856. if (hook(buf, val, data))
  857. return 1;
  858. free(buf);
  859. }
  860. }
  861. return 0;
  862. }
  863. /*
  864. * Read in the data of a zap object and find the value for a matching
  865. * property name.
  866. *
  867. */
  868. static int
  869. zap_lookup(dnode_end_t *zap_dnode, char *name, uint64_t *val,
  870. struct zfs_data *data)
  871. {
  872. uint64_t block_type;
  873. int size;
  874. void *zapbuf;
  875. int err;
  876. zfs_endian_t endian;
  877. /* Read in the first block of the zap object data. */
  878. size = zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec,
  879. zap_dnode->endian) << SPA_MINBLOCKSHIFT;
  880. err = dmu_read(zap_dnode, 0, &zapbuf, &endian, data);
  881. if (err)
  882. return err;
  883. block_type = zfs_to_cpu64(*((uint64_t *) zapbuf), endian);
  884. if (block_type == ZBT_MICRO) {
  885. err = (mzap_lookup(zapbuf, endian, size, name, val));
  886. free(zapbuf);
  887. return err;
  888. } else if (block_type == ZBT_HEADER) {
  889. /* this is a fat zap */
  890. err = (fzap_lookup(zap_dnode, zapbuf, name, val, data));
  891. free(zapbuf);
  892. return err;
  893. }
  894. printf("unknown ZAP type\n");
  895. free(zapbuf);
  896. return ZFS_ERR_BAD_FS;
  897. }
  898. static int
  899. zap_iterate(dnode_end_t *zap_dnode,
  900. int (*hook)(const char *name, uint64_t val,
  901. struct zfs_data *data),
  902. struct zfs_data *data)
  903. {
  904. uint64_t block_type;
  905. int size;
  906. void *zapbuf;
  907. int err;
  908. int ret;
  909. zfs_endian_t endian;
  910. /* Read in the first block of the zap object data. */
  911. size = zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec, zap_dnode->endian) << SPA_MINBLOCKSHIFT;
  912. err = dmu_read(zap_dnode, 0, &zapbuf, &endian, data);
  913. if (err)
  914. return 0;
  915. block_type = zfs_to_cpu64(*((uint64_t *) zapbuf), endian);
  916. if (block_type == ZBT_MICRO) {
  917. ret = mzap_iterate(zapbuf, endian, size, hook, data);
  918. free(zapbuf);
  919. return ret;
  920. } else if (block_type == ZBT_HEADER) {
  921. /* this is a fat zap */
  922. ret = fzap_iterate(zap_dnode, zapbuf, hook, data);
  923. free(zapbuf);
  924. return ret;
  925. }
  926. printf("unknown ZAP type\n");
  927. free(zapbuf);
  928. return 0;
  929. }
  930. /*
  931. * Get the dnode of an object number from the metadnode of an object set.
  932. *
  933. * Input
  934. * mdn - metadnode to get the object dnode
  935. * objnum - object number for the object dnode
  936. * buf - data buffer that holds the returning dnode
  937. */
  938. static int
  939. dnode_get(dnode_end_t *mdn, uint64_t objnum, uint8_t type,
  940. dnode_end_t *buf, struct zfs_data *data)
  941. {
  942. uint64_t blkid, blksz; /* the block id this object dnode is in */
  943. int epbs; /* shift of number of dnodes in a block */
  944. int idx; /* index within a block */
  945. void *dnbuf;
  946. int err;
  947. zfs_endian_t endian;
  948. blksz = zfs_to_cpu16(mdn->dn.dn_datablkszsec,
  949. mdn->endian) << SPA_MINBLOCKSHIFT;
  950. epbs = zfs_log2(blksz) - DNODE_SHIFT;
  951. blkid = objnum >> epbs;
  952. idx = objnum & ((1 << epbs) - 1);
  953. if (data->dnode_buf != NULL && memcmp(data->dnode_mdn, mdn,
  954. sizeof(*mdn)) == 0
  955. && objnum >= data->dnode_start && objnum < data->dnode_end) {
  956. memmove(&(buf->dn), &(data->dnode_buf)[idx], DNODE_SIZE);
  957. buf->endian = data->dnode_endian;
  958. if (type && buf->dn.dn_type != type) {
  959. printf("incorrect dnode type: %02X != %02x\n", buf->dn.dn_type, type);
  960. return ZFS_ERR_BAD_FS;
  961. }
  962. return ZFS_ERR_NONE;
  963. }
  964. err = dmu_read(mdn, blkid, &dnbuf, &endian, data);
  965. if (err)
  966. return err;
  967. free(data->dnode_buf);
  968. free(data->dnode_mdn);
  969. data->dnode_mdn = malloc(sizeof(*mdn));
  970. if (!data->dnode_mdn) {
  971. data->dnode_buf = 0;
  972. } else {
  973. memcpy(data->dnode_mdn, mdn, sizeof(*mdn));
  974. data->dnode_buf = dnbuf;
  975. data->dnode_start = blkid << epbs;
  976. data->dnode_end = (blkid + 1) << epbs;
  977. data->dnode_endian = endian;
  978. }
  979. memmove(&(buf->dn), (dnode_phys_t *) dnbuf + idx, DNODE_SIZE);
  980. buf->endian = endian;
  981. if (type && buf->dn.dn_type != type) {
  982. printf("incorrect dnode type\n");
  983. return ZFS_ERR_BAD_FS;
  984. }
  985. return ZFS_ERR_NONE;
  986. }
  987. /*
  988. * Get the file dnode for a given file name where mdn is the meta dnode
  989. * for this ZFS object set. When found, place the file dnode in dn.
  990. * The 'path' argument will be mangled.
  991. *
  992. */
  993. static int
  994. dnode_get_path(dnode_end_t *mdn, const char *path_in, dnode_end_t *dn,
  995. struct zfs_data *data)
  996. {
  997. uint64_t objnum, version;
  998. char *cname, ch;
  999. int err = ZFS_ERR_NONE;
  1000. char *path, *path_buf;
  1001. struct dnode_chain {
  1002. struct dnode_chain *next;
  1003. dnode_end_t dn;
  1004. };
  1005. struct dnode_chain *dnode_path = 0, *dn_new, *root;
  1006. dn_new = malloc(sizeof(*dn_new));
  1007. if (!dn_new)
  1008. return ZFS_ERR_OUT_OF_MEMORY;
  1009. dn_new->next = 0;
  1010. dnode_path = root = dn_new;
  1011. err = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
  1012. &(dnode_path->dn), data);
  1013. if (err) {
  1014. free(dn_new);
  1015. return err;
  1016. }
  1017. err = zap_lookup(&(dnode_path->dn), ZPL_VERSION_STR, &version, data);
  1018. if (err) {
  1019. free(dn_new);
  1020. return err;
  1021. }
  1022. if (version > ZPL_VERSION) {
  1023. free(dn_new);
  1024. printf("too new ZPL version\n");
  1025. return ZFS_ERR_NOT_IMPLEMENTED_YET;
  1026. }
  1027. err = zap_lookup(&(dnode_path->dn), ZFS_ROOT_OBJ, &objnum, data);
  1028. if (err) {
  1029. free(dn_new);
  1030. return err;
  1031. }
  1032. err = dnode_get(mdn, objnum, 0, &(dnode_path->dn), data);
  1033. if (err) {
  1034. free(dn_new);
  1035. return err;
  1036. }
  1037. path = path_buf = strdup(path_in);
  1038. if (!path_buf) {
  1039. free(dn_new);
  1040. return ZFS_ERR_OUT_OF_MEMORY;
  1041. }
  1042. while (1) {
  1043. /* skip leading slashes */
  1044. while (*path == '/')
  1045. path++;
  1046. if (!*path)
  1047. break;
  1048. /* get the next component name */
  1049. cname = path;
  1050. while (*path && *path != '/')
  1051. path++;
  1052. /* Skip dot. */
  1053. if (cname + 1 == path && cname[0] == '.')
  1054. continue;
  1055. /* Handle double dot. */
  1056. if (cname + 2 == path && cname[0] == '.' && cname[1] == '.') {
  1057. if (dn_new->next) {
  1058. dn_new = dnode_path;
  1059. dnode_path = dn_new->next;
  1060. free(dn_new);
  1061. } else {
  1062. printf("can't resolve ..\n");
  1063. err = ZFS_ERR_FILE_NOT_FOUND;
  1064. break;
  1065. }
  1066. continue;
  1067. }
  1068. ch = *path;
  1069. *path = 0; /* ensure null termination */
  1070. if (dnode_path->dn.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) {
  1071. free(path_buf);
  1072. printf("not a directory\n");
  1073. return ZFS_ERR_BAD_FILE_TYPE;
  1074. }
  1075. err = zap_lookup(&(dnode_path->dn), cname, &objnum, data);
  1076. if (err)
  1077. break;
  1078. dn_new = malloc(sizeof(*dn_new));
  1079. if (!dn_new) {
  1080. err = ZFS_ERR_OUT_OF_MEMORY;
  1081. break;
  1082. }
  1083. dn_new->next = dnode_path;
  1084. dnode_path = dn_new;
  1085. objnum = ZFS_DIRENT_OBJ(objnum);
  1086. err = dnode_get(mdn, objnum, 0, &(dnode_path->dn), data);
  1087. if (err)
  1088. break;
  1089. *path = ch;
  1090. }
  1091. if (!err)
  1092. memcpy(dn, &(dnode_path->dn), sizeof(*dn));
  1093. while (dnode_path) {
  1094. dn_new = dnode_path->next;
  1095. free(dnode_path);
  1096. dnode_path = dn_new;
  1097. }
  1098. free(path_buf);
  1099. return err;
  1100. }
  1101. /*
  1102. * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
  1103. * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
  1104. * of pool/rootfs.
  1105. *
  1106. * If no fsname and no obj are given, return the DSL_DIR metadnode.
  1107. * If fsname is given, return its metadnode and its matching object number.
  1108. * If only obj is given, return the metadnode for this object number.
  1109. *
  1110. */
  1111. static int
  1112. get_filesystem_dnode(dnode_end_t *mosmdn, char *fsname,
  1113. dnode_end_t *mdn, struct zfs_data *data)
  1114. {
  1115. uint64_t objnum;
  1116. int err;
  1117. err = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
  1118. DMU_OT_OBJECT_DIRECTORY, mdn, data);
  1119. if (err)
  1120. return err;
  1121. err = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum, data);
  1122. if (err)
  1123. return err;
  1124. err = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data);
  1125. if (err)
  1126. return err;
  1127. while (*fsname) {
  1128. uint64_t childobj;
  1129. char *cname, ch;
  1130. while (*fsname == '/')
  1131. fsname++;
  1132. if (!*fsname || *fsname == '@')
  1133. break;
  1134. cname = fsname;
  1135. while (*fsname && !isspace(*fsname) && *fsname != '/')
  1136. fsname++;
  1137. ch = *fsname;
  1138. *fsname = 0;
  1139. childobj = zfs_to_cpu64((((dsl_dir_phys_t *) DN_BONUS(&mdn->dn)))->dd_child_dir_zapobj, mdn->endian);
  1140. err = dnode_get(mosmdn, childobj,
  1141. DMU_OT_DSL_DIR_CHILD_MAP, mdn, data);
  1142. if (err)
  1143. return err;
  1144. err = zap_lookup(mdn, cname, &objnum, data);
  1145. if (err)
  1146. return err;
  1147. err = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data);
  1148. if (err)
  1149. return err;
  1150. *fsname = ch;
  1151. }
  1152. return ZFS_ERR_NONE;
  1153. }
  1154. static int
  1155. make_mdn(dnode_end_t *mdn, struct zfs_data *data)
  1156. {
  1157. void *osp;
  1158. blkptr_t *bp;
  1159. size_t ospsize;
  1160. int err;
  1161. bp = &(((dsl_dataset_phys_t *) DN_BONUS(&mdn->dn))->ds_bp);
  1162. err = zio_read(bp, mdn->endian, &osp, &ospsize, data);
  1163. if (err)
  1164. return err;
  1165. if (ospsize < OBJSET_PHYS_SIZE_V14) {
  1166. free(osp);
  1167. printf("too small osp\n");
  1168. return ZFS_ERR_BAD_FS;
  1169. }
  1170. mdn->endian = (zfs_to_cpu64(bp->blk_prop, mdn->endian)>>63) & 1;
  1171. memmove((char *) &(mdn->dn),
  1172. (char *) &((objset_phys_t *) osp)->os_meta_dnode, DNODE_SIZE);
  1173. free(osp);
  1174. return ZFS_ERR_NONE;
  1175. }
  1176. static int
  1177. dnode_get_fullpath(const char *fullpath, dnode_end_t *mdn,
  1178. uint64_t *mdnobj, dnode_end_t *dn, int *isfs,
  1179. struct zfs_data *data)
  1180. {
  1181. char *fsname, *snapname;
  1182. const char *ptr_at, *filename;
  1183. uint64_t headobj;
  1184. int err;
  1185. ptr_at = strchr(fullpath, '@');
  1186. if (!ptr_at) {
  1187. *isfs = 1;
  1188. filename = 0;
  1189. snapname = 0;
  1190. fsname = strdup(fullpath);
  1191. } else {
  1192. const char *ptr_slash = strchr(ptr_at, '/');
  1193. *isfs = 0;
  1194. fsname = malloc(ptr_at - fullpath + 1);
  1195. if (!fsname)
  1196. return ZFS_ERR_OUT_OF_MEMORY;
  1197. memcpy(fsname, fullpath, ptr_at - fullpath);
  1198. fsname[ptr_at - fullpath] = 0;
  1199. if (ptr_at[1] && ptr_at[1] != '/') {
  1200. snapname = malloc(ptr_slash - ptr_at);
  1201. if (!snapname) {
  1202. free(fsname);
  1203. return ZFS_ERR_OUT_OF_MEMORY;
  1204. }
  1205. memcpy(snapname, ptr_at + 1, ptr_slash - ptr_at - 1);
  1206. snapname[ptr_slash - ptr_at - 1] = 0;
  1207. } else {
  1208. snapname = 0;
  1209. }
  1210. if (ptr_slash)
  1211. filename = ptr_slash;
  1212. else
  1213. filename = "/";
  1214. printf("zfs fsname = '%s' snapname='%s' filename = '%s'\n",
  1215. fsname, snapname, filename);
  1216. }
  1217. err = get_filesystem_dnode(&(data->mos), fsname, dn, data);
  1218. if (err) {
  1219. free(fsname);
  1220. free(snapname);
  1221. return err;
  1222. }
  1223. headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&dn->dn))->dd_head_dataset_obj, dn->endian);
  1224. err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data);
  1225. if (err) {
  1226. free(fsname);
  1227. free(snapname);
  1228. return err;
  1229. }
  1230. if (snapname) {
  1231. uint64_t snapobj;
  1232. snapobj = zfs_to_cpu64(((dsl_dataset_phys_t *) DN_BONUS(&mdn->dn))->ds_snapnames_zapobj, mdn->endian);
  1233. err = dnode_get(&(data->mos), snapobj,
  1234. DMU_OT_DSL_DS_SNAP_MAP, mdn, data);
  1235. if (!err)
  1236. err = zap_lookup(mdn, snapname, &headobj, data);
  1237. if (!err)
  1238. err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data);
  1239. if (err) {
  1240. free(fsname);
  1241. free(snapname);
  1242. return err;
  1243. }
  1244. }
  1245. if (mdnobj)
  1246. *mdnobj = headobj;
  1247. make_mdn(mdn, data);
  1248. if (*isfs) {
  1249. free(fsname);
  1250. free(snapname);
  1251. return ZFS_ERR_NONE;
  1252. }
  1253. err = dnode_get_path(mdn, filename, dn, data);
  1254. free(fsname);
  1255. free(snapname);
  1256. return err;
  1257. }
  1258. /*
  1259. * For a given XDR packed nvlist, verify the first 4 bytes and move on.
  1260. *
  1261. * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
  1262. *
  1263. * encoding method/host endian (4 bytes)
  1264. * nvl_version (4 bytes)
  1265. * nvl_nvflag (4 bytes)
  1266. * encoded nvpairs:
  1267. * encoded size of the nvpair (4 bytes)
  1268. * decoded size of the nvpair (4 bytes)
  1269. * name string size (4 bytes)
  1270. * name string data (sizeof(NV_ALIGN4(string))
  1271. * data type (4 bytes)
  1272. * # of elements in the nvpair (4 bytes)
  1273. * data
  1274. * 2 zero's for the last nvpair
  1275. * (end of the entire list) (8 bytes)
  1276. *
  1277. */
  1278. static int
  1279. nvlist_find_value(char *nvlist, char *name, int valtype, char **val,
  1280. size_t *size_out, size_t *nelm_out)
  1281. {
  1282. int name_len, type, encode_size;
  1283. char *nvpair, *nvp_name;
  1284. /* Verify if the 1st and 2nd byte in the nvlist are valid. */
  1285. /* NOTE: independently of what endianness header announces all
  1286. subsequent values are big-endian. */
  1287. if (nvlist[0] != NV_ENCODE_XDR || (nvlist[1] != NV_LITTLE_ENDIAN
  1288. && nvlist[1] != NV_BIG_ENDIAN)) {
  1289. printf("zfs incorrect nvlist header\n");
  1290. return ZFS_ERR_BAD_FS;
  1291. }
  1292. /* skip the header, nvl_version, and nvl_nvflag */
  1293. nvlist = nvlist + 4 * 3;
  1294. /*
  1295. * Loop thru the nvpair list
  1296. * The XDR representation of an integer is in big-endian byte order.
  1297. */
  1298. while ((encode_size = be32_to_cpu(*(uint32_t *) nvlist))) {
  1299. int nelm;
  1300. nvpair = nvlist + 4 * 2; /* skip the encode/decode size */
  1301. name_len = be32_to_cpu(*(uint32_t *) nvpair);
  1302. nvpair += 4;
  1303. nvp_name = nvpair;
  1304. nvpair = nvpair + ((name_len + 3) & ~3); /* align */
  1305. type = be32_to_cpu(*(uint32_t *) nvpair);
  1306. nvpair += 4;
  1307. nelm = be32_to_cpu(*(uint32_t *) nvpair);
  1308. if (nelm < 1) {
  1309. printf("empty nvpair\n");
  1310. return ZFS_ERR_BAD_FS;
  1311. }
  1312. nvpair += 4;
  1313. if ((strncmp(nvp_name, name, name_len) == 0) && type == valtype) {
  1314. *val = nvpair;
  1315. *size_out = encode_size;
  1316. if (nelm_out)
  1317. *nelm_out = nelm;
  1318. return 1;
  1319. }
  1320. nvlist += encode_size; /* goto the next nvpair */
  1321. }
  1322. return 0;
  1323. }
  1324. int
  1325. zfs_nvlist_lookup_uint64(char *nvlist, char *name, uint64_t *out)
  1326. {
  1327. char *nvpair;
  1328. size_t size;
  1329. int found;
  1330. found = nvlist_find_value(nvlist, name, DATA_TYPE_UINT64, &nvpair, &size, 0);
  1331. if (!found)
  1332. return 0;
  1333. if (size < sizeof(uint64_t)) {
  1334. printf("invalid uint64\n");
  1335. return ZFS_ERR_BAD_FS;
  1336. }
  1337. *out = be64_to_cpu(*(uint64_t *) nvpair);
  1338. return 1;
  1339. }
  1340. char *
  1341. zfs_nvlist_lookup_string(char *nvlist, char *name)
  1342. {
  1343. char *nvpair;
  1344. char *ret;
  1345. size_t slen;
  1346. size_t size;
  1347. int found;
  1348. found = nvlist_find_value(nvlist, name, DATA_TYPE_STRING, &nvpair, &size, 0);
  1349. if (!found)
  1350. return 0;
  1351. if (size < 4) {
  1352. printf("invalid string\n");
  1353. return 0;
  1354. }
  1355. slen = be32_to_cpu(*(uint32_t *) nvpair);
  1356. if (slen > size - 4)
  1357. slen = size - 4;
  1358. ret = malloc(slen + 1);
  1359. if (!ret)
  1360. return 0;
  1361. memcpy(ret, nvpair + 4, slen);
  1362. ret[slen] = 0;
  1363. return ret;
  1364. }
  1365. char *
  1366. zfs_nvlist_lookup_nvlist(char *nvlist, char *name)
  1367. {
  1368. char *nvpair;
  1369. char *ret;
  1370. size_t size;
  1371. int found;
  1372. found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair,
  1373. &size, 0);
  1374. if (!found)
  1375. return 0;
  1376. ret = calloc(1, size + 3 * sizeof(uint32_t));
  1377. if (!ret)
  1378. return 0;
  1379. memcpy(ret, nvlist, sizeof(uint32_t));
  1380. memcpy(ret + sizeof(uint32_t), nvpair, size);
  1381. return ret;
  1382. }
  1383. int
  1384. zfs_nvlist_lookup_nvlist_array_get_nelm(char *nvlist, char *name)
  1385. {
  1386. char *nvpair;
  1387. size_t nelm, size;
  1388. int found;
  1389. found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair,
  1390. &size, &nelm);
  1391. if (!found)
  1392. return -1;
  1393. return nelm;
  1394. }
  1395. char *
  1396. zfs_nvlist_lookup_nvlist_array(char *nvlist, char *name,
  1397. size_t index)
  1398. {
  1399. char *nvpair, *nvpairptr;
  1400. int found;
  1401. char *ret;
  1402. size_t size;
  1403. unsigned i;
  1404. size_t nelm;
  1405. found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair,
  1406. &size, &nelm);
  1407. if (!found)
  1408. return 0;
  1409. if (index >= nelm) {
  1410. printf("trying to lookup past nvlist array\n");
  1411. return 0;
  1412. }
  1413. nvpairptr = nvpair;
  1414. for (i = 0; i < index; i++) {
  1415. uint32_t encode_size;
  1416. /* skip the header, nvl_version, and nvl_nvflag */
  1417. nvpairptr = nvpairptr + 4 * 2;
  1418. while (nvpairptr < nvpair + size
  1419. && (encode_size = be32_to_cpu(*(uint32_t *) nvpairptr)))
  1420. nvlist += encode_size; /* goto the next nvpair */
  1421. nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
  1422. }
  1423. if (nvpairptr >= nvpair + size
  1424. || nvpairptr + be32_to_cpu(*(uint32_t *) (nvpairptr + 4 * 2))
  1425. >= nvpair + size) {
  1426. printf("incorrect nvlist array\n");
  1427. return 0;
  1428. }
  1429. ret = calloc(1, be32_to_cpu(*(uint32_t *) (nvpairptr + 4 * 2))
  1430. + 3 * sizeof(uint32_t));
  1431. if (!ret)
  1432. return 0;
  1433. memcpy(ret, nvlist, sizeof(uint32_t));
  1434. memcpy(ret + sizeof(uint32_t), nvpairptr, size);
  1435. return ret;
  1436. }
  1437. static int
  1438. int_zfs_fetch_nvlist(struct zfs_data *data, char **nvlist)
  1439. {
  1440. int err;
  1441. *nvlist = malloc(VDEV_PHYS_SIZE);
  1442. /* Read in the vdev name-value pair list (112K). */
  1443. err = zfs_devread(data->vdev_phys_sector, 0, VDEV_PHYS_SIZE, *nvlist);
  1444. if (err) {
  1445. free(*nvlist);
  1446. *nvlist = 0;
  1447. return err;
  1448. }
  1449. return ZFS_ERR_NONE;
  1450. }
  1451. /*
  1452. * Check the disk label information and retrieve needed vdev name-value pairs.
  1453. *
  1454. */
  1455. static int
  1456. check_pool_label(struct zfs_data *data)
  1457. {
  1458. uint64_t pool_state;
  1459. char *nvlist; /* for the pool */
  1460. char *vdevnvlist; /* for the vdev */
  1461. uint64_t diskguid;
  1462. uint64_t version;
  1463. int found;
  1464. int err;
  1465. err = int_zfs_fetch_nvlist(data, &nvlist);
  1466. if (err)
  1467. return err;
  1468. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_STATE,
  1469. &pool_state);
  1470. if (!found) {
  1471. free(nvlist);
  1472. printf("zfs pool state not found\n");
  1473. return ZFS_ERR_BAD_FS;
  1474. }
  1475. if (pool_state == POOL_STATE_DESTROYED) {
  1476. free(nvlist);
  1477. printf("zpool is marked as destroyed\n");
  1478. return ZFS_ERR_BAD_FS;
  1479. }
  1480. data->label_txg = 0;
  1481. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_TXG,
  1482. &data->label_txg);
  1483. if (!found) {
  1484. free(nvlist);
  1485. printf("zfs pool txg not found\n");
  1486. return ZFS_ERR_BAD_FS;
  1487. }
  1488. /* not an active device */
  1489. if (data->label_txg == 0) {
  1490. free(nvlist);
  1491. printf("zpool is not active\n");
  1492. return ZFS_ERR_BAD_FS;
  1493. }
  1494. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_VERSION,
  1495. &version);
  1496. if (!found) {
  1497. free(nvlist);
  1498. printf("zpool config version not found\n");
  1499. return ZFS_ERR_BAD_FS;
  1500. }
  1501. if (version > SPA_VERSION) {
  1502. free(nvlist);
  1503. printf("SPA version too new %llu > %llu\n",
  1504. (unsigned long long) version,
  1505. (unsigned long long) SPA_VERSION);
  1506. return ZFS_ERR_NOT_IMPLEMENTED_YET;
  1507. }
  1508. vdevnvlist = zfs_nvlist_lookup_nvlist(nvlist, ZPOOL_CONFIG_VDEV_TREE);
  1509. if (!vdevnvlist) {
  1510. free(nvlist);
  1511. printf("ZFS config vdev tree not found\n");
  1512. return ZFS_ERR_BAD_FS;
  1513. }
  1514. found = zfs_nvlist_lookup_uint64(vdevnvlist, ZPOOL_CONFIG_ASHIFT,
  1515. &data->vdev_ashift);
  1516. free(vdevnvlist);
  1517. if (!found) {
  1518. free(nvlist);
  1519. printf("ZPOOL config ashift not found\n");
  1520. return ZFS_ERR_BAD_FS;
  1521. }
  1522. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_GUID, &diskguid);
  1523. if (!found) {
  1524. free(nvlist);
  1525. printf("ZPOOL config guid not found\n");
  1526. return ZFS_ERR_BAD_FS;
  1527. }
  1528. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_GUID, &data->pool_guid);
  1529. if (!found) {
  1530. free(nvlist);
  1531. printf("ZPOOL config pool guid not found\n");
  1532. return ZFS_ERR_BAD_FS;
  1533. }
  1534. free(nvlist);
  1535. printf("ZFS Pool GUID: %llu (%016llx) Label: GUID: %llu (%016llx), txg: %llu, SPA v%llu, ashift: %llu\n",
  1536. (unsigned long long) data->pool_guid,
  1537. (unsigned long long) data->pool_guid,
  1538. (unsigned long long) diskguid,
  1539. (unsigned long long) diskguid,
  1540. (unsigned long long) data->label_txg,
  1541. (unsigned long long) version,
  1542. (unsigned long long) data->vdev_ashift);
  1543. return ZFS_ERR_NONE;
  1544. }
  1545. /*
  1546. * vdev_label_start returns the physical disk offset (in bytes) of
  1547. * label "l".
  1548. */
  1549. static uint64_t vdev_label_start(uint64_t psize, int l)
  1550. {
  1551. return (l * sizeof(vdev_label_t) + (l < VDEV_LABELS / 2 ?
  1552. 0 : psize -
  1553. VDEV_LABELS * sizeof(vdev_label_t)));
  1554. }
  1555. void
  1556. zfs_unmount(struct zfs_data *data)
  1557. {
  1558. free(data->dnode_buf);
  1559. free(data->dnode_mdn);
  1560. free(data->file_buf);
  1561. free(data);
  1562. }
  1563. /*
  1564. * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
  1565. * to the memory address MOS.
  1566. *
  1567. */
  1568. struct zfs_data *
  1569. zfs_mount(device_t dev)
  1570. {
  1571. struct zfs_data *data = 0;
  1572. int label = 0, bestlabel = -1;
  1573. char *ub_array;
  1574. uberblock_t *ubbest;
  1575. uberblock_t *ubcur = NULL;
  1576. void *osp = 0;
  1577. size_t ospsize;
  1578. int err;
  1579. data = malloc(sizeof(*data));
  1580. if (!data)
  1581. return 0;
  1582. memset(data, 0, sizeof(*data));
  1583. ub_array = malloc(VDEV_UBERBLOCK_RING);
  1584. if (!ub_array) {
  1585. zfs_unmount(data);
  1586. return 0;
  1587. }
  1588. ubbest = malloc(sizeof(*ubbest));
  1589. if (!ubbest) {
  1590. free(ub_array);
  1591. zfs_unmount(data);
  1592. return 0;
  1593. }
  1594. memset(ubbest, 0, sizeof(*ubbest));
  1595. /*
  1596. * some eltorito stacks don't give us a size and
  1597. * we end up setting the size to MAXUINT, further
  1598. * some of these devices stop working once a single
  1599. * read past the end has been issued. Checking
  1600. * for a maximum part_length and skipping the backup
  1601. * labels at the end of the slice/partition/device
  1602. * avoids breaking down on such devices.
  1603. */
  1604. const int vdevnum =
  1605. dev->part_length == 0 ?
  1606. VDEV_LABELS / 2 : VDEV_LABELS;
  1607. /* Size in bytes of the device (disk or partition) aligned to label size*/
  1608. uint64_t device_size =
  1609. dev->part_length << SECTOR_BITS;
  1610. const uint64_t alignedbytes =
  1611. P2ALIGN(device_size, (uint64_t) sizeof(vdev_label_t));
  1612. for (label = 0; label < vdevnum; label++) {
  1613. uint64_t labelstartbytes = vdev_label_start(alignedbytes, label);
  1614. uint64_t labelstart = labelstartbytes >> SECTOR_BITS;
  1615. debug("zfs reading label %d at sector %llu (byte %llu)\n",
  1616. label, (unsigned long long) labelstart,
  1617. (unsigned long long) labelstartbytes);
  1618. data->vdev_phys_sector = labelstart +
  1619. ((VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE) >> SECTOR_BITS);
  1620. err = check_pool_label(data);
  1621. if (err) {
  1622. printf("zfs error checking label %d\n", label);
  1623. continue;
  1624. }
  1625. /* Read in the uberblock ring (128K). */
  1626. err = zfs_devread(data->vdev_phys_sector +
  1627. (VDEV_PHYS_SIZE >> SECTOR_BITS),
  1628. 0, VDEV_UBERBLOCK_RING, ub_array);
  1629. if (err) {
  1630. printf("zfs error reading uberblock ring for label %d\n", label);
  1631. continue;
  1632. }
  1633. ubcur = find_bestub(ub_array, data);
  1634. if (!ubcur) {
  1635. printf("zfs No good uberblocks found in label %d\n", label);
  1636. continue;
  1637. }
  1638. if (vdev_uberblock_compare(ubcur, ubbest) > 0) {
  1639. /* Looks like the block is good, so use it.*/
  1640. memcpy(ubbest, ubcur, sizeof(*ubbest));
  1641. bestlabel = label;
  1642. debug("zfs Current best uberblock found in label %d\n", label);
  1643. }
  1644. }
  1645. free(ub_array);
  1646. /* We zero'd the structure to begin with. If we never assigned to it,
  1647. magic will still be zero. */
  1648. if (!ubbest->ub_magic) {
  1649. printf("couldn't find a valid ZFS label\n");
  1650. zfs_unmount(data);
  1651. free(ubbest);
  1652. return 0;
  1653. }
  1654. debug("zfs ubbest %p in label %d\n", ubbest, bestlabel);
  1655. zfs_endian_t ub_endian =
  1656. zfs_to_cpu64(ubbest->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC
  1657. ? LITTLE_ENDIAN : BIG_ENDIAN;
  1658. debug("zfs endian set to %s\n", !ub_endian ? "big" : "little");
  1659. err = zio_read(&ubbest->ub_rootbp, ub_endian, &osp, &ospsize, data);
  1660. if (err) {
  1661. printf("couldn't zio_read object directory\n");
  1662. zfs_unmount(data);
  1663. free(osp);
  1664. free(ubbest);
  1665. return 0;
  1666. }
  1667. if (ospsize < OBJSET_PHYS_SIZE_V14) {
  1668. printf("osp too small\n");
  1669. zfs_unmount(data);
  1670. free(osp);
  1671. free(ubbest);
  1672. return 0;
  1673. }
  1674. /* Got the MOS. Save it at the memory addr MOS. */
  1675. memmove(&(data->mos.dn), &((objset_phys_t *) osp)->os_meta_dnode, DNODE_SIZE);
  1676. data->mos.endian =
  1677. (zfs_to_cpu64(ubbest->ub_rootbp.blk_prop, ub_endian) >> 63) & 1;
  1678. memmove(&(data->current_uberblock), ubbest, sizeof(uberblock_t));
  1679. free(osp);
  1680. free(ubbest);
  1681. return data;
  1682. }
  1683. int
  1684. zfs_fetch_nvlist(device_t dev, char **nvlist)
  1685. {
  1686. struct zfs_data *zfs;
  1687. int err;
  1688. zfs = zfs_mount(dev);
  1689. if (!zfs)
  1690. return ZFS_ERR_BAD_FS;
  1691. err = int_zfs_fetch_nvlist(zfs, nvlist);
  1692. zfs_unmount(zfs);
  1693. return err;
  1694. }
  1695. /*
  1696. * zfs_open() locates a file in the rootpool by following the
  1697. * MOS and places the dnode of the file in the memory address DNODE.
  1698. */
  1699. int
  1700. zfs_open(struct zfs_file *file, const char *fsfilename)
  1701. {
  1702. struct zfs_data *data;
  1703. int err;
  1704. int isfs;
  1705. data = zfs_mount(file->device);
  1706. if (!data)
  1707. return ZFS_ERR_BAD_FS;
  1708. err = dnode_get_fullpath(fsfilename, &(data->mdn), 0,
  1709. &(data->dnode), &isfs, data);
  1710. if (err) {
  1711. zfs_unmount(data);
  1712. return err;
  1713. }
  1714. if (isfs) {
  1715. zfs_unmount(data);
  1716. printf("Missing @ or / separator\n");
  1717. return ZFS_ERR_FILE_NOT_FOUND;
  1718. }
  1719. /* We found the dnode for this file. Verify if it is a plain file. */
  1720. if (data->dnode.dn.dn_type != DMU_OT_PLAIN_FILE_CONTENTS) {
  1721. zfs_unmount(data);
  1722. printf("not a file\n");
  1723. return ZFS_ERR_BAD_FILE_TYPE;
  1724. }
  1725. /* get the file size and set the file position to 0 */
  1726. /*
  1727. * For DMU_OT_SA we will need to locate the SIZE attribute
  1728. * attribute, which could be either in the bonus buffer
  1729. * or the "spill" block.
  1730. */
  1731. if (data->dnode.dn.dn_bonustype == DMU_OT_SA) {
  1732. void *sahdrp;
  1733. int hdrsize;
  1734. if (data->dnode.dn.dn_bonuslen != 0) {
  1735. sahdrp = (sa_hdr_phys_t *) DN_BONUS(&data->dnode.dn);
  1736. } else if (data->dnode.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
  1737. blkptr_t *bp = &data->dnode.dn.dn_spill;
  1738. err = zio_read(bp, data->dnode.endian, &sahdrp, NULL, data);
  1739. if (err)
  1740. return err;
  1741. } else {
  1742. printf("filesystem is corrupt :(\n");
  1743. return ZFS_ERR_BAD_FS;
  1744. }
  1745. hdrsize = SA_HDR_SIZE(((sa_hdr_phys_t *) sahdrp));
  1746. file->size = *(uint64_t *) ((char *) sahdrp + hdrsize + SA_SIZE_OFFSET);
  1747. if ((data->dnode.dn.dn_bonuslen == 0) &&
  1748. (data->dnode.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR))
  1749. free(sahdrp);
  1750. } else {
  1751. file->size = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&data->dnode.dn))->zp_size, data->dnode.endian);
  1752. }
  1753. file->data = data;
  1754. file->offset = 0;
  1755. return ZFS_ERR_NONE;
  1756. }
  1757. uint64_t
  1758. zfs_read(zfs_file_t file, char *buf, uint64_t len)
  1759. {
  1760. struct zfs_data *data = (struct zfs_data *) file->data;
  1761. int blksz, movesize;
  1762. uint64_t length;
  1763. int64_t red;
  1764. int err;
  1765. if (data->file_buf == NULL) {
  1766. data->file_buf = malloc(SPA_MAXBLOCKSIZE);
  1767. if (!data->file_buf)
  1768. return -1;
  1769. data->file_start = data->file_end = 0;
  1770. }
  1771. /*
  1772. * If offset is in memory, move it into the buffer provided and return.
  1773. */
  1774. if (file->offset >= data->file_start
  1775. && file->offset + len <= data->file_end) {
  1776. memmove(buf, data->file_buf + file->offset - data->file_start,
  1777. len);
  1778. return len;
  1779. }
  1780. blksz = zfs_to_cpu16(data->dnode.dn.dn_datablkszsec,
  1781. data->dnode.endian) << SPA_MINBLOCKSHIFT;
  1782. /*
  1783. * Entire Dnode is too big to fit into the space available. We
  1784. * will need to read it in chunks. This could be optimized to
  1785. * read in as large a chunk as there is space available, but for
  1786. * now, this only reads in one data block at a time.
  1787. */
  1788. length = len;
  1789. red = 0;
  1790. while (length) {
  1791. void *t;
  1792. /*
  1793. * Find requested blkid and the offset within that block.
  1794. */
  1795. uint64_t blkid = file->offset + red;
  1796. blkid = do_div(blkid, blksz);
  1797. free(data->file_buf);
  1798. data->file_buf = 0;
  1799. err = dmu_read(&(data->dnode), blkid, &t,
  1800. 0, data);
  1801. data->file_buf = t;
  1802. if (err)
  1803. return -1;
  1804. data->file_start = blkid * blksz;
  1805. data->file_end = data->file_start + blksz;
  1806. movesize = min(length, data->file_end - (int)file->offset - red);
  1807. memmove(buf, data->file_buf + file->offset + red
  1808. - data->file_start, movesize);
  1809. buf += movesize;
  1810. length -= movesize;
  1811. red += movesize;
  1812. }
  1813. return len;
  1814. }
  1815. int
  1816. zfs_close(zfs_file_t file)
  1817. {
  1818. zfs_unmount((struct zfs_data *) file->data);
  1819. return ZFS_ERR_NONE;
  1820. }
  1821. int
  1822. zfs_getmdnobj(device_t dev, const char *fsfilename,
  1823. uint64_t *mdnobj)
  1824. {
  1825. struct zfs_data *data;
  1826. int err;
  1827. int isfs;
  1828. data = zfs_mount(dev);
  1829. if (!data)
  1830. return ZFS_ERR_BAD_FS;
  1831. err = dnode_get_fullpath(fsfilename, &(data->mdn), mdnobj,
  1832. &(data->dnode), &isfs, data);
  1833. zfs_unmount(data);
  1834. return err;
  1835. }
  1836. static void
  1837. fill_fs_info(struct zfs_dirhook_info *info,
  1838. dnode_end_t mdn, struct zfs_data *data)
  1839. {
  1840. int err;
  1841. dnode_end_t dn;
  1842. uint64_t objnum;
  1843. uint64_t headobj;
  1844. memset(info, 0, sizeof(*info));
  1845. info->dir = 1;
  1846. if (mdn.dn.dn_type == DMU_OT_DSL_DIR) {
  1847. headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&mdn.dn))->dd_head_dataset_obj, mdn.endian);
  1848. err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, &mdn, data);
  1849. if (err) {
  1850. printf("zfs failed here 1\n");
  1851. return;
  1852. }
  1853. }
  1854. make_mdn(&mdn, data);
  1855. err = dnode_get(&mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
  1856. &dn, data);
  1857. if (err) {
  1858. printf("zfs failed here 2\n");
  1859. return;
  1860. }
  1861. err = zap_lookup(&dn, ZFS_ROOT_OBJ, &objnum, data);
  1862. if (err) {
  1863. printf("zfs failed here 3\n");
  1864. return;
  1865. }
  1866. err = dnode_get(&mdn, objnum, 0, &dn, data);
  1867. if (err) {
  1868. printf("zfs failed here 4\n");
  1869. return;
  1870. }
  1871. info->mtimeset = 1;
  1872. info->mtime = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&dn.dn))->zp_mtime[0], dn.endian);
  1873. return;
  1874. }
  1875. static int iterate_zap(const char *name, uint64_t val, struct zfs_data *data)
  1876. {
  1877. struct zfs_dirhook_info info;
  1878. dnode_end_t dn;
  1879. memset(&info, 0, sizeof(info));
  1880. dnode_get(&(data->mdn), val, 0, &dn, data);
  1881. info.mtimeset = 1;
  1882. info.mtime = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&dn.dn))->zp_mtime[0], dn.endian);
  1883. info.dir = (dn.dn.dn_type == DMU_OT_DIRECTORY_CONTENTS);
  1884. debug("zfs type=%d, name=%s\n",
  1885. (int)dn.dn.dn_type, (char *)name);
  1886. if (!data->userhook)
  1887. return 0;
  1888. return data->userhook(name, &info);
  1889. }
  1890. static int iterate_zap_fs(const char *name, uint64_t val, struct zfs_data *data)
  1891. {
  1892. struct zfs_dirhook_info info;
  1893. dnode_end_t mdn;
  1894. int err;
  1895. err = dnode_get(&(data->mos), val, 0, &mdn, data);
  1896. if (err)
  1897. return 0;
  1898. if (mdn.dn.dn_type != DMU_OT_DSL_DIR)
  1899. return 0;
  1900. fill_fs_info(&info, mdn, data);
  1901. if (!data->userhook)
  1902. return 0;
  1903. return data->userhook(name, &info);
  1904. }
  1905. static int iterate_zap_snap(const char *name, uint64_t val, struct zfs_data *data)
  1906. {
  1907. struct zfs_dirhook_info info;
  1908. char *name2;
  1909. int ret = 0;
  1910. dnode_end_t mdn;
  1911. int err;
  1912. err = dnode_get(&(data->mos), val, 0, &mdn, data);
  1913. if (err)
  1914. return 0;
  1915. if (mdn.dn.dn_type != DMU_OT_DSL_DATASET)
  1916. return 0;
  1917. fill_fs_info(&info, mdn, data);
  1918. name2 = malloc(strlen(name) + 2);
  1919. name2[0] = '@';
  1920. memcpy(name2 + 1, name, strlen(name) + 1);
  1921. if (data->userhook)
  1922. ret = data->userhook(name2, &info);
  1923. free(name2);
  1924. return ret;
  1925. }
  1926. int
  1927. zfs_ls(device_t device, const char *path,
  1928. int (*hook)(const char *, const struct zfs_dirhook_info *))
  1929. {
  1930. struct zfs_data *data;
  1931. int err;
  1932. int isfs;
  1933. data = zfs_mount(device);
  1934. if (!data)
  1935. return ZFS_ERR_BAD_FS;
  1936. data->userhook = hook;
  1937. err = dnode_get_fullpath(path, &(data->mdn), 0, &(data->dnode), &isfs, data);
  1938. if (err) {
  1939. zfs_unmount(data);
  1940. return err;
  1941. }
  1942. if (isfs) {
  1943. uint64_t childobj, headobj;
  1944. uint64_t snapobj;
  1945. dnode_end_t dn;
  1946. struct zfs_dirhook_info info;
  1947. fill_fs_info(&info, data->dnode, data);
  1948. hook("@", &info);
  1949. childobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&data->dnode.dn))->dd_child_dir_zapobj, data->dnode.endian);
  1950. headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&data->dnode.dn))->dd_head_dataset_obj, data->dnode.endian);
  1951. err = dnode_get(&(data->mos), childobj,
  1952. DMU_OT_DSL_DIR_CHILD_MAP, &dn, data);
  1953. if (err) {
  1954. zfs_unmount(data);
  1955. return err;
  1956. }
  1957. zap_iterate(&dn, iterate_zap_fs, data);
  1958. err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, &dn, data);
  1959. if (err) {
  1960. zfs_unmount(data);
  1961. return err;
  1962. }
  1963. snapobj = zfs_to_cpu64(((dsl_dataset_phys_t *) DN_BONUS(&dn.dn))->ds_snapnames_zapobj, dn.endian);
  1964. err = dnode_get(&(data->mos), snapobj,
  1965. DMU_OT_DSL_DS_SNAP_MAP, &dn, data);
  1966. if (err) {
  1967. zfs_unmount(data);
  1968. return err;
  1969. }
  1970. zap_iterate(&dn, iterate_zap_snap, data);
  1971. } else {
  1972. if (data->dnode.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) {
  1973. zfs_unmount(data);
  1974. printf("not a directory\n");
  1975. return ZFS_ERR_BAD_FILE_TYPE;
  1976. }
  1977. zap_iterate(&(data->dnode), iterate_zap, data);
  1978. }
  1979. zfs_unmount(data);
  1980. return ZFS_ERR_NONE;
  1981. }