zfs.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * ZFS filesystem ported to u-boot by
  5. * Jorgen Lundman <lundman at lundman.net>
  6. *
  7. * GRUB -- GRand Unified Bootloader
  8. * Copyright (C) 1999,2000,2001,2002,2003,2004
  9. * Free Software Foundation, Inc.
  10. * Copyright 2004 Sun Microsystems, Inc.
  11. */
  12. #include <common.h>
  13. #include <log.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. struct blk_desc *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 = NULL;
  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. free(zapbuf);
  897. return ZFS_ERR_BAD_FS;
  898. }
  899. static int
  900. zap_iterate(dnode_end_t *zap_dnode,
  901. int (*hook)(const char *name, uint64_t val,
  902. struct zfs_data *data),
  903. struct zfs_data *data)
  904. {
  905. uint64_t block_type;
  906. int size;
  907. void *zapbuf;
  908. int err;
  909. int ret;
  910. zfs_endian_t endian;
  911. /* Read in the first block of the zap object data. */
  912. size = zfs_to_cpu16(zap_dnode->dn.dn_datablkszsec, zap_dnode->endian) << SPA_MINBLOCKSHIFT;
  913. err = dmu_read(zap_dnode, 0, &zapbuf, &endian, data);
  914. if (err)
  915. return 0;
  916. block_type = zfs_to_cpu64(*((uint64_t *) zapbuf), endian);
  917. if (block_type == ZBT_MICRO) {
  918. ret = mzap_iterate(zapbuf, endian, size, hook, data);
  919. free(zapbuf);
  920. return ret;
  921. } else if (block_type == ZBT_HEADER) {
  922. /* this is a fat zap */
  923. ret = fzap_iterate(zap_dnode, zapbuf, hook, data);
  924. free(zapbuf);
  925. return ret;
  926. }
  927. printf("unknown ZAP type\n");
  928. free(zapbuf);
  929. return 0;
  930. }
  931. /*
  932. * Get the dnode of an object number from the metadnode of an object set.
  933. *
  934. * Input
  935. * mdn - metadnode to get the object dnode
  936. * objnum - object number for the object dnode
  937. * buf - data buffer that holds the returning dnode
  938. */
  939. static int
  940. dnode_get(dnode_end_t *mdn, uint64_t objnum, uint8_t type,
  941. dnode_end_t *buf, struct zfs_data *data)
  942. {
  943. uint64_t blkid, blksz; /* the block id this object dnode is in */
  944. int epbs; /* shift of number of dnodes in a block */
  945. int idx; /* index within a block */
  946. void *dnbuf;
  947. int err;
  948. zfs_endian_t endian;
  949. blksz = zfs_to_cpu16(mdn->dn.dn_datablkszsec,
  950. mdn->endian) << SPA_MINBLOCKSHIFT;
  951. epbs = zfs_log2(blksz) - DNODE_SHIFT;
  952. blkid = objnum >> epbs;
  953. idx = objnum & ((1 << epbs) - 1);
  954. if (data->dnode_buf != NULL && memcmp(data->dnode_mdn, mdn,
  955. sizeof(*mdn)) == 0
  956. && objnum >= data->dnode_start && objnum < data->dnode_end) {
  957. memmove(&(buf->dn), &(data->dnode_buf)[idx], DNODE_SIZE);
  958. buf->endian = data->dnode_endian;
  959. if (type && buf->dn.dn_type != type) {
  960. printf("incorrect dnode type: %02X != %02x\n", buf->dn.dn_type, type);
  961. return ZFS_ERR_BAD_FS;
  962. }
  963. return ZFS_ERR_NONE;
  964. }
  965. err = dmu_read(mdn, blkid, &dnbuf, &endian, data);
  966. if (err)
  967. return err;
  968. free(data->dnode_buf);
  969. free(data->dnode_mdn);
  970. data->dnode_mdn = malloc(sizeof(*mdn));
  971. if (!data->dnode_mdn) {
  972. data->dnode_buf = 0;
  973. } else {
  974. memcpy(data->dnode_mdn, mdn, sizeof(*mdn));
  975. data->dnode_buf = dnbuf;
  976. data->dnode_start = blkid << epbs;
  977. data->dnode_end = (blkid + 1) << epbs;
  978. data->dnode_endian = endian;
  979. }
  980. memmove(&(buf->dn), (dnode_phys_t *) dnbuf + idx, DNODE_SIZE);
  981. buf->endian = endian;
  982. if (type && buf->dn.dn_type != type) {
  983. printf("incorrect dnode type\n");
  984. return ZFS_ERR_BAD_FS;
  985. }
  986. return ZFS_ERR_NONE;
  987. }
  988. /*
  989. * Get the file dnode for a given file name where mdn is the meta dnode
  990. * for this ZFS object set. When found, place the file dnode in dn.
  991. * The 'path' argument will be mangled.
  992. *
  993. */
  994. static int
  995. dnode_get_path(dnode_end_t *mdn, const char *path_in, dnode_end_t *dn,
  996. struct zfs_data *data)
  997. {
  998. uint64_t objnum, version;
  999. char *cname, ch;
  1000. int err = ZFS_ERR_NONE;
  1001. char *path, *path_buf;
  1002. struct dnode_chain {
  1003. struct dnode_chain *next;
  1004. dnode_end_t dn;
  1005. };
  1006. struct dnode_chain *dnode_path = 0, *dn_new, *root;
  1007. dn_new = malloc(sizeof(*dn_new));
  1008. if (!dn_new)
  1009. return ZFS_ERR_OUT_OF_MEMORY;
  1010. dn_new->next = 0;
  1011. dnode_path = root = dn_new;
  1012. err = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
  1013. &(dnode_path->dn), data);
  1014. if (err) {
  1015. free(dn_new);
  1016. return err;
  1017. }
  1018. err = zap_lookup(&(dnode_path->dn), ZPL_VERSION_STR, &version, data);
  1019. if (err) {
  1020. free(dn_new);
  1021. return err;
  1022. }
  1023. if (version > ZPL_VERSION) {
  1024. free(dn_new);
  1025. printf("too new ZPL version\n");
  1026. return ZFS_ERR_NOT_IMPLEMENTED_YET;
  1027. }
  1028. err = zap_lookup(&(dnode_path->dn), ZFS_ROOT_OBJ, &objnum, data);
  1029. if (err) {
  1030. free(dn_new);
  1031. return err;
  1032. }
  1033. err = dnode_get(mdn, objnum, 0, &(dnode_path->dn), data);
  1034. if (err) {
  1035. free(dn_new);
  1036. return err;
  1037. }
  1038. path = path_buf = strdup(path_in);
  1039. if (!path_buf) {
  1040. free(dn_new);
  1041. return ZFS_ERR_OUT_OF_MEMORY;
  1042. }
  1043. while (1) {
  1044. /* skip leading slashes */
  1045. while (*path == '/')
  1046. path++;
  1047. if (!*path)
  1048. break;
  1049. /* get the next component name */
  1050. cname = path;
  1051. while (*path && *path != '/')
  1052. path++;
  1053. /* Skip dot. */
  1054. if (cname + 1 == path && cname[0] == '.')
  1055. continue;
  1056. /* Handle double dot. */
  1057. if (cname + 2 == path && cname[0] == '.' && cname[1] == '.') {
  1058. if (dn_new->next) {
  1059. dn_new = dnode_path;
  1060. dnode_path = dn_new->next;
  1061. free(dn_new);
  1062. } else {
  1063. printf("can't resolve ..\n");
  1064. err = ZFS_ERR_FILE_NOT_FOUND;
  1065. break;
  1066. }
  1067. continue;
  1068. }
  1069. ch = *path;
  1070. *path = 0; /* ensure null termination */
  1071. if (dnode_path->dn.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) {
  1072. free(path_buf);
  1073. printf("not a directory\n");
  1074. return ZFS_ERR_BAD_FILE_TYPE;
  1075. }
  1076. err = zap_lookup(&(dnode_path->dn), cname, &objnum, data);
  1077. if (err)
  1078. break;
  1079. dn_new = malloc(sizeof(*dn_new));
  1080. if (!dn_new) {
  1081. err = ZFS_ERR_OUT_OF_MEMORY;
  1082. break;
  1083. }
  1084. dn_new->next = dnode_path;
  1085. dnode_path = dn_new;
  1086. objnum = ZFS_DIRENT_OBJ(objnum);
  1087. err = dnode_get(mdn, objnum, 0, &(dnode_path->dn), data);
  1088. if (err)
  1089. break;
  1090. *path = ch;
  1091. }
  1092. if (!err)
  1093. memcpy(dn, &(dnode_path->dn), sizeof(*dn));
  1094. while (dnode_path) {
  1095. dn_new = dnode_path->next;
  1096. free(dnode_path);
  1097. dnode_path = dn_new;
  1098. }
  1099. free(path_buf);
  1100. return err;
  1101. }
  1102. /*
  1103. * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
  1104. * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
  1105. * of pool/rootfs.
  1106. *
  1107. * If no fsname and no obj are given, return the DSL_DIR metadnode.
  1108. * If fsname is given, return its metadnode and its matching object number.
  1109. * If only obj is given, return the metadnode for this object number.
  1110. *
  1111. */
  1112. static int
  1113. get_filesystem_dnode(dnode_end_t *mosmdn, char *fsname,
  1114. dnode_end_t *mdn, struct zfs_data *data)
  1115. {
  1116. uint64_t objnum;
  1117. int err;
  1118. err = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
  1119. DMU_OT_OBJECT_DIRECTORY, mdn, data);
  1120. if (err)
  1121. return err;
  1122. err = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum, data);
  1123. if (err)
  1124. return err;
  1125. err = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data);
  1126. if (err)
  1127. return err;
  1128. while (*fsname) {
  1129. uint64_t childobj;
  1130. char *cname, ch;
  1131. while (*fsname == '/')
  1132. fsname++;
  1133. if (!*fsname || *fsname == '@')
  1134. break;
  1135. cname = fsname;
  1136. while (*fsname && !isspace(*fsname) && *fsname != '/')
  1137. fsname++;
  1138. ch = *fsname;
  1139. *fsname = 0;
  1140. childobj = zfs_to_cpu64((((dsl_dir_phys_t *) DN_BONUS(&mdn->dn)))->dd_child_dir_zapobj, mdn->endian);
  1141. err = dnode_get(mosmdn, childobj,
  1142. DMU_OT_DSL_DIR_CHILD_MAP, mdn, data);
  1143. if (err)
  1144. return err;
  1145. err = zap_lookup(mdn, cname, &objnum, data);
  1146. if (err)
  1147. return err;
  1148. err = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data);
  1149. if (err)
  1150. return err;
  1151. *fsname = ch;
  1152. }
  1153. return ZFS_ERR_NONE;
  1154. }
  1155. static int
  1156. make_mdn(dnode_end_t *mdn, struct zfs_data *data)
  1157. {
  1158. void *osp;
  1159. blkptr_t *bp;
  1160. size_t ospsize;
  1161. int err;
  1162. bp = &(((dsl_dataset_phys_t *) DN_BONUS(&mdn->dn))->ds_bp);
  1163. err = zio_read(bp, mdn->endian, &osp, &ospsize, data);
  1164. if (err)
  1165. return err;
  1166. if (ospsize < OBJSET_PHYS_SIZE_V14) {
  1167. free(osp);
  1168. printf("too small osp\n");
  1169. return ZFS_ERR_BAD_FS;
  1170. }
  1171. mdn->endian = (zfs_to_cpu64(bp->blk_prop, mdn->endian)>>63) & 1;
  1172. memmove((char *) &(mdn->dn),
  1173. (char *) &((objset_phys_t *) osp)->os_meta_dnode, DNODE_SIZE);
  1174. free(osp);
  1175. return ZFS_ERR_NONE;
  1176. }
  1177. static int
  1178. dnode_get_fullpath(const char *fullpath, dnode_end_t *mdn,
  1179. uint64_t *mdnobj, dnode_end_t *dn, int *isfs,
  1180. struct zfs_data *data)
  1181. {
  1182. char *fsname, *snapname;
  1183. const char *ptr_at, *filename;
  1184. uint64_t headobj;
  1185. int err;
  1186. ptr_at = strchr(fullpath, '@');
  1187. if (!ptr_at) {
  1188. *isfs = 1;
  1189. filename = 0;
  1190. snapname = 0;
  1191. fsname = strdup(fullpath);
  1192. } else {
  1193. const char *ptr_slash = strchr(ptr_at, '/');
  1194. *isfs = 0;
  1195. fsname = malloc(ptr_at - fullpath + 1);
  1196. if (!fsname)
  1197. return ZFS_ERR_OUT_OF_MEMORY;
  1198. memcpy(fsname, fullpath, ptr_at - fullpath);
  1199. fsname[ptr_at - fullpath] = 0;
  1200. if (ptr_at[1] && ptr_at[1] != '/') {
  1201. snapname = malloc(ptr_slash - ptr_at);
  1202. if (!snapname) {
  1203. free(fsname);
  1204. return ZFS_ERR_OUT_OF_MEMORY;
  1205. }
  1206. memcpy(snapname, ptr_at + 1, ptr_slash - ptr_at - 1);
  1207. snapname[ptr_slash - ptr_at - 1] = 0;
  1208. } else {
  1209. snapname = 0;
  1210. }
  1211. if (ptr_slash)
  1212. filename = ptr_slash;
  1213. else
  1214. filename = "/";
  1215. printf("zfs fsname = '%s' snapname='%s' filename = '%s'\n",
  1216. fsname, snapname, filename);
  1217. }
  1218. err = get_filesystem_dnode(&(data->mos), fsname, dn, data);
  1219. if (err) {
  1220. free(fsname);
  1221. free(snapname);
  1222. return err;
  1223. }
  1224. headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&dn->dn))->dd_head_dataset_obj, dn->endian);
  1225. err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data);
  1226. if (err) {
  1227. free(fsname);
  1228. free(snapname);
  1229. return err;
  1230. }
  1231. if (snapname) {
  1232. uint64_t snapobj;
  1233. snapobj = zfs_to_cpu64(((dsl_dataset_phys_t *) DN_BONUS(&mdn->dn))->ds_snapnames_zapobj, mdn->endian);
  1234. err = dnode_get(&(data->mos), snapobj,
  1235. DMU_OT_DSL_DS_SNAP_MAP, mdn, data);
  1236. if (!err)
  1237. err = zap_lookup(mdn, snapname, &headobj, data);
  1238. if (!err)
  1239. err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data);
  1240. if (err) {
  1241. free(fsname);
  1242. free(snapname);
  1243. return err;
  1244. }
  1245. }
  1246. if (mdnobj)
  1247. *mdnobj = headobj;
  1248. make_mdn(mdn, data);
  1249. if (*isfs) {
  1250. free(fsname);
  1251. free(snapname);
  1252. return ZFS_ERR_NONE;
  1253. }
  1254. err = dnode_get_path(mdn, filename, dn, data);
  1255. free(fsname);
  1256. free(snapname);
  1257. return err;
  1258. }
  1259. /*
  1260. * For a given XDR packed nvlist, verify the first 4 bytes and move on.
  1261. *
  1262. * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
  1263. *
  1264. * encoding method/host endian (4 bytes)
  1265. * nvl_version (4 bytes)
  1266. * nvl_nvflag (4 bytes)
  1267. * encoded nvpairs:
  1268. * encoded size of the nvpair (4 bytes)
  1269. * decoded size of the nvpair (4 bytes)
  1270. * name string size (4 bytes)
  1271. * name string data (sizeof(NV_ALIGN4(string))
  1272. * data type (4 bytes)
  1273. * # of elements in the nvpair (4 bytes)
  1274. * data
  1275. * 2 zero's for the last nvpair
  1276. * (end of the entire list) (8 bytes)
  1277. *
  1278. */
  1279. static int
  1280. nvlist_find_value(char *nvlist, char *name, int valtype, char **val,
  1281. size_t *size_out, size_t *nelm_out)
  1282. {
  1283. int name_len, type, encode_size;
  1284. char *nvpair, *nvp_name;
  1285. /* Verify if the 1st and 2nd byte in the nvlist are valid. */
  1286. /* NOTE: independently of what endianness header announces all
  1287. subsequent values are big-endian. */
  1288. if (nvlist[0] != NV_ENCODE_XDR || (nvlist[1] != NV_LITTLE_ENDIAN
  1289. && nvlist[1] != NV_BIG_ENDIAN)) {
  1290. printf("zfs incorrect nvlist header\n");
  1291. return ZFS_ERR_BAD_FS;
  1292. }
  1293. /* skip the header, nvl_version, and nvl_nvflag */
  1294. nvlist = nvlist + 4 * 3;
  1295. /*
  1296. * Loop thru the nvpair list
  1297. * The XDR representation of an integer is in big-endian byte order.
  1298. */
  1299. while ((encode_size = be32_to_cpu(*(uint32_t *) nvlist))) {
  1300. int nelm;
  1301. nvpair = nvlist + 4 * 2; /* skip the encode/decode size */
  1302. name_len = be32_to_cpu(*(uint32_t *) nvpair);
  1303. nvpair += 4;
  1304. nvp_name = nvpair;
  1305. nvpair = nvpair + ((name_len + 3) & ~3); /* align */
  1306. type = be32_to_cpu(*(uint32_t *) nvpair);
  1307. nvpair += 4;
  1308. nelm = be32_to_cpu(*(uint32_t *) nvpair);
  1309. if (nelm < 1) {
  1310. printf("empty nvpair\n");
  1311. return ZFS_ERR_BAD_FS;
  1312. }
  1313. nvpair += 4;
  1314. if ((strncmp(nvp_name, name, name_len) == 0) && type == valtype) {
  1315. *val = nvpair;
  1316. *size_out = encode_size;
  1317. if (nelm_out)
  1318. *nelm_out = nelm;
  1319. return 1;
  1320. }
  1321. nvlist += encode_size; /* goto the next nvpair */
  1322. }
  1323. return 0;
  1324. }
  1325. int
  1326. zfs_nvlist_lookup_uint64(char *nvlist, char *name, uint64_t *out)
  1327. {
  1328. char *nvpair;
  1329. size_t size;
  1330. int found;
  1331. found = nvlist_find_value(nvlist, name, DATA_TYPE_UINT64, &nvpair, &size, 0);
  1332. if (!found)
  1333. return 0;
  1334. if (size < sizeof(uint64_t)) {
  1335. printf("invalid uint64\n");
  1336. return ZFS_ERR_BAD_FS;
  1337. }
  1338. *out = be64_to_cpu(*(uint64_t *) nvpair);
  1339. return 1;
  1340. }
  1341. char *
  1342. zfs_nvlist_lookup_string(char *nvlist, char *name)
  1343. {
  1344. char *nvpair;
  1345. char *ret;
  1346. size_t slen;
  1347. size_t size;
  1348. int found;
  1349. found = nvlist_find_value(nvlist, name, DATA_TYPE_STRING, &nvpair, &size, 0);
  1350. if (!found)
  1351. return 0;
  1352. if (size < 4) {
  1353. printf("invalid string\n");
  1354. return 0;
  1355. }
  1356. slen = be32_to_cpu(*(uint32_t *) nvpair);
  1357. if (slen > size - 4)
  1358. slen = size - 4;
  1359. ret = malloc(slen + 1);
  1360. if (!ret)
  1361. return 0;
  1362. memcpy(ret, nvpair + 4, slen);
  1363. ret[slen] = 0;
  1364. return ret;
  1365. }
  1366. char *
  1367. zfs_nvlist_lookup_nvlist(char *nvlist, char *name)
  1368. {
  1369. char *nvpair;
  1370. char *ret;
  1371. size_t size;
  1372. int found;
  1373. found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair,
  1374. &size, 0);
  1375. if (!found)
  1376. return 0;
  1377. ret = calloc(1, size + 3 * sizeof(uint32_t));
  1378. if (!ret)
  1379. return 0;
  1380. memcpy(ret, nvlist, sizeof(uint32_t));
  1381. memcpy(ret + sizeof(uint32_t), nvpair, size);
  1382. return ret;
  1383. }
  1384. int
  1385. zfs_nvlist_lookup_nvlist_array_get_nelm(char *nvlist, char *name)
  1386. {
  1387. char *nvpair;
  1388. size_t nelm, size;
  1389. int found;
  1390. found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair,
  1391. &size, &nelm);
  1392. if (!found)
  1393. return -1;
  1394. return nelm;
  1395. }
  1396. char *
  1397. zfs_nvlist_lookup_nvlist_array(char *nvlist, char *name,
  1398. size_t index)
  1399. {
  1400. char *nvpair, *nvpairptr;
  1401. int found;
  1402. char *ret;
  1403. size_t size;
  1404. unsigned i;
  1405. size_t nelm;
  1406. found = nvlist_find_value(nvlist, name, DATA_TYPE_NVLIST, &nvpair,
  1407. &size, &nelm);
  1408. if (!found)
  1409. return 0;
  1410. if (index >= nelm) {
  1411. printf("trying to lookup past nvlist array\n");
  1412. return 0;
  1413. }
  1414. nvpairptr = nvpair;
  1415. for (i = 0; i < index; i++) {
  1416. uint32_t encode_size;
  1417. /* skip the header, nvl_version, and nvl_nvflag */
  1418. nvpairptr = nvpairptr + 4 * 2;
  1419. while (nvpairptr < nvpair + size
  1420. && (encode_size = be32_to_cpu(*(uint32_t *) nvpairptr)))
  1421. nvlist += encode_size; /* goto the next nvpair */
  1422. nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
  1423. }
  1424. if (nvpairptr >= nvpair + size
  1425. || nvpairptr + be32_to_cpu(*(uint32_t *) (nvpairptr + 4 * 2))
  1426. >= nvpair + size) {
  1427. printf("incorrect nvlist array\n");
  1428. return 0;
  1429. }
  1430. ret = calloc(1, be32_to_cpu(*(uint32_t *) (nvpairptr + 4 * 2))
  1431. + 3 * sizeof(uint32_t));
  1432. if (!ret)
  1433. return 0;
  1434. memcpy(ret, nvlist, sizeof(uint32_t));
  1435. memcpy(ret + sizeof(uint32_t), nvpairptr, size);
  1436. return ret;
  1437. }
  1438. static int
  1439. int_zfs_fetch_nvlist(struct zfs_data *data, char **nvlist)
  1440. {
  1441. int err;
  1442. *nvlist = malloc(VDEV_PHYS_SIZE);
  1443. /* Read in the vdev name-value pair list (112K). */
  1444. err = zfs_devread(data->vdev_phys_sector, 0, VDEV_PHYS_SIZE, *nvlist);
  1445. if (err) {
  1446. free(*nvlist);
  1447. *nvlist = 0;
  1448. return err;
  1449. }
  1450. return ZFS_ERR_NONE;
  1451. }
  1452. /*
  1453. * Check the disk label information and retrieve needed vdev name-value pairs.
  1454. *
  1455. */
  1456. static int
  1457. check_pool_label(struct zfs_data *data)
  1458. {
  1459. uint64_t pool_state;
  1460. char *nvlist; /* for the pool */
  1461. char *vdevnvlist; /* for the vdev */
  1462. uint64_t diskguid;
  1463. uint64_t version;
  1464. int found;
  1465. int err;
  1466. err = int_zfs_fetch_nvlist(data, &nvlist);
  1467. if (err)
  1468. return err;
  1469. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_STATE,
  1470. &pool_state);
  1471. if (!found) {
  1472. free(nvlist);
  1473. printf("zfs pool state not found\n");
  1474. return ZFS_ERR_BAD_FS;
  1475. }
  1476. if (pool_state == POOL_STATE_DESTROYED) {
  1477. free(nvlist);
  1478. printf("zpool is marked as destroyed\n");
  1479. return ZFS_ERR_BAD_FS;
  1480. }
  1481. data->label_txg = 0;
  1482. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_TXG,
  1483. &data->label_txg);
  1484. if (!found) {
  1485. free(nvlist);
  1486. printf("zfs pool txg not found\n");
  1487. return ZFS_ERR_BAD_FS;
  1488. }
  1489. /* not an active device */
  1490. if (data->label_txg == 0) {
  1491. free(nvlist);
  1492. printf("zpool is not active\n");
  1493. return ZFS_ERR_BAD_FS;
  1494. }
  1495. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_VERSION,
  1496. &version);
  1497. if (!found) {
  1498. free(nvlist);
  1499. printf("zpool config version not found\n");
  1500. return ZFS_ERR_BAD_FS;
  1501. }
  1502. if (version > SPA_VERSION) {
  1503. free(nvlist);
  1504. printf("SPA version too new %llu > %llu\n",
  1505. (unsigned long long) version,
  1506. (unsigned long long) SPA_VERSION);
  1507. return ZFS_ERR_NOT_IMPLEMENTED_YET;
  1508. }
  1509. vdevnvlist = zfs_nvlist_lookup_nvlist(nvlist, ZPOOL_CONFIG_VDEV_TREE);
  1510. if (!vdevnvlist) {
  1511. free(nvlist);
  1512. printf("ZFS config vdev tree not found\n");
  1513. return ZFS_ERR_BAD_FS;
  1514. }
  1515. found = zfs_nvlist_lookup_uint64(vdevnvlist, ZPOOL_CONFIG_ASHIFT,
  1516. &data->vdev_ashift);
  1517. free(vdevnvlist);
  1518. if (!found) {
  1519. free(nvlist);
  1520. printf("ZPOOL config ashift not found\n");
  1521. return ZFS_ERR_BAD_FS;
  1522. }
  1523. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_GUID, &diskguid);
  1524. if (!found) {
  1525. free(nvlist);
  1526. printf("ZPOOL config guid not found\n");
  1527. return ZFS_ERR_BAD_FS;
  1528. }
  1529. found = zfs_nvlist_lookup_uint64(nvlist, ZPOOL_CONFIG_POOL_GUID, &data->pool_guid);
  1530. if (!found) {
  1531. free(nvlist);
  1532. printf("ZPOOL config pool guid not found\n");
  1533. return ZFS_ERR_BAD_FS;
  1534. }
  1535. free(nvlist);
  1536. printf("ZFS Pool GUID: %llu (%016llx) Label: GUID: %llu (%016llx), txg: %llu, SPA v%llu, ashift: %llu\n",
  1537. (unsigned long long) data->pool_guid,
  1538. (unsigned long long) data->pool_guid,
  1539. (unsigned long long) diskguid,
  1540. (unsigned long long) diskguid,
  1541. (unsigned long long) data->label_txg,
  1542. (unsigned long long) version,
  1543. (unsigned long long) data->vdev_ashift);
  1544. return ZFS_ERR_NONE;
  1545. }
  1546. /*
  1547. * vdev_label_start returns the physical disk offset (in bytes) of
  1548. * label "l".
  1549. */
  1550. static uint64_t vdev_label_start(uint64_t psize, int l)
  1551. {
  1552. return (l * sizeof(vdev_label_t) + (l < VDEV_LABELS / 2 ?
  1553. 0 : psize -
  1554. VDEV_LABELS * sizeof(vdev_label_t)));
  1555. }
  1556. void
  1557. zfs_unmount(struct zfs_data *data)
  1558. {
  1559. free(data->dnode_buf);
  1560. free(data->dnode_mdn);
  1561. free(data->file_buf);
  1562. free(data);
  1563. }
  1564. /*
  1565. * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
  1566. * to the memory address MOS.
  1567. *
  1568. */
  1569. struct zfs_data *
  1570. zfs_mount(device_t dev)
  1571. {
  1572. struct zfs_data *data = 0;
  1573. int label = 0, bestlabel = -1;
  1574. char *ub_array;
  1575. uberblock_t *ubbest;
  1576. uberblock_t *ubcur = NULL;
  1577. void *osp = 0;
  1578. size_t ospsize;
  1579. int err;
  1580. data = malloc(sizeof(*data));
  1581. if (!data)
  1582. return 0;
  1583. memset(data, 0, sizeof(*data));
  1584. ub_array = malloc(VDEV_UBERBLOCK_RING);
  1585. if (!ub_array) {
  1586. zfs_unmount(data);
  1587. return 0;
  1588. }
  1589. ubbest = malloc(sizeof(*ubbest));
  1590. if (!ubbest) {
  1591. free(ub_array);
  1592. zfs_unmount(data);
  1593. return 0;
  1594. }
  1595. memset(ubbest, 0, sizeof(*ubbest));
  1596. /*
  1597. * some eltorito stacks don't give us a size and
  1598. * we end up setting the size to MAXUINT, further
  1599. * some of these devices stop working once a single
  1600. * read past the end has been issued. Checking
  1601. * for a maximum part_length and skipping the backup
  1602. * labels at the end of the slice/partition/device
  1603. * avoids breaking down on such devices.
  1604. */
  1605. const int vdevnum =
  1606. dev->part_length == 0 ?
  1607. VDEV_LABELS / 2 : VDEV_LABELS;
  1608. /* Size in bytes of the device (disk or partition) aligned to label size*/
  1609. uint64_t device_size =
  1610. dev->part_length << SECTOR_BITS;
  1611. const uint64_t alignedbytes =
  1612. P2ALIGN(device_size, (uint64_t) sizeof(vdev_label_t));
  1613. for (label = 0; label < vdevnum; label++) {
  1614. uint64_t labelstartbytes = vdev_label_start(alignedbytes, label);
  1615. uint64_t labelstart = labelstartbytes >> SECTOR_BITS;
  1616. debug("zfs reading label %d at sector %llu (byte %llu)\n",
  1617. label, (unsigned long long) labelstart,
  1618. (unsigned long long) labelstartbytes);
  1619. data->vdev_phys_sector = labelstart +
  1620. ((VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE) >> SECTOR_BITS);
  1621. err = check_pool_label(data);
  1622. if (err) {
  1623. printf("zfs error checking label %d\n", label);
  1624. continue;
  1625. }
  1626. /* Read in the uberblock ring (128K). */
  1627. err = zfs_devread(data->vdev_phys_sector +
  1628. (VDEV_PHYS_SIZE >> SECTOR_BITS),
  1629. 0, VDEV_UBERBLOCK_RING, ub_array);
  1630. if (err) {
  1631. printf("zfs error reading uberblock ring for label %d\n", label);
  1632. continue;
  1633. }
  1634. ubcur = find_bestub(ub_array, data);
  1635. if (!ubcur) {
  1636. printf("zfs No good uberblocks found in label %d\n", label);
  1637. continue;
  1638. }
  1639. if (vdev_uberblock_compare(ubcur, ubbest) > 0) {
  1640. /* Looks like the block is good, so use it.*/
  1641. memcpy(ubbest, ubcur, sizeof(*ubbest));
  1642. bestlabel = label;
  1643. debug("zfs Current best uberblock found in label %d\n", label);
  1644. }
  1645. }
  1646. free(ub_array);
  1647. /* We zero'd the structure to begin with. If we never assigned to it,
  1648. magic will still be zero. */
  1649. if (!ubbest->ub_magic) {
  1650. printf("couldn't find a valid ZFS label\n");
  1651. zfs_unmount(data);
  1652. free(ubbest);
  1653. return 0;
  1654. }
  1655. debug("zfs ubbest %p in label %d\n", ubbest, bestlabel);
  1656. zfs_endian_t ub_endian =
  1657. zfs_to_cpu64(ubbest->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC
  1658. ? LITTLE_ENDIAN : BIG_ENDIAN;
  1659. debug("zfs endian set to %s\n", !ub_endian ? "big" : "little");
  1660. err = zio_read(&ubbest->ub_rootbp, ub_endian, &osp, &ospsize, data);
  1661. if (err) {
  1662. printf("couldn't zio_read object directory\n");
  1663. zfs_unmount(data);
  1664. free(osp);
  1665. free(ubbest);
  1666. return 0;
  1667. }
  1668. if (ospsize < OBJSET_PHYS_SIZE_V14) {
  1669. printf("osp too small\n");
  1670. zfs_unmount(data);
  1671. free(osp);
  1672. free(ubbest);
  1673. return 0;
  1674. }
  1675. /* Got the MOS. Save it at the memory addr MOS. */
  1676. memmove(&(data->mos.dn), &((objset_phys_t *) osp)->os_meta_dnode, DNODE_SIZE);
  1677. data->mos.endian =
  1678. (zfs_to_cpu64(ubbest->ub_rootbp.blk_prop, ub_endian) >> 63) & 1;
  1679. memmove(&(data->current_uberblock), ubbest, sizeof(uberblock_t));
  1680. free(osp);
  1681. free(ubbest);
  1682. return data;
  1683. }
  1684. int
  1685. zfs_fetch_nvlist(device_t dev, char **nvlist)
  1686. {
  1687. struct zfs_data *zfs;
  1688. int err;
  1689. zfs = zfs_mount(dev);
  1690. if (!zfs)
  1691. return ZFS_ERR_BAD_FS;
  1692. err = int_zfs_fetch_nvlist(zfs, nvlist);
  1693. zfs_unmount(zfs);
  1694. return err;
  1695. }
  1696. /*
  1697. * zfs_open() locates a file in the rootpool by following the
  1698. * MOS and places the dnode of the file in the memory address DNODE.
  1699. */
  1700. int
  1701. zfs_open(struct zfs_file *file, const char *fsfilename)
  1702. {
  1703. struct zfs_data *data;
  1704. int err;
  1705. int isfs;
  1706. data = zfs_mount(file->device);
  1707. if (!data)
  1708. return ZFS_ERR_BAD_FS;
  1709. err = dnode_get_fullpath(fsfilename, &(data->mdn), 0,
  1710. &(data->dnode), &isfs, data);
  1711. if (err) {
  1712. zfs_unmount(data);
  1713. return err;
  1714. }
  1715. if (isfs) {
  1716. zfs_unmount(data);
  1717. printf("Missing @ or / separator\n");
  1718. return ZFS_ERR_FILE_NOT_FOUND;
  1719. }
  1720. /* We found the dnode for this file. Verify if it is a plain file. */
  1721. if (data->dnode.dn.dn_type != DMU_OT_PLAIN_FILE_CONTENTS) {
  1722. zfs_unmount(data);
  1723. printf("not a file\n");
  1724. return ZFS_ERR_BAD_FILE_TYPE;
  1725. }
  1726. /* get the file size and set the file position to 0 */
  1727. /*
  1728. * For DMU_OT_SA we will need to locate the SIZE attribute
  1729. * attribute, which could be either in the bonus buffer
  1730. * or the "spill" block.
  1731. */
  1732. if (data->dnode.dn.dn_bonustype == DMU_OT_SA) {
  1733. void *sahdrp;
  1734. int hdrsize;
  1735. if (data->dnode.dn.dn_bonuslen != 0) {
  1736. sahdrp = (sa_hdr_phys_t *) DN_BONUS(&data->dnode.dn);
  1737. } else if (data->dnode.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
  1738. blkptr_t *bp = &data->dnode.dn.dn_spill;
  1739. err = zio_read(bp, data->dnode.endian, &sahdrp, NULL, data);
  1740. if (err)
  1741. return err;
  1742. } else {
  1743. printf("filesystem is corrupt :(\n");
  1744. return ZFS_ERR_BAD_FS;
  1745. }
  1746. hdrsize = SA_HDR_SIZE(((sa_hdr_phys_t *) sahdrp));
  1747. file->size = *(uint64_t *) ((char *) sahdrp + hdrsize + SA_SIZE_OFFSET);
  1748. if ((data->dnode.dn.dn_bonuslen == 0) &&
  1749. (data->dnode.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR))
  1750. free(sahdrp);
  1751. } else {
  1752. file->size = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&data->dnode.dn))->zp_size, data->dnode.endian);
  1753. }
  1754. file->data = data;
  1755. file->offset = 0;
  1756. return ZFS_ERR_NONE;
  1757. }
  1758. uint64_t
  1759. zfs_read(zfs_file_t file, char *buf, uint64_t len)
  1760. {
  1761. struct zfs_data *data = (struct zfs_data *) file->data;
  1762. int blksz, movesize;
  1763. uint64_t length;
  1764. int64_t red;
  1765. int err;
  1766. if (data->file_buf == NULL) {
  1767. data->file_buf = malloc(SPA_MAXBLOCKSIZE);
  1768. if (!data->file_buf)
  1769. return -1;
  1770. data->file_start = data->file_end = 0;
  1771. }
  1772. /*
  1773. * If offset is in memory, move it into the buffer provided and return.
  1774. */
  1775. if (file->offset >= data->file_start
  1776. && file->offset + len <= data->file_end) {
  1777. memmove(buf, data->file_buf + file->offset - data->file_start,
  1778. len);
  1779. return len;
  1780. }
  1781. blksz = zfs_to_cpu16(data->dnode.dn.dn_datablkszsec,
  1782. data->dnode.endian) << SPA_MINBLOCKSHIFT;
  1783. /*
  1784. * Entire Dnode is too big to fit into the space available. We
  1785. * will need to read it in chunks. This could be optimized to
  1786. * read in as large a chunk as there is space available, but for
  1787. * now, this only reads in one data block at a time.
  1788. */
  1789. length = len;
  1790. red = 0;
  1791. while (length) {
  1792. void *t;
  1793. /*
  1794. * Find requested blkid and the offset within that block.
  1795. */
  1796. uint64_t blkid = file->offset + red;
  1797. blkid = do_div(blkid, blksz);
  1798. free(data->file_buf);
  1799. data->file_buf = 0;
  1800. err = dmu_read(&(data->dnode), blkid, &t,
  1801. 0, data);
  1802. data->file_buf = t;
  1803. if (err)
  1804. return -1;
  1805. data->file_start = blkid * blksz;
  1806. data->file_end = data->file_start + blksz;
  1807. movesize = min(length, data->file_end - (int)file->offset - red);
  1808. memmove(buf, data->file_buf + file->offset + red
  1809. - data->file_start, movesize);
  1810. buf += movesize;
  1811. length -= movesize;
  1812. red += movesize;
  1813. }
  1814. return len;
  1815. }
  1816. int
  1817. zfs_close(zfs_file_t file)
  1818. {
  1819. zfs_unmount((struct zfs_data *) file->data);
  1820. return ZFS_ERR_NONE;
  1821. }
  1822. int
  1823. zfs_getmdnobj(device_t dev, const char *fsfilename,
  1824. uint64_t *mdnobj)
  1825. {
  1826. struct zfs_data *data;
  1827. int err;
  1828. int isfs;
  1829. data = zfs_mount(dev);
  1830. if (!data)
  1831. return ZFS_ERR_BAD_FS;
  1832. err = dnode_get_fullpath(fsfilename, &(data->mdn), mdnobj,
  1833. &(data->dnode), &isfs, data);
  1834. zfs_unmount(data);
  1835. return err;
  1836. }
  1837. static void
  1838. fill_fs_info(struct zfs_dirhook_info *info,
  1839. dnode_end_t mdn, struct zfs_data *data)
  1840. {
  1841. int err;
  1842. dnode_end_t dn;
  1843. uint64_t objnum;
  1844. uint64_t headobj;
  1845. memset(info, 0, sizeof(*info));
  1846. info->dir = 1;
  1847. if (mdn.dn.dn_type == DMU_OT_DSL_DIR) {
  1848. headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&mdn.dn))->dd_head_dataset_obj, mdn.endian);
  1849. err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, &mdn, data);
  1850. if (err) {
  1851. printf("zfs failed here 1\n");
  1852. return;
  1853. }
  1854. }
  1855. make_mdn(&mdn, data);
  1856. err = dnode_get(&mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
  1857. &dn, data);
  1858. if (err) {
  1859. printf("zfs failed here 2\n");
  1860. return;
  1861. }
  1862. err = zap_lookup(&dn, ZFS_ROOT_OBJ, &objnum, data);
  1863. if (err) {
  1864. printf("zfs failed here 3\n");
  1865. return;
  1866. }
  1867. err = dnode_get(&mdn, objnum, 0, &dn, data);
  1868. if (err) {
  1869. printf("zfs failed here 4\n");
  1870. return;
  1871. }
  1872. info->mtimeset = 1;
  1873. info->mtime = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&dn.dn))->zp_mtime[0], dn.endian);
  1874. return;
  1875. }
  1876. static int iterate_zap(const char *name, uint64_t val, struct zfs_data *data)
  1877. {
  1878. struct zfs_dirhook_info info;
  1879. dnode_end_t dn;
  1880. memset(&info, 0, sizeof(info));
  1881. dnode_get(&(data->mdn), val, 0, &dn, data);
  1882. info.mtimeset = 1;
  1883. info.mtime = zfs_to_cpu64(((znode_phys_t *) DN_BONUS(&dn.dn))->zp_mtime[0], dn.endian);
  1884. info.dir = (dn.dn.dn_type == DMU_OT_DIRECTORY_CONTENTS);
  1885. debug("zfs type=%d, name=%s\n",
  1886. (int)dn.dn.dn_type, (char *)name);
  1887. if (!data->userhook)
  1888. return 0;
  1889. return data->userhook(name, &info);
  1890. }
  1891. static int iterate_zap_fs(const char *name, uint64_t val, struct zfs_data *data)
  1892. {
  1893. struct zfs_dirhook_info info;
  1894. dnode_end_t mdn;
  1895. int err;
  1896. err = dnode_get(&(data->mos), val, 0, &mdn, data);
  1897. if (err)
  1898. return 0;
  1899. if (mdn.dn.dn_type != DMU_OT_DSL_DIR)
  1900. return 0;
  1901. fill_fs_info(&info, mdn, data);
  1902. if (!data->userhook)
  1903. return 0;
  1904. return data->userhook(name, &info);
  1905. }
  1906. static int iterate_zap_snap(const char *name, uint64_t val, struct zfs_data *data)
  1907. {
  1908. struct zfs_dirhook_info info;
  1909. char *name2;
  1910. int ret = 0;
  1911. dnode_end_t mdn;
  1912. int err;
  1913. err = dnode_get(&(data->mos), val, 0, &mdn, data);
  1914. if (err)
  1915. return 0;
  1916. if (mdn.dn.dn_type != DMU_OT_DSL_DATASET)
  1917. return 0;
  1918. fill_fs_info(&info, mdn, data);
  1919. name2 = malloc(strlen(name) + 2);
  1920. name2[0] = '@';
  1921. memcpy(name2 + 1, name, strlen(name) + 1);
  1922. if (data->userhook)
  1923. ret = data->userhook(name2, &info);
  1924. free(name2);
  1925. return ret;
  1926. }
  1927. int
  1928. zfs_ls(device_t device, const char *path,
  1929. int (*hook)(const char *, const struct zfs_dirhook_info *))
  1930. {
  1931. struct zfs_data *data;
  1932. int err;
  1933. int isfs;
  1934. data = zfs_mount(device);
  1935. if (!data)
  1936. return ZFS_ERR_BAD_FS;
  1937. data->userhook = hook;
  1938. err = dnode_get_fullpath(path, &(data->mdn), 0, &(data->dnode), &isfs, data);
  1939. if (err) {
  1940. zfs_unmount(data);
  1941. return err;
  1942. }
  1943. if (isfs) {
  1944. uint64_t childobj, headobj;
  1945. uint64_t snapobj;
  1946. dnode_end_t dn;
  1947. struct zfs_dirhook_info info;
  1948. fill_fs_info(&info, data->dnode, data);
  1949. hook("@", &info);
  1950. childobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&data->dnode.dn))->dd_child_dir_zapobj, data->dnode.endian);
  1951. headobj = zfs_to_cpu64(((dsl_dir_phys_t *) DN_BONUS(&data->dnode.dn))->dd_head_dataset_obj, data->dnode.endian);
  1952. err = dnode_get(&(data->mos), childobj,
  1953. DMU_OT_DSL_DIR_CHILD_MAP, &dn, data);
  1954. if (err) {
  1955. zfs_unmount(data);
  1956. return err;
  1957. }
  1958. zap_iterate(&dn, iterate_zap_fs, data);
  1959. err = dnode_get(&(data->mos), headobj, DMU_OT_DSL_DATASET, &dn, data);
  1960. if (err) {
  1961. zfs_unmount(data);
  1962. return err;
  1963. }
  1964. snapobj = zfs_to_cpu64(((dsl_dataset_phys_t *) DN_BONUS(&dn.dn))->ds_snapnames_zapobj, dn.endian);
  1965. err = dnode_get(&(data->mos), snapobj,
  1966. DMU_OT_DSL_DS_SNAP_MAP, &dn, data);
  1967. if (err) {
  1968. zfs_unmount(data);
  1969. return err;
  1970. }
  1971. zap_iterate(&dn, iterate_zap_snap, data);
  1972. } else {
  1973. if (data->dnode.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) {
  1974. zfs_unmount(data);
  1975. printf("not a directory\n");
  1976. return ZFS_ERR_BAD_FILE_TYPE;
  1977. }
  1978. zap_iterate(&(data->dnode), iterate_zap, data);
  1979. }
  1980. zfs_unmount(data);
  1981. return ZFS_ERR_NONE;
  1982. }