removed accidental commit
[grub2/phcoder.git] / fs / zfs.c
blob5004dc1f275c1c152de0f2c45104fc088fd1a943
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
4 * Copyright 2008 Sun Microsystems, Inc.
5 * Copyright (C) 2009 Free Software Foundation, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * The zfs plug-in routines for GRUB are:
24 * zfs_mount() - locates a valid uberblock of the root pool and reads
25 * in its MOS at the memory address MOS.
27 * zfs_open() - locates a plain file object by following the MOS
28 * and places its dnode at the memory address DNODE.
30 * zfs_read() - read in the data blocks pointed by the DNODE.
34 #include <grub/err.h>
35 #include <grub/file.h>
36 #include <grub/mm.h>
37 #include <grub/misc.h>
38 #include <grub/disk.h>
39 #include <grub/dl.h>
40 #include <grub/types.h>
41 #include <grub/zfs/zfs.h>
42 #include <grub/zfs/zio.h>
43 #include <grub/zfs/dnode.h>
44 #include <grub/zfs/uberblock_impl.h>
45 #include <grub/zfs/vdev_impl.h>
46 #include <grub/zfs/zio_checksum.h>
47 #include <grub/zfs/zap_impl.h>
48 #include <grub/zfs/zap_leaf.h>
49 #include <grub/zfs/zfs_znode.h>
50 #include <grub/zfs/dmu.h>
51 #include <grub/zfs/dmu_objset.h>
52 #include <grub/zfs/dsl_dir.h>
53 #include <grub/zfs/dsl_dataset.h>
55 #define ZPOOL_PROP_BOOTFS "bootfs"
57 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
60 * For nvlist manipulation. (from nvpair.h)
62 #define NV_ENCODE_NATIVE 0
63 #define NV_ENCODE_XDR 1
64 #define NV_BIG_ENDIAN 0
65 #define NV_LITTLE_ENDIAN 1
66 #define DATA_TYPE_UINT64 8
67 #define DATA_TYPE_STRING 9
68 #define DATA_TYPE_NVLIST 19
69 #define DATA_TYPE_NVLIST_ARRAY 20
71 #ifndef GRUB_UTIL
72 static grub_dl_t my_mod;
73 #endif
75 #define P2PHASE(x, align) ((x) & ((align) - 1))
76 #define DVA_OFFSET_TO_PHYS_SECTOR(offset) \
77 ((offset + VDEV_LABEL_START_SIZE) >> SPA_MINBLOCKSHIFT)
80 * FAT ZAP data structures
82 #define ZFS_CRC64_POLY 0xC96C5795D7870F42ULL /* ECMA-182, reflected form */
83 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n))))
84 #define CHAIN_END 0xffff /* end of the chunk chain */
87 * The amount of space within the chunk available for the array is:
88 * chunk size - space for type (1) - space for next pointer (2)
90 #define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
92 #define ZAP_LEAF_HASH_SHIFT(bs) (bs - 5)
93 #define ZAP_LEAF_HASH_NUMENTRIES(bs) (1 << ZAP_LEAF_HASH_SHIFT(bs))
94 #define LEAF_HASH(bs, h) \
95 ((ZAP_LEAF_HASH_NUMENTRIES(bs)-1) & \
96 ((h) >> (64 - ZAP_LEAF_HASH_SHIFT(bs)-l->l_hdr.lh_prefix_len)))
99 * The amount of space available for chunks is:
100 * block size shift - hash entry size (2) * number of hash
101 * entries - header space (2*chunksize)
103 #define ZAP_LEAF_NUMCHUNKS(bs) \
104 (((1<<bs) - 2*ZAP_LEAF_HASH_NUMENTRIES(bs)) / \
105 ZAP_LEAF_CHUNKSIZE - 2)
108 * The chunks start immediately after the hash table. The end of the
109 * hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
110 * chunk_t.
112 #define ZAP_LEAF_CHUNK(l, bs, idx) \
113 ((zap_leaf_chunk_t *)(l->l_hash + ZAP_LEAF_HASH_NUMENTRIES(bs)))[idx]
114 #define ZAP_LEAF_ENTRY(l, bs, idx) (&ZAP_LEAF_CHUNK(l, bs, idx).l_entry)
118 * Decompression Entry - lzjb
120 #ifndef NBBY
121 #define NBBY 8
122 #endif
124 extern grub_err_t lzjb_decompress (void *, void *, grub_size_t, grub_size_t);
126 typedef grub_err_t zfs_decomp_func_t (void *s_start, void *d_start,
127 grub_size_t s_len, grub_size_t d_len);
128 typedef struct decomp_entry
130 char *name;
131 zfs_decomp_func_t *decomp_func;
132 } decomp_entry_t;
134 typedef struct dnode_end
136 dnode_phys_t dn;
137 grub_zfs_endian_t endian;
138 } dnode_end_t;
140 struct grub_zfs_data
142 /* cache for a file block of the currently zfs_open()-ed file */
143 void *file_buf;
144 grub_uint64_t file_start;
145 grub_uint64_t file_end;
147 /* cache for a dnode block */
148 dnode_phys_t *dnode_buf;
149 dnode_phys_t *dnode_mdn;
150 grub_uint64_t dnode_start;
151 grub_uint64_t dnode_end;
152 grub_zfs_endian_t dnode_endian;
154 uberblock_t current_uberblock;
155 grub_disk_t disk;
157 dnode_end_t mos;
158 dnode_end_t mdn;
159 dnode_end_t dnode;
161 #if 0
162 char current_bootfs[MAXNAMELEN];
163 char current_bootpath[MAXPATHLEN];
164 char current_devid[MAXPATHLEN];
165 #endif
167 int current_label;
170 decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] = {
171 {"inherit", 0}, /* ZIO_COMPRESS_INHERIT */
172 {"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */
173 {"off", 0}, /* ZIO_COMPRESS_OFF */
174 {"lzjb", lzjb_decompress}, /* ZIO_COMPRESS_LZJB */
175 {"empty", 0} /* ZIO_COMPRESS_EMPTY */
178 static grub_err_t zio_read_data (blkptr_t * bp, grub_zfs_endian_t endian,
179 void *buf, struct grub_zfs_data *data);
182 * Our own version of log2(). Same thing as highbit()-1.
184 static int
185 zfs_log2 (grub_uint64_t num)
187 int i = 0;
189 while (num > 1)
191 i++;
192 num = num >> 1;
195 return (i);
198 /* Checksum Functions */
199 static void
200 zio_checksum_off (const void *buf __attribute__ ((unused)),
201 grub_uint64_t size __attribute__ ((unused)),
202 grub_zfs_endian_t endian __attribute__ ((unused)),
203 zio_cksum_t * zcp)
205 ZIO_SET_CHECKSUM (zcp, 0, 0, 0, 0);
208 /* Checksum Table and Values */
209 zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
210 {NULL, 0, 0, "inherit"},
211 {NULL, 0, 0, "on"},
212 {zio_checksum_off, 0, 0, "off"},
213 {zio_checksum_SHA256, 1, 1, "label"},
214 {zio_checksum_SHA256, 1, 1, "gang_header"},
215 {fletcher_2, 0, 1, "zilog"},
216 {fletcher_2, 0, 0, "fletcher2"},
217 {fletcher_4, 1, 0, "fletcher4"},
218 {zio_checksum_SHA256, 1, 0, "SHA256"},
222 * zio_checksum_verify: Provides support for checksum verification.
224 * Fletcher2, Fletcher4, and SHA256 are supported.
227 static grub_err_t
228 zio_checksum_verify (zio_cksum_t zc, grub_uint32_t checksum,
229 grub_zfs_endian_t endian, char *buf, int size)
231 zio_block_tail_t *zbt = (zio_block_tail_t *) (buf + size) - 1;
232 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
233 zio_cksum_t actual_cksum, expected_cksum;
235 if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func == NULL)
237 grub_dprintf ("zfs", "unknown checksum function %d\n", checksum);
238 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
239 "unknown checksum function %d", checksum);
242 if (ci->ci_zbt)
244 expected_cksum = zbt->zbt_cksum;
245 zbt->zbt_cksum = zc;
246 ci->ci_func (buf, size, endian, &actual_cksum);
247 zbt->zbt_cksum = expected_cksum;
248 zc = expected_cksum;
250 else
251 ci->ci_func (buf, size, endian, &actual_cksum);
253 if ((actual_cksum.zc_word[0] != zc.zc_word[0])
254 || (actual_cksum.zc_word[1] != zc.zc_word[1])
255 || (actual_cksum.zc_word[2] != zc.zc_word[2])
256 || (actual_cksum.zc_word[3] != zc.zc_word[3]))
258 grub_dprintf ("zfs", "checksum %d verification failed\n", checksum);
259 grub_dprintf ("zfs", "actual checksum %16llx %16llx %16llx %16llx\n",
260 (unsigned long long) actual_cksum.zc_word[0],
261 (unsigned long long) actual_cksum.zc_word[1],
262 (unsigned long long) actual_cksum.zc_word[2],
263 (unsigned long long) actual_cksum.zc_word[3]);
264 grub_dprintf ("zfs", "expected checksum %16llx %16llx %16llx %16llx\n",
265 (unsigned long long) zc.zc_word[0],
266 (unsigned long long) zc.zc_word[1],
267 (unsigned long long) zc.zc_word[2],
268 (unsigned long long) zc.zc_word[3]);
269 return grub_error (GRUB_ERR_BAD_FS, "checksum verification failed");
272 return GRUB_ERR_NONE;
276 * vdev_label_offset takes "offset" (the offset within a vdev_label) and
277 * returns its physical disk offset (starting from the beginning of the vdev).
279 * Input:
280 * psize : Physical size of this vdev
281 * l : Label Number (0-3)
282 * offset : The offset with a vdev_label in which we want the physical
283 * address
284 * sector : where physical disk offset will be put
286 static grub_err_t
287 vdev_label_offset (grub_uint64_t psize, int l, grub_uint64_t offset,
288 grub_disk_addr_t *sector)
290 /* XXX Need to add back label support! */
291 if (l >= VDEV_LABELS / 2 || offset > sizeof (vdev_label_t))
292 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
293 "back labels aren't supported yet");
295 *sector = offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
296 0 : psize -
297 VDEV_LABELS *
298 sizeof (vdev_label_t));
300 return GRUB_ERR_NONE;
304 * vdev_uberblock_compare takes two uberblock structures and returns an integer
305 * indicating the more recent of the two.
306 * Return Value = 1 if ub2 is more recent
307 * Return Value = -1 if ub1 is more recent
308 * The most recent uberblock is determined using its transaction number and
309 * timestamp. The uberblock with the highest transaction number is
310 * considered "newer". If the transaction numbers of the two blocks match, the
311 * timestamps are compared to determine the "newer" of the two.
313 static int
314 vdev_uberblock_compare (uberblock_t * ub1, uberblock_t * ub2)
316 grub_zfs_endian_t ub1_endian, ub2_endian;
317 if (grub_zfs_to_cpu64 (ub1->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC)
318 ub1_endian = LITTLE_ENDIAN;
319 else
320 ub1_endian = BIG_ENDIAN;
321 if (grub_zfs_to_cpu64 (ub2->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC)
322 ub2_endian = LITTLE_ENDIAN;
323 else
324 ub2_endian = BIG_ENDIAN;
326 if (grub_zfs_to_cpu64 (ub1->ub_txg, ub1_endian)
327 < grub_zfs_to_cpu64 (ub2->ub_txg, ub2_endian))
328 return (-1);
329 if (grub_zfs_to_cpu64 (ub1->ub_txg, ub1_endian)
330 > grub_zfs_to_cpu64 (ub2->ub_txg, ub2_endian))
331 return (1);
333 if (grub_zfs_to_cpu64 (ub1->ub_timestamp, ub1_endian)
334 < grub_zfs_to_cpu64 (ub2->ub_timestamp, ub2_endian))
335 return (-1);
336 if (grub_zfs_to_cpu64 (ub1->ub_timestamp, ub1_endian)
337 > grub_zfs_to_cpu64 (ub2->ub_timestamp, ub2_endian))
338 return (1);
340 return (0);
344 * Three pieces of information are needed to verify an uberblock: the magic
345 * number, the version number, and the checksum.
347 * Currently Implemented: version number, magic number
348 * Need to Implement: checksum
351 static grub_err_t
352 uberblock_verify (uberblock_phys_t * ub, int offset)
354 uberblock_t *uber = &ub->ubp_uberblock;
355 grub_err_t err;
356 grub_zfs_endian_t endian = UNKNOWN_ENDIAN;
357 zio_cksum_t zc;
360 if (grub_zfs_to_cpu64 (uber->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC
361 && grub_zfs_to_cpu64 (uber->ub_version, LITTLE_ENDIAN) > 0
362 && grub_zfs_to_cpu64 (uber->ub_version, LITTLE_ENDIAN) <= SPA_VERSION)
363 endian = LITTLE_ENDIAN;
365 if (grub_zfs_to_cpu64 (uber->ub_magic, BIG_ENDIAN) == UBERBLOCK_MAGIC
366 && grub_zfs_to_cpu64 (uber->ub_version, BIG_ENDIAN) > 0
367 && grub_zfs_to_cpu64 (uber->ub_version, BIG_ENDIAN) <= SPA_VERSION)
368 endian = BIG_ENDIAN;
370 if (endian == UNKNOWN_ENDIAN)
371 return grub_error (GRUB_ERR_BAD_FS, "invalid uberblock magic");
373 grub_memset (&zc, 0, sizeof (zc));
375 if (endian == BIG_ENDIAN)
376 zc.zc_word[0] = grub_cpu_to_zfs64 (offset, endian);
377 else
378 zc.zc_word[0] = grub_cpu_to_zfs64 (offset, endian);
379 err = zio_checksum_verify (zc, ZIO_CHECKSUM_LABEL, endian,
380 (char *) ub, UBERBLOCK_SIZE);
382 return err;
386 * Find the best uberblock.
387 * Return:
388 * Success - Pointer to the best uberblock.
389 * Failure - NULL
391 static uberblock_phys_t *
392 find_bestub (uberblock_phys_t * ub_array, int label)
394 uberblock_phys_t *ubbest = NULL;
395 int i;
396 grub_disk_addr_t offset;
397 grub_err_t err = GRUB_ERR_NONE;
399 for (i = 0; i < (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT); i++)
401 err = vdev_label_offset (0, label, VDEV_UBERBLOCK_OFFSET (i), &offset);
402 if (err)
404 return 0;
406 err = uberblock_verify (&ub_array[i], offset);
407 if (err)
409 grub_errno = GRUB_ERR_NONE;
410 continue;
412 if (ubbest == NULL
413 || vdev_uberblock_compare (&(ub_array[i].ubp_uberblock),
414 &(ubbest->ubp_uberblock)) > 0)
415 ubbest = &ub_array[i];
417 if (!ubbest)
418 grub_errno = err;
420 return (ubbest);
423 static inline grub_size_t
424 get_psize (blkptr_t * bp, grub_zfs_endian_t endian)
426 return ((((grub_zfs_to_cpu64 ((bp)->blk_prop, endian)>>16) & 0xffff) + 1)
427 << SPA_MINBLOCKSHIFT);
430 grub_uint64_t
431 dva_get_offset (dva_t * dva, grub_zfs_endian_t endian)
433 grub_dprintf ("zfs", "dva=%llx, %llx\n",
434 (unsigned long long) dva->dva_word[0],
435 (unsigned long long) dva->dva_word[1]);
436 return grub_zfs_to_cpu64 ((dva)->dva_word[1],
437 endian) << SPA_MINBLOCKSHIFT;
442 * Read a block of data based on the gang block address dva,
443 * and put its data in buf.
446 static grub_err_t
447 zio_read_gang (blkptr_t * bp, grub_zfs_endian_t endian, dva_t * dva, void *buf,
448 struct grub_zfs_data *data)
450 zio_gbh_phys_t *zio_gb;
451 grub_uint64_t offset, sector;
452 unsigned i;
453 grub_err_t err;
454 zio_cksum_t zc;
456 grub_memset (&zc, 0, sizeof (zc));
458 zio_gb = grub_malloc (SPA_GANGBLOCKSIZE);
459 if (!zio_gb)
460 return grub_errno;
461 grub_dprintf ("zfs", endian == LITTLE_ENDIAN ? "little-endian gang\n"
462 :"big-endian gang\n");
463 offset = dva_get_offset (dva, endian);
464 sector = DVA_OFFSET_TO_PHYS_SECTOR (offset);
465 grub_dprintf ("zfs", "offset=%llx\n", (unsigned long long) offset);
467 /* read in the gang block header */
468 err = grub_disk_read (data->disk, sector, 0, SPA_GANGBLOCKSIZE,
469 (char *) zio_gb);
470 if (err)
472 grub_free (zio_gb);
473 return err;
476 /* XXX */
477 /* self checksuming the gang block header */
478 ZIO_SET_CHECKSUM (&zc, DVA_GET_VDEV (dva),
479 dva_get_offset (dva, endian), bp->blk_birth, 0);
480 err = zio_checksum_verify (zc, ZIO_CHECKSUM_GANG_HEADER, endian,
481 (char *) zio_gb, SPA_GANGBLOCKSIZE);
482 if (err)
484 grub_free (zio_gb);
485 return err;
488 endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1;
490 for (i = 0; i < SPA_GBH_NBLKPTRS; i++)
492 if (zio_gb->zg_blkptr[i].blk_birth == 0)
493 continue;
495 if (zio_read_data (&zio_gb->zg_blkptr[i], endian, buf, data))
497 grub_free (zio_gb);
498 return (1);
500 buf += get_psize (&zio_gb->zg_blkptr[i], endian);
502 grub_free (zio_gb);
503 return (0);
507 * Read in a block of raw data to buf.
509 static grub_err_t
510 zio_read_data (blkptr_t * bp, grub_zfs_endian_t endian, void *buf,
511 struct grub_zfs_data *data)
513 int i, psize;
514 grub_err_t err = GRUB_ERR_NONE;
516 psize = get_psize (bp, endian);
518 /* pick a good dva from the block pointer */
519 for (i = 0; i < SPA_DVAS_PER_BP; i++)
521 grub_uint64_t offset, sector;
523 if (bp->blk_dva[i].dva_word[0] == 0 && bp->blk_dva[i].dva_word[1] == 0)
524 continue;
526 if ((grub_zfs_to_cpu64 (bp->blk_dva[i].dva_word[1], endian)>>63) & 1)
527 err = zio_read_gang (bp, endian, &bp->blk_dva[i], buf, data);
528 else
530 /* read in a data block */
531 offset = dva_get_offset (&bp->blk_dva[i], endian);
532 sector = DVA_OFFSET_TO_PHYS_SECTOR (offset);
533 err = grub_disk_read (data->disk, sector, 0, psize, buf);
535 if (!err)
536 return GRUB_ERR_NONE;
537 grub_errno = GRUB_ERR_NONE;
540 if (!err)
541 err = grub_error (GRUB_ERR_BAD_FS, "couldn't find a valid DVA");
542 grub_errno = err;
544 return err;
548 * Read in a block of data, verify its checksum, decompress if needed,
549 * and put the uncompressed data in buf.
551 static grub_err_t
552 zio_read (blkptr_t * bp, grub_zfs_endian_t endian, void **buf,
553 grub_size_t *size, struct grub_zfs_data *data)
555 grub_size_t lsize, psize;
556 int comp;
557 char *compbuf;
558 grub_err_t err;
559 zio_cksum_t zc = bp->blk_cksum;
560 grub_uint32_t checksum;
562 checksum = (grub_zfs_to_cpu64((bp)->blk_prop, endian) >> 40) & 0xff;
563 comp = (grub_zfs_to_cpu64((bp)->blk_prop, endian)>>32) & 0xff;
564 lsize = (BP_IS_HOLE(bp) ? 0 :
565 (((grub_zfs_to_cpu64 ((bp)->blk_prop, endian) & 0xffff) + 1)
566 << SPA_MINBLOCKSHIFT));
567 psize = get_psize (bp, endian);
569 if (size)
570 *size = lsize;
572 if ((unsigned int) comp >= ZIO_COMPRESS_FUNCTIONS ||
573 (comp != ZIO_COMPRESS_OFF && decomp_table[comp].decomp_func == NULL))
575 grub_dprintf ("zfs", "comp=%d\n", comp);
576 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
577 "compression algorithm not supported\n");
580 if (comp != ZIO_COMPRESS_OFF)
582 compbuf = grub_malloc (psize);
583 if (!compbuf)
584 return GRUB_ERR_NONE;
586 else
587 compbuf = *buf = grub_malloc (lsize);
589 grub_dprintf ("zfs", "endian = %d\n", endian);
590 err = zio_read_data (bp, endian, compbuf, data);
591 if (err)
593 grub_free (compbuf);
594 return err;
597 err = zio_checksum_verify (zc, checksum, endian, compbuf, psize);
598 if (err)
600 grub_dprintf ("zfs", "incorrect checksum\n");
601 grub_free (compbuf);
602 return err;
605 if (comp != ZIO_COMPRESS_OFF)
607 *buf = grub_malloc (lsize);
608 err = decomp_table[comp].decomp_func (compbuf, *buf, psize, lsize);
609 grub_free (compbuf);
610 if (err)
611 return err;
614 return GRUB_ERR_NONE;
618 * Get the block from a block id.
619 * push the block onto the stack.
622 static grub_err_t
623 dmu_read (dnode_end_t * dn, grub_uint64_t blkid, void **buf,
624 grub_zfs_endian_t *endian_out, struct grub_zfs_data *data)
626 int idx, level;
627 blkptr_t *bp_array = dn->dn.dn_blkptr;
628 int epbs = dn->dn.dn_indblkshift - SPA_BLKPTRSHIFT;
629 blkptr_t *bp, *tmpbuf = 0;
630 grub_zfs_endian_t endian;
631 grub_err_t err;
633 bp = grub_malloc (sizeof (blkptr_t));
634 if (!bp)
635 return grub_errno;
637 endian = dn->endian;
638 for (level = dn->dn.dn_nlevels - 1; level >= 0; level--)
640 grub_dprintf ("zfs", "endian = %d\n", endian);
641 idx = (blkid >> (epbs * level)) & ((1 << epbs) - 1);
642 *bp = bp_array[idx];
643 if (bp_array != dn->dn.dn_blkptr)
645 grub_free (bp_array);
646 bp_array = 0;
649 if (BP_IS_HOLE (bp))
651 grub_size_t size = grub_zfs_to_cpu16 (dn->dn.dn_datablkszsec,
652 dn->endian)
653 << SPA_MINBLOCKSHIFT;
654 *buf = grub_malloc (size);
655 if (*buf)
657 err = grub_errno;
658 break;
660 grub_memset (*buf, 0, size);
661 endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1;
662 break;
664 if (level == 0)
666 grub_dprintf ("zfs", "endian = %d\n", endian);
667 err = zio_read (bp, endian, buf, 0, data);
668 endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1;
669 break;
671 grub_dprintf ("zfs", "endian = %d\n", endian);
672 err = zio_read (bp, endian, (void **) &tmpbuf, 0, data);
673 endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1;
674 if (err)
675 break;
676 bp_array = tmpbuf;
678 if (bp_array != dn->dn.dn_blkptr)
679 grub_free (bp_array);
680 if (endian_out)
681 *endian_out = endian;
683 grub_free (bp);
684 return err;
688 * mzap_lookup: Looks up property described by "name" and returns the value
689 * in "value".
691 static grub_err_t
692 mzap_lookup (mzap_phys_t * zapobj, grub_zfs_endian_t endian,
693 int objsize, char *name, grub_uint64_t * value)
695 int i, chunks;
696 mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
698 chunks = objsize / MZAP_ENT_LEN - 1;
699 for (i = 0; i < chunks; i++)
701 if (grub_strcmp (mzap_ent[i].mze_name, name) == 0)
703 *value = grub_zfs_to_cpu64 (mzap_ent[i].mze_value, endian);
704 return GRUB_ERR_NONE;
708 return grub_error (GRUB_ERR_FILE_NOT_FOUND, "couldn't find %s", name);
711 static int
712 mzap_iterate (mzap_phys_t * zapobj, grub_zfs_endian_t endian, int objsize,
713 int NESTED_FUNC_ATTR (*hook) (const char *name,
714 grub_uint64_t val))
716 int i, chunks;
717 mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
719 chunks = objsize / MZAP_ENT_LEN - 1;
720 for (i = 0; i < chunks; i++)
722 grub_dprintf ("zfs", "zap: name = %s, value = %llx, cd = %x\n",
723 mzap_ent[i].mze_name, (long long)mzap_ent[i].mze_value,
724 (int)mzap_ent[i].mze_cd);
725 if (hook (mzap_ent[i].mze_name,
726 grub_zfs_to_cpu64 (mzap_ent[i].mze_value, endian)))
727 return 1;
730 return 0;
733 static grub_uint64_t
734 zap_hash (grub_uint64_t salt, const char *name)
736 static grub_uint64_t table[256];
737 const grub_uint8_t *cp;
738 grub_uint8_t c;
739 grub_uint64_t crc = salt;
741 if (table[128] == 0)
743 grub_uint64_t *ct;
744 int i, j;
745 for (i = 0; i < 256; i++)
747 for (ct = table + i, *ct = i, j = 8; j > 0; j--)
748 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
752 for (cp = (const grub_uint8_t *) name; (c = *cp) != '\0'; cp++)
753 crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
756 * Only use 28 bits, since we need 4 bits in the cookie for the
757 * collision differentiator. We MUST use the high bits, since
758 * those are the onces that we first pay attention to when
759 * chosing the bucket.
761 crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1);
763 return (crc);
767 * Only to be used on 8-bit arrays.
768 * array_len is actual len in bytes (not encoded le_value_length).
769 * buf is null-terminated.
771 /* XXX */
772 static int
773 zap_leaf_array_equal (zap_leaf_phys_t * l, grub_zfs_endian_t endian,
774 int blksft, int chunk, int array_len, const char *buf)
776 int bseen = 0;
778 while (bseen < array_len)
780 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK (l, blksft, chunk).l_array;
781 int toread = MIN (array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
783 if (chunk >= ZAP_LEAF_NUMCHUNKS (blksft))
784 return (0);
786 if (grub_memcmp (la->la_array, buf + bseen, toread) != 0)
787 break;
788 chunk = grub_zfs_to_cpu16 (la->la_next, endian);
789 bseen += toread;
791 return (bseen == array_len);
794 /* XXX */
795 static grub_err_t
796 zap_leaf_array_get (zap_leaf_phys_t * l, grub_zfs_endian_t endian, int blksft,
797 int chunk, int array_len, char *buf)
799 int bseen = 0;
801 while (bseen < array_len)
803 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK (l, blksft, chunk).l_array;
804 int toread = MIN (array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
806 if (chunk >= ZAP_LEAF_NUMCHUNKS (blksft))
807 /* Don't use grub_error because this error is to be ignored. */
808 return GRUB_ERR_BAD_FS;
810 grub_memcpy (buf + bseen,la->la_array, toread);
811 chunk = grub_zfs_to_cpu16 (la->la_next, endian);
812 bseen += toread;
814 return GRUB_ERR_NONE;
819 * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
820 * value for the property "name".
823 /* XXX */
824 static grub_err_t
825 zap_leaf_lookup (zap_leaf_phys_t * l, grub_zfs_endian_t endian,
826 int blksft, grub_uint64_t h,
827 const char *name, grub_uint64_t * value)
829 grub_uint16_t chunk;
830 struct zap_leaf_entry *le;
831 grub_size_t tlen = grub_strlen (name);
833 /* Verify if this is a valid leaf block */
834 if (grub_zfs_to_cpu64 (l->l_hdr.lh_block_type, endian) != ZBT_LEAF)
835 return grub_error (GRUB_ERR_BAD_FS, "invalid leaf type");
836 if (grub_zfs_to_cpu32 (l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC)
837 return grub_error (GRUB_ERR_BAD_FS, "invalid leaf magic");
839 for (chunk = grub_zfs_to_cpu16 (l->l_hash[LEAF_HASH (blksft, h)], endian);
840 chunk != CHAIN_END; chunk = le->le_next)
843 if (chunk >= ZAP_LEAF_NUMCHUNKS (blksft))
844 return grub_error (GRUB_ERR_BAD_FS, "invalid chunk number");
846 le = ZAP_LEAF_ENTRY (l, blksft, chunk);
848 /* Verify the chunk entry */
849 if (le->le_type != ZAP_CHUNK_ENTRY)
850 return grub_error (GRUB_ERR_BAD_FS, "invalid chunk entry");
852 if (grub_zfs_to_cpu64 (le->le_hash,endian) != h)
853 continue;
855 grub_dprintf ("zfs", "fzap: length %d:%d", (int) tlen,
856 (int) le->le_name_length);
858 if (zap_leaf_array_equal (l, endian, blksft,
859 grub_zfs_to_cpu16 (le->le_name_chunk,endian),
860 grub_zfs_to_cpu16 (le->le_name_length, endian),
861 name))
863 struct zap_leaf_array *la;
864 grub_uint8_t *ip;
866 if (le->le_int_size != 8 || le->le_value_length != 1)
867 return (GRUB_ERR_BAD_FS);
869 /* get the uint64_t property value */
870 la = &ZAP_LEAF_CHUNK (l, blksft, le->le_value_chunk).l_array;
871 ip = la->la_array;
873 *value = grub_be_to_cpu64 (*(grub_uint64_t *)la->la_array);
875 return GRUB_ERR_NONE;
879 return grub_error (GRUB_ERR_FILE_NOT_FOUND, "couldn't find %s", name);
883 * Fat ZAP lookup
886 /* XXX */
887 static grub_err_t
888 fzap_lookup (dnode_end_t * zap_dnode, zap_phys_t * zap,
889 char *name, grub_uint64_t * value, struct grub_zfs_data *data)
891 zap_leaf_phys_t *l;
892 grub_uint64_t hash, idx, blkid;
893 int blksft = zfs_log2 (grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec,
894 zap_dnode->endian) << DNODE_SHIFT);
895 grub_err_t err;
896 grub_zfs_endian_t leafendian;
898 /* Verify if this is a fat zap header block */
899 if (zap->zap_magic != (grub_uint64_t) ZAP_MAGIC)
900 return grub_error (GRUB_ERR_BAD_FS, "bad ZAP magic");
902 if (zap->zap_salt == 0)
903 return grub_error (GRUB_ERR_BAD_FS, "bad ZAP salt");
904 hash = zap_hash (zap->zap_salt, name);
906 /* get block id from index */
907 if (zap->zap_ptrtbl.zt_numblks != 0)
908 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
909 "external pointer tables not supported");
910 idx = ZAP_HASH_IDX (hash, zap->zap_ptrtbl.zt_shift);
911 blkid = ((grub_uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))];
913 /* Get the leaf block */
914 if ((1U << blksft) < sizeof (zap_leaf_phys_t))
915 return grub_error (GRUB_ERR_BAD_FS, "ZAP leaf is too small");
916 err = dmu_read (zap_dnode, blkid, (void **) &l, &leafendian, data);
917 if (err)
918 return err;
920 err = zap_leaf_lookup (l, leafendian, blksft, hash, name, value);
921 grub_free (l);
922 return err;
925 /* XXX */
926 static int
927 fzap_iterate (dnode_end_t * zap_dnode, zap_phys_t * zap,
928 int NESTED_FUNC_ATTR (*hook) (const char *name,
929 grub_uint64_t val),
930 struct grub_zfs_data *data)
932 zap_leaf_phys_t *l;
933 grub_uint64_t idx, blkid;
934 grub_uint16_t chunk;
935 int blksft = zfs_log2 (grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec,
936 zap_dnode->endian) << DNODE_SHIFT);
937 grub_err_t err;
938 grub_zfs_endian_t endian;
940 /* Verify if this is a fat zap header block */
941 if (zap->zap_magic != (grub_uint64_t) ZAP_MAGIC)
943 grub_error (GRUB_ERR_BAD_FS, "bad ZAP magic");
944 return 0;
947 /* get block id from index */
948 if (zap->zap_ptrtbl.zt_numblks != 0)
950 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
951 "external pointer tables not supported");
952 return 0;
954 /* Get the leaf block */
955 if ((1U << blksft) < sizeof (zap_leaf_phys_t))
957 grub_error (GRUB_ERR_BAD_FS, "ZAP leaf is too small");
958 return 0;
960 for (idx = 0; idx < zap->zap_ptrtbl.zt_numblks; idx++)
962 blkid = ((grub_uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))];
964 err = dmu_read (zap_dnode, blkid, (void **) &l, &endian, data);
965 if (err)
967 grub_errno = GRUB_ERR_NONE;
968 continue;
971 /* Verify if this is a valid leaf block */
972 if (grub_zfs_to_cpu64 (l->l_hdr.lh_block_type, endian) != ZBT_LEAF)
974 grub_free (l);
975 continue;
977 if (grub_zfs_to_cpu32 (l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC)
979 grub_free (l);
980 continue;
983 for (chunk = 0; chunk < ZAP_LEAF_NUMCHUNKS (blksft); chunk++)
985 char *buf;
986 struct zap_leaf_array *la;
987 struct zap_leaf_entry *le;
988 grub_uint64_t val;
989 le = ZAP_LEAF_ENTRY (l, blksft, chunk);
991 /* Verify the chunk entry */
992 if (le->le_type != ZAP_CHUNK_ENTRY)
993 continue;
995 buf = grub_malloc (grub_zfs_to_cpu16 (le->le_name_length, endian)
996 + 1);
997 if (zap_leaf_array_get (l, endian, blksft, le->le_name_chunk,
998 le->le_name_length, buf))
1000 grub_free (buf);
1001 continue;
1003 buf[le->le_name_length] = 0;
1005 if (le->le_int_size != 8
1006 || grub_zfs_to_cpu16 (le->le_value_length, endian) != 1)
1007 continue;
1009 /* get the uint64_t property value */
1010 la = &ZAP_LEAF_CHUNK (l, blksft, le->le_value_chunk).l_array;
1011 val = grub_be_to_cpu64 (*(grub_uint64_t *)la->la_array);
1012 if (hook (buf, val))
1013 return 1;
1014 grub_free (buf);
1017 return 0;
1022 * Read in the data of a zap object and find the value for a matching
1023 * property name.
1026 static grub_err_t
1027 zap_lookup (dnode_end_t * zap_dnode, char *name, grub_uint64_t * val,
1028 struct grub_zfs_data *data)
1030 grub_uint64_t block_type;
1031 int size;
1032 void *zapbuf;
1033 grub_err_t err;
1034 grub_zfs_endian_t endian;
1036 grub_dprintf ("zfs", "looking for '%s'\n", name);
1038 /* Read in the first block of the zap object data. */
1039 size = grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec,
1040 zap_dnode->endian) << SPA_MINBLOCKSHIFT;
1041 err = dmu_read (zap_dnode, 0, &zapbuf, &endian, data);
1042 if (err)
1043 return err;
1044 block_type = grub_zfs_to_cpu64 (*((grub_uint64_t *) zapbuf), endian);
1046 grub_dprintf ("zfs", "zap read\n");
1048 if (block_type == ZBT_MICRO)
1050 grub_dprintf ("zfs", "micro zap\n");
1051 err = (mzap_lookup (zapbuf, endian, size, name, val));
1052 grub_dprintf ("zfs", "returned %d\n", err);
1053 grub_free (zapbuf);
1054 return err;
1056 else if (block_type == ZBT_HEADER)
1058 grub_dprintf ("zfs", "fat zap\n");
1059 /* this is a fat zap */
1060 err = (fzap_lookup (zap_dnode, zapbuf, name, val, data));
1061 grub_dprintf ("zfs", "returned %d\n", err);
1062 grub_free (zapbuf);
1063 return err;
1066 return grub_error (GRUB_ERR_BAD_FS, "unknown ZAP type");
1069 static int
1070 zap_iterate (dnode_end_t * zap_dnode,
1071 int NESTED_FUNC_ATTR (*hook) (const char *name, grub_uint64_t val),
1072 struct grub_zfs_data *data)
1074 grub_uint64_t block_type;
1075 int size;
1076 void *zapbuf;
1077 grub_err_t err;
1078 int ret;
1079 grub_zfs_endian_t endian;
1081 /* Read in the first block of the zap object data. */
1082 size = grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec, zap_dnode->endian) << SPA_MINBLOCKSHIFT;
1083 err = dmu_read (zap_dnode, 0, &zapbuf, &endian, data);
1084 if (err)
1085 return 0;
1086 block_type = grub_zfs_to_cpu64 (*((grub_uint64_t *) zapbuf), endian);
1088 grub_dprintf ("zfs", "zap read\n");
1090 if (block_type == ZBT_MICRO)
1092 grub_dprintf ("zfs", "micro zap\n");
1093 ret = mzap_iterate (zapbuf, endian, size, hook);
1094 grub_free (zapbuf);
1095 return ret;
1097 else if (block_type == ZBT_HEADER)
1099 grub_dprintf ("zfs", "fat zap\n");
1100 /* this is a fat zap */
1101 ret = fzap_iterate (zap_dnode, zapbuf, hook, data);
1102 grub_free (zapbuf);
1103 return ret;
1105 grub_error (GRUB_ERR_BAD_FS, "unknown ZAP type");
1106 return 0;
1111 * Get the dnode of an object number from the metadnode of an object set.
1113 * Input
1114 * mdn - metadnode to get the object dnode
1115 * objnum - object number for the object dnode
1116 * buf - data buffer that holds the returning dnode
1118 static grub_err_t
1119 dnode_get (dnode_end_t * mdn, grub_uint64_t objnum, grub_uint8_t type,
1120 dnode_end_t * buf, struct grub_zfs_data *data)
1122 grub_uint64_t blkid, blksz; /* the block id this object dnode is in */
1123 int epbs; /* shift of number of dnodes in a block */
1124 int idx; /* index within a block */
1125 dnode_phys_t *dnbuf;
1126 grub_err_t err;
1127 grub_zfs_endian_t endian;
1129 blksz = grub_zfs_to_cpu16 (mdn->dn.dn_datablkszsec,
1130 mdn->endian) << SPA_MINBLOCKSHIFT;
1131 epbs = zfs_log2 (blksz) - DNODE_SHIFT;
1132 blkid = objnum >> epbs;
1133 idx = objnum & ((1 << epbs) - 1);
1135 if (data->dnode_buf != NULL && grub_memcmp (data->dnode_mdn, mdn,
1136 sizeof (*mdn)) == 0
1137 && objnum >= data->dnode_start && objnum < data->dnode_end)
1139 grub_memmove (&(buf->dn), &(data->dnode_buf)[idx], DNODE_SIZE);
1140 buf->endian = data->dnode_endian;
1141 if (type && buf->dn.dn_type != type)
1142 return grub_error(GRUB_ERR_BAD_FS, "incorrect dnode type");
1143 return GRUB_ERR_NONE;
1146 grub_dprintf ("zfs", "endian = %d, blkid=%llx\n", mdn->endian,
1147 (unsigned long long) blkid);
1148 err = dmu_read (mdn, blkid, (void **) &dnbuf, &endian, data);
1149 if (err)
1150 return err;
1151 grub_dprintf ("zfs", "alive\n");
1153 grub_free (data->dnode_buf);
1154 grub_free (data->dnode_mdn);
1155 data->dnode_mdn = grub_malloc (sizeof (*mdn));
1156 if (! data->dnode_mdn)
1158 grub_errno = GRUB_ERR_NONE;
1159 data->dnode_buf = 0;
1161 else
1163 grub_memcpy (data->dnode_mdn, mdn, sizeof (*mdn));
1164 data->dnode_buf = dnbuf;
1165 data->dnode_start = blkid << epbs;
1166 data->dnode_end = (blkid + 1) << epbs;
1167 data->dnode_endian = endian;
1170 grub_memmove (&(buf->dn), &dnbuf[idx], DNODE_SIZE);
1171 buf->endian = endian;
1172 if (type && buf->dn.dn_type != type)
1173 return grub_error(GRUB_ERR_BAD_FS, "incorrect dnode type");
1175 return GRUB_ERR_NONE;
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.
1184 static grub_err_t
1185 dnode_get_path (dnode_end_t * mdn, const char *path_in, dnode_end_t * dn,
1186 struct grub_zfs_data *data)
1188 grub_uint64_t objnum, version;
1189 char *cname, ch;
1190 grub_err_t err = GRUB_ERR_NONE;
1191 char *path, *path_buf;
1192 struct dnode_chain
1194 struct dnode_chain *next;
1195 dnode_end_t dn;
1197 struct dnode_chain *dnode_path = 0, *dn_new, *root;
1199 dn_new = grub_malloc (sizeof (*dn_new));
1200 if (! dn_new)
1201 return grub_errno;
1202 dn_new->next = 0;
1203 dnode_path = root = dn_new;
1205 err = dnode_get (mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
1206 &(dnode_path->dn), data);
1207 if (err)
1209 grub_free (dn_new);
1210 return err;
1213 err = zap_lookup (&(dnode_path->dn), ZPL_VERSION_STR, &version, data);
1214 if (err)
1216 grub_free (dn_new);
1217 return err;
1219 if (version > ZPL_VERSION)
1221 grub_free (dn_new);
1222 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "too new ZPL version");
1225 err = zap_lookup (&(dnode_path->dn), ZFS_ROOT_OBJ, &objnum, data);
1226 if (err)
1228 grub_free (dn_new);
1229 return err;
1232 err = dnode_get (mdn, objnum, 0, &(dnode_path->dn), data);
1233 if (err)
1235 grub_free (dn_new);
1236 return err;
1239 path = path_buf = grub_strdup (path_in);
1240 if (!path_buf)
1242 grub_free (dn_new);
1243 return grub_errno;
1246 while (1)
1248 /* skip leading slashes */
1249 while (*path == '/')
1250 path++;
1251 if (!*path)
1252 break;
1253 /* get the next component name */
1254 cname = path;
1255 while (*path && *path != '/')
1256 path++;
1257 /* Skip dot. */
1258 if (cname + 1 == path && cname[0] == '.')
1259 continue;
1260 /* Handle double dot. */
1261 if (cname + 2 == path && cname[0] == '.' && cname[1] == '.')
1263 if (dn_new->next)
1265 dn_new = dnode_path;
1266 dnode_path = dn_new->next;
1267 grub_free (dn_new);
1269 else
1271 err = grub_error (GRUB_ERR_FILE_NOT_FOUND,
1272 "can't resolve ..");
1273 break;
1275 continue;
1278 ch = *path;
1279 *path = 0; /* ensure null termination */
1281 if (dnode_path->dn.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS)
1283 grub_free (path_buf);
1284 return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
1286 err = zap_lookup (&(dnode_path->dn), cname, &objnum, data);
1287 if (err)
1288 break;
1290 dn_new = grub_malloc (sizeof (*dn_new));
1291 if (! dn_new)
1293 err = grub_errno;
1294 break;
1296 dn_new->next = dnode_path;
1297 dnode_path = dn_new;
1299 objnum = ZFS_DIRENT_OBJ (objnum);
1300 err = dnode_get (mdn, objnum, 0, &(dnode_path->dn), data);
1301 if (err)
1302 break;
1304 *path = ch;
1305 if (((grub_zfs_to_cpu64(((znode_phys_t *) &dnode_path->dn.dn.dn_bonus)->zp_mode, dnode_path->dn.endian) >> 12) & 0xf) == 0xa && ch)
1307 char *oldpath = path, *oldpathbuf = path_buf;
1308 path = path_buf
1309 = grub_malloc (sizeof (dnode_path->dn.dn.dn_bonus)
1310 - sizeof (znode_phys_t) + grub_strlen (oldpath) + 1);
1311 if (!path_buf)
1313 grub_free (oldpathbuf);
1314 return grub_errno;
1316 grub_memcpy (path,
1317 (char *) &dnode_path->dn.dn.dn_bonus[sizeof (znode_phys_t)],
1318 sizeof (dnode_path->dn.dn.dn_bonus) - sizeof (znode_phys_t));
1319 path [sizeof (dnode_path->dn.dn.dn_bonus) - sizeof (znode_phys_t)] = 0;
1320 grub_memcpy (path + grub_strlen (path), oldpath,
1321 grub_strlen (oldpath) + 1);
1323 grub_free (oldpathbuf);
1324 if (path[0] != '/')
1326 dn_new = dnode_path;
1327 dnode_path = dn_new->next;
1328 grub_free (dn_new);
1330 else while (dnode_path != root)
1332 dn_new = dnode_path;
1333 dnode_path = dn_new->next;
1334 grub_free (dn_new);
1339 if (!err)
1340 grub_memcpy (dn, &(dnode_path->dn), sizeof (*dn));
1342 while (dnode_path)
1344 dn_new = dnode_path->next;
1345 grub_free (dnode_path);
1346 dnode_path = dn_new;
1348 grub_free (path_buf);
1349 return err;
1352 #if 0
1354 * Get the default 'bootfs' property value from the rootpool.
1357 static grub_err_t
1358 get_default_bootfsobj (dnode_phys_t * mosmdn, grub_uint64_t * obj,
1359 struct grub_zfs_data *data)
1361 grub_uint64_t objnum = 0;
1362 dnode_phys_t *dn;
1363 if (!dn)
1364 return grub_errno;
1366 if ((grub_errno = dnode_get (mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1367 DMU_OT_OBJECT_DIRECTORY, dn, data)))
1369 grub_free (dn);
1370 return (grub_errno);
1374 * find the object number for 'pool_props', and get the dnode
1375 * of the 'pool_props'.
1377 if (zap_lookup (dn, DMU_POOL_PROPS, &objnum, data))
1379 grub_free (dn);
1380 return (GRUB_ERR_BAD_FS);
1382 if ((grub_errno = dnode_get (mosmdn, objnum, DMU_OT_POOL_PROPS, dn, data)))
1384 grub_free (dn);
1385 return (grub_errno);
1387 if (zap_lookup (dn, ZPOOL_PROP_BOOTFS, &objnum, data))
1389 grub_free (dn);
1390 return (GRUB_ERR_BAD_FS);
1393 if (!objnum)
1395 grub_free (dn);
1396 return (GRUB_ERR_BAD_FS);
1399 *obj = objnum;
1400 return (0);
1402 #endif
1404 * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
1405 * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
1406 * of pool/rootfs.
1408 * If no fsname and no obj are given, return the DSL_DIR metadnode.
1409 * If fsname is given, return its metadnode and its matching object number.
1410 * If only obj is given, return the metadnode for this object number.
1413 static grub_err_t
1414 get_filesystem_dnode (dnode_end_t * mosmdn, char *fsname,
1415 dnode_end_t * mdn, struct grub_zfs_data *data)
1417 grub_uint64_t objnum;
1418 grub_err_t err;
1420 grub_dprintf ("zfs", "endian = %d\n", mosmdn->endian);
1422 err = dnode_get (mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1423 DMU_OT_OBJECT_DIRECTORY, mdn, data);
1424 if (err)
1425 return err;
1427 grub_dprintf ("zfs", "alive\n");
1429 err = zap_lookup (mdn, DMU_POOL_ROOT_DATASET, &objnum, data);
1430 if (err)
1431 return err;
1433 grub_dprintf ("zfs", "alive\n");
1435 err = dnode_get (mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data);
1436 if (err)
1437 return err;
1439 grub_dprintf ("zfs", "alive\n");
1441 while (*fsname)
1443 grub_uint64_t childobj;
1444 char *cname, ch;
1446 while (*fsname == '/')
1447 fsname++;
1449 if (! *fsname || *fsname == '@')
1450 break;
1452 cname = fsname;
1453 while (*fsname && !grub_isspace (*fsname) && *fsname != '/')
1454 fsname++;
1455 ch = *fsname;
1456 *fsname = 0;
1458 childobj = grub_zfs_to_cpu64(((dsl_dir_phys_t *) &(mdn->dn.dn_bonus))->dd_child_dir_zapobj, mdn->endian);
1459 err = dnode_get (mosmdn, childobj,
1460 DMU_OT_DSL_DIR_CHILD_MAP, mdn, data);
1461 if (err)
1462 return err;
1464 err = zap_lookup (mdn, cname, &objnum, data);
1465 if (err)
1466 return err;
1468 err = dnode_get (mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data);
1469 if (err)
1470 return err;
1472 *fsname = ch;
1474 return GRUB_ERR_NONE;
1477 static grub_err_t
1478 make_mdn (dnode_end_t * mdn, struct grub_zfs_data *data)
1480 objset_phys_t *osp;
1481 blkptr_t *bp;
1482 grub_size_t ospsize;
1483 grub_err_t err;
1485 grub_dprintf ("zfs", "endian = %d\n", mdn->endian);
1487 bp = &((dsl_dataset_phys_t *) &(mdn->dn.dn_bonus))->ds_bp;
1488 err = zio_read (bp, mdn->endian, (void **) &osp, &ospsize, data);
1489 if (err)
1490 return err;
1491 if (ospsize < sizeof (objset_phys_t))
1493 grub_free (osp);
1494 return grub_error (GRUB_ERR_BAD_FS, "too small osp");
1497 mdn->endian = (grub_zfs_to_cpu64 (bp->blk_prop, mdn->endian)>>63) & 1;
1498 grub_memmove ((char *) &(mdn->dn), (char *) &osp->os_meta_dnode, DNODE_SIZE);
1499 grub_free (osp);
1500 return GRUB_ERR_NONE;
1503 static grub_err_t
1504 dnode_get_fullpath (const char *fullpath, dnode_end_t * mdn,
1505 dnode_end_t * dn, int *isfs,
1506 struct grub_zfs_data *data)
1508 char *fsname, *snapname;
1509 const char *ptr_at, *ptr_slash, *filename;
1510 grub_uint64_t headobj;
1511 grub_err_t err;
1513 ptr_at = grub_strchr (fullpath, '@');
1514 if (ptr_at)
1515 ptr_slash = grub_strchr (ptr_at, '/');
1516 if (! ptr_at)
1518 *isfs = 1;
1519 filename = 0;
1520 snapname = 0;
1521 fsname = grub_strdup (fullpath);
1523 else
1525 *isfs = 0;
1526 fsname = grub_malloc (ptr_at - fullpath + 1);
1527 if (!fsname)
1528 return grub_errno;
1529 grub_memcpy (fsname, fullpath, ptr_at - fullpath);
1530 fsname[ptr_at - fullpath] = 0;
1531 if (ptr_at[1] && ptr_at[1] != '/')
1533 snapname = grub_malloc (ptr_slash - ptr_at);
1534 if (!snapname)
1536 grub_free (fsname);
1537 return grub_errno;
1539 grub_memcpy (snapname, ptr_at + 1, ptr_slash - ptr_at - 1);
1540 snapname[ptr_slash - ptr_at - 1] = 0;
1542 else
1543 snapname = 0;
1544 if (ptr_slash)
1545 filename = ptr_slash;
1546 else
1547 filename = "/";
1548 grub_dprintf ("zfs", "fsname = '%s' snapname='%s' filename = '%s'\n",
1549 fsname, snapname, filename);
1551 grub_dprintf ("zfs", "alive\n");
1552 err = get_filesystem_dnode (&(data->mos), fsname, dn, data);
1553 if (err)
1555 grub_free (fsname);
1556 grub_free (snapname);
1557 return err;
1560 grub_dprintf ("zfs", "alive\n");
1562 headobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) &(dn->dn.dn_bonus))->dd_head_dataset_obj, dn->endian);
1564 grub_dprintf ("zfs", "endian = %d\n", mdn->endian);
1566 err = dnode_get (&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data);
1567 if (err)
1569 grub_free (fsname);
1570 grub_free (snapname);
1571 return err;
1573 grub_dprintf ("zfs", "endian = %d\n", mdn->endian);
1575 if (snapname)
1577 grub_uint64_t snapobj;
1579 snapobj = grub_zfs_to_cpu64 (((dsl_dataset_phys_t *) &(mdn->dn.dn_bonus))->ds_snapnames_zapobj, mdn->endian);
1581 err = dnode_get (&(data->mos), snapobj,
1582 DMU_OT_DSL_DS_SNAP_MAP, mdn, data);
1583 if (!err)
1584 err = zap_lookup (mdn, snapname, &headobj, data);
1585 if (!err)
1586 err = dnode_get (&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data);
1587 if (err)
1589 grub_free (fsname);
1590 grub_free (snapname);
1591 return err;
1595 make_mdn (mdn, data);
1597 grub_dprintf ("zfs", "endian = %d\n", mdn->endian);
1599 if (*isfs)
1601 grub_free (fsname);
1602 grub_free (snapname);
1603 return GRUB_ERR_NONE;
1605 err = dnode_get_path (mdn, filename, dn, data);
1606 grub_free (fsname);
1607 grub_free (snapname);
1608 return err;
1612 * For a given XDR packed nvlist, verify the first 4 bytes and move on.
1614 * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
1616 * encoding method/host endian (4 bytes)
1617 * nvl_version (4 bytes)
1618 * nvl_nvflag (4 bytes)
1619 * encoded nvpairs:
1620 * encoded size of the nvpair (4 bytes)
1621 * decoded size of the nvpair (4 bytes)
1622 * name string size (4 bytes)
1623 * name string data (sizeof(NV_ALIGN4(string))
1624 * data type (4 bytes)
1625 * # of elements in the nvpair (4 bytes)
1626 * data
1627 * 2 zero's for the last nvpair
1628 * (end of the entire list) (8 bytes)
1630 * Return:
1631 * 0 - success
1632 * 1 - failure
1635 #if 0
1636 static char *
1637 nvlist_array (char *nvlist, int index)
1639 int i, encode_size;
1641 for (i = 0; i < index; i++)
1643 /* skip the header, nvl_version, and nvl_nvflag */
1644 nvlist = nvlist + 4 * 2;
1646 while ((encode_size = BSWAP_32 (*(grub_uint32_t *) nvlist)))
1647 nvlist += encode_size; /* goto the next nvpair */
1649 nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
1652 return (nvlist);
1654 #endif
1656 static grub_err_t
1657 nvlist_find_value (char *nvlist, char *name, int valtype, char **val)
1659 int name_len, type, encode_size;
1660 char *nvpair, *nvp_name;
1662 /* Verify if the 1st and 2nd byte in the nvlist are valid. */
1663 /* NOTE: independently of what endianness header announces all
1664 subsequent values are big-endian. */
1665 if (nvlist[0] != NV_ENCODE_XDR || (nvlist[1] != NV_LITTLE_ENDIAN
1666 && nvlist[1] != NV_BIG_ENDIAN))
1667 return grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist");
1669 /* skip the header, nvl_version, and nvl_nvflag */
1670 nvlist = nvlist + 4 * 3;
1672 * Loop thru the nvpair list
1673 * The XDR representation of an integer is in big-endian byte order.
1675 while ((encode_size = grub_be_to_cpu32 (*(grub_uint32_t *) nvlist)))
1677 int nelm;
1679 nvpair = nvlist + 4 * 2; /* skip the encode/decode size */
1681 name_len = grub_be_to_cpu32 (*(grub_uint32_t *) nvpair);
1682 nvpair += 4;
1684 nvp_name = nvpair;
1685 nvpair = nvpair + ((name_len + 3) & ~3); /* align */
1687 type = grub_be_to_cpu32 (*(grub_uint32_t *) nvpair);
1688 nvpair += 4;
1690 nelm = grub_be_to_cpu32 (*(grub_uint32_t *) nvpair);
1691 if (nelm < 1)
1692 return grub_error (GRUB_ERR_BAD_FS, "empty nvpair");
1694 nvpair += 4;
1696 if ((grub_strncmp (nvp_name, name, name_len) == 0) && type == valtype)
1698 *val = nvpair;
1699 return GRUB_ERR_NONE;
1702 nvlist += encode_size; /* goto the next nvpair */
1704 return grub_error (GRUB_ERR_BAD_ARGUMENT, "key %s not found", name);
1707 static grub_err_t
1708 nvlist_lookup_uint64 (char *nvlist, char *name, grub_uint64_t * out)
1710 char *nvpair;
1711 grub_err_t err;
1712 err = nvlist_find_value (nvlist, name, DATA_TYPE_UINT64, &nvpair);
1713 if (err)
1714 return err;
1715 *out = grub_be_to_cpu64 (*(grub_uint64_t *) nvpair);
1716 return GRUB_ERR_NONE;
1719 static char *
1720 nvlist_lookup_string (char *nvlist, char *name)
1722 char *nvpair;
1723 grub_err_t err;
1724 char *ret;
1725 grub_size_t slen;
1727 err = nvlist_find_value (nvlist, name, DATA_TYPE_STRING, &nvpair);
1728 if (err)
1729 return 0;
1730 slen = grub_be_to_cpu32 (*(grub_uint32_t *) nvpair);
1731 ret = grub_malloc (slen + 1);
1732 if (!ret)
1733 return 0;
1734 grub_memcpy (ret, nvpair + 4, slen);
1735 ret[slen] = 0;
1736 return ret;
1739 #if 0
1740 static int
1741 nvlist_lookup_value (char *nvlist, char *name, void *val, int valtype,
1742 int *nelmp)
1744 grub_uint64_t *intval = val;
1746 switch (valtype)
1749 case DATA_TYPE_NVLIST:
1750 *(void **) val = (void *) nvpair;
1751 return (0);
1753 case DATA_TYPE_NVLIST_ARRAY:
1754 *(void **) val = (void *) nvpair;
1755 if (nelmp)
1756 *nelmp = nelm;
1757 return (0);
1761 #endif
1763 #if 0
1765 * Check if this vdev is online and is in a good state.
1767 static int
1768 vdev_validate (char *nv)
1770 grub_uint64_t ival;
1772 if (nvlist_lookup_value (nv, ZPOOL_CONFIG_OFFLINE, &ival,
1773 DATA_TYPE_UINT64, NULL) == 0 ||
1774 nvlist_lookup_value (nv, ZPOOL_CONFIG_FAULTED, &ival,
1775 DATA_TYPE_UINT64, NULL) == 0 ||
1776 nvlist_lookup_value (nv, ZPOOL_CONFIG_REMOVED, &ival,
1777 DATA_TYPE_UINT64, NULL) == 0)
1778 return (GRUB_ERR_BAD_FS);
1780 return (0);
1784 * Get a list of valid vdev pathname from the boot device.
1785 * The caller should already allocate MAXPATHLEN memory for bootpath and devid.
1788 vdev_get_bootpath (char *nv, grub_uint64_t inguid, char *devid,
1789 char *bootpath)
1791 char type[16];
1793 if (nvlist_lookup_value (nv, ZPOOL_CONFIG_TYPE, &type, DATA_TYPE_STRING,
1794 NULL))
1795 return (GRUB_ERR_BAD_FS);
1797 if (grub_strcmp (type, VDEV_TYPE_DISK) == 0)
1799 grub_uint64_t guid;
1801 if (vdev_validate (nv) != 0)
1802 return (GRUB_ERR_FILE_NOT_FOUND);
1804 if (nvlist_lookup_value (nv, ZPOOL_CONFIG_GUID,
1805 &guid, DATA_TYPE_UINT64, NULL) != 0)
1806 return (GRUB_ERR_FILE_NOT_FOUND);
1808 if (guid != inguid)
1809 return (GRUB_ERR_FILE_NOT_FOUND);
1811 if (nvlist_lookup_value (nv, ZPOOL_CONFIG_PHYS_PATH,
1812 bootpath, DATA_TYPE_STRING, NULL) != 0)
1813 bootpath[0] = '\0';
1815 if (nvlist_lookup_value (nv, ZPOOL_CONFIG_DEVID,
1816 devid, DATA_TYPE_STRING, NULL) != 0)
1817 devid[0] = '\0';
1819 if (grub_strlen (bootpath) >= MAXPATHLEN ||
1820 grub_strlen (devid) >= MAXPATHLEN)
1821 return (GRUB_ERR_OUT_OF_MEMORY);
1823 return (0);
1826 else if (grub_strcmp (type, VDEV_TYPE_MIRROR) == 0)
1828 int nelm, i;
1829 char *child;
1831 if (nvlist_lookup_value (nv, ZPOOL_CONFIG_CHILDREN, &child,
1832 DATA_TYPE_NVLIST_ARRAY, &nelm))
1833 return (GRUB_ERR_BAD_FS);
1835 for (i = 0; i < nelm; i++)
1837 char *child_i;
1839 child_i = nvlist_array (child, i);
1840 if (vdev_get_bootpath (child_i, inguid, devid, bootpath) == 0)
1841 return (0);
1845 return (GRUB_ERR_FILE_NOT_FOUND);
1847 #endif
1849 grub_err_t
1850 grub_zfs_fetch_nvlist (struct grub_zfs_data * data, char **nvlist)
1852 grub_uint64_t sector;
1853 grub_err_t err;
1855 sector = (data->current_label * sizeof (vdev_label_t) + VDEV_SKIP_SIZE +
1856 VDEV_BOOT_HEADER_SIZE) >> SPA_MINBLOCKSHIFT;
1857 *nvlist = grub_malloc (VDEV_PHYS_SIZE);
1858 /* Read in the vdev name-value pair list (112K). */
1859 err = grub_disk_read (data->disk, sector, 0, VDEV_PHYS_SIZE, *nvlist);
1860 if (err)
1862 grub_free (*nvlist);
1863 *nvlist = 0;
1864 return err;
1866 return GRUB_ERR_NONE;
1871 * Check the disk label information and retrieve needed vdev name-value pairs.
1874 static grub_err_t
1875 check_pool_label (struct grub_zfs_data *data)
1877 grub_uint64_t pool_state, txg = 0;
1878 char *nvlist;
1879 #if 0
1880 char *nv;
1881 #endif
1882 grub_uint64_t diskguid;
1883 grub_uint64_t version;
1884 grub_err_t err;
1886 err = grub_zfs_fetch_nvlist (data, &nvlist);
1887 if (err)
1888 return err;
1890 grub_dprintf ("zfs", "check 2 passed\n");
1892 err = nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_STATE, &pool_state);
1893 if (err)
1895 grub_free (nvlist);
1896 return err;
1898 grub_dprintf ("zfs", "check 3 passed\n");
1900 if (pool_state == POOL_STATE_DESTROYED)
1902 grub_free (nvlist);
1903 return grub_error (GRUB_ERR_BAD_FS, "zpool is marked as destroyed");
1905 grub_dprintf ("zfs", "check 4 passed\n");
1907 err = nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_TXG, &txg);
1908 if (err)
1910 grub_free (nvlist);
1911 return err;
1913 grub_dprintf ("zfs", "check 6 passed\n");
1915 /* not an active device */
1916 if (txg == 0)
1918 grub_free (nvlist);
1919 return grub_error (GRUB_ERR_BAD_FS, "zpool isn't active");
1921 grub_dprintf ("zfs", "check 7 passed\n");
1923 err = nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_VERSION, &version);
1924 if (err)
1926 grub_free (nvlist);
1927 return err;
1929 grub_dprintf ("zfs", "check 8 passed\n");
1931 if (version > SPA_VERSION)
1933 grub_free (nvlist);
1934 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
1935 "too new version %llu > %llu",
1936 (unsigned long long) version,
1937 (unsigned long long) SPA_VERSION);
1939 grub_dprintf ("zfs", "check 9 passed\n");
1940 #if 0
1941 if (nvlist_lookup_value (nvlist, ZPOOL_CONFIG_VDEV_TREE, &nv,
1942 DATA_TYPE_NVLIST, NULL))
1944 grub_free (vdev);
1945 return (GRUB_ERR_BAD_FS);
1947 grub_dprintf ("zfs", "check 10 passed\n");
1948 #endif
1950 err = nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_GUID, &diskguid);
1951 if (err)
1953 grub_free (nvlist);
1954 return err;
1956 grub_dprintf ("zfs", "check 11 passed\n");
1958 #if 0
1959 if (vdev_get_bootpath (nv, diskguid, outdevid, outpath))
1961 grub_free (vdev);
1962 return (GRUB_ERR_FILE_NOT_FOUND);
1964 #endif
1965 grub_dprintf ("zfs", "check 12 passed\n");
1966 grub_free (nvlist);
1968 return GRUB_ERR_NONE;
1971 static void
1972 zfs_unmount (struct grub_zfs_data *data)
1974 grub_free (data->dnode_buf);
1975 grub_free (data->dnode_mdn);
1976 grub_free (data->file_buf);
1977 grub_free (data);
1981 * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
1982 * to the memory address MOS.
1985 struct grub_zfs_data *
1986 zfs_mount (grub_disk_t disk)
1988 struct grub_zfs_data *data = 0;
1989 int label = 0;
1990 uberblock_phys_t *ub_array, *ubbest = NULL;
1991 vdev_boot_header_t *bh;
1992 objset_phys_t *osp = 0;
1993 grub_size_t ospsize;
1994 grub_err_t err;
1995 #if 0
1996 char tmp_bootpath[MAXNAMELEN];
1997 char tmp_devid[MAXNAMELEN];
1998 #endif
2000 data = grub_malloc (sizeof (*data));
2001 if (!data)
2002 return 0;
2003 grub_memset (data, 0, sizeof (*data));
2004 #if 0
2005 /* if it's our first time here, zero the best uberblock out */
2006 if (data->best_drive == 0 && data->best_part == 0 && find_best_root)
2007 grub_memset (&current_uberblock, 0, sizeof (uberblock_t));
2008 #endif
2010 data->disk = disk;
2012 ub_array = grub_malloc (VDEV_UBERBLOCK_RING);
2013 if (!ub_array)
2015 zfs_unmount (data);
2016 return 0;
2019 bh = grub_malloc (VDEV_BOOT_HEADER_SIZE);
2020 if (!bh)
2022 zfs_unmount (data);
2023 grub_free (ub_array);
2024 return 0;
2028 /* XXX add back labels support? */
2029 for (label = 0; ubbest == NULL && label < (VDEV_LABELS / 2); label++)
2031 grub_uint64_t sector = (label * sizeof (vdev_label_t) +
2032 VDEV_SKIP_SIZE) >> SPA_MINBLOCKSHIFT;
2033 grub_zfs_endian_t vdev_endian = UNKNOWN_ENDIAN;
2034 grub_zfs_endian_t ub_endian = UNKNOWN_ENDIAN;
2035 grub_dprintf ("zfs", "label %d\n", label);
2036 err = grub_disk_read (data->disk, sector, 0, VDEV_BOOT_HEADER_SIZE,
2037 (char *) bh);
2038 if (err)
2040 grub_errno = GRUB_ERR_NONE;
2041 continue;
2043 if ((grub_zfs_to_cpu64 (bh->vb_magic, LITTLE_ENDIAN) == VDEV_BOOT_MAGIC)
2044 && (grub_zfs_to_cpu64 (bh->vb_version, LITTLE_ENDIAN)
2045 == VDEV_BOOT_VERSION))
2047 grub_dprintf ("zfs", "little endian VDEV\n");
2048 vdev_endian = LITTLE_ENDIAN;
2051 if ((grub_zfs_to_cpu64 (bh->vb_magic, BIG_ENDIAN) == VDEV_BOOT_MAGIC)
2052 && (grub_zfs_to_cpu64 (bh->vb_version, BIG_ENDIAN)
2053 == VDEV_BOOT_VERSION))
2055 grub_dprintf ("zfs", "big endian VDEV\n");
2056 vdev_endian = BIG_ENDIAN;
2059 if (vdev_endian == UNKNOWN_ENDIAN)
2061 grub_dprintf ("zfs", "incorrect magic\n");
2062 continue;
2064 sector += (VDEV_BOOT_HEADER_SIZE + VDEV_PHYS_SIZE) >> SPA_MINBLOCKSHIFT;
2066 /* Read in the uberblock ring (128K). */
2067 err = grub_disk_read (data->disk, sector, 0, VDEV_UBERBLOCK_RING,
2068 (char *) ub_array);
2069 if (err)
2071 grub_errno = GRUB_ERR_NONE;
2072 continue;
2074 grub_dprintf ("zfs", "label ok %d\n", label);
2076 ubbest = find_bestub (ub_array, label);
2077 if (!ubbest)
2079 grub_dprintf ("zfs", "No uberblock found\n");
2080 grub_errno = GRUB_ERR_NONE;
2081 continue;
2083 ub_endian = (grub_zfs_to_cpu64 (ubbest->ubp_uberblock.ub_magic,
2084 LITTLE_ENDIAN) == UBERBLOCK_MAGIC
2085 ? LITTLE_ENDIAN : BIG_ENDIAN);
2086 err = zio_read (&ubbest->ubp_uberblock.ub_rootbp,
2087 ub_endian,
2088 (void **) &osp, &ospsize, data);
2089 if (err)
2091 grub_dprintf ("zfs", "couldn't zio_read\n");
2092 grub_errno = GRUB_ERR_NONE;
2093 continue;
2096 if (ospsize < sizeof (objset_phys_t))
2098 grub_dprintf ("zfs", "osp too small\n");
2099 grub_free (osp);
2100 continue;
2102 grub_dprintf ("zfs", "ubbest %p\n", ubbest);
2104 data->current_label = label;
2105 err = check_pool_label (data);
2106 if (err)
2108 grub_errno = GRUB_ERR_NONE;
2109 continue;
2111 #if 0
2112 if (find_best_root &&
2113 vdev_uberblock_compare (&ubbest->ubp_uberblock,
2114 &(current_uberblock)) <= 0)
2115 continue;
2116 #endif
2117 /* Got the MOS. Save it at the memory addr MOS. */
2118 grub_memmove (&(data->mos.dn), &osp->os_meta_dnode, DNODE_SIZE);
2119 data->mos.endian = (grub_zfs_to_cpu64 (ubbest->ubp_uberblock.ub_rootbp.blk_prop, ub_endian) >> 63) & 1;
2120 grub_memmove (&(data->current_uberblock),
2121 &ubbest->ubp_uberblock, sizeof (uberblock_t));
2122 #if 0
2123 grub_memmove (data->current_bootpath, tmp_bootpath, MAXNAMELEN);
2124 grub_memmove (data->current_devid, tmp_devid,
2125 grub_strlen (tmp_devid));
2126 #endif
2127 grub_free (ub_array);
2128 grub_free (bh);
2129 grub_free (osp);
2130 return data;
2132 grub_error (GRUB_ERR_BAD_FS, "couldn't find a valid label");
2133 zfs_unmount (data);
2134 grub_free (ub_array);
2135 grub_free (bh);
2136 grub_free (osp);
2138 return 0;
2141 static grub_err_t
2142 zfs_label (grub_device_t device, char **label)
2144 char *nvlist;
2145 grub_err_t err;
2146 struct grub_zfs_data *data;
2148 data = zfs_mount (device->disk);
2149 if (! data)
2150 return grub_errno;
2152 err = grub_zfs_fetch_nvlist (data, &nvlist);
2153 if (err)
2155 zfs_unmount (data);
2156 return err;
2159 *label = nvlist_lookup_string (nvlist, ZPOOL_CONFIG_POOL_NAME);
2160 grub_free (nvlist);
2161 zfs_unmount (data);
2162 return grub_errno;
2165 static grub_err_t
2166 zfs_uuid (grub_device_t device, char **uuid)
2168 char *nvlist;
2169 grub_err_t err;
2170 struct grub_zfs_data *data;
2171 grub_uint64_t guid;
2173 data = zfs_mount (device->disk);
2174 if (! data)
2175 return grub_errno;
2177 err = grub_zfs_fetch_nvlist (data, &nvlist);
2178 if (err)
2180 zfs_unmount (data);
2181 return err;
2184 err = nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_NAME, &guid);
2185 if (err)
2186 return err;
2187 grub_free (nvlist);
2188 *uuid = grub_malloc (16 + sizeof ('\0'));
2189 if (! *uuid)
2190 return grub_errno;
2191 grub_sprintf (*uuid, "%016llx", (long long unsigned) guid);
2192 zfs_unmount (data);
2193 return GRUB_ERR_NONE;
2197 * zfs_open() locates a file in the rootpool by following the
2198 * MOS and places the dnode of the file in the memory address DNODE.
2200 static grub_err_t
2201 grub_zfs_open (struct grub_file *file, const char *fsfilename)
2203 struct grub_zfs_data *data;
2204 grub_err_t err;
2205 int isfs;
2207 data = zfs_mount (file->device->disk);
2208 if (! data)
2209 return grub_errno;
2211 err = dnode_get_fullpath (fsfilename, &(data->mdn),
2212 &(data->dnode), &isfs, data);
2213 if (err)
2215 zfs_unmount (data);
2216 return err;
2219 if (isfs)
2221 zfs_unmount (data);
2222 return grub_error (GRUB_ERR_FILE_NOT_FOUND, "Missing @ or / separator");
2225 /* We found the dnode for this file. Verify if it is a plain file. */
2226 if (data->dnode.dn.dn_type != DMU_OT_PLAIN_FILE_CONTENTS)
2228 zfs_unmount (data);
2229 return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a file");
2232 /* get the file size and set the file position to 0 */
2233 file->data = data;
2234 file->offset = 0;
2235 file->size = grub_zfs_to_cpu64 (((znode_phys_t *) &(data->dnode.dn.dn_bonus))->zp_size, data->dnode.endian);
2237 #ifndef GRUB_UTIL
2238 grub_dl_ref (my_mod);
2239 #endif
2241 return GRUB_ERR_NONE;
2244 static grub_ssize_t
2245 grub_zfs_read (grub_file_t file, char *buf, grub_size_t len)
2247 struct grub_zfs_data *data = (struct grub_zfs_data *) file->data;
2248 int blksz, movesize;
2249 grub_size_t length;
2250 grub_size_t read;
2251 grub_err_t err;
2253 if (data->file_buf == NULL)
2255 data->file_buf = grub_malloc (SPA_MAXBLOCKSIZE);
2256 if (!data->file_buf)
2257 return -1;
2258 data->file_start = data->file_end = 0;
2262 * If offset is in memory, move it into the buffer provided and return.
2264 if (file->offset >= data->file_start
2265 && file->offset + len <= data->file_end)
2267 grub_memmove (buf, data->file_buf + file->offset - data->file_start,
2268 len);
2269 return len;
2272 blksz = grub_zfs_to_cpu16 (data->dnode.dn.dn_datablkszsec,
2273 data->dnode.endian) << SPA_MINBLOCKSHIFT;
2276 * Entire Dnode is too big to fit into the space available. We
2277 * will need to read it in chunks. This could be optimized to
2278 * read in as large a chunk as there is space available, but for
2279 * now, this only reads in one data block at a time.
2281 length = len;
2282 read = 0;
2283 while (length)
2286 * Find requested blkid and the offset within that block.
2288 grub_uint64_t blkid = grub_divmod64 (file->offset + read, blksz, 0);
2289 grub_free (data->file_buf);
2290 data->file_buf = 0;
2292 err = dmu_read (&(data->dnode), blkid, &(data->file_buf), 0, data);
2293 if (err)
2294 return -1;
2296 data->file_start = blkid * blksz;
2297 data->file_end = data->file_start + blksz;
2299 movesize = MIN (length, data->file_end - (int) file->offset - read);
2301 grub_memmove (buf, data->file_buf + file->offset + read
2302 - data->file_start, movesize);
2303 buf += movesize;
2304 length -= movesize;
2305 read += movesize;
2308 return len;
2311 static grub_err_t
2312 grub_zfs_close (grub_file_t file)
2314 zfs_unmount ((struct grub_zfs_data *) file->data);
2316 #ifndef GRUB_UTIL
2317 grub_dl_unref (my_mod);
2318 #endif
2320 return GRUB_ERR_NONE;
2323 static void
2324 fill_fs_info (struct grub_dirhook_info *info,
2325 dnode_end_t mdn, struct grub_zfs_data *data)
2327 grub_err_t err;
2328 dnode_end_t dn;
2329 grub_uint64_t objnum;
2330 grub_uint64_t headobj;
2332 grub_memset (info, 0, sizeof (*info));
2334 info->dir = 1;
2336 if (mdn.dn.dn_type == DMU_OT_DSL_DIR)
2338 headobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) &(mdn.dn.dn_bonus))->dd_head_dataset_obj, mdn.endian);
2340 err = dnode_get (&(data->mos), headobj, DMU_OT_DSL_DATASET, &mdn, data);
2341 if (err)
2343 grub_dprintf ("zfs", "failed here\n");
2344 return;
2347 make_mdn (&mdn, data);
2348 err = dnode_get (&mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
2349 &dn, data);
2350 if (err)
2352 grub_dprintf ("zfs", "failed here\n");
2353 return;
2356 err = zap_lookup (&dn, ZFS_ROOT_OBJ, &objnum, data);
2357 if (err)
2359 grub_dprintf ("zfs", "failed here\n");
2360 return;
2363 err = dnode_get (&mdn, objnum, 0, &dn, data);
2364 if (err)
2366 grub_dprintf ("zfs", "failed here\n");
2367 return;
2370 info->mtimeset = 1;
2371 info->mtime = grub_zfs_to_cpu64 (((znode_phys_t *) &dn.dn.dn_bonus)->zp_mtime[0], dn.endian);
2372 return;
2375 static grub_err_t
2376 grub_zfs_dir (grub_device_t device, const char *path,
2377 int (*hook) (const char *, const struct grub_dirhook_info *))
2379 struct grub_zfs_data *data;
2380 grub_err_t err;
2381 int isfs;
2382 auto int NESTED_FUNC_ATTR iterate_zap (const char *name, grub_uint64_t val);
2383 auto int NESTED_FUNC_ATTR iterate_zap_fs (const char *name,
2384 grub_uint64_t val);
2385 auto int NESTED_FUNC_ATTR iterate_zap_snap (const char *name,
2386 grub_uint64_t val);
2388 int NESTED_FUNC_ATTR iterate_zap (const char *name, grub_uint64_t val)
2390 struct grub_dirhook_info info;
2391 dnode_end_t dn;
2392 grub_memset (&info, 0, sizeof (info));
2394 dnode_get (&(data->mdn), val, 0, &dn, data);
2395 info.mtimeset = 1;
2396 info.mtime = grub_zfs_to_cpu64 (((znode_phys_t *) &dn.dn.dn_bonus)->zp_mtime[0], dn.endian);
2397 info.dir = (dn.dn.dn_type == DMU_OT_DIRECTORY_CONTENTS);
2398 grub_dprintf ("zfs", "type=%d, name=%s\n",
2399 (int)dn.dn.dn_type, (char *)name);
2400 return hook (name, &info);
2403 int NESTED_FUNC_ATTR iterate_zap_fs (const char *name, grub_uint64_t val)
2405 struct grub_dirhook_info info;
2406 dnode_end_t mdn;
2407 err = dnode_get (&(data->mos), val, 0, &mdn, data);
2408 if (err)
2409 return 0;
2410 if (mdn.dn.dn_type != DMU_OT_DSL_DIR)
2411 return 0;
2413 fill_fs_info (&info, mdn, data);
2414 return hook (name, &info);
2416 int NESTED_FUNC_ATTR iterate_zap_snap (const char *name, grub_uint64_t val)
2418 struct grub_dirhook_info info;
2419 char *name2;
2420 int ret;
2421 dnode_end_t mdn;
2423 err = dnode_get (&(data->mos), val, 0, &mdn, data);
2424 if (err)
2425 return 0;
2427 if (mdn.dn.dn_type != DMU_OT_DSL_DATASET)
2428 return 0;
2430 fill_fs_info (&info, mdn, data);
2432 name2 = grub_malloc (grub_strlen (name) + 2);
2433 name2[0] = '@';
2434 grub_memcpy (name2 + 1, name, grub_strlen (name) + 1);
2435 ret = hook (name2, &info);
2436 grub_free (name2);
2437 return ret;
2440 data = zfs_mount (device->disk);
2441 if (! data)
2442 return grub_errno;
2443 err = dnode_get_fullpath (path, &(data->mdn), &(data->dnode), &isfs, data);
2444 if (err)
2446 zfs_unmount (data);
2447 return err;
2449 if (isfs)
2451 grub_uint64_t childobj, headobj;
2452 grub_uint64_t snapobj;
2453 dnode_end_t dn;
2454 struct grub_dirhook_info info;
2455 grub_err_t err;
2457 fill_fs_info (&info, data->dnode, data);
2458 hook ("@", &info);
2460 childobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) &(data->dnode.dn.dn_bonus))->dd_child_dir_zapobj, data->dnode.endian);
2461 headobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) &(data->dnode.dn.dn_bonus))->dd_head_dataset_obj, data->dnode.endian);
2462 err = dnode_get (&(data->mos), childobj,
2463 DMU_OT_DSL_DIR_CHILD_MAP, &dn, data);
2464 if (err)
2466 zfs_unmount (data);
2467 return err;
2470 zap_iterate (&dn, iterate_zap_fs, data);
2472 err = dnode_get (&(data->mos), headobj, DMU_OT_DSL_DATASET, &dn, data);
2473 if (err)
2475 zfs_unmount (data);
2476 return err;
2479 snapobj = grub_zfs_to_cpu64 (((dsl_dataset_phys_t *) &dn.dn.dn_bonus)->ds_snapnames_zapobj, dn.endian);
2481 err = dnode_get (&(data->mos), snapobj,
2482 DMU_OT_DSL_DS_SNAP_MAP, &dn, data);
2483 if (err)
2485 zfs_unmount (data);
2486 return err;
2489 zap_iterate (&dn, iterate_zap_snap, data);
2491 else
2493 if (data->dnode.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS)
2495 zfs_unmount (data);
2496 return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory");
2498 zap_iterate (&(data->dnode), iterate_zap, data);
2500 zfs_unmount (data);
2501 return grub_errno;
2504 static struct grub_fs grub_zfs_fs = {
2505 .name = "zfs",
2506 .dir = grub_zfs_dir,
2507 .open = grub_zfs_open,
2508 .read = grub_zfs_read,
2509 .close = grub_zfs_close,
2510 .label = zfs_label,
2511 .uuid = zfs_uuid,
2512 .mtime = 0,
2513 .next = 0
2516 GRUB_MOD_INIT (zfs)
2518 grub_fs_register (&grub_zfs_fs);
2519 #ifndef GRUB_UTIL
2520 my_mod = mod;
2521 #endif
2524 GRUB_MOD_FINI (zfs)
2526 grub_fs_unregister (&grub_zfs_fs);