1N/A/*
1N/A * GRUB -- GRand Unified Bootloader
1N/A * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1N/A */
1N/A/*
1N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1N/A */
1N/A
1N/A/*
1N/A * The zfs plug-in routines for GRUB are:
1N/A *
1N/A * zfs_mount() - locates a valid uberblock of the root pool and reads
1N/A * in its MOS at the memory address MOS.
1N/A *
1N/A * zfs_open() - locates a plain file object by following the MOS
1N/A * and places its dnode at the memory address DNODE.
1N/A *
1N/A * zfs_read() - read in the data blocks pointed by the DNODE.
1N/A *
1N/A * ZFS_SCRATCH is used as a working area.
1N/A *
1N/A * (memory addr) MOS DNODE ZFS_SCRATCH
1N/A * | | |
1N/A * +-------V---------V----------V---------------+
1N/A * memory | | dnode | dnode | scratch |
1N/A * | | 512B | 512B | area |
1N/A * +--------------------------------------------+
1N/A */
1N/A
1N/A#ifdef FSYS_ZFS
1N/A
1N/A#include "shared.h"
1N/A#include "filesys.h"
1N/A#include "fsys_zfs.h"
1N/A
1N/A/* cache for a file block of the currently zfs_open()-ed file */
1N/Astatic void *file_buf = NULL;
1N/Astatic uint64_t file_start = 0;
1N/Astatic uint64_t file_end = 0;
1N/A
1N/A/* cache for a dnode block */
1N/Astatic dnode_phys_t *dnode_buf = NULL;
1N/Astatic dnode_phys_t *dnode_mdn = NULL;
1N/Astatic uint64_t dnode_start = 0;
1N/Astatic uint64_t dnode_end = 0;
1N/A
1N/Astatic uint64_t pool_guid = 0;
1N/Astatic uberblock_t current_uberblock;
1N/Astatic char *stackbase;
1N/A
1N/Adecomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] =
1N/A{
1N/A {"inherit", 0}, /* ZIO_COMPRESS_INHERIT */
1N/A {"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */
1N/A {"off", 0}, /* ZIO_COMPRESS_OFF */
1N/A {"lzjb", lzjb_decompress}, /* ZIO_COMPRESS_LZJB */
1N/A {"empty", 0} /* ZIO_COMPRESS_EMPTY */
1N/A};
1N/A
1N/Astatic int zio_read_data(blkptr_t *bp, void *buf, char *stack);
1N/Astatic int zio_read_common(blkptr_t *bp, dva_t *dva, void *buf, char *stack);
1N/A
1N/A/*
1N/A * Our own version of bcmp().
1N/A */
1N/Astatic int
1N/Azfs_bcmp(const void *s1, const void *s2, size_t n)
1N/A{
1N/A const uchar_t *ps1 = s1;
1N/A const uchar_t *ps2 = s2;
1N/A
1N/A if (s1 != s2 && n != 0) {
1N/A do {
1N/A if (*ps1++ != *ps2++)
1N/A return (1);
1N/A } while (--n != 0);
1N/A }
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Our own version of log2(). Same thing as highbit()-1.
1N/A */
1N/Astatic int
1N/Azfs_log2(uint64_t num)
1N/A{
1N/A int i = 0;
1N/A
1N/A while (num > 1) {
1N/A i++;
1N/A num = num >> 1;
1N/A }
1N/A
1N/A return (i);
1N/A}
1N/A
1N/A/* Checksum Functions */
1N/Astatic void
1N/Azio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
1N/A{
1N/A ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
1N/A}
1N/A
1N/A/* Checksum Table and Values */
1N/Azio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
1N/A NULL, NULL, 0, 0, "inherit",
1N/A NULL, NULL, 0, 0, "on",
1N/A zio_checksum_off, zio_checksum_off, 0, 0, "off",
1N/A zio_checksum_SHA256, zio_checksum_SHA256, 1, 1, "label",
1N/A zio_checksum_SHA256, zio_checksum_SHA256, 1, 1, "gang_header",
1N/A NULL, NULL, 0, 0, "zilog",
1N/A fletcher_2_native, fletcher_2_byteswap, 0, 0, "fletcher2",
1N/A fletcher_4_native, fletcher_4_byteswap, 1, 0, "fletcher4",
1N/A zio_checksum_SHA256, zio_checksum_SHA256, 1, 0, "SHA256",
1N/A NULL, NULL, 0, 0, "zilog2",
1N/A};
1N/A
1N/A/*
1N/A * zio_checksum_verify: Provides support for checksum verification.
1N/A *
1N/A * Fletcher2, Fletcher4, and SHA256 are supported.
1N/A *
1N/A * Return:
1N/A * -1 = Failure
1N/A * 0 = Success
1N/A */
1N/Astatic int
1N/Azio_checksum_verify(blkptr_t *bp, char *data, int size)
1N/A{
1N/A zio_cksum_t zc = bp->blk_cksum;
1N/A uint32_t checksum = BP_GET_CHECKSUM(bp);
1N/A int byteswap = BP_SHOULD_BYTESWAP(bp);
1N/A zio_eck_t *zec = (zio_eck_t *)(data + size) - 1;
1N/A zio_checksum_info_t *ci = &zio_checksum_table[checksum];
1N/A zio_cksum_t actual_cksum, expected_cksum;
1N/A
1N/A /* byteswap is not supported */
1N/A if (byteswap)
1N/A return (-1);
1N/A
1N/A if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
1N/A return (-1);
1N/A
1N/A if (ci->ci_eck) {
1N/A expected_cksum = zec->zec_cksum;
1N/A zec->zec_cksum = zc;
1N/A ci->ci_func[0](data, size, &actual_cksum);
1N/A zec->zec_cksum = expected_cksum;
1N/A zc = expected_cksum;
1N/A
1N/A } else {
1N/A ci->ci_func[byteswap](data, size, &actual_cksum);
1N/A }
1N/A
1N/A if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
1N/A (actual_cksum.zc_word[1] - zc.zc_word[1]) |
1N/A (actual_cksum.zc_word[2] - zc.zc_word[2]) |
1N/A (actual_cksum.zc_word[3] - zc.zc_word[3]))
1N/A return (-1);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * vdev_label_start returns the physical disk offset (in bytes) of
1N/A * label "l".
1N/A */
1N/Astatic uint64_t
1N/Avdev_label_start(uint64_t psize, int l)
1N/A{
1N/A return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
1N/A 0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
1N/A}
1N/A
1N/A/*
1N/A * vdev_uberblock_compare takes two uberblock structures and returns an integer
1N/A * indicating the more recent of the two.
1N/A * Return Value = 1 if ub2 is more recent
1N/A * Return Value = -1 if ub1 is more recent
1N/A * The most recent uberblock is determined using its transaction number and
1N/A * timestamp. The uberblock with the highest transaction number is
1N/A * considered "newer". If the transaction numbers of the two blocks match, the
1N/A * timestamps are compared to determine the "newer" of the two.
1N/A */
1N/Astatic int
1N/Avdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
1N/A{
1N/A if (ub1->ub_txg < ub2->ub_txg)
1N/A return (-1);
1N/A if (ub1->ub_txg > ub2->ub_txg)
1N/A return (1);
1N/A
1N/A if (ub1->ub_timestamp < ub2->ub_timestamp)
1N/A return (-1);
1N/A if (ub1->ub_timestamp > ub2->ub_timestamp)
1N/A return (1);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Three pieces of information are needed to verify an uberblock: the magic
1N/A * number, the version number, and the checksum.
1N/A *
1N/A * Currently Implemented: version number, magic number
1N/A * Need to Implement: checksum
1N/A *
1N/A * Return:
1N/A * 0 - Success
1N/A * -1 - Failure
1N/A */
1N/Astatic int
1N/Auberblock_verify(uberblock_phys_t *ub, uint64_t offset)
1N/A{
1N/A
1N/A uberblock_t *uber = &ub->ubp_uberblock;
1N/A blkptr_t bp;
1N/A
1N/A BP_ZERO(&bp);
1N/A BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
1N/A BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
1N/A ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
1N/A
1N/A if (zio_checksum_verify(&bp, (char *)ub, UBERBLOCK_SIZE) != 0)
1N/A return (-1);
1N/A
1N/A if (uber->ub_magic == UBERBLOCK_MAGIC &&
1N/A uber->ub_version > 0 && uber->ub_version <= SPA_VERSION)
1N/A return (0);
1N/A
1N/A return (-1);
1N/A}
1N/A
1N/A/*
1N/A * Find the best uberblock.
1N/A * Return:
1N/A * Success - Pointer to the best uberblock.
1N/A * Failure - NULL
1N/A */
1N/Astatic uberblock_phys_t *
1N/Afind_bestub(uberblock_phys_t *ub_array, uint64_t sector)
1N/A{
1N/A uberblock_phys_t *ubbest = NULL;
1N/A uint64_t offset;
1N/A int i;
1N/A
1N/A for (i = 0; i < (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT); i++) {
1N/A offset = (sector << SPA_MINBLOCKSHIFT) +
1N/A VDEV_UBERBLOCK_OFFSET(i);
1N/A if (uberblock_verify(&ub_array[i], offset) == 0) {
1N/A if (ubbest == NULL) {
1N/A ubbest = &ub_array[i];
1N/A } else if (vdev_uberblock_compare(
1N/A &(ub_array[i].ubp_uberblock),
1N/A &(ubbest->ubp_uberblock)) > 0) {
1N/A ubbest = &ub_array[i];
1N/A }
1N/A }
1N/A }
1N/A
1N/A return (ubbest);
1N/A}
1N/A
1N/A/*
1N/A * Read a block of data based on the gang block address dva,
1N/A * and put its data in buf.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * 1 - failure
1N/A */
1N/Astatic int
1N/Azio_read_gang_data(blkptr_t *bp, void *buf, char *stack)
1N/A{
1N/A int i;
1N/A
1N/A /* pick a good dva from the block pointer */
1N/A for (i = 0; i < BP_GET_NDVAS(bp); i++) {
1N/A if (zio_read_common(bp, &bp->blk_dva[i], buf, stack) == 0)
1N/A return (0);
1N/A }
1N/A
1N/A return (1);
1N/A}
1N/A
1N/A/*
1N/A * Read gang block header, verify its checksum, loop through all gang blocks
1N/A * to collect its data based on the gang block address dva and put it in buf.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * 1 - failure
1N/A */
1N/Astatic int
1N/Azio_read_gang(uint64_t bp_blk_birth, dva_t *dva, void *buf, char *stack)
1N/A{
1N/A zio_gbh_phys_t *zio_gb;
1N/A uint64_t offset, sector;
1N/A blkptr_t tmpbp;
1N/A int i;
1N/A
1N/A zio_gb = (zio_gbh_phys_t *)stack;
1N/A stack += SPA_GANGBLOCKSIZE;
1N/A offset = DVA_GET_OFFSET(dva);
1N/A sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
1N/A
1N/A /* read in the gang block header */
1N/A if (devread(sector, 0, SPA_GANGBLOCKSIZE, (char *)zio_gb) == 0) {
1N/A grub_printf("failed to read in a gang block header\n");
1N/A return (1);
1N/A }
1N/A
1N/A /* self checksuming the gang block header */
1N/A BP_ZERO(&tmpbp);
1N/A BP_SET_CHECKSUM(&tmpbp, ZIO_CHECKSUM_GANG_HEADER);
1N/A BP_SET_BYTEORDER(&tmpbp, ZFS_HOST_BYTEORDER);
1N/A ZIO_SET_CHECKSUM(&tmpbp.blk_cksum, DVA_GET_VDEV(dva),
1N/A DVA_GET_OFFSET(dva), bp_blk_birth, 0);
1N/A if (zio_checksum_verify(&tmpbp, (char *)zio_gb, SPA_GANGBLOCKSIZE)) {
1N/A grub_printf("failed to checksum a gang block header\n");
1N/A return (1);
1N/A }
1N/A
1N/A for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
1N/A if (zio_gb->zg_blkptr[i].blk_birth == 0)
1N/A continue;
1N/A
1N/A if (zio_read_gang_data(&zio_gb->zg_blkptr[i], buf, stack))
1N/A return (1);
1N/A buf += BP_GET_PSIZE(&zio_gb->zg_blkptr[i]);
1N/A }
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Read in a block of raw data to buf.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * 1 - failure
1N/A */
1N/Astatic int
1N/Azio_read_common(blkptr_t *bp, dva_t *dva, void *buf, char *stack)
1N/A{
1N/A /* Only one top-level vdev is supported in the root pool */
1N/A if (DVA_GET_VDEV(dva) != 0)
1N/A return (1);
1N/A
1N/A if (DVA_GET_GANG(dva)) {
1N/A if (zio_read_gang(bp->blk_birth, dva, buf, stack) == 0)
1N/A return (0);
1N/A } else {
1N/A uint64_t offset, sector;
1N/A
1N/A /* read in a data block */
1N/A offset = DVA_GET_OFFSET(dva);
1N/A sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
1N/A if (devread(sector, 0, BP_GET_PSIZE(bp), buf))
1N/A return (0);
1N/A }
1N/A
1N/A return (1);
1N/A}
1N/A
1N/A/*
1N/A * Loop through DVAs to read in a block of raw data to buf and verify the
1N/A * checksum.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * 1 - failure
1N/A */
1N/Astatic int
1N/Azio_read_data(blkptr_t *bp, void *buf, char *stack)
1N/A{
1N/A int i;
1N/A
1N/A /* pick a good dva from the block pointer */
1N/A for (i = 0; i < BP_GET_NDVAS(bp); i++) {
1N/A if (zio_read_common(bp, &bp->blk_dva[i], buf, stack) != 0)
1N/A continue;
1N/A
1N/A if (zio_checksum_verify(bp, buf, BP_GET_PSIZE(bp)) != 0) {
1N/A grub_printf("checksum verification failed\n");
1N/A continue;
1N/A }
1N/A /* if no errors, return from here */
1N/A return (0);
1N/A }
1N/A
1N/A return (1);
1N/A}
1N/A
1N/A/*
1N/A * Read in a block of data, verify its checksum, decompress if needed,
1N/A * and put the uncompressed data in buf.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum - failure
1N/A */
1N/Astatic int
1N/Azio_read(blkptr_t *bp, void *buf, char *stack)
1N/A{
1N/A int lsize, psize, comp;
1N/A char *retbuf;
1N/A
1N/A comp = BP_GET_COMPRESS(bp);
1N/A lsize = BP_GET_LSIZE(bp);
1N/A psize = BP_GET_PSIZE(bp);
1N/A
1N/A if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
1N/A (comp != ZIO_COMPRESS_OFF &&
1N/A decomp_table[comp].decomp_func == NULL)) {
1N/A grub_printf("compression algorithm not supported\n");
1N/A return (ERR_FSYS_CORRUPT);
1N/A }
1N/A
1N/A if ((char *)buf < stack && ((char *)buf) + lsize > stack) {
1N/A grub_printf("not enough memory allocated\n");
1N/A return (ERR_WONT_FIT);
1N/A }
1N/A
1N/A retbuf = buf;
1N/A if (comp != ZIO_COMPRESS_OFF) {
1N/A buf = stack;
1N/A stack += psize;
1N/A }
1N/A
1N/A if (zio_read_data(bp, buf, stack)) {
1N/A grub_printf("zio_read_data failed\n");
1N/A return (ERR_FSYS_CORRUPT);
1N/A }
1N/A
1N/A if (comp != ZIO_COMPRESS_OFF)
1N/A decomp_table[comp].decomp_func(buf, retbuf, psize, lsize);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Get the block from a block id.
1N/A * push the block onto the stack.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum - failure
1N/A */
1N/Astatic int
1N/Admu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
1N/A{
1N/A int idx, level;
1N/A blkptr_t *bp_array = dn->dn_blkptr;
1N/A int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1N/A blkptr_t *bp, *tmpbuf;
1N/A
1N/A bp = (blkptr_t *)stack;
1N/A stack += sizeof (blkptr_t);
1N/A
1N/A tmpbuf = (blkptr_t *)stack;
1N/A stack += 1<<dn->dn_indblkshift;
1N/A
1N/A for (level = dn->dn_nlevels - 1; level >= 0; level--) {
1N/A idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
1N/A *bp = bp_array[idx];
1N/A if (level == 0)
1N/A tmpbuf = buf;
1N/A if (BP_IS_HOLE(bp)) {
1N/A grub_memset(buf, 0,
1N/A dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1N/A break;
1N/A } else if (errnum = zio_read(bp, tmpbuf, stack)) {
1N/A return (errnum);
1N/A }
1N/A
1N/A bp_array = tmpbuf;
1N/A }
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * mzap_lookup: Looks up property described by "name" and returns the value
1N/A * in "value".
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum - failure
1N/A */
1N/Astatic int
1N/Amzap_lookup(mzap_phys_t *zapobj, int objsize, char *name,
1N/A uint64_t *value)
1N/A{
1N/A int i, chunks;
1N/A mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
1N/A
1N/A chunks = objsize/MZAP_ENT_LEN - 1;
1N/A for (i = 0; i < chunks; i++) {
1N/A if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
1N/A *value = mzap_ent[i].mze_value;
1N/A return (0);
1N/A }
1N/A }
1N/A
1N/A return (ERR_FSYS_CORRUPT);
1N/A}
1N/A
1N/Astatic uint64_t
1N/Azap_hash(uint64_t salt, const char *name)
1N/A{
1N/A static uint64_t table[256];
1N/A const uint8_t *cp;
1N/A uint8_t c;
1N/A uint64_t crc = salt;
1N/A
1N/A if (table[128] == 0) {
1N/A uint64_t *ct;
1N/A int i, j;
1N/A for (i = 0; i < 256; i++) {
1N/A for (ct = table + i, *ct = i, j = 8; j > 0; j--)
1N/A *ct = (*ct >> 1) ^ (-(*ct & 1) &
1N/A ZFS_CRC64_POLY);
1N/A }
1N/A }
1N/A
1N/A if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
1N/A errnum = ERR_FSYS_CORRUPT;
1N/A return (0);
1N/A }
1N/A
1N/A for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
1N/A crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
1N/A
1N/A /*
1N/A * Only use 28 bits, since we need 4 bits in the cookie for the
1N/A * collision differentiator. We MUST use the high bits, since
1N/A * those are the onces that we first pay attention to when
1N/A * chosing the bucket.
1N/A */
1N/A crc &= ~((1ULL << (64 - 28)) - 1);
1N/A
1N/A return (crc);
1N/A}
1N/A
1N/A/*
1N/A * Only to be used on 8-bit arrays.
1N/A * array_len is actual len in bytes (not encoded le_value_length).
1N/A * buf is null-terminated.
1N/A */
1N/Astatic int
1N/Azap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
1N/A int array_len, const char *buf)
1N/A{
1N/A int bseen = 0;
1N/A
1N/A while (bseen < array_len) {
1N/A struct zap_leaf_array *la =
1N/A &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
1N/A int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
1N/A
1N/A if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
1N/A return (0);
1N/A
1N/A if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
1N/A break;
1N/A chunk = la->la_next;
1N/A bseen += toread;
1N/A }
1N/A return (bseen == array_len);
1N/A}
1N/A
1N/A/*
1N/A * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
1N/A * value for the property "name".
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum - failure
1N/A */
1N/Astatic int
1N/Azap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
1N/A const char *name, uint64_t *value)
1N/A{
1N/A uint16_t chunk;
1N/A struct zap_leaf_entry *le;
1N/A
1N/A /* Verify if this is a valid leaf block */
1N/A if (l->l_hdr.lh_block_type != ZBT_LEAF)
1N/A return (ERR_FSYS_CORRUPT);
1N/A if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
1N/A chunk != CHAIN_END; chunk = le->le_next) {
1N/A
1N/A if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A le = ZAP_LEAF_ENTRY(l, blksft, chunk);
1N/A
1N/A /* Verify the chunk entry */
1N/A if (le->le_type != ZAP_CHUNK_ENTRY)
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A if (le->le_hash != h)
1N/A continue;
1N/A
1N/A if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
1N/A le->le_name_length, name)) {
1N/A
1N/A struct zap_leaf_array *la;
1N/A uint8_t *ip;
1N/A
1N/A if (le->le_int_size != 8 || le->le_value_length != 1)
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A /* get the uint64_t property value */
1N/A la = &ZAP_LEAF_CHUNK(l, blksft,
1N/A le->le_value_chunk).l_array;
1N/A ip = la->la_array;
1N/A
1N/A *value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
1N/A (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
1N/A (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
1N/A (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
1N/A
1N/A return (0);
1N/A }
1N/A }
1N/A
1N/A return (ERR_FSYS_CORRUPT);
1N/A}
1N/A
1N/A/*
1N/A * Fat ZAP lookup
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum - failure
1N/A */
1N/Astatic int
1N/Afzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
1N/A char *name, uint64_t *value, char *stack)
1N/A{
1N/A zap_leaf_phys_t *l;
1N/A uint64_t hash, idx, blkid;
1N/A int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
1N/A
1N/A /* Verify if this is a fat zap header block */
1N/A if (zap->zap_magic != (uint64_t)ZAP_MAGIC ||
1N/A zap->zap_flags != 0)
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A hash = zap_hash(zap->zap_salt, name);
1N/A if (errnum)
1N/A return (errnum);
1N/A
1N/A /* get block id from index */
1N/A if (zap->zap_ptrtbl.zt_numblks != 0) {
1N/A /* external pointer tables not supported */
1N/A return (ERR_FSYS_CORRUPT);
1N/A }
1N/A idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
1N/A blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
1N/A
1N/A /* Get the leaf block */
1N/A l = (zap_leaf_phys_t *)stack;
1N/A stack += 1<<blksft;
1N/A if ((1<<blksft) < sizeof (zap_leaf_phys_t))
1N/A return (ERR_FSYS_CORRUPT);
1N/A if (errnum = dmu_read(zap_dnode, blkid, l, stack))
1N/A return (errnum);
1N/A
1N/A return (zap_leaf_lookup(l, blksft, hash, name, value));
1N/A}
1N/A
1N/A/*
1N/A * Read in the data of a zap object and find the value for a matching
1N/A * property name.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum - failure
1N/A */
1N/Astatic int
1N/Azap_lookup(dnode_phys_t *zap_dnode, char *name, uint64_t *val, char *stack)
1N/A{
1N/A uint64_t block_type;
1N/A int size;
1N/A void *zapbuf;
1N/A
1N/A /* Read in the first block of the zap object data. */
1N/A zapbuf = stack;
1N/A size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1N/A stack += size;
1N/A
1N/A if (errnum = dmu_read(zap_dnode, 0, zapbuf, stack))
1N/A return (errnum);
1N/A
1N/A block_type = *((uint64_t *)zapbuf);
1N/A
1N/A if (block_type == ZBT_MICRO) {
1N/A return (mzap_lookup(zapbuf, size, name, val));
1N/A } else if (block_type == ZBT_HEADER) {
1N/A /* this is a fat zap */
1N/A return (fzap_lookup(zap_dnode, zapbuf, name,
1N/A val, stack));
1N/A }
1N/A
1N/A return (ERR_FSYS_CORRUPT);
1N/A}
1N/A
1N/A/*
1N/A * Get the dnode of an object number from the metadnode of an object set.
1N/A *
1N/A * Input
1N/A * mdn - metadnode to get the object dnode
1N/A * objnum - object number for the object dnode
1N/A * buf - data buffer that holds the returning dnode
1N/A * stack - scratch area
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum - failure
1N/A */
1N/Astatic int
1N/Adnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
1N/A char *stack)
1N/A{
1N/A uint64_t blkid, blksz; /* the block id this object dnode is in */
1N/A int epbs; /* shift of number of dnodes in a block */
1N/A int idx; /* index within a block */
1N/A dnode_phys_t *dnbuf;
1N/A
1N/A blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1N/A epbs = zfs_log2(blksz) - DNODE_SHIFT;
1N/A blkid = objnum >> epbs;
1N/A idx = objnum & ((1<<epbs)-1);
1N/A
1N/A if (dnode_buf != NULL && dnode_mdn == mdn &&
1N/A objnum >= dnode_start && objnum < dnode_end) {
1N/A grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
1N/A VERIFY_DN_TYPE(buf, type);
1N/A return (0);
1N/A }
1N/A
1N/A if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
1N/A dnbuf = dnode_buf;
1N/A dnode_mdn = mdn;
1N/A dnode_start = blkid << epbs;
1N/A dnode_end = (blkid + 1) << epbs;
1N/A } else {
1N/A dnbuf = (dnode_phys_t *)stack;
1N/A stack += blksz;
1N/A }
1N/A
1N/A if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
1N/A return (errnum);
1N/A
1N/A grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
1N/A VERIFY_DN_TYPE(buf, type);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Check if this is a special file that resides at the top
1N/A * dataset of the pool. Currently this is the GRUB menu,
1N/A * boot signature and boot signature backup.
1N/A * str starts with '/'.
1N/A */
1N/Astatic int
1N/Ais_top_dataset_file(char *str)
1N/A{
1N/A char *tptr;
1N/A
1N/A if ((tptr = grub_strstr(str, "menu.lst")) &&
1N/A (tptr[8] == '\0' || tptr[8] == ' ') &&
1N/A *(tptr-1) == '/')
1N/A return (1);
1N/A
1N/A if (grub_strncmp(str, BOOTSIGN_DIR"/",
1N/A grub_strlen(BOOTSIGN_DIR) + 1) == 0)
1N/A return (1);
1N/A
1N/A if (grub_strcmp(str, BOOTSIGN_BACKUP) == 0)
1N/A return (1);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Get the file dnode for a given file name where mdn is the meta dnode
1N/A * for this ZFS object set. When found, place the file dnode in dn.
1N/A * The 'path' argument will be mangled.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum - failure
1N/A */
1N/Astatic int
1N/Adnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
1N/A char *stack)
1N/A{
1N/A uint64_t objnum, version;
1N/A char *cname, ch;
1N/A
1N/A if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
1N/A dn, stack))
1N/A return (errnum);
1N/A
1N/A if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
1N/A return (errnum);
1N/A if (version > ZPL_VERSION)
1N/A return (-1);
1N/A
1N/A if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
1N/A return (errnum);
1N/A
1N/A if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
1N/A dn, stack))
1N/A return (errnum);
1N/A
1N/A /* skip leading slashes */
1N/A while (*path == '/')
1N/A path++;
1N/A
1N/A while (*path && !isspace(*path)) {
1N/A
1N/A /* get the next component name */
1N/A cname = path;
1N/A while (*path && !isspace(*path) && *path != '/')
1N/A path++;
1N/A ch = *path;
1N/A *path = 0; /* ensure null termination */
1N/A
1N/A if (errnum = zap_lookup(dn, cname, &objnum, stack))
1N/A return (errnum);
1N/A
1N/A objnum = ZFS_DIRENT_OBJ(objnum);
1N/A if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
1N/A return (errnum);
1N/A
1N/A *path = ch;
1N/A while (*path == '/')
1N/A path++;
1N/A }
1N/A
1N/A /* We found the dnode for this file. Verify if it is a plain file. */
1N/A VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Get the default 'bootfs' property value from the rootpool.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum -failure
1N/A */
1N/Astatic int
1N/Aget_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
1N/A{
1N/A uint64_t objnum = 0;
1N/A dnode_phys_t *dn = (dnode_phys_t *)stack;
1N/A stack += DNODE_SIZE;
1N/A
1N/A if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1N/A DMU_OT_OBJECT_DIRECTORY, dn, stack))
1N/A return (errnum);
1N/A
1N/A /*
1N/A * find the object number for 'pool_props', and get the dnode
1N/A * of the 'pool_props'.
1N/A */
1N/A if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
1N/A return (ERR_FILESYSTEM_NOT_FOUND);
1N/A
1N/A if (errnum = dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
1N/A return (errnum);
1N/A
1N/A if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
1N/A return (ERR_FILESYSTEM_NOT_FOUND);
1N/A
1N/A if (!objnum)
1N/A return (ERR_FILESYSTEM_NOT_FOUND);
1N/A
1N/A *obj = objnum;
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
1N/A * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
1N/A * of pool/rootfs.
1N/A *
1N/A * If no fsname and no obj are given, return the DSL_DIR metadnode.
1N/A * If fsname is given, return its metadnode and its matching object number.
1N/A * If only obj is given, return the metadnode for this object number.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * errnum - failure
1N/A */
1N/Astatic int
1N/Aget_objset_mdn(dnode_phys_t *mosmdn, char *fsname, uint64_t *obj,
1N/A dnode_phys_t *mdn, char *stack)
1N/A{
1N/A uint64_t objnum, headobj;
1N/A char *cname, ch;
1N/A blkptr_t *bp;
1N/A objset_phys_t *osp;
1N/A int issnapshot = 0;
1N/A char *snapname;
1N/A
1N/A if (fsname == NULL && obj) {
1N/A headobj = *obj;
1N/A goto skip;
1N/A }
1N/A
1N/A if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1N/A DMU_OT_OBJECT_DIRECTORY, mdn, stack))
1N/A return (errnum);
1N/A
1N/A if (errnum = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum,
1N/A stack))
1N/A return (errnum);
1N/A
1N/A if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, stack))
1N/A return (errnum);
1N/A
1N/A if (fsname == NULL) {
1N/A headobj =
1N/A ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1N/A goto skip;
1N/A }
1N/A
1N/A /* take out the pool name */
1N/A while (*fsname && !isspace(*fsname) && *fsname != '/')
1N/A fsname++;
1N/A
1N/A while (*fsname && !isspace(*fsname)) {
1N/A uint64_t childobj;
1N/A
1N/A while (*fsname == '/')
1N/A fsname++;
1N/A
1N/A cname = fsname;
1N/A while (*fsname && !isspace(*fsname) && *fsname != '/')
1N/A fsname++;
1N/A ch = *fsname;
1N/A *fsname = 0;
1N/A
1N/A snapname = cname;
1N/A while (*snapname && !isspace(*snapname) && *snapname != '@')
1N/A snapname++;
1N/A if (*snapname == '@') {
1N/A issnapshot = 1;
1N/A *snapname = 0;
1N/A }
1N/A childobj =
1N/A ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_child_dir_zapobj;
1N/A if (childobj == 0)
1N/A return (ERR_FILESYSTEM_NOT_FOUND);
1N/A if (errnum = dnode_get(mosmdn, childobj,
1N/A DMU_OT_DSL_DIR_CHILD_MAP, mdn, stack))
1N/A return (errnum);
1N/A
1N/A if (zap_lookup(mdn, cname, &objnum, stack))
1N/A return (ERR_FILESYSTEM_NOT_FOUND);
1N/A
1N/A if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR,
1N/A mdn, stack))
1N/A return (errnum);
1N/A
1N/A *fsname = ch;
1N/A if (issnapshot)
1N/A *snapname = '@';
1N/A }
1N/A headobj = ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
1N/A if (obj)
1N/A *obj = headobj;
1N/A
1N/Askip:
1N/A if (errnum = dnode_get(mosmdn, headobj, DMU_OT_DSL_DATASET, mdn, stack))
1N/A return (errnum);
1N/A if (issnapshot) {
1N/A uint64_t snapobj;
1N/A
1N/A snapobj = ((dsl_dataset_phys_t *)DN_BONUS(mdn))->
1N/A ds_snapnames_zapobj;
1N/A
1N/A if (errnum = dnode_get(mosmdn, snapobj,
1N/A DMU_OT_DSL_DS_SNAP_MAP, mdn, stack))
1N/A return (errnum);
1N/A if (zap_lookup(mdn, snapname + 1, &headobj, stack))
1N/A return (ERR_FILESYSTEM_NOT_FOUND);
1N/A if (errnum = dnode_get(mosmdn, headobj,
1N/A DMU_OT_DSL_DATASET, mdn, stack))
1N/A return (errnum);
1N/A if (obj)
1N/A *obj = headobj;
1N/A }
1N/A
1N/A bp = &((dsl_dataset_phys_t *)DN_BONUS(mdn))->ds_bp;
1N/A osp = (objset_phys_t *)stack;
1N/A stack += sizeof (objset_phys_t);
1N/A if (errnum = zio_read(bp, osp, stack))
1N/A return (errnum);
1N/A
1N/A grub_memmove((char *)mdn, (char *)&osp->os_meta_dnode, DNODE_SIZE);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * For a given XDR packed nvlist, verify the first 4 bytes and move on.
1N/A *
1N/A * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
1N/A *
1N/A * encoding method/host endian (4 bytes)
1N/A * nvl_version (4 bytes)
1N/A * nvl_nvflag (4 bytes)
1N/A * encoded nvpairs:
1N/A * encoded size of the nvpair (4 bytes)
1N/A * decoded size of the nvpair (4 bytes)
1N/A * name string size (4 bytes)
1N/A * name string data (sizeof(NV_ALIGN4(string))
1N/A * data type (4 bytes)
1N/A * # of elements in the nvpair (4 bytes)
1N/A * data
1N/A * 2 zero's for the last nvpair
1N/A * (end of the entire list) (8 bytes)
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * 1 - failure
1N/A */
1N/Astatic int
1N/Anvlist_unpack(char *nvlist, char **out)
1N/A{
1N/A /* Verify if the 1st and 2nd byte in the nvlist are valid. */
1N/A if (nvlist[0] != NV_ENCODE_XDR || nvlist[1] != HOST_ENDIAN)
1N/A return (1);
1N/A
1N/A nvlist += 4;
1N/A *out = nvlist;
1N/A return (0);
1N/A}
1N/A
1N/Astatic char *
1N/Anvlist_array(char *nvlist, int index)
1N/A{
1N/A int i, encode_size;
1N/A
1N/A for (i = 0; i < index; i++) {
1N/A /* skip the header, nvl_version, and nvl_nvflag */
1N/A nvlist = nvlist + 4 * 2;
1N/A
1N/A while (encode_size = BSWAP_32(*(uint32_t *)nvlist))
1N/A nvlist += encode_size; /* goto the next nvpair */
1N/A
1N/A nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
1N/A }
1N/A
1N/A return (nvlist);
1N/A}
1N/A
1N/Astatic int
1N/Anvlist_lookup_value(char *nvlist, char *name, void *val, int valtype,
1N/A int *nelmp)
1N/A{
1N/A int name_len, type, slen, encode_size;
1N/A char *nvpair, *nvp_name, *strval = val;
1N/A uint64_t *intval = val;
1N/A
1N/A /* skip the header, nvl_version, and nvl_nvflag */
1N/A nvlist = nvlist + 4 * 2;
1N/A
1N/A /*
1N/A * Loop thru the nvpair list
1N/A * The XDR representation of an integer is in big-endian byte order.
1N/A */
1N/A while (encode_size = BSWAP_32(*(uint32_t *)nvlist)) {
1N/A
1N/A nvpair = nvlist + 4 * 2; /* skip the encode/decode size */
1N/A
1N/A name_len = BSWAP_32(*(uint32_t *)nvpair);
1N/A nvpair += 4;
1N/A
1N/A nvp_name = nvpair;
1N/A nvpair = nvpair + ((name_len + 3) & ~3); /* align */
1N/A
1N/A type = BSWAP_32(*(uint32_t *)nvpair);
1N/A nvpair += 4;
1N/A
1N/A if ((grub_strncmp(nvp_name, name, name_len) == 0) &&
1N/A type == valtype) {
1N/A int nelm;
1N/A
1N/A if ((nelm = BSWAP_32(*(uint32_t *)nvpair)) < 1)
1N/A return (1);
1N/A nvpair += 4;
1N/A
1N/A switch (valtype) {
1N/A case DATA_TYPE_STRING:
1N/A slen = BSWAP_32(*(uint32_t *)nvpair);
1N/A nvpair += 4;
1N/A grub_memmove(strval, nvpair, slen);
1N/A strval[slen] = '\0';
1N/A return (0);
1N/A
1N/A case DATA_TYPE_UINT64:
1N/A *intval = BSWAP_64(*(uint64_t *)nvpair);
1N/A return (0);
1N/A
1N/A case DATA_TYPE_NVLIST:
1N/A *(void **)val = (void *)nvpair;
1N/A return (0);
1N/A
1N/A case DATA_TYPE_NVLIST_ARRAY:
1N/A *(void **)val = (void *)nvpair;
1N/A if (nelmp)
1N/A *nelmp = nelm;
1N/A return (0);
1N/A }
1N/A }
1N/A
1N/A nvlist += encode_size; /* goto the next nvpair */
1N/A }
1N/A
1N/A return (1);
1N/A}
1N/A
1N/A/*
1N/A * Check if this vdev is online and is in a good state.
1N/A */
1N/Astatic int
1N/Avdev_validate(char *nv)
1N/A{
1N/A uint64_t ival;
1N/A
1N/A if (nvlist_lookup_value(nv, ZPOOL_CONFIG_OFFLINE, &ival,
1N/A DATA_TYPE_UINT64, NULL) == 0 ||
1N/A nvlist_lookup_value(nv, ZPOOL_CONFIG_FAULTED, &ival,
1N/A DATA_TYPE_UINT64, NULL) == 0 ||
1N/A nvlist_lookup_value(nv, ZPOOL_CONFIG_REMOVED, &ival,
1N/A DATA_TYPE_UINT64, NULL) == 0)
1N/A return (ERR_DEV_VALUES);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Get a valid vdev pathname/devid from the boot device.
1N/A * The caller should already allocate MAXPATHLEN memory for bootpath and devid.
1N/A */
1N/Astatic int
1N/Avdev_get_bootpath(char *nv, uint64_t inguid, char *devid, char *bootpath,
1N/A int is_spare)
1N/A{
1N/A char type[16];
1N/A
1N/A if (nvlist_lookup_value(nv, ZPOOL_CONFIG_TYPE, &type, DATA_TYPE_STRING,
1N/A NULL))
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1N/A uint64_t guid;
1N/A
1N/A if (vdev_validate(nv) != 0)
1N/A return (ERR_NO_BOOTPATH);
1N/A
1N/A if (nvlist_lookup_value(nv, ZPOOL_CONFIG_GUID,
1N/A &guid, DATA_TYPE_UINT64, NULL) != 0)
1N/A return (ERR_NO_BOOTPATH);
1N/A
1N/A if (guid != inguid)
1N/A return (ERR_NO_BOOTPATH);
1N/A
1N/A /* for a spare vdev, pick the disk labeled with "is_spare" */
1N/A if (is_spare) {
1N/A uint64_t spare = 0;
1N/A (void) nvlist_lookup_value(nv, ZPOOL_CONFIG_IS_SPARE,
1N/A &spare, DATA_TYPE_UINT64, NULL);
1N/A if (!spare)
1N/A return (ERR_NO_BOOTPATH);
1N/A }
1N/A
1N/A if (nvlist_lookup_value(nv, ZPOOL_CONFIG_PHYS_PATH,
1N/A bootpath, DATA_TYPE_STRING, NULL) != 0)
1N/A bootpath[0] = '\0';
1N/A
1N/A if (nvlist_lookup_value(nv, ZPOOL_CONFIG_DEVID,
1N/A devid, DATA_TYPE_STRING, NULL) != 0)
1N/A devid[0] = '\0';
1N/A
1N/A if (strlen(bootpath) >= MAXPATHLEN ||
1N/A strlen(devid) >= MAXPATHLEN)
1N/A return (ERR_WONT_FIT);
1N/A
1N/A return (0);
1N/A
1N/A } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
1N/A strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
1N/A (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
1N/A int nelm, i;
1N/A char *child;
1N/A
1N/A if (nvlist_lookup_value(nv, ZPOOL_CONFIG_CHILDREN, &child,
1N/A DATA_TYPE_NVLIST_ARRAY, &nelm))
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A for (i = 0; i < nelm; i++) {
1N/A char *child_i;
1N/A
1N/A child_i = nvlist_array(child, i);
1N/A if (vdev_get_bootpath(child_i, inguid, devid,
1N/A bootpath, is_spare) == 0)
1N/A return (0);
1N/A }
1N/A }
1N/A
1N/A return (ERR_NO_BOOTPATH);
1N/A}
1N/A
1N/A/*
1N/A * Check the disk label information and retrieve needed vdev name-value pairs.
1N/A *
1N/A * Return:
1N/A * 0 - success
1N/A * ERR_* - failure
1N/A */
1N/Aint
1N/Acheck_pool_label(uint64_t sector, char *stack, char *outdevid,
1N/A char *outpath, uint64_t *outguid)
1N/A{
1N/A vdev_phys_t *vdev;
1N/A uint64_t pool_state, txg = 0;
1N/A char *nvlist, *nv;
1N/A uint64_t diskguid;
1N/A uint64_t version;
1N/A
1N/A sector += (VDEV_SKIP_SIZE >> SPA_MINBLOCKSHIFT);
1N/A
1N/A /* Read in the vdev name-value pair list (112K). */
1N/A if (devread(sector, 0, VDEV_PHYS_SIZE, stack) == 0)
1N/A return (ERR_READ);
1N/A
1N/A vdev = (vdev_phys_t *)stack;
1N/A stack += sizeof (vdev_phys_t);
1N/A
1N/A if (nvlist_unpack(vdev->vp_nvlist, &nvlist))
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_STATE, &pool_state,
1N/A DATA_TYPE_UINT64, NULL))
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A if (pool_state == POOL_STATE_DESTROYED)
1N/A return (ERR_FILESYSTEM_NOT_FOUND);
1N/A
1N/A if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_NAME,
1N/A current_rootpool, DATA_TYPE_STRING, NULL))
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_TXG, &txg,
1N/A DATA_TYPE_UINT64, NULL))
1N/A return (ERR_FSYS_CORRUPT);
1N/A
1N/A /* not an active device */
1N/A if (txg == 0)
1N/A return (ERR_NO_BOOTPATH);
1N/A
1N/A if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VERSION, &version,
1N/A DATA_TYPE_UINT64, NULL))
1N/A return (ERR_FSYS_CORRUPT);
1N/A if (version > SPA_VERSION)
1N/A return (ERR_NEWER_VERSION);
1N/A if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VDEV_TREE, &nv,
1N/A DATA_TYPE_NVLIST, NULL))
1N/A return (ERR_FSYS_CORRUPT);
1N/A if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_GUID, &diskguid,
1N/A DATA_TYPE_UINT64, NULL))
1N/A return (ERR_FSYS_CORRUPT);
1N/A if (vdev_get_bootpath(nv, diskguid, outdevid, outpath, 0))
1N/A return (ERR_NO_BOOTPATH);
1N/A if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_GUID, outguid,
1N/A DATA_TYPE_UINT64, NULL))
1N/A return (ERR_FSYS_CORRUPT);
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
1N/A * to the memory address MOS.
1N/A *
1N/A * Return:
1N/A * 1 - success
1N/A * 0 - failure
1N/A */
1N/Aint
1N/Azfs_mount(void)
1N/A{
1N/A char *stack;
1N/A int label = 0;
1N/A uberblock_phys_t *ub_array, *ubbest;
1N/A objset_phys_t *osp;
1N/A char tmp_bootpath[MAXNAMELEN];
1N/A char tmp_devid[MAXNAMELEN];
1N/A uint64_t tmp_guid;
1N/A uint64_t adjpl = (uint64_t)part_length << SPA_MINBLOCKSHIFT;
1N/A int err = errnum; /* preserve previous errnum state */
1N/A
1N/A /* if it's our first time here, zero the best uberblock out */
1N/A if (best_drive == 0 && best_part == 0 && find_best_root) {
1N/A grub_memset(&current_uberblock, 0, sizeof (uberblock_t));
1N/A pool_guid = 0;
1N/A }
1N/A
1N/A stackbase = ZFS_SCRATCH;
1N/A stack = stackbase;
1N/A ub_array = (uberblock_phys_t *)stack;
1N/A stack += VDEV_UBERBLOCK_RING;
1N/A
1N/A osp = (objset_phys_t *)stack;
1N/A stack += sizeof (objset_phys_t);
1N/A adjpl = P2ALIGN(adjpl, (uint64_t)sizeof (vdev_label_t));
1N/A
1N/A for (label = 0; label < VDEV_LABELS; label++) {
1N/A
1N/A /*
1N/A * some eltorito stacks don't give us a size and
1N/A * we end up setting the size to MAXUINT, further
1N/A * some of these devices stop working once a single
1N/A * read past the end has been issued. Checking
1N/A * for a maximum part_length and skipping the backup
1N/A * labels at the end of the slice/partition/device
1N/A * avoids breaking down on such devices.
1N/A */
1N/A if (part_length == MAXUINT && label == 2)
1N/A break;
1N/A
1N/A uint64_t sector = vdev_label_start(adjpl,
1N/A label) >> SPA_MINBLOCKSHIFT;
1N/A
1N/A /* Read in the uberblock ring (128K). */
1N/A if (devread(sector +
1N/A ((VDEV_SKIP_SIZE + VDEV_PHYS_SIZE) >>
1N/A SPA_MINBLOCKSHIFT), 0, VDEV_UBERBLOCK_RING,
1N/A (char *)ub_array) == 0)
1N/A continue;
1N/A
1N/A if ((ubbest = find_bestub(ub_array, sector)) != NULL &&
1N/A zio_read(&ubbest->ubp_uberblock.ub_rootbp, osp, stack)
1N/A == 0) {
1N/A
1N/A VERIFY_OS_TYPE(osp, DMU_OST_META);
1N/A
1N/A if (check_pool_label(sector, stack, tmp_devid,
1N/A tmp_bootpath, &tmp_guid))
1N/A continue;
1N/A if (pool_guid == 0)
1N/A pool_guid = tmp_guid;
1N/A
1N/A if (find_best_root && ((pool_guid != tmp_guid) ||
1N/A vdev_uberblock_compare(&ubbest->ubp_uberblock,
1N/A &(current_uberblock)) <= 0))
1N/A continue;
1N/A
1N/A /* Got the MOS. Save it at the memory addr MOS. */
1N/A grub_memmove(MOS, &osp->os_meta_dnode, DNODE_SIZE);
1N/A grub_memmove(&current_uberblock,
1N/A &ubbest->ubp_uberblock, sizeof (uberblock_t));
1N/A grub_memmove(current_bootpath, tmp_bootpath,
1N/A MAXNAMELEN);
1N/A grub_memmove(current_devid, tmp_devid,
1N/A grub_strlen(tmp_devid));
1N/A is_zfs_mount = 1;
1N/A return (1);
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * While some fs impls. (tftp) rely on setting and keeping
1N/A * global errnums set, others won't reset it and will break
1N/A * when issuing rawreads. The goal here is to simply not
1N/A * have zfs mount attempts impact the previous state.
1N/A */
1N/A errnum = err;
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * zfs_open() locates a file in the rootpool by following the
1N/A * MOS and places the dnode of the file in the memory address DNODE.
1N/A *
1N/A * Return:
1N/A * 1 - success
1N/A * 0 - failure
1N/A */
1N/Aint
1N/Azfs_open(char *filename)
1N/A{
1N/A char *stack;
1N/A dnode_phys_t *mdn;
1N/A
1N/A file_buf = NULL;
1N/A stackbase = ZFS_SCRATCH;
1N/A stack = stackbase;
1N/A
1N/A mdn = (dnode_phys_t *)stack;
1N/A stack += sizeof (dnode_phys_t);
1N/A
1N/A dnode_mdn = NULL;
1N/A dnode_buf = (dnode_phys_t *)stack;
1N/A stack += 1<<DNODE_BLOCK_SHIFT;
1N/A
1N/A /*
1N/A * menu.lst is placed at the root pool filesystem level,
1N/A * do not goto 'current_bootfs'.
1N/A */
1N/A if (is_top_dataset_file(filename)) {
1N/A if (errnum = get_objset_mdn(MOS, NULL, NULL, mdn, stack))
1N/A return (0);
1N/A
1N/A current_bootfs_obj = 0;
1N/A } else {
1N/A if (current_bootfs[0] == '\0') {
1N/A /* Get the default root filesystem object number */
1N/A if (errnum = get_default_bootfsobj(MOS,
1N/A &current_bootfs_obj, stack))
1N/A return (0);
1N/A
1N/A if (errnum = get_objset_mdn(MOS, NULL,
1N/A &current_bootfs_obj, mdn, stack))
1N/A return (0);
1N/A } else {
1N/A if (errnum = get_objset_mdn(MOS, current_bootfs,
1N/A &current_bootfs_obj, mdn, stack)) {
1N/A grub_memset(current_bootfs, 0, MAXNAMELEN);
1N/A return (0);
1N/A }
1N/A }
1N/A }
1N/A
1N/A if (dnode_get_path(mdn, filename, DNODE, stack)) {
1N/A errnum = ERR_FILE_NOT_FOUND;
1N/A return (0);
1N/A }
1N/A
1N/A /* get the file size and set the file position to 0 */
1N/A
1N/A /*
1N/A * For DMU_OT_SA we will need to locate the SIZE attribute
1N/A * attribute, which could be either in the bonus buffer
1N/A * or the "spill" block.
1N/A */
1N/A if (DNODE->dn_bonustype == DMU_OT_SA) {
1N/A sa_hdr_phys_t *sahdrp;
1N/A int hdrsize;
1N/A
1N/A if (DNODE->dn_bonuslen != 0) {
1N/A sahdrp = (sa_hdr_phys_t *)DN_BONUS(DNODE);
1N/A } else {
1N/A if (DNODE->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
1N/A blkptr_t *bp = &DNODE->dn_spill;
1N/A void *buf;
1N/A
1N/A buf = (void *)stack;
1N/A stack += BP_GET_LSIZE(bp);
1N/A
1N/A /* reset errnum to rawread() failure */
1N/A errnum = 0;
1N/A if (zio_read(bp, buf, stack) != 0) {
1N/A return (0);
1N/A }
1N/A sahdrp = buf;
1N/A } else {
1N/A errnum = ERR_FSYS_CORRUPT;
1N/A return (0);
1N/A }
1N/A }
1N/A hdrsize = SA_HDR_SIZE(sahdrp);
1N/A filemax = *(uint64_t *)((char *)sahdrp + hdrsize +
1N/A SA_SIZE_OFFSET);
1N/A } else {
1N/A filemax = ((znode_phys_t *)DN_BONUS(DNODE))->zp_size;
1N/A }
1N/A filepos = 0;
1N/A
1N/A dnode_buf = NULL;
1N/A return (1);
1N/A}
1N/A
1N/A/*
1N/A * zfs_read reads in the data blocks pointed by the DNODE.
1N/A *
1N/A * Return:
1N/A * len - the length successfully read in to the buffer
1N/A * 0 - failure
1N/A */
1N/Aint
1N/Azfs_read(char *buf, int len)
1N/A{
1N/A char *stack;
1N/A char *tmpbuf;
1N/A int blksz, length, movesize;
1N/A
1N/A if (file_buf == NULL) {
1N/A file_buf = stackbase;
1N/A stackbase += SPA_MAXBLOCKSIZE;
1N/A file_start = file_end = 0;
1N/A }
1N/A stack = stackbase;
1N/A
1N/A /*
1N/A * If offset is in memory, move it into the buffer provided and return.
1N/A */
1N/A if (filepos >= file_start && filepos+len <= file_end) {
1N/A grub_memmove(buf, file_buf + filepos - file_start, len);
1N/A filepos += len;
1N/A return (len);
1N/A }
1N/A
1N/A blksz = DNODE->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1N/A
1N/A /*
1N/A * Entire Dnode is too big to fit into the space available. We
1N/A * will need to read it in chunks. This could be optimized to
1N/A * read in as large a chunk as there is space available, but for
1N/A * now, this only reads in one data block at a time.
1N/A */
1N/A length = len;
1N/A while (length) {
1N/A /*
1N/A * Find requested blkid and the offset within that block.
1N/A */
1N/A uint64_t blkid = filepos / blksz;
1N/A
1N/A if (errnum = dmu_read(DNODE, blkid, file_buf, stack))
1N/A return (0);
1N/A
1N/A file_start = blkid * blksz;
1N/A file_end = file_start + blksz;
1N/A
1N/A movesize = MIN(length, file_end - filepos);
1N/A
1N/A grub_memmove(buf, file_buf + filepos - file_start,
1N/A movesize);
1N/A buf += movesize;
1N/A length -= movesize;
1N/A filepos += movesize;
1N/A }
1N/A
1N/A return (len);
1N/A}
1N/A
1N/A/*
1N/A * No-Op
1N/A */
1N/Aint
1N/Azfs_embed(int *start_sector, int needed_sectors)
1N/A{
1N/A return (1);
1N/A}
1N/A
1N/A#endif /* FSYS_ZFS */