usr/src/grub/grub-0.97/stage2/fsys_zfs.c
author Christopher Siden <chris.siden@delphix.com>
Mon, 21 May 2012 12:11:39 -0700
changeset 13700 2889e2596bd6
parent 13421 a4721c685d96
child 13921 9d721847e469
permissions -rw-r--r--
2619 asynchronous destruction of ZFS file systems 2747 SPA versioning with zfs feature flags Reviewed by: Matt Ahrens <[email protected]> Reviewed by: George Wilson <[email protected]> Reviewed by: Richard Lowe <[email protected]> Reviewed by: Dan Kruchinin <[email protected]> Approved by: Eric Schrock <[email protected]>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
     1
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
     2
 *  GRUB  --  GRand Unified Bootloader
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
     3
 *  Copyright (C) 1999,2000,2001,2002,2003,2004  Free Software Foundation, Inc.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
     4
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
     5
 *  This program is free software; you can redistribute it and/or modify
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
     6
 *  it under the terms of the GNU General Public License as published by
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
     7
 *  the Free Software Foundation; either version 2 of the License, or
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
     8
 *  (at your option) any later version.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
     9
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    10
 *  This program is distributed in the hope that it will be useful,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    13
 *  GNU General Public License for more details.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    14
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    15
 *  You should have received a copy of the GNU General Public License
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    16
 *  along with this program; if not, write to the Free Software
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    17
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    18
 */
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
    19
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    20
/*
11670
1d964fb5d948 6595532 ZIL is too talkative
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 11019
diff changeset
    21
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    22
 * Use is subject to license terms.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    23
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    24
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    25
/*
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
    26
 * Copyright (c) 2012 by Delphix. All rights reserved.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
    27
 */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
    28
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
    29
/*
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    30
 * The zfs plug-in routines for GRUB are:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    31
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    32
 * zfs_mount() - locates a valid uberblock of the root pool and reads
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    33
 *		in its MOS at the memory address MOS.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    34
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    35
 * zfs_open() - locates a plain file object by following the MOS
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    36
 *		and places its dnode at the memory address DNODE.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    37
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    38
 * zfs_read() - read in the data blocks pointed by the DNODE.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    39
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    40
 * ZFS_SCRATCH is used as a working area.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    41
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    42
 * (memory addr)   MOS      DNODE	ZFS_SCRATCH
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    43
 *		    |         |          |
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    44
 *	    +-------V---------V----------V---------------+
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    45
 *   memory |       | dnode   | dnode    |  scratch      |
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    46
 *	    |       | 512B    | 512B     |  area         |
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    47
 *	    +--------------------------------------------+
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    48
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    49
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    50
#ifdef	FSYS_ZFS
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    51
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    52
#include "shared.h"
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    53
#include "filesys.h"
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    54
#include "fsys_zfs.h"
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    55
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    56
/* cache for a file block of the currently zfs_open()-ed file */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    57
static void *file_buf = NULL;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    58
static uint64_t file_start = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    59
static uint64_t file_end = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    60
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    61
/* cache for a dnode block */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    62
static dnode_phys_t *dnode_buf = NULL;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    63
static dnode_phys_t *dnode_mdn = NULL;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    64
static uint64_t dnode_start = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    65
static uint64_t dnode_end = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    66
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
    67
static uint64_t pool_guid = 0;
7147
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
    68
static uberblock_t current_uberblock;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    69
static char *stackbase;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    70
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    71
decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] =
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    72
{
7042
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 6659
diff changeset
    73
	{"inherit", 0},			/* ZIO_COMPRESS_INHERIT */
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    74
	{"on", lzjb_decompress}, 	/* ZIO_COMPRESS_ON */
7042
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 6659
diff changeset
    75
	{"off", 0},			/* ZIO_COMPRESS_OFF */
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 6659
diff changeset
    76
	{"lzjb", lzjb_decompress},	/* ZIO_COMPRESS_LZJB */
46fc4b6db23e 6721094 Setting certain properties on root pools should not be allowed
gw25295
parents: 6659
diff changeset
    77
	{"empty", 0}			/* ZIO_COMPRESS_EMPTY */
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    78
};
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    79
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
    80
static int zio_read_data(blkptr_t *bp, void *buf, char *stack);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
    81
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    82
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    83
 * Our own version of bcmp().
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    84
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    85
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    86
zfs_bcmp(const void *s1, const void *s2, size_t n)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    87
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    88
	const uchar_t *ps1 = s1;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    89
	const uchar_t *ps2 = s2;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    90
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    91
	if (s1 != s2 && n != 0) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    92
		do {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    93
			if (*ps1++ != *ps2++)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    94
				return (1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    95
		} while (--n != 0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    96
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    97
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    98
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
    99
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   100
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   101
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   102
 * Our own version of log2().  Same thing as highbit()-1.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   103
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   104
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   105
zfs_log2(uint64_t num)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   106
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   107
	int i = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   108
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   109
	while (num > 1) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   110
		i++;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   111
		num = num >> 1;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   112
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   113
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   114
	return (i);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   115
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   116
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   117
/* Checksum Functions */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   118
static void
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   119
zio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   120
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   121
	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   122
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   123
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   124
/* Checksum Table and Values */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   125
zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   126
	{{NULL,			NULL},			0, 0,	"inherit"},
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   127
	{{NULL,			NULL},			0, 0,	"on"},
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   128
	{{zio_checksum_off,	zio_checksum_off},	0, 0,	"off"},
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   129
	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1,	"label"},
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   130
	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1,	"gang_header"},
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   131
	{{NULL,			NULL},			0, 0,	"zilog"},
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   132
	{{fletcher_2_native,	fletcher_2_byteswap},	0, 0,	"fletcher2"},
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   133
	{{fletcher_4_native,	fletcher_4_byteswap},	1, 0,	"fletcher4"},
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   134
	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 0,	"SHA256"},
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   135
	{{NULL,			NULL},			0, 0,	"zilog2"},
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   136
};
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   137
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   138
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   139
 * zio_checksum_verify: Provides support for checksum verification.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   140
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   141
 * Fletcher2, Fletcher4, and SHA256 are supported.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   142
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   143
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   144
 * 	-1 = Failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   145
 *	 0 = Success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   146
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   147
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   148
zio_checksum_verify(blkptr_t *bp, char *data, int size)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   149
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   150
	zio_cksum_t zc = bp->blk_cksum;
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   151
	uint32_t checksum = BP_GET_CHECKSUM(bp);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   152
	int byteswap = BP_SHOULD_BYTESWAP(bp);
11670
1d964fb5d948 6595532 ZIL is too talkative
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 11019
diff changeset
   153
	zio_eck_t *zec = (zio_eck_t *)(data + size) - 1;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   154
	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   155
	zio_cksum_t actual_cksum, expected_cksum;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   156
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   157
	/* byteswap is not supported */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   158
	if (byteswap)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   159
		return (-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   160
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   161
	if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   162
		return (-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   163
11670
1d964fb5d948 6595532 ZIL is too talkative
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 11019
diff changeset
   164
	if (ci->ci_eck) {
1d964fb5d948 6595532 ZIL is too talkative
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 11019
diff changeset
   165
		expected_cksum = zec->zec_cksum;
1d964fb5d948 6595532 ZIL is too talkative
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 11019
diff changeset
   166
		zec->zec_cksum = zc;
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   167
		ci->ci_func[0](data, size, &actual_cksum);
11670
1d964fb5d948 6595532 ZIL is too talkative
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 11019
diff changeset
   168
		zec->zec_cksum = expected_cksum;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   169
		zc = expected_cksum;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   170
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   171
	} else {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   172
		ci->ci_func[byteswap](data, size, &actual_cksum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   173
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   174
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   175
	if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   176
	    (actual_cksum.zc_word[1] - zc.zc_word[1]) |
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   177
	    (actual_cksum.zc_word[2] - zc.zc_word[2]) |
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   178
	    (actual_cksum.zc_word[3] - zc.zc_word[3]))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   179
		return (-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   180
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   181
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   182
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   183
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   184
/*
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
   185
 * vdev_label_start returns the physical disk offset (in bytes) of
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
   186
 * label "l".
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   187
 */
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
   188
static uint64_t
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
   189
vdev_label_start(uint64_t psize, int l)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   190
{
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
   191
	return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   192
	    0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   193
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   194
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   195
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   196
 * vdev_uberblock_compare takes two uberblock structures and returns an integer
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   197
 * indicating the more recent of the two.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   198
 * 	Return Value = 1 if ub2 is more recent
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   199
 * 	Return Value = -1 if ub1 is more recent
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   200
 * The most recent uberblock is determined using its transaction number and
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   201
 * timestamp.  The uberblock with the highest transaction number is
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   202
 * considered "newer".  If the transaction numbers of the two blocks match, the
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   203
 * timestamps are compared to determine the "newer" of the two.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   204
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   205
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   206
vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   207
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   208
	if (ub1->ub_txg < ub2->ub_txg)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   209
		return (-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   210
	if (ub1->ub_txg > ub2->ub_txg)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   211
		return (1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   212
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   213
	if (ub1->ub_timestamp < ub2->ub_timestamp)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   214
		return (-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   215
	if (ub1->ub_timestamp > ub2->ub_timestamp)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   216
		return (1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   217
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   218
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   219
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   220
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   221
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   222
 * Three pieces of information are needed to verify an uberblock: the magic
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   223
 * number, the version number, and the checksum.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   224
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   225
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   226
 *     0 - Success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   227
 *    -1 - Failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   228
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   229
static int
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   230
uberblock_verify(uberblock_t *uber, uint64_t ub_size, uint64_t offset)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   231
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   232
	blkptr_t bp;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   233
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   234
	BP_ZERO(&bp);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   235
	BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   236
	BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   237
	ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   238
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   239
	if (zio_checksum_verify(&bp, (char *)uber, ub_size) != 0)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   240
		return (-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   241
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   242
	if (uber->ub_magic == UBERBLOCK_MAGIC &&
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   243
	    SPA_VERSION_IS_SUPPORTED(uber->ub_version))
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   244
		return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   245
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   246
	return (-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   247
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   248
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   249
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   250
 * Find the best uberblock.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   251
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   252
 *    Success - Pointer to the best uberblock.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   253
 *    Failure - NULL
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   254
 */
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   255
static uberblock_t *
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   256
find_bestub(char *ub_array, uint64_t ashift, uint64_t sector)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   257
{
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   258
	uberblock_t *ubbest = NULL;
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   259
	uberblock_t *ubnext;
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   260
	uint64_t offset, ub_size;
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
   261
	int i;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   262
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   263
	ub_size = VDEV_UBERBLOCK_SIZE(ashift);
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   264
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   265
	for (i = 0; i < VDEV_UBERBLOCK_COUNT(ashift); i++) {
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   266
		ubnext = (uberblock_t *)ub_array;
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   267
		ub_array += ub_size;
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
   268
		offset = (sector << SPA_MINBLOCKSHIFT) +
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   269
		    VDEV_UBERBLOCK_OFFSET(ashift, i);
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   270
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   271
		if (uberblock_verify(ubnext, ub_size, offset) != 0)
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   272
			continue;
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   273
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   274
		if (ubbest == NULL ||
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   275
		    vdev_uberblock_compare(ubnext, ubbest) > 0)
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
   276
			ubbest = ubnext;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   277
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   278
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   279
	return (ubbest);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   280
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   281
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   282
/*
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   283
 * Read a block of data based on the gang block address dva,
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   284
 * and put its data in buf.
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   285
 *
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   286
 * Return:
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   287
 *	0 - success
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   288
 *	1 - failure
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   289
 */
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   290
static int
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   291
zio_read_gang(blkptr_t *bp, dva_t *dva, void *buf, char *stack)
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   292
{
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   293
	zio_gbh_phys_t *zio_gb;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   294
	uint64_t offset, sector;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   295
	blkptr_t tmpbp;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   296
	int i;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   297
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   298
	zio_gb = (zio_gbh_phys_t *)stack;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   299
	stack += SPA_GANGBLOCKSIZE;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   300
	offset = DVA_GET_OFFSET(dva);
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   301
	sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   302
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   303
	/* read in the gang block header */
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   304
	if (devread(sector, 0, SPA_GANGBLOCKSIZE, (char *)zio_gb) == 0) {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   305
		grub_printf("failed to read in a gang block header\n");
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   306
		return (1);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   307
	}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   308
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   309
	/* self checksuming the gang block header */
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   310
	BP_ZERO(&tmpbp);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   311
	BP_SET_CHECKSUM(&tmpbp, ZIO_CHECKSUM_GANG_HEADER);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   312
	BP_SET_BYTEORDER(&tmpbp, ZFS_HOST_BYTEORDER);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   313
	ZIO_SET_CHECKSUM(&tmpbp.blk_cksum, DVA_GET_VDEV(dva),
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   314
	    DVA_GET_OFFSET(dva), bp->blk_birth, 0);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   315
	if (zio_checksum_verify(&tmpbp, (char *)zio_gb, SPA_GANGBLOCKSIZE)) {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   316
		grub_printf("failed to checksum a gang block header\n");
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   317
		return (1);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   318
	}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   319
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   320
	for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   321
		if (zio_gb->zg_blkptr[i].blk_birth == 0)
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   322
			continue;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   323
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   324
		if (zio_read_data(&zio_gb->zg_blkptr[i], buf, stack))
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   325
			return (1);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   326
		buf += BP_GET_PSIZE(&zio_gb->zg_blkptr[i]);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   327
	}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   328
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   329
	return (0);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   330
}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   331
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   332
/*
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   333
 * Read in a block of raw data to buf.
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   334
 *
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   335
 * Return:
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   336
 *	0 - success
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   337
 *	1 - failure
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   338
 */
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   339
static int
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   340
zio_read_data(blkptr_t *bp, void *buf, char *stack)
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   341
{
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   342
	int i, psize;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   343
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   344
	psize = BP_GET_PSIZE(bp);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   345
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   346
	/* pick a good dva from the block pointer */
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   347
	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   348
		uint64_t offset, sector;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   349
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   350
		if (bp->blk_dva[i].dva_word[0] == 0 &&
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   351
		    bp->blk_dva[i].dva_word[1] == 0)
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   352
			continue;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   353
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   354
		if (DVA_GET_GANG(&bp->blk_dva[i])) {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   355
			if (zio_read_gang(bp, &bp->blk_dva[i], buf, stack) == 0)
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   356
				return (0);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   357
		} else {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   358
			/* read in a data block */
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   359
			offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   360
			sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   361
			if (devread(sector, 0, psize, buf) != 0)
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   362
				return (0);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   363
		}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   364
	}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   365
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   366
	return (1);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   367
}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   368
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   369
/*
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   370
 * Read in a block of data, verify its checksum, decompress if needed,
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   371
 * and put the uncompressed data in buf.
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   372
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   373
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   374
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   375
 *	errnum - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   376
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   377
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   378
zio_read(blkptr_t *bp, void *buf, char *stack)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   379
{
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   380
	int lsize, psize, comp;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   381
	char *retbuf;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   382
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   383
	comp = BP_GET_COMPRESS(bp);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   384
	lsize = BP_GET_LSIZE(bp);
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   385
	psize = BP_GET_PSIZE(bp);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   386
4725
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   387
	if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   388
	    (comp != ZIO_COMPRESS_OFF &&
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   389
	    decomp_table[comp].decomp_func == NULL)) {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   390
		grub_printf("compression algorithm not supported\n");
7147
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
   391
		return (ERR_FSYS_CORRUPT);
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   392
	}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   393
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   394
	if ((char *)buf < stack && ((char *)buf) + lsize > stack) {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   395
		grub_printf("not enough memory allocated\n");
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   396
		return (ERR_WONT_FIT);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   397
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   398
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   399
	retbuf = buf;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   400
	if (comp != ZIO_COMPRESS_OFF) {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   401
		buf = stack;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   402
		stack += psize;
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   403
	}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   404
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   405
	if (zio_read_data(bp, buf, stack) != 0) {
8386
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   406
		grub_printf("zio_read_data failed\n");
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   407
		return (ERR_FSYS_CORRUPT);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   408
	}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   409
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   410
	if (zio_checksum_verify(bp, buf, psize) != 0) {
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   411
		grub_printf("checksum verification failed\n");
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   412
		return (ERR_FSYS_CORRUPT);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   413
	}
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   414
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   415
	if (comp != ZIO_COMPRESS_OFF)
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   416
		decomp_table[comp].decomp_func(buf, retbuf, psize, lsize);
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   417
b7d2676eafda 6774616 GRUB/ZFS Error 16 (Inconsistent filesystem structure) due to lack of GANG Block support
Lin Ling <Lin.Ling@Sun.COM>
parents: 8242
diff changeset
   418
	return (0);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   419
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   420
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   421
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   422
 * Get the block from a block id.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   423
 * push the block onto the stack.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   424
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   425
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   426
 * 	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   427
 * 	errnum - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   428
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   429
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   430
dmu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   431
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   432
	int idx, level;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   433
	blkptr_t *bp_array = dn->dn_blkptr;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   434
	int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   435
	blkptr_t *bp, *tmpbuf;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   436
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   437
	bp = (blkptr_t *)stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   438
	stack += sizeof (blkptr_t);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   439
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   440
	tmpbuf = (blkptr_t *)stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   441
	stack += 1<<dn->dn_indblkshift;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   442
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   443
	for (level = dn->dn_nlevels - 1; level >= 0; level--) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   444
		idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   445
		*bp = bp_array[idx];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   446
		if (level == 0)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   447
			tmpbuf = buf;
4725
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   448
		if (BP_IS_HOLE(bp)) {
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   449
			grub_memset(buf, 0,
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   450
			    dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   451
			break;
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   452
		} else if (errnum = zio_read(bp, tmpbuf, stack)) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   453
			return (errnum);
4725
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   454
		}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   455
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   456
		bp_array = tmpbuf;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   457
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   458
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   459
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   460
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   461
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   462
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   463
 * mzap_lookup: Looks up property described by "name" and returns the value
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   464
 * in "value".
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   465
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   466
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   467
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   468
 *	errnum - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   469
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   470
static int
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   471
mzap_lookup(mzap_phys_t *zapobj, int objsize, const char *name,
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   472
	uint64_t *value)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   473
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   474
	int i, chunks;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   475
	mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   476
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   477
	chunks = objsize / MZAP_ENT_LEN - 1;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   478
	for (i = 0; i < chunks; i++) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   479
		if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   480
			*value = mzap_ent[i].mze_value;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   481
			return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   482
		}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   483
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   484
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   485
	return (ERR_FSYS_CORRUPT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   486
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   487
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   488
static uint64_t
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   489
zap_hash(uint64_t salt, const char *name)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   490
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   491
	static uint64_t table[256];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   492
	const uint8_t *cp;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   493
	uint8_t c;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   494
	uint64_t crc = salt;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   495
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   496
	if (table[128] == 0) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   497
		uint64_t *ct;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   498
		int i, j;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   499
		for (i = 0; i < 256; i++) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   500
			for (ct = table + i, *ct = i, j = 8; j > 0; j--)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   501
				*ct = (*ct >> 1) ^ (-(*ct & 1) &
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   502
				    ZFS_CRC64_POLY);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   503
		}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   504
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   505
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   506
	if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   507
		errnum = ERR_FSYS_CORRUPT;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   508
		return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   509
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   510
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   511
	for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   512
		crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   513
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   514
	/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   515
	 * Only use 28 bits, since we need 4 bits in the cookie for the
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   516
	 * collision differentiator.  We MUST use the high bits, since
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   517
	 * those are the ones that we first pay attention to when
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   518
	 * choosing the bucket.
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   519
	 */
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10237
diff changeset
   520
	crc &= ~((1ULL << (64 - 28)) - 1);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   521
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   522
	return (crc);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   523
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   524
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   525
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   526
 * Only to be used on 8-bit arrays.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   527
 * array_len is actual len in bytes (not encoded le_value_length).
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   528
 * buf is null-terminated.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   529
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   530
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   531
zap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   532
    int array_len, const char *buf)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   533
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   534
	int bseen = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   535
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   536
	while (bseen < array_len) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   537
		struct zap_leaf_array *la =
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   538
		    &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   539
		int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   540
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   541
		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   542
			return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   543
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   544
		if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   545
			break;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   546
		chunk = la->la_next;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   547
		bseen += toread;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   548
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   549
	return (bseen == array_len);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   550
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   551
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   552
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   553
 * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   554
 * value for the property "name".
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   555
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   556
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   557
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   558
 *	errnum - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   559
 */
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
   560
static int
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   561
zap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   562
    const char *name, uint64_t *value)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   563
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   564
	uint16_t chunk;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   565
	struct zap_leaf_entry *le;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   566
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   567
	/* Verify if this is a valid leaf block */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   568
	if (l->l_hdr.lh_block_type != ZBT_LEAF)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   569
		return (ERR_FSYS_CORRUPT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   570
	if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   571
		return (ERR_FSYS_CORRUPT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   572
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   573
	for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   574
	    chunk != CHAIN_END; chunk = le->le_next) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   575
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   576
		if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   577
			return (ERR_FSYS_CORRUPT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   578
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   579
		le = ZAP_LEAF_ENTRY(l, blksft, chunk);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   580
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   581
		/* Verify the chunk entry */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   582
		if (le->le_type != ZAP_CHUNK_ENTRY)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   583
			return (ERR_FSYS_CORRUPT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   584
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   585
		if (le->le_hash != h)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   586
			continue;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   587
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   588
		if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   589
		    le->le_name_length, name)) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   590
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   591
			struct zap_leaf_array *la;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   592
			uint8_t *ip;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   593
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   594
			if (le->le_int_size != 8 || le->le_value_length != 1)
7149
7e0cb110a48b 6704717 ZFS mirrored root doesn't live up to expectations (fix cstyle)
taylor
parents: 7148
diff changeset
   595
				return (ERR_FSYS_CORRUPT);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   596
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   597
			/* get the uint64_t property value */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   598
			la = &ZAP_LEAF_CHUNK(l, blksft,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   599
			    le->le_value_chunk).l_array;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   600
			ip = la->la_array;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   601
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   602
			*value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   603
			    (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   604
			    (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   605
			    (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   606
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   607
			return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   608
		}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   609
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   610
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   611
	return (ERR_FSYS_CORRUPT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   612
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   613
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   614
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   615
 * Fat ZAP lookup
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   616
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   617
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   618
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   619
 *	errnum - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   620
 */
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
   621
static int
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   622
fzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   623
    const char *name, uint64_t *value, char *stack)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   624
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   625
	zap_leaf_phys_t *l;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   626
	uint64_t hash, idx, blkid;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   627
	int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   628
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   629
	/* Verify if this is a fat zap header block */
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10237
diff changeset
   630
	if (zap->zap_magic != (uint64_t)ZAP_MAGIC ||
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10237
diff changeset
   631
	    zap->zap_flags != 0)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   632
		return (ERR_FSYS_CORRUPT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   633
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   634
	hash = zap_hash(zap->zap_salt, name);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   635
	if (errnum)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   636
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   637
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   638
	/* get block id from index */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   639
	if (zap->zap_ptrtbl.zt_numblks != 0) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   640
		/* external pointer tables not supported */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   641
		return (ERR_FSYS_CORRUPT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   642
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   643
	idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   644
	blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   645
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   646
	/* Get the leaf block */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   647
	l = (zap_leaf_phys_t *)stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   648
	stack += 1<<blksft;
7147
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
   649
	if ((1<<blksft) < sizeof (zap_leaf_phys_t))
7149
7e0cb110a48b 6704717 ZFS mirrored root doesn't live up to expectations (fix cstyle)
taylor
parents: 7148
diff changeset
   650
		return (ERR_FSYS_CORRUPT);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   651
	if (errnum = dmu_read(zap_dnode, blkid, l, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   652
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   653
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   654
	return (zap_leaf_lookup(l, blksft, hash, name, value));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   655
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   656
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   657
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   658
 * Read in the data of a zap object and find the value for a matching
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   659
 * property name.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   660
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   661
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   662
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   663
 *	errnum - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   664
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   665
static int
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   666
zap_lookup(dnode_phys_t *zap_dnode, const char *name, uint64_t *val,
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   667
    char *stack)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   668
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   669
	uint64_t block_type;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   670
	int size;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   671
	void *zapbuf;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   672
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   673
	/* Read in the first block of the zap object data. */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   674
	zapbuf = stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   675
	size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   676
	stack += size;
11935
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
   677
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   678
	if ((errnum = dmu_read(zap_dnode, 0, zapbuf, stack)) != 0)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   679
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   680
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   681
	block_type = *((uint64_t *)zapbuf);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   682
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   683
	if (block_type == ZBT_MICRO) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   684
		return (mzap_lookup(zapbuf, size, name, val));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   685
	} else if (block_type == ZBT_HEADER) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   686
		/* this is a fat zap */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   687
		return (fzap_lookup(zap_dnode, zapbuf, name,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   688
		    val, stack));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   689
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   690
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   691
	return (ERR_FSYS_CORRUPT);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   692
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   693
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   694
typedef struct zap_attribute {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   695
	int za_integer_length;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   696
	uint64_t za_num_integers;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   697
	uint64_t za_first_integer;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   698
	char *za_name;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   699
} zap_attribute_t;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   701
typedef int (zap_cb_t)(zap_attribute_t *za, void *arg, char *stack);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   702
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   703
static int
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   704
zap_iterate(dnode_phys_t *zap_dnode, zap_cb_t *cb, void *arg, char *stack)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   705
{
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   706
	uint32_t size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   707
	zap_attribute_t za;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   708
	int i;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   709
	mzap_phys_t *mzp = (mzap_phys_t *)stack;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   710
	stack += size;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   711
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   712
	if ((errnum = dmu_read(zap_dnode, 0, mzp, stack)) != 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   713
		return (errnum);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   714
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   715
	/*
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   716
	 * Iteration over fatzap objects has not yet been implemented.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   717
	 * If we encounter a pool in which there are more features for
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   718
	 * read than can fit inside a microzap (i.e., more than 2048
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   719
	 * features for read), we can add support for fatzap iteration.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   720
	 * For now, fail.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   721
	 */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   722
	if (mzp->mz_block_type != ZBT_MICRO) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   723
		grub_printf("feature information stored in fatzap, pool "
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   724
		    "version not supported\n");
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   725
		return (1);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   726
	}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   727
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   728
	za.za_integer_length = 8;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   729
	za.za_num_integers = 1;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   730
	for (i = 0; i < size / MZAP_ENT_LEN - 1; i++) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   731
		mzap_ent_phys_t *mzep = &mzp->mz_chunk[i];
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   732
		int err;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   733
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   734
		za.za_first_integer = mzep->mze_value;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   735
		za.za_name = mzep->mze_name;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   736
		err = cb(&za, arg, stack);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   737
		if (err != 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   738
			return (err);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   739
	}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   740
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   741
	return (0);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   742
}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   743
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   744
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   745
 * Get the dnode of an object number from the metadnode of an object set.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   746
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   747
 * Input
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   748
 *	mdn - metadnode to get the object dnode
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   749
 *	objnum - object number for the object dnode
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   750
 *	buf - data buffer that holds the returning dnode
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   751
 *	stack - scratch area
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   752
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   753
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   754
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   755
 *	errnum - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   756
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   757
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   758
dnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   759
	char *stack)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   760
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   761
	uint64_t blkid, blksz; /* the block id this object dnode is in */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   762
	int epbs; /* shift of number of dnodes in a block */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   763
	int idx; /* index within a block */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   764
	dnode_phys_t *dnbuf;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   765
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   766
	blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   767
	epbs = zfs_log2(blksz) - DNODE_SHIFT;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   768
	blkid = objnum >> epbs;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   769
	idx = objnum & ((1<<epbs)-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   770
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   771
	if (dnode_buf != NULL && dnode_mdn == mdn &&
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   772
	    objnum >= dnode_start && objnum < dnode_end) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   773
		grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   774
		VERIFY_DN_TYPE(buf, type);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   775
		return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   776
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   777
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   778
	if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   779
		dnbuf = dnode_buf;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   780
		dnode_mdn = mdn;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   781
		dnode_start = blkid << epbs;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   782
		dnode_end = (blkid + 1) << epbs;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   783
	} else {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   784
		dnbuf = (dnode_phys_t *)stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   785
		stack += blksz;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   786
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   787
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   788
	if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   789
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   790
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   791
	grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   792
	VERIFY_DN_TYPE(buf, type);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   793
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   794
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   795
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   796
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   797
/*
6448
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   798
 * Check if this is a special file that resides at the top
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   799
 * dataset of the pool. Currently this is the GRUB menu,
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   800
 * boot signature and boot signature backup.
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   801
 * str starts with '/'.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   802
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   803
static int
6448
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   804
is_top_dataset_file(char *str)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   805
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   806
	char *tptr;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   807
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   808
	if ((tptr = grub_strstr(str, "menu.lst")) &&
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   809
	    (tptr[8] == '\0' || tptr[8] == ' ') &&
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   810
	    *(tptr-1) == '/')
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   811
		return (1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   812
6448
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   813
	if (grub_strncmp(str, BOOTSIGN_DIR"/",
6449
9715e41771e2 6655040 need a mechanism to replace biosdev usage (fix cstyle)
vikram
parents: 6448
diff changeset
   814
	    grub_strlen(BOOTSIGN_DIR) + 1) == 0)
6448
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   815
		return (1);
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   816
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   817
	if (grub_strcmp(str, BOOTSIGN_BACKUP) == 0)
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   818
		return (1);
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
   819
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   820
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   821
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   822
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   823
static int
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   824
check_feature(zap_attribute_t *za, void *arg, char *stack)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   825
{
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   826
	const char **names = arg;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   827
	int i;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   828
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   829
	if (za->za_first_integer == 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   830
		return (0);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   831
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   832
	for (i = 0; names[i] != NULL; i++) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   833
		if (grub_strcmp(za->za_name, names[i]) == 0) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   834
			return (0);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   835
		}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   836
	}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   837
	grub_printf("missing feature for read '%s'\n", za->za_name);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   838
	return (ERR_NEWER_VERSION);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   839
}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   840
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   841
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   842
 * Get the file dnode for a given file name where mdn is the meta dnode
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   843
 * for this ZFS object set. When found, place the file dnode in dn.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   844
 * The 'path' argument will be mangled.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   845
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   846
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   847
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   848
 *	errnum - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   849
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   850
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   851
dnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   852
    char *stack)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   853
{
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   854
	uint64_t objnum, version;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   855
	char *cname, ch;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   856
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   857
	if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   858
	    dn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   859
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   860
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   861
	if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   862
		return (errnum);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   863
	if (version > ZPL_VERSION)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   864
		return (-1);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   865
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   866
	if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   867
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   868
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   869
	if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   870
	    dn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   871
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   872
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   873
	/* skip leading slashes */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   874
	while (*path == '/')
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   875
		path++;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   876
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   877
	while (*path && !grub_isspace(*path)) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   878
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   879
		/* get the next component name */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   880
		cname = path;
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   881
		while (*path && !grub_isspace(*path) && *path != '/')
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   882
			path++;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   883
		ch = *path;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   884
		*path = 0;   /* ensure null termination */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   885
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   886
		if (errnum = zap_lookup(dn, cname, &objnum, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   887
			return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   888
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   889
		objnum = ZFS_DIRENT_OBJ(objnum);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   890
		if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   891
			return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   892
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   893
		*path = ch;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   894
		while (*path == '/')
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   895
			path++;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   896
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   897
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   898
	/* We found the dnode for this file. Verify if it is a plain file. */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   899
	VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   900
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   901
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   902
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   903
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   904
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   905
 * Get the default 'bootfs' property value from the rootpool.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   906
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   907
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   908
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   909
 *	errnum -failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   910
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   911
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   912
get_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   913
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   914
	uint64_t objnum = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   915
	dnode_phys_t *dn = (dnode_phys_t *)stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   916
	stack += DNODE_SIZE;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   917
4725
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   918
	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   919
	    DMU_OT_OBJECT_DIRECTORY, dn, stack))
4725
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   920
		return (errnum);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   921
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   922
	/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   923
	 * find the object number for 'pool_props', and get the dnode
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   924
	 * of the 'pool_props'.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   925
	 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   926
	if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   927
		return (ERR_FILESYSTEM_NOT_FOUND);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   928
4725
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   929
	if (errnum = dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
   930
		return (errnum);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   931
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   932
	if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   933
		return (ERR_FILESYSTEM_NOT_FOUND);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   934
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   935
	if (!objnum)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   936
		return (ERR_FILESYSTEM_NOT_FOUND);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   937
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   938
	*obj = objnum;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   939
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   940
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   941
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   942
/*
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   943
 * List of pool features that the grub implementation of ZFS supports for
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   944
 * read. Note that features that are only required for write do not need
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   945
 * to be listed here since grub opens pools in read-only mode.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   946
 */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   947
static const char *spa_feature_names[] = {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   948
	NULL
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   949
};
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   950
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   951
/*
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   952
 * Checks whether the MOS features that are active are supported by this
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   953
 * (GRUB's) implementation of ZFS.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   954
 *
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   955
 * Return:
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   956
 *	0: Success.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   957
 *	errnum: Failure.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   958
 */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   959
static int
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   960
check_mos_features(dnode_phys_t *mosmdn, char *stack)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   961
{
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   962
	uint64_t objnum;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   963
	dnode_phys_t *dn;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   964
	uint8_t error = 0;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   965
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   966
	dn = (dnode_phys_t *)stack;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   967
	stack += DNODE_SIZE;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   968
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   969
	if ((errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   970
	    DMU_OT_OBJECT_DIRECTORY, dn, stack)) != 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   971
		return (errnum);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   972
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   973
	/*
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   974
	 * Find the object number for 'features_for_read' and retrieve its
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   975
	 * corresponding dnode. Note that we don't check features_for_write
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   976
	 * because GRUB is not opening the pool for write.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   977
	 */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   978
	if ((errnum = zap_lookup(dn, DMU_POOL_FEATURES_FOR_READ, &objnum,
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   979
	    stack)) != 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   980
		return (errnum);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   981
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   982
	if ((errnum = dnode_get(mosmdn, objnum, DMU_OTN_ZAP_METADATA,
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   983
	    dn, stack)) != 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   984
		return (errnum);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   985
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   986
	return (zap_iterate(dn, check_feature, spa_feature_names, stack));
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   987
}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   988
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
   989
/*
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   990
 * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   991
 * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   992
 * of pool/rootfs.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   993
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   994
 * If no fsname and no obj are given, return the DSL_DIR metadnode.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   995
 * If fsname is given, return its metadnode and its matching object number.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   996
 * If only obj is given, return the metadnode for this object number.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   997
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   998
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
   999
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1000
 *	errnum - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1001
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1002
static int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1003
get_objset_mdn(dnode_phys_t *mosmdn, char *fsname, uint64_t *obj,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1004
    dnode_phys_t *mdn, char *stack)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1005
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1006
	uint64_t objnum, headobj;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1007
	char *cname, ch;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1008
	blkptr_t *bp;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1009
	objset_phys_t *osp;
7300
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1010
	int issnapshot = 0;
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1011
	char *snapname;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1012
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1013
	if (fsname == NULL && obj) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1014
		headobj = *obj;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1015
		goto skip;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1016
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1017
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1018
	if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1019
	    DMU_OT_OBJECT_DIRECTORY, mdn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1020
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1021
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1022
	if (errnum = zap_lookup(mdn, DMU_POOL_ROOT_DATASET, &objnum,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1023
	    stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1024
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1025
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1026
	if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR, mdn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1027
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1028
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1029
	if (fsname == NULL) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1030
		headobj =
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1031
		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1032
		goto skip;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1033
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1034
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1035
	/* take out the pool name */
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1036
	while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1037
		fsname++;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1038
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1039
	while (*fsname && !grub_isspace(*fsname)) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1040
		uint64_t childobj;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1041
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1042
		while (*fsname == '/')
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1043
			fsname++;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1044
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1045
		cname = fsname;
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1046
		while (*fsname && !grub_isspace(*fsname) && *fsname != '/')
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1047
			fsname++;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1048
		ch = *fsname;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1049
		*fsname = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1050
7300
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1051
		snapname = cname;
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1052
		while (*snapname && !grub_isspace(*snapname) && *snapname !=
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1053
		    '@')
7300
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1054
			snapname++;
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1055
		if (*snapname == '@') {
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1056
			issnapshot = 1;
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1057
			*snapname = 0;
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1058
		}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1059
		childobj =
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1060
		    ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_child_dir_zapobj;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1061
		if (errnum = dnode_get(mosmdn, childobj,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1062
		    DMU_OT_DSL_DIR_CHILD_MAP, mdn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1063
			return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1064
4725
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
  1065
		if (zap_lookup(mdn, cname, &objnum, stack))
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
  1066
			return (ERR_FILESYSTEM_NOT_FOUND);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1067
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1068
		if (errnum = dnode_get(mosmdn, objnum, DMU_OT_DSL_DIR,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1069
		    mdn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1070
			return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1071
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1072
		*fsname = ch;
7300
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1073
		if (issnapshot)
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1074
			*snapname = '@';
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1075
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1076
	headobj = ((dsl_dir_phys_t *)DN_BONUS(mdn))->dd_head_dataset_obj;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1077
	if (obj)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1078
		*obj = headobj;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1079
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1080
skip:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1081
	if (errnum = dnode_get(mosmdn, headobj, DMU_OT_DSL_DATASET, mdn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1082
		return (errnum);
7300
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1083
	if (issnapshot) {
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1084
		uint64_t snapobj;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1085
7300
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1086
		snapobj = ((dsl_dataset_phys_t *)DN_BONUS(mdn))->
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1087
		    ds_snapnames_zapobj;
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1088
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1089
		if (errnum = dnode_get(mosmdn, snapobj,
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1090
		    DMU_OT_DSL_DS_SNAP_MAP, mdn, stack))
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1091
			return (errnum);
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1092
		if (zap_lookup(mdn, snapname + 1, &headobj, stack))
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1093
			return (ERR_FILESYSTEM_NOT_FOUND);
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1094
		if (errnum = dnode_get(mosmdn, headobj,
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1095
		    DMU_OT_DSL_DATASET, mdn, stack))
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1096
			return (errnum);
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1097
		if (obj)
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1098
			*obj = headobj;
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1099
	}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1100
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1101
	bp = &((dsl_dataset_phys_t *)DN_BONUS(mdn))->ds_bp;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1102
	osp = (objset_phys_t *)stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1103
	stack += sizeof (objset_phys_t);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1104
	if (errnum = zio_read(bp, osp, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1105
		return (errnum);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1106
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1107
	grub_memmove((char *)mdn, (char *)&osp->os_meta_dnode, DNODE_SIZE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1108
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1109
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1110
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1111
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1112
/*
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1113
 * For a given XDR packed nvlist, verify the first 4 bytes and move on.
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1114
 *
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1115
 * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1116
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1117
 *      encoding method/host endian     (4 bytes)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1118
 *      nvl_version                     (4 bytes)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1119
 *      nvl_nvflag                      (4 bytes)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1120
 *	encoded nvpairs:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1121
 *		encoded size of the nvpair      (4 bytes)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1122
 *		decoded size of the nvpair      (4 bytes)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1123
 *		name string size                (4 bytes)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1124
 *		name string data                (sizeof(NV_ALIGN4(string))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1125
 *		data type                       (4 bytes)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1126
 *		# of elements in the nvpair     (4 bytes)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1127
 *		data
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1128
 *      2 zero's for the last nvpair
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1129
 *		(end of the entire list)	(8 bytes)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1130
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1131
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1132
 *	0 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1133
 *	1 - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1134
 */
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1135
static int
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1136
nvlist_unpack(char *nvlist, char **out)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1137
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1138
	/* Verify if the 1st and 2nd byte in the nvlist are valid. */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1139
	if (nvlist[0] != NV_ENCODE_XDR || nvlist[1] != HOST_ENDIAN)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1140
		return (1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1141
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1142
	*out = nvlist + 4;
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1143
	return (0);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1144
}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1145
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1146
static char *
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1147
nvlist_array(char *nvlist, int index)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1148
{
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1149
	int i, encode_size;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1150
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1151
	for (i = 0; i < index; i++) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1152
		/* skip the header, nvl_version, and nvl_nvflag */
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1153
		nvlist = nvlist + 4 * 2;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1154
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1155
		while (encode_size = BSWAP_32(*(uint32_t *)nvlist))
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1156
			nvlist += encode_size; /* goto the next nvpair */
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1157
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1158
		nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1159
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1160
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1161
	return (nvlist);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1162
}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1163
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1164
/*
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1165
 * The nvlist_next_nvpair() function returns a handle to the next nvpair in the
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1166
 * list following nvpair. If nvpair is NULL, the first pair is returned. If
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1167
 * nvpair is the last pair in the nvlist, NULL is returned.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1168
 */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1169
static char *
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1170
nvlist_next_nvpair(char *nvl, char *nvpair)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1171
{
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1172
	char *cur, *prev;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1173
	int encode_size;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1174
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1175
	if (nvl == NULL)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1176
		return (NULL);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1177
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1178
	if (nvpair == NULL) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1179
		/* skip over nvl_version and nvl_nvflag */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1180
		nvpair = nvl + 4 * 2;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1181
	} else {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1182
		/* skip to the next nvpair */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1183
		encode_size = BSWAP_32(*(uint32_t *)nvpair);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1184
		nvpair += encode_size;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1185
	}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1186
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1187
	/* 8 bytes of 0 marks the end of the list */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1188
	if (*(uint64_t *)nvpair == 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1189
		return (NULL);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1190
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1191
	return (nvpair);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1192
}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1193
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1194
/*
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1195
 * This function returns 0 on success and 1 on failure. On success, a string
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1196
 * containing the name of nvpair is saved in buf.
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1197
 */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1198
static int
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1199
nvpair_name(char *nvp, char *buf, int buflen)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1200
{
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1201
	int len;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1202
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1203
	/* skip over encode/decode size */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1204
	nvp += 4 * 2;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1205
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1206
	len = BSWAP_32(*(uint32_t *)nvp);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1207
	if (buflen < len + 1)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1208
		return (1);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1209
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1210
	grub_memmove(buf, nvp + 4, len);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1211
	buf[len] = '\0';
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1212
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1213
	return (0);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1214
}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1215
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1216
/*
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1217
 * This function retrieves the value of the nvpair in the form of enumerated
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1218
 * type data_type_t. This is used to determine the appropriate type to pass to
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1219
 * nvpair_value().
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1220
 */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1221
static int
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1222
nvpair_type(char *nvp)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1223
{
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1224
	int name_len, type;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1225
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1226
	/* skip over encode/decode size */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1227
	nvp += 4 * 2;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1228
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1229
	/* skip over name_len */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1230
	name_len = BSWAP_32(*(uint32_t *)nvp);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1231
	nvp += 4;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1232
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1233
	/* skip over name */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1234
	nvp = nvp + ((name_len + 3) & ~3); /* align */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1235
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1236
	type = BSWAP_32(*(uint32_t *)nvp);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1237
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1238
	return (type);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1239
}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1240
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1241
static int
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1242
nvpair_value(char *nvp, void *val, int valtype, int *nelmp)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1243
{
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1244
	int name_len, type, slen;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1245
	char *strval = val;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1246
	uint64_t *intval = val;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1247
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1248
	/* skip over encode/decode size */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1249
	nvp += 4 * 2;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1250
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1251
	/* skip over name_len */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1252
	name_len = BSWAP_32(*(uint32_t *)nvp);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1253
	nvp += 4;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1254
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1255
	/* skip over name */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1256
	nvp = nvp + ((name_len + 3) & ~3); /* align */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1257
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1258
	/* skip over type */
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1259
	type = BSWAP_32(*(uint32_t *)nvp);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1260
	nvp += 4;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1261
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1262
	if (type == valtype) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1263
		int nelm;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1264
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1265
		nelm = BSWAP_32(*(uint32_t *)nvp);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1266
		if (valtype != DATA_TYPE_BOOLEAN && nelm < 1)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1267
			return (1);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1268
		nvp += 4;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1269
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1270
		switch (valtype) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1271
		case DATA_TYPE_BOOLEAN:
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1272
			return (0);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1273
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1274
		case DATA_TYPE_STRING:
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1275
			slen = BSWAP_32(*(uint32_t *)nvp);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1276
			nvp += 4;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1277
			grub_memmove(strval, nvp, slen);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1278
			strval[slen] = '\0';
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1279
			return (0);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1280
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1281
		case DATA_TYPE_UINT64:
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1282
			*intval = BSWAP_64(*(uint64_t *)nvp);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1283
			return (0);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1284
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1285
		case DATA_TYPE_NVLIST:
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1286
			*(void **)val = (void *)nvp;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1287
			return (0);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1288
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1289
		case DATA_TYPE_NVLIST_ARRAY:
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1290
			*(void **)val = (void *)nvp;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1291
			if (nelmp)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1292
				*nelmp = nelm;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1293
			return (0);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1294
		}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1295
	}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1296
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1297
	return (1);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1298
}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1299
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1300
static int
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1301
nvlist_lookup_value(char *nvlist, char *name, void *val, int valtype,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1302
    int *nelmp)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1303
{
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1304
	char *nvpair;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1305
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1306
	for (nvpair = nvlist_next_nvpair(nvlist, NULL);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1307
	    nvpair != NULL;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1308
	    nvpair = nvlist_next_nvpair(nvlist, nvpair)) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1309
		int name_len = BSWAP_32(*(uint32_t *)(nvpair + 4 * 2));
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1310
		char *nvp_name = nvpair + 4 * 3;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1311
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1312
		if ((grub_strncmp(nvp_name, name, name_len) == 0) &&
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1313
		    nvpair_type(nvpair) == valtype) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1314
			return (nvpair_value(nvpair, val, valtype, nelmp));
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1315
		}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1316
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1317
	return (1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1318
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1319
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1320
/*
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1321
 * Check if this vdev is online and is in a good state.
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1322
 */
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1323
static int
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1324
vdev_validate(char *nv)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1325
{
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1326
	uint64_t ival;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1327
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1328
	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_OFFLINE, &ival,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1329
	    DATA_TYPE_UINT64, NULL) == 0 ||
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1330
	    nvlist_lookup_value(nv, ZPOOL_CONFIG_FAULTED, &ival,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1331
	    DATA_TYPE_UINT64, NULL) == 0 ||
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1332
	    nvlist_lookup_value(nv, ZPOOL_CONFIG_REMOVED, &ival,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1333
	    DATA_TYPE_UINT64, NULL) == 0)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1334
		return (ERR_DEV_VALUES);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1335
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1336
	return (0);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1337
}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1338
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1339
/*
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1340
 * Get a valid vdev pathname/devid from the boot device.
8242
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1341
 * The caller should already allocate MAXPATHLEN memory for bootpath and devid.
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1342
 */
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1343
static int
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1344
vdev_get_bootpath(char *nv, uint64_t inguid, char *devid, char *bootpath,
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1345
    int is_spare)
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1346
{
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1347
	char type[16];
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1348
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1349
	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_TYPE, &type, DATA_TYPE_STRING,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1350
	    NULL))
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1351
		return (ERR_FSYS_CORRUPT);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1352
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1353
	if (grub_strcmp(type, VDEV_TYPE_DISK) == 0) {
8242
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1354
		uint64_t guid;
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1355
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1356
		if (vdev_validate(nv) != 0)
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1357
			return (ERR_NO_BOOTPATH);
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1358
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1359
		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_GUID,
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1360
		    &guid, DATA_TYPE_UINT64, NULL) != 0)
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1361
			return (ERR_NO_BOOTPATH);
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1362
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1363
		if (guid != inguid)
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1364
			return (ERR_NO_BOOTPATH);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1365
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1366
		/* for a spare vdev, pick the disk labeled with "is_spare" */
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1367
		if (is_spare) {
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1368
			uint64_t spare = 0;
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1369
			(void) nvlist_lookup_value(nv, ZPOOL_CONFIG_IS_SPARE,
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1370
			    &spare, DATA_TYPE_UINT64, NULL);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1371
			if (!spare)
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1372
				return (ERR_NO_BOOTPATH);
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1373
		}
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1374
8242
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1375
		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_PHYS_PATH,
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1376
		    bootpath, DATA_TYPE_STRING, NULL) != 0)
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1377
			bootpath[0] = '\0';
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1378
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1379
		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_DEVID,
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1380
		    devid, DATA_TYPE_STRING, NULL) != 0)
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1381
			devid[0] = '\0';
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1382
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1383
		if (grub_strlen(bootpath) >= MAXPATHLEN ||
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1384
		    grub_strlen(devid) >= MAXPATHLEN)
8242
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1385
			return (ERR_WONT_FIT);
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1386
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1387
		return (0);
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1388
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1389
	} else if (grub_strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1390
	    grub_strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1391
	    (is_spare = (grub_strcmp(type, VDEV_TYPE_SPARE) == 0))) {
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1392
		int nelm, i;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1393
		char *child;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1394
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1395
		if (nvlist_lookup_value(nv, ZPOOL_CONFIG_CHILDREN, &child,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1396
		    DATA_TYPE_NVLIST_ARRAY, &nelm))
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1397
			return (ERR_FSYS_CORRUPT);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1398
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1399
		for (i = 0; i < nelm; i++) {
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1400
			char *child_i;
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1401
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1402
			child_i = nvlist_array(child, i);
8242
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1403
			if (vdev_get_bootpath(child_i, inguid, devid,
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1404
			    bootpath, is_spare) == 0)
8242
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1405
				return (0);
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1406
		}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1407
	}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1408
8242
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1409
	return (ERR_NO_BOOTPATH);
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1410
}
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1411
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1412
/*
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1413
 * Check the disk label information and retrieve needed vdev name-value pairs.
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1414
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1415
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1416
 *	0 - success
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1417
 *	ERR_* - failure
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1418
 */
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1419
static int
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1420
check_pool_label(uint64_t sector, char *stack, char *outdevid,
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1421
    char *outpath, uint64_t *outguid, uint64_t *outashift, uint64_t *outversion)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1422
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1423
	vdev_phys_t *vdev;
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1424
	uint64_t pool_state, txg = 0;
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1425
	char *nvlist, *nv, *features;
7147
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1426
	uint64_t diskguid;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1427
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1428
	sector += (VDEV_SKIP_SIZE >> SPA_MINBLOCKSHIFT);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1429
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1430
	/* Read in the vdev name-value pair list (112K). */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1431
	if (devread(sector, 0, VDEV_PHYS_SIZE, stack) == 0)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1432
		return (ERR_READ);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1433
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1434
	vdev = (vdev_phys_t *)stack;
7148
3009692eae80 6704717 ZFS mirrored root doesn't live up to expectations (fix cstyle)
taylor
parents: 7147
diff changeset
  1435
	stack += sizeof (vdev_phys_t);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1436
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1437
	if (nvlist_unpack(vdev->vp_nvlist, &nvlist))
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1438
		return (ERR_FSYS_CORRUPT);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1439
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1440
	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_STATE, &pool_state,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1441
	    DATA_TYPE_UINT64, NULL))
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1442
		return (ERR_FSYS_CORRUPT);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1443
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1444
	if (pool_state == POOL_STATE_DESTROYED)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1445
		return (ERR_FILESYSTEM_NOT_FOUND);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1446
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1447
	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_NAME,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1448
	    current_rootpool, DATA_TYPE_STRING, NULL))
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1449
		return (ERR_FSYS_CORRUPT);
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1450
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1451
	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_TXG, &txg,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1452
	    DATA_TYPE_UINT64, NULL))
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1453
		return (ERR_FSYS_CORRUPT);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1454
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1455
	/* not an active device */
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1456
	if (txg == 0)
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1457
		return (ERR_NO_BOOTPATH);
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1458
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1459
	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VERSION, outversion,
7300
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1460
	    DATA_TYPE_UINT64, NULL))
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1461
		return (ERR_FSYS_CORRUPT);
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1462
	if (!SPA_VERSION_IS_SUPPORTED(*outversion))
7300
3f0c3af330d3 6667976 support booting up a snapshot root dataset
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7265
diff changeset
  1463
		return (ERR_NEWER_VERSION);
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1464
	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_VDEV_TREE, &nv,
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1465
	    DATA_TYPE_NVLIST, NULL))
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1466
		return (ERR_FSYS_CORRUPT);
7147
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1467
	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_GUID, &diskguid,
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1468
	    DATA_TYPE_UINT64, NULL))
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1469
		return (ERR_FSYS_CORRUPT);
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1470
	if (nvlist_lookup_value(nv, ZPOOL_CONFIG_ASHIFT, outashift,
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1471
	    DATA_TYPE_UINT64, NULL) != 0)
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1472
		return (ERR_FSYS_CORRUPT);
9790
e276ee006ff6 6747441 GRUB/vdev_get_bootpath, spa_get_rootconf, zpool_get_physpath should take care of spare vdev
Lin Ling <Lin.Ling@Sun.COM>
parents: 8876
diff changeset
  1473
	if (vdev_get_bootpath(nv, diskguid, outdevid, outpath, 0))
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1474
		return (ERR_NO_BOOTPATH);
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1475
	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_POOL_GUID, outguid,
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1476
	    DATA_TYPE_UINT64, NULL))
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1477
		return (ERR_FSYS_CORRUPT);
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1478
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1479
	if (nvlist_lookup_value(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ,
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1480
	    &features, DATA_TYPE_NVLIST, NULL) == 0) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1481
		char *nvp;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1482
		char *name = stack;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1483
		stack += MAXNAMELEN;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1484
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1485
		for (nvp = nvlist_next_nvpair(features, NULL);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1486
		    nvp != NULL;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1487
		    nvp = nvlist_next_nvpair(features, nvp)) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1488
			zap_attribute_t za;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1489
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1490
			if (nvpair_name(nvp, name, MAXNAMELEN) != 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1491
				return (ERR_FSYS_CORRUPT);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1492
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1493
			za.za_integer_length = 8;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1494
			za.za_num_integers = 1;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1495
			za.za_first_integer = 1;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1496
			za.za_name = name;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1497
			if (check_feature(&za, spa_feature_names, stack) != 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1498
				return (ERR_NEWER_VERSION);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1499
		}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1500
	}
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1501
6423
437422a29d3a PSARC 2006/370 ZFS Boot Support
gw25295
parents: 4725
diff changeset
  1502
	return (0);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1503
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1504
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1505
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1506
 * zfs_mount() locates a valid uberblock of the root pool and read in its MOS
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1507
 * to the memory address MOS.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1508
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1509
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1510
 *	1 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1511
 *	0 - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1512
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1513
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1514
zfs_mount(void)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1515
{
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1516
	char *stack, *ub_array;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1517
	int label = 0;
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1518
	uberblock_t *ubbest;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1519
	objset_phys_t *osp;
7147
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1520
	char tmp_bootpath[MAXNAMELEN];
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1521
	char tmp_devid[MAXNAMELEN];
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1522
	uint64_t tmp_guid, ashift, version;
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1523
	uint64_t adjpl = (uint64_t)part_length << SPA_MINBLOCKSHIFT;
10237
1cca4617b3a8 6867241 fix for 6866143 breaks pxegrub menu load on non-primary NIC
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10056
diff changeset
  1524
	int err = errnum; /* preserve previous errnum state */
7147
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1525
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1526
	/* if it's our first time here, zero the best uberblock out */
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1527
	if (best_drive == 0 && best_part == 0 && find_best_root) {
7149
7e0cb110a48b 6704717 ZFS mirrored root doesn't live up to expectations (fix cstyle)
taylor
parents: 7148
diff changeset
  1528
		grub_memset(&current_uberblock, 0, sizeof (uberblock_t));
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1529
		pool_guid = 0;
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1530
	}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1531
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1532
	stackbase = ZFS_SCRATCH;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1533
	stack = stackbase;
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1534
	ub_array = stack;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1535
	stack += VDEV_UBERBLOCK_RING;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1536
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1537
	osp = (objset_phys_t *)stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1538
	stack += sizeof (objset_phys_t);
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1539
	adjpl = P2ALIGN(adjpl, (uint64_t)sizeof (vdev_label_t));
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1540
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1541
	for (label = 0; label < VDEV_LABELS; label++) {
11019
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1542
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1543
		/*
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1544
		 * some eltorito stacks don't give us a size and
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1545
		 * we end up setting the size to MAXUINT, further
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1546
		 * some of these devices stop working once a single
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1547
		 * read past the end has been issued. Checking
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1548
		 * for a maximum part_length and skipping the backup
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1549
		 * labels at the end of the slice/partition/device
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1550
		 * avoids breaking down on such devices.
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1551
		 */
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1552
		if (part_length == MAXUINT && label == 2)
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1553
			break;
133bd84ab259 6882425 delta for 6819150 breaks boot from DVD on a number of systems
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10922
diff changeset
  1554
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1555
		uint64_t sector = vdev_label_start(adjpl,
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1556
		    label) >> SPA_MINBLOCKSHIFT;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1557
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1558
		/* Read in the uberblock ring (128K). */
10056
9f478b0e65da 6770808 error 37 when encountering multiple pools w/same name but unique bootfs
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 9790
diff changeset
  1559
		if (devread(sector  +
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1560
		    ((VDEV_SKIP_SIZE + VDEV_PHYS_SIZE) >> SPA_MINBLOCKSHIFT),
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1561
		    0, VDEV_UBERBLOCK_RING, ub_array) == 0)
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1562
			continue;
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1563
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1564
		if (check_pool_label(sector, stack, tmp_devid,
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1565
		    tmp_bootpath, &tmp_guid, &ashift, &version))
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1566
			continue;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1567
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1568
		if (pool_guid == 0)
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1569
			pool_guid = tmp_guid;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1570
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1571
		if ((ubbest = find_bestub(ub_array, ashift, sector)) == NULL ||
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1572
		    zio_read(&ubbest->ub_rootbp, osp, stack) != 0)
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1573
			continue;
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1574
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1575
		VERIFY_OS_TYPE(osp, DMU_OST_META);
7147
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1576
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1577
		if (version >= SPA_VERSION_FEATURES &&
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1578
		    check_mos_features(&osp->os_meta_dnode, stack) != 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1579
			continue;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13421
diff changeset
  1580
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1581
		if (find_best_root && ((pool_guid != tmp_guid) ||
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1582
		    vdev_uberblock_compare(ubbest, &(current_uberblock)) <= 0))
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1583
			continue;
8242
e46e4b2f0a03 6770866 GRUB/ZFS should require physical path or devid, but not both
Lin Ling <Lin.Ling@Sun.COM>
parents: 8044
diff changeset
  1584
13421
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1585
		/* Got the MOS. Save it at the memory addr MOS. */
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1586
		grub_memmove(MOS, &osp->os_meta_dnode, DNODE_SIZE);
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1587
		grub_memmove(&current_uberblock, ubbest, sizeof (uberblock_t));
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1588
		grub_memmove(current_bootpath, tmp_bootpath, MAXNAMELEN);
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1589
		grub_memmove(current_devid, tmp_devid, grub_strlen(tmp_devid));
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1590
		is_zfs_mount = 1;
a4721c685d96 1303 Grub should support large sector disks
Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
parents: 12034
diff changeset
  1591
		return (1);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1592
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1593
10237
1cca4617b3a8 6867241 fix for 6866143 breaks pxegrub menu load on non-primary NIC
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10056
diff changeset
  1594
	/*
1cca4617b3a8 6867241 fix for 6866143 breaks pxegrub menu load on non-primary NIC
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10056
diff changeset
  1595
	 * While some fs impls. (tftp) rely on setting and keeping
1cca4617b3a8 6867241 fix for 6866143 breaks pxegrub menu load on non-primary NIC
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10056
diff changeset
  1596
	 * global errnums set, others won't reset it and will break
1cca4617b3a8 6867241 fix for 6866143 breaks pxegrub menu load on non-primary NIC
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10056
diff changeset
  1597
	 * when issuing rawreads. The goal here is to simply not
1cca4617b3a8 6867241 fix for 6866143 breaks pxegrub menu load on non-primary NIC
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10056
diff changeset
  1598
	 * have zfs mount attempts impact the previous state.
1cca4617b3a8 6867241 fix for 6866143 breaks pxegrub menu load on non-primary NIC
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10056
diff changeset
  1599
	 */
1cca4617b3a8 6867241 fix for 6866143 breaks pxegrub menu load on non-primary NIC
Jan Setje-Eilers <Jan.Setje-Eilers@Sun.COM>
parents: 10056
diff changeset
  1600
	errnum = err;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1601
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1602
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1603
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1604
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1605
 * zfs_open() locates a file in the rootpool by following the
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1606
 * MOS and places the dnode of the file in the memory address DNODE.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1607
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1608
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1609
 *	1 - success
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1610
 *	0 - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1611
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1612
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1613
zfs_open(char *filename)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1614
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1615
	char *stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1616
	dnode_phys_t *mdn;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1617
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1618
	file_buf = NULL;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1619
	stackbase = ZFS_SCRATCH;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1620
	stack = stackbase;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1621
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1622
	mdn = (dnode_phys_t *)stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1623
	stack += sizeof (dnode_phys_t);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1624
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1625
	dnode_mdn = NULL;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1626
	dnode_buf = (dnode_phys_t *)stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1627
	stack += 1<<DNODE_BLOCK_SHIFT;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1628
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1629
	/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1630
	 * menu.lst is placed at the root pool filesystem level,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1631
	 * do not goto 'current_bootfs'.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1632
	 */
6448
dfd58b2129f6 PSARC 2008/201 Grub Findroot
vikram
parents: 6423
diff changeset
  1633
	if (is_top_dataset_file(filename)) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1634
		if (errnum = get_objset_mdn(MOS, NULL, NULL, mdn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1635
			return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1636
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1637
		current_bootfs_obj = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1638
	} else {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1639
		if (current_bootfs[0] == '\0') {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1640
			/* Get the default root filesystem object number */
4725
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
  1641
			if (errnum = get_default_bootfsobj(MOS,
0452c60b6e69 6541114 GRUB/ZFS fails to load files from a default compressed (lzjb) root
lling
parents: 4577
diff changeset
  1642
			    &current_bootfs_obj, stack))
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1643
				return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1644
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1645
			if (errnum = get_objset_mdn(MOS, NULL,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1646
			    &current_bootfs_obj, mdn, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1647
				return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1648
		} else {
6659
77371f47272b 6694438 zfs grub code doesn't unset "bootfs" after a failed boot attempt
taylor
parents: 6449
diff changeset
  1649
			if (errnum = get_objset_mdn(MOS, current_bootfs,
77371f47272b 6694438 zfs grub code doesn't unset "bootfs" after a failed boot attempt
taylor
parents: 6449
diff changeset
  1650
			    &current_bootfs_obj, mdn, stack)) {
7147
1e1d75c88283 6704717 ZFS mirrored root doesn't live up to expectations
taylor
parents: 7042
diff changeset
  1651
				grub_memset(current_bootfs, 0, MAXNAMELEN);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1652
				return (0);
6659
77371f47272b 6694438 zfs grub code doesn't unset "bootfs" after a failed boot attempt
taylor
parents: 6449
diff changeset
  1653
			}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1654
		}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1655
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1656
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1657
	if (dnode_get_path(mdn, filename, DNODE, stack)) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1658
		errnum = ERR_FILE_NOT_FOUND;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1659
		return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1660
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1661
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1662
	/* get the file size and set the file position to 0 */
11935
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1663
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1664
	/*
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1665
	 * For DMU_OT_SA we will need to locate the SIZE attribute
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1666
	 * attribute, which could be either in the bonus buffer
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1667
	 * or the "spill" block.
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1668
	 */
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1669
	if (DNODE->dn_bonustype == DMU_OT_SA) {
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1670
		sa_hdr_phys_t *sahdrp;
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1671
		int hdrsize;
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1672
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1673
		if (DNODE->dn_bonuslen != 0) {
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1674
			sahdrp = (sa_hdr_phys_t *)DN_BONUS(DNODE);
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1675
		} else {
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1676
			if (DNODE->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1677
				blkptr_t *bp = &DNODE->dn_spill;
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1678
				void *buf;
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1679
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1680
				buf = (void *)stack;
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1681
				stack += BP_GET_LSIZE(bp);
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1682
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1683
				/* reset errnum to rawread() failure */
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1684
				errnum = 0;
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1685
				if (zio_read(bp, buf, stack) != 0) {
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1686
					return (0);
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1687
				}
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1688
				sahdrp = buf;
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1689
			} else {
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1690
				errnum = ERR_FSYS_CORRUPT;
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1691
				return (0);
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1692
			}
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1693
		}
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1694
		hdrsize = SA_HDR_SIZE(sahdrp);
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1695
		filemax = *(uint64_t *)((char *)sahdrp + hdrsize +
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1696
		    SA_SIZE_OFFSET);
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1697
	} else {
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1698
		filemax = ((znode_phys_t *)DN_BONUS(DNODE))->zp_size;
538c866aaac6 6716117 ZFS needs native system attribute infrastructure
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 11670
diff changeset
  1699
	}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1700
	filepos = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1701
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1702
	dnode_buf = NULL;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1703
	return (1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1704
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1705
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1706
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1707
 * zfs_read reads in the data blocks pointed by the DNODE.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1708
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1709
 * Return:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1710
 *	len - the length successfully read in to the buffer
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1711
 *	0   - failure
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1712
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1713
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1714
zfs_read(char *buf, int len)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1715
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1716
	char *stack;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1717
	int blksz, length, movesize;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1718
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1719
	if (file_buf == NULL) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1720
		file_buf = stackbase;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1721
		stackbase += SPA_MAXBLOCKSIZE;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1722
		file_start = file_end = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1723
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1724
	stack = stackbase;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1725
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1726
	/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1727
	 * If offset is in memory, move it into the buffer provided and return.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1728
	 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1729
	if (filepos >= file_start && filepos+len <= file_end) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1730
		grub_memmove(buf, file_buf + filepos - file_start, len);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1731
		filepos += len;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1732
		return (len);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1733
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1734
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1735
	blksz = DNODE->dn_datablkszsec << SPA_MINBLOCKSHIFT;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1736
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1737
	/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1738
	 * Entire Dnode is too big to fit into the space available.  We
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1739
	 * will need to read it in chunks.  This could be optimized to
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1740
	 * read in as large a chunk as there is space available, but for
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1741
	 * now, this only reads in one data block at a time.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1742
	 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1743
	length = len;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1744
	while (length) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1745
		/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1746
		 * Find requested blkid and the offset within that block.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1747
		 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1748
		uint64_t blkid = filepos / blksz;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1749
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1750
		if (errnum = dmu_read(DNODE, blkid, file_buf, stack))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1751
			return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1752
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1753
		file_start = blkid * blksz;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1754
		file_end = file_start + blksz;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1755
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1756
		movesize = MIN(length, file_end - filepos);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1757
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1758
		grub_memmove(buf, file_buf + filepos - file_start,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1759
		    movesize);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1760
		buf += movesize;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1761
		length -= movesize;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1762
		filepos += movesize;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1763
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1764
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1765
	return (len);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1766
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1767
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1768
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1769
 * No-Op
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1770
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1771
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1772
zfs_embed(int *start_sector, int needed_sectors)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1773
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1774
	return (1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1775
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1776
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents:
diff changeset
  1777
#endif /* FSYS_ZFS */