zfs.c 54 KB

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