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