zfs.c 55 KB

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