1b1b8ab34Slling /*
2b1b8ab34Slling * GRUB -- GRand Unified Bootloader
3b1b8ab34Slling * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
4*1a065e93SAndrew Stormont * Copyright 2021 RackTop Systems, Inc.
5b1b8ab34Slling *
6b1b8ab34Slling * This program is free software; you can redistribute it and/or modify
7b1b8ab34Slling * it under the terms of the GNU General Public License as published by
8b1b8ab34Slling * the Free Software Foundation; either version 2 of the License, or
9b1b8ab34Slling * (at your option) any later version.
10b1b8ab34Slling *
11b1b8ab34Slling * This program is distributed in the hope that it will be useful,
12b1b8ab34Slling * but WITHOUT ANY WARRANTY; without even the implied warranty of
13b1b8ab34Slling * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14b1b8ab34Slling * GNU General Public License for more details.
15b1b8ab34Slling *
16b1b8ab34Slling * You should have received a copy of the GNU General Public License
17b1b8ab34Slling * along with this program; if not, write to the Free Software
18b1b8ab34Slling * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19b1b8ab34Slling */
20ad135b5dSChristopher Siden
21b1b8ab34Slling /*
226e1f5caaSNeil Perrin * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23b1b8ab34Slling * Use is subject to license terms.
24b1b8ab34Slling */
25b1b8ab34Slling
26ad135b5dSChristopher Siden /*
27d94527b3SDan Kimmel * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
28a6f561b4SSašo Kiselkov * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
29c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
30ad135b5dSChristopher Siden */
31ad135b5dSChristopher Siden
32b1b8ab34Slling /*
33b1b8ab34Slling * The zfs plug-in routines for GRUB are:
34b1b8ab34Slling *
35b1b8ab34Slling * zfs_mount() - locates a valid uberblock of the root pool and reads
36b1b8ab34Slling * in its MOS at the memory address MOS.
37b1b8ab34Slling *
38b1b8ab34Slling * zfs_open() - locates a plain file object by following the MOS
39b1b8ab34Slling * and places its dnode at the memory address DNODE.
40b1b8ab34Slling *
41b1b8ab34Slling * zfs_read() - read in the data blocks pointed by the DNODE.
42b1b8ab34Slling *
43b1b8ab34Slling * ZFS_SCRATCH is used as a working area.
44b1b8ab34Slling *
45b1b8ab34Slling * (memory addr) MOS DNODE ZFS_SCRATCH
46b1b8ab34Slling * | | |
47b1b8ab34Slling * +-------V---------V----------V---------------+
48b1b8ab34Slling * memory | | dnode | dnode | scratch |
49b1b8ab34Slling * | | 512B | 512B | area |
50b1b8ab34Slling * +--------------------------------------------+
51b1b8ab34Slling */
52b1b8ab34Slling
53b1b8ab34Slling #ifdef FSYS_ZFS
54b1b8ab34Slling
55b1b8ab34Slling #include "shared.h"
56b1b8ab34Slling #include "filesys.h"
57b1b8ab34Slling #include "fsys_zfs.h"
58b1b8ab34Slling
59b1b8ab34Slling /* cache for a file block of the currently zfs_open()-ed file */
60b1b8ab34Slling static void *file_buf = NULL;
61b1b8ab34Slling static uint64_t file_start = 0;
62b1b8ab34Slling static uint64_t file_end = 0;
63b1b8ab34Slling
64b1b8ab34Slling /* cache for a dnode block */
65b1b8ab34Slling static dnode_phys_t *dnode_buf = NULL;
66b1b8ab34Slling static dnode_phys_t *dnode_mdn = NULL;
67b1b8ab34Slling static uint64_t dnode_start = 0;
68b1b8ab34Slling static uint64_t dnode_end = 0;
69b1b8ab34Slling
70e23347b1SEric Taylor static uint64_t pool_guid = 0;
71051aabe6Staylor static uberblock_t current_uberblock;
72b1b8ab34Slling static char *stackbase;
73b1b8ab34Slling
74b1b8ab34Slling decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] =
75b1b8ab34Slling {
7615e6edf1Sgw {"inherit", 0}, /* ZIO_COMPRESS_INHERIT */
77b1b8ab34Slling {"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */
7815e6edf1Sgw {"off", 0}, /* ZIO_COMPRESS_OFF */
7915e6edf1Sgw {"lzjb", lzjb_decompress}, /* ZIO_COMPRESS_LZJB */
80a6f561b4SSašo Kiselkov {"empty", 0}, /* ZIO_COMPRESS_EMPTY */
81a6f561b4SSašo Kiselkov {"gzip-1", 0}, /* ZIO_COMPRESS_GZIP_1 */
82a6f561b4SSašo Kiselkov {"gzip-2", 0}, /* ZIO_COMPRESS_GZIP_2 */
83a6f561b4SSašo Kiselkov {"gzip-3", 0}, /* ZIO_COMPRESS_GZIP_3 */
84a6f561b4SSašo Kiselkov {"gzip-4", 0}, /* ZIO_COMPRESS_GZIP_4 */
85a6f561b4SSašo Kiselkov {"gzip-5", 0}, /* ZIO_COMPRESS_GZIP_5 */
86a6f561b4SSašo Kiselkov {"gzip-6", 0}, /* ZIO_COMPRESS_GZIP_6 */
87a6f561b4SSašo Kiselkov {"gzip-7", 0}, /* ZIO_COMPRESS_GZIP_7 */
88a6f561b4SSašo Kiselkov {"gzip-8", 0}, /* ZIO_COMPRESS_GZIP_8 */
89a6f561b4SSašo Kiselkov {"gzip-9", 0}, /* ZIO_COMPRESS_GZIP_9 */
90a6f561b4SSašo Kiselkov {"zle", 0}, /* ZIO_COMPRESS_ZLE */
91a6f561b4SSašo Kiselkov {"lz4", lz4_decompress} /* ZIO_COMPRESS_LZ4 */
92b1b8ab34Slling };
93b1b8ab34Slling
94cd9c78d9SLin Ling static int zio_read_data(blkptr_t *bp, void *buf, char *stack);
95cd9c78d9SLin Ling
96b1b8ab34Slling /*
97b1b8ab34Slling * Our own version of bcmp().
98b1b8ab34Slling */
99b1b8ab34Slling static int
zfs_bcmp(const void * s1,const void * s2,size_t n)100b1b8ab34Slling zfs_bcmp(const void *s1, const void *s2, size_t n)
101b1b8ab34Slling {
102b1b8ab34Slling const uchar_t *ps1 = s1;
103b1b8ab34Slling const uchar_t *ps2 = s2;
104b1b8ab34Slling
105b1b8ab34Slling if (s1 != s2 && n != 0) {
106b1b8ab34Slling do {
107b1b8ab34Slling if (*ps1++ != *ps2++)
108b1b8ab34Slling return (1);
109b1b8ab34Slling } while (--n != 0);
110b1b8ab34Slling }
111b1b8ab34Slling
112b1b8ab34Slling return (0);
113b1b8ab34Slling }
114b1b8ab34Slling
115b1b8ab34Slling /*
116b1b8ab34Slling * Our own version of log2(). Same thing as highbit()-1.
117b1b8ab34Slling */
118b1b8ab34Slling static int
zfs_log2(uint64_t num)119b1b8ab34Slling zfs_log2(uint64_t num)
120b1b8ab34Slling {
121b1b8ab34Slling int i = 0;
122b1b8ab34Slling
123b1b8ab34Slling while (num > 1) {
124b1b8ab34Slling i++;
125b1b8ab34Slling num = num >> 1;
126b1b8ab34Slling }
127b1b8ab34Slling
128b1b8ab34Slling return (i);
129b1b8ab34Slling }
130b1b8ab34Slling
131b1b8ab34Slling /* Checksum Functions */
132b1b8ab34Slling static void
zio_checksum_off(const void * buf,uint64_t size,zio_cksum_t * zcp)133b1b8ab34Slling zio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
134b1b8ab34Slling {
135b1b8ab34Slling ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
136b1b8ab34Slling }
137b1b8ab34Slling
138b1b8ab34Slling /* Checksum Table and Values */
139b1b8ab34Slling zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
140ad135b5dSChristopher Siden {{NULL, NULL}, 0, 0, "inherit"},
141ad135b5dSChristopher Siden {{NULL, NULL}, 0, 0, "on"},
142ad135b5dSChristopher Siden {{zio_checksum_off, zio_checksum_off}, 0, 0, "off"},
143ad135b5dSChristopher Siden {{zio_checksum_SHA256, zio_checksum_SHA256}, 1, 1, "label"},
144ad135b5dSChristopher Siden {{zio_checksum_SHA256, zio_checksum_SHA256}, 1, 1, "gang_header"},
145ad135b5dSChristopher Siden {{NULL, NULL}, 0, 0, "zilog"},
146ad135b5dSChristopher Siden {{fletcher_2_native, fletcher_2_byteswap}, 0, 0, "fletcher2"},
147ad135b5dSChristopher Siden {{fletcher_4_native, fletcher_4_byteswap}, 1, 0, "fletcher4"},
148ad135b5dSChristopher Siden {{zio_checksum_SHA256, zio_checksum_SHA256}, 1, 0, "SHA256"},
149ad135b5dSChristopher Siden {{NULL, NULL}, 0, 0, "zilog2"},
15045818ee1SMatthew Ahrens {{zio_checksum_off, zio_checksum_off}, 0, 0, "noparity"},
15145818ee1SMatthew Ahrens {{zio_checksum_SHA512, NULL}, 0, 0, "SHA512"}
152b1b8ab34Slling };
153b1b8ab34Slling
154b1b8ab34Slling /*
155b1b8ab34Slling * zio_checksum_verify: Provides support for checksum verification.
156b1b8ab34Slling *
15745818ee1SMatthew Ahrens * Fletcher2, Fletcher4, SHA-256 and SHA-512/256 are supported.
158b1b8ab34Slling *
159b1b8ab34Slling * Return:
160b1b8ab34Slling * -1 = Failure
161b1b8ab34Slling * 0 = Success
162b1b8ab34Slling */
163b1b8ab34Slling static int
zio_checksum_verify(blkptr_t * bp,char * data,int size)164b1b8ab34Slling zio_checksum_verify(blkptr_t *bp, char *data, int size)
165b1b8ab34Slling {
166b1b8ab34Slling zio_cksum_t zc = bp->blk_cksum;
167cd9c78d9SLin Ling uint32_t checksum = BP_GET_CHECKSUM(bp);
168b1b8ab34Slling int byteswap = BP_SHOULD_BYTESWAP(bp);
1696e1f5caaSNeil Perrin zio_eck_t *zec = (zio_eck_t *)(data + size) - 1;
170b1b8ab34Slling zio_checksum_info_t *ci = &zio_checksum_table[checksum];
171b1b8ab34Slling zio_cksum_t actual_cksum, expected_cksum;
172b1b8ab34Slling
1735d7b4d43SMatthew Ahrens if (byteswap) {
1745d7b4d43SMatthew Ahrens grub_printf("byteswap not supported\n");
175b1b8ab34Slling return (-1);
1765d7b4d43SMatthew Ahrens }
177b1b8ab34Slling
1785d7b4d43SMatthew Ahrens if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL) {
1795d7b4d43SMatthew Ahrens grub_printf("checksum algorithm %u not supported\n", checksum);
180b1b8ab34Slling return (-1);
1815d7b4d43SMatthew Ahrens }
182b1b8ab34Slling
1836e1f5caaSNeil Perrin if (ci->ci_eck) {
1846e1f5caaSNeil Perrin expected_cksum = zec->zec_cksum;
1856e1f5caaSNeil Perrin zec->zec_cksum = zc;
186cd9c78d9SLin Ling ci->ci_func[0](data, size, &actual_cksum);
1876e1f5caaSNeil Perrin zec->zec_cksum = expected_cksum;
188b1b8ab34Slling zc = expected_cksum;
189b1b8ab34Slling } else {
190b1b8ab34Slling ci->ci_func[byteswap](data, size, &actual_cksum);
191b1b8ab34Slling }
192b1b8ab34Slling
193b1b8ab34Slling if ((actual_cksum.zc_word[0] - zc.zc_word[0]) |
194b1b8ab34Slling (actual_cksum.zc_word[1] - zc.zc_word[1]) |
195b1b8ab34Slling (actual_cksum.zc_word[2] - zc.zc_word[2]) |
196b1b8ab34Slling (actual_cksum.zc_word[3] - zc.zc_word[3]))
197b1b8ab34Slling return (-1);
198b1b8ab34Slling
199b1b8ab34Slling return (0);
200b1b8ab34Slling }
201b1b8ab34Slling
202b1b8ab34Slling /*
203e23347b1SEric Taylor * vdev_label_start returns the physical disk offset (in bytes) of
204e23347b1SEric Taylor * label "l".
205b1b8ab34Slling */
206e7cbe64fSgw static uint64_t
vdev_label_start(uint64_t psize,int l)207e23347b1SEric Taylor vdev_label_start(uint64_t psize, int l)
208b1b8ab34Slling {
209e23347b1SEric Taylor return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
210b1b8ab34Slling 0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
211b1b8ab34Slling }
212b1b8ab34Slling
213b1b8ab34Slling /*
214b1b8ab34Slling * vdev_uberblock_compare takes two uberblock structures and returns an integer
215b1b8ab34Slling * indicating the more recent of the two.
216b1b8ab34Slling * Return Value = 1 if ub2 is more recent
217b1b8ab34Slling * Return Value = -1 if ub1 is more recent
218b1b8ab34Slling * The most recent uberblock is determined using its transaction number and
219b1b8ab34Slling * timestamp. The uberblock with the highest transaction number is
220b1b8ab34Slling * considered "newer". If the transaction numbers of the two blocks match, the
221b1b8ab34Slling * timestamps are compared to determine the "newer" of the two.
222b1b8ab34Slling */
223b1b8ab34Slling static int
vdev_uberblock_compare(uberblock_t * ub1,uberblock_t * ub2)224b1b8ab34Slling vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
225b1b8ab34Slling {
226b1b8ab34Slling if (ub1->ub_txg < ub2->ub_txg)
227b1b8ab34Slling return (-1);
228b1b8ab34Slling if (ub1->ub_txg > ub2->ub_txg)
229b1b8ab34Slling return (1);
230b1b8ab34Slling
231b1b8ab34Slling if (ub1->ub_timestamp < ub2->ub_timestamp)
232b1b8ab34Slling return (-1);
233b1b8ab34Slling if (ub1->ub_timestamp > ub2->ub_timestamp)
234b1b8ab34Slling return (1);
235b1b8ab34Slling
236b1b8ab34Slling return (0);
237b1b8ab34Slling }
238b1b8ab34Slling
239b1b8ab34Slling /*
240b1b8ab34Slling * Three pieces of information are needed to verify an uberblock: the magic
241b1b8ab34Slling * number, the version number, and the checksum.
242b1b8ab34Slling *
243b1b8ab34Slling * Return:
244b1b8ab34Slling * 0 - Success
245b1b8ab34Slling * -1 - Failure
246b1b8ab34Slling */
247b1b8ab34Slling static int
uberblock_verify(uberblock_t * uber,uint64_t ub_size,uint64_t offset)24881b2d573SHans Rosenfeld uberblock_verify(uberblock_t *uber, uint64_t ub_size, uint64_t offset)
249b1b8ab34Slling {
250b1b8ab34Slling blkptr_t bp;
251b1b8ab34Slling
252b1b8ab34Slling BP_ZERO(&bp);
253b1b8ab34Slling BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
254b1b8ab34Slling BP_SET_BYTEORDER(&bp, ZFS_HOST_BYTEORDER);
255b1b8ab34Slling ZIO_SET_CHECKSUM(&bp.blk_cksum, offset, 0, 0, 0);
256b1b8ab34Slling
25781b2d573SHans Rosenfeld if (zio_checksum_verify(&bp, (char *)uber, ub_size) != 0)
258b1b8ab34Slling return (-1);
259b1b8ab34Slling
260b1b8ab34Slling if (uber->ub_magic == UBERBLOCK_MAGIC &&
261ad135b5dSChristopher Siden SPA_VERSION_IS_SUPPORTED(uber->ub_version))
262b1b8ab34Slling return (0);
263b1b8ab34Slling
264b1b8ab34Slling return (-1);
265b1b8ab34Slling }
266b1b8ab34Slling
267b1b8ab34Slling /*
268b1b8ab34Slling * Find the best uberblock.
269b1b8ab34Slling * Return:
270b1b8ab34Slling * Success - Pointer to the best uberblock.
271b1b8ab34Slling * Failure - NULL
272b1b8ab34Slling */
27381b2d573SHans Rosenfeld static uberblock_t *
find_bestub(char * ub_array,uint64_t ashift,uint64_t sector)27481b2d573SHans Rosenfeld find_bestub(char *ub_array, uint64_t ashift, uint64_t sector)
275b1b8ab34Slling {
27681b2d573SHans Rosenfeld uberblock_t *ubbest = NULL;
27781b2d573SHans Rosenfeld uberblock_t *ubnext;
27881b2d573SHans Rosenfeld uint64_t offset, ub_size;
279e23347b1SEric Taylor int i;
280b1b8ab34Slling
28181b2d573SHans Rosenfeld ub_size = VDEV_UBERBLOCK_SIZE(ashift);
28281b2d573SHans Rosenfeld
28381b2d573SHans Rosenfeld for (i = 0; i < VDEV_UBERBLOCK_COUNT(ashift); i++) {
28481b2d573SHans Rosenfeld ubnext = (uberblock_t *)ub_array;
28581b2d573SHans Rosenfeld ub_array += ub_size;
286e23347b1SEric Taylor offset = (sector << SPA_MINBLOCKSHIFT) +
28781b2d573SHans Rosenfeld VDEV_UBERBLOCK_OFFSET(ashift, i);
28881b2d573SHans Rosenfeld
28981b2d573SHans Rosenfeld if (uberblock_verify(ubnext, ub_size, offset) != 0)
29081b2d573SHans Rosenfeld continue;
29181b2d573SHans Rosenfeld
29281b2d573SHans Rosenfeld if (ubbest == NULL ||
29381b2d573SHans Rosenfeld vdev_uberblock_compare(ubnext, ubbest) > 0)
29481b2d573SHans Rosenfeld ubbest = ubnext;
295b1b8ab34Slling }
296b1b8ab34Slling
297b1b8ab34Slling return (ubbest);
298b1b8ab34Slling }
299b1b8ab34Slling
300b1b8ab34Slling /*
301cd9c78d9SLin Ling * Read a block of data based on the gang block address dva,
302cd9c78d9SLin Ling * and put its data in buf.
303b1b8ab34Slling *
304b1b8ab34Slling * Return:
305b1b8ab34Slling * 0 - success
306cd9c78d9SLin Ling * 1 - failure
307b1b8ab34Slling */
308b1b8ab34Slling static int
zio_read_gang(blkptr_t * bp,dva_t * dva,void * buf,char * stack)309cd9c78d9SLin Ling zio_read_gang(blkptr_t *bp, dva_t *dva, void *buf, char *stack)
310b1b8ab34Slling {
311cd9c78d9SLin Ling zio_gbh_phys_t *zio_gb;
312b1b8ab34Slling uint64_t offset, sector;
313cd9c78d9SLin Ling blkptr_t tmpbp;
314cd9c78d9SLin Ling int i;
315b1b8ab34Slling
316cd9c78d9SLin Ling zio_gb = (zio_gbh_phys_t *)stack;
317cd9c78d9SLin Ling stack += SPA_GANGBLOCKSIZE;
318cd9c78d9SLin Ling offset = DVA_GET_OFFSET(dva);
319ad135b5dSChristopher Siden sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
320b1b8ab34Slling
321cd9c78d9SLin Ling /* read in the gang block header */
322cd9c78d9SLin Ling if (devread(sector, 0, SPA_GANGBLOCKSIZE, (char *)zio_gb) == 0) {
323cd9c78d9SLin Ling grub_printf("failed to read in a gang block header\n");
324cd9c78d9SLin Ling return (1);
325cd9c78d9SLin Ling }
326cd9c78d9SLin Ling
327cd9c78d9SLin Ling /* self checksuming the gang block header */
328cd9c78d9SLin Ling BP_ZERO(&tmpbp);
329cd9c78d9SLin Ling BP_SET_CHECKSUM(&tmpbp, ZIO_CHECKSUM_GANG_HEADER);
330cd9c78d9SLin Ling BP_SET_BYTEORDER(&tmpbp, ZFS_HOST_BYTEORDER);
331cd9c78d9SLin Ling ZIO_SET_CHECKSUM(&tmpbp.blk_cksum, DVA_GET_VDEV(dva),
332cd9c78d9SLin Ling DVA_GET_OFFSET(dva), bp->blk_birth, 0);
333cd9c78d9SLin Ling if (zio_checksum_verify(&tmpbp, (char *)zio_gb, SPA_GANGBLOCKSIZE)) {
334cd9c78d9SLin Ling grub_printf("failed to checksum a gang block header\n");
335cd9c78d9SLin Ling return (1);
336cd9c78d9SLin Ling }
337cd9c78d9SLin Ling
338cd9c78d9SLin Ling for (i = 0; i < SPA_GBH_NBLKPTRS; i++) {
33943466aaeSMax Grossman if (BP_IS_HOLE(&zio_gb->zg_blkptr[i]))
340cd9c78d9SLin Ling continue;
341cd9c78d9SLin Ling
342cd9c78d9SLin Ling if (zio_read_data(&zio_gb->zg_blkptr[i], buf, stack))
343cd9c78d9SLin Ling return (1);
344cd9c78d9SLin Ling buf += BP_GET_PSIZE(&zio_gb->zg_blkptr[i]);
345cd9c78d9SLin Ling }
346cd9c78d9SLin Ling
347cd9c78d9SLin Ling return (0);
348cd9c78d9SLin Ling }
349cd9c78d9SLin Ling
350cd9c78d9SLin Ling /*
351cd9c78d9SLin Ling * Read in a block of raw data to buf.
352cd9c78d9SLin Ling *
353cd9c78d9SLin Ling * Return:
354cd9c78d9SLin Ling * 0 - success
355cd9c78d9SLin Ling * 1 - failure
356cd9c78d9SLin Ling */
357cd9c78d9SLin Ling static int
zio_read_data(blkptr_t * bp,void * buf,char * stack)358cd9c78d9SLin Ling zio_read_data(blkptr_t *bp, void *buf, char *stack)
359cd9c78d9SLin Ling {
360cd9c78d9SLin Ling int i, psize;
361cd9c78d9SLin Ling
362cd9c78d9SLin Ling psize = BP_GET_PSIZE(bp);
363ae8180dbSlling
364b1b8ab34Slling /* pick a good dva from the block pointer */
365b1b8ab34Slling for (i = 0; i < SPA_DVAS_PER_BP; i++) {
366cd9c78d9SLin Ling uint64_t offset, sector;
367b1b8ab34Slling
368b1b8ab34Slling if (bp->blk_dva[i].dva_word[0] == 0 &&
369b1b8ab34Slling bp->blk_dva[i].dva_word[1] == 0)
370b1b8ab34Slling continue;
371b1b8ab34Slling
372cd9c78d9SLin Ling if (DVA_GET_GANG(&bp->blk_dva[i])) {
373d94527b3SDan Kimmel if (zio_read_gang(bp, &bp->blk_dva[i], buf, stack) != 0)
374d94527b3SDan Kimmel continue;
375b1b8ab34Slling } else {
376cd9c78d9SLin Ling /* read in a data block */
377cd9c78d9SLin Ling offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
378ad135b5dSChristopher Siden sector = DVA_OFFSET_TO_PHYS_SECTOR(offset);
379d94527b3SDan Kimmel if (devread(sector, 0, psize, buf) == 0)
380d94527b3SDan Kimmel continue;
381d94527b3SDan Kimmel }
382d94527b3SDan Kimmel
383d94527b3SDan Kimmel /* verify that the checksum matches */
384d94527b3SDan Kimmel if (zio_checksum_verify(bp, buf, psize) == 0) {
385d94527b3SDan Kimmel return (0);
386b1b8ab34Slling }
387b1b8ab34Slling }
388b1b8ab34Slling
389d94527b3SDan Kimmel grub_printf("could not read block due to EIO or ECKSUM\n");
390cd9c78d9SLin Ling return (1);
391cd9c78d9SLin Ling }
392cd9c78d9SLin Ling
3935d7b4d43SMatthew Ahrens /*
3945d7b4d43SMatthew Ahrens * buf must be at least BPE_GET_PSIZE(bp) bytes long (which will never be
3955d7b4d43SMatthew Ahrens * more than BPE_PAYLOAD_SIZE bytes).
3965d7b4d43SMatthew Ahrens */
3975d7b4d43SMatthew Ahrens static void
decode_embedded_bp_compressed(const blkptr_t * bp,void * buf)3985d7b4d43SMatthew Ahrens decode_embedded_bp_compressed(const blkptr_t *bp, void *buf)
3995d7b4d43SMatthew Ahrens {
4005d7b4d43SMatthew Ahrens int psize, i;
4015d7b4d43SMatthew Ahrens uint8_t *buf8 = buf;
4025d7b4d43SMatthew Ahrens uint64_t w = 0;
4035d7b4d43SMatthew Ahrens const uint64_t *bp64 = (const uint64_t *)bp;
4045d7b4d43SMatthew Ahrens
4055d7b4d43SMatthew Ahrens psize = BPE_GET_PSIZE(bp);
4065d7b4d43SMatthew Ahrens
4075d7b4d43SMatthew Ahrens /*
4085d7b4d43SMatthew Ahrens * Decode the words of the block pointer into the byte array.
4095d7b4d43SMatthew Ahrens * Low bits of first word are the first byte (little endian).
4105d7b4d43SMatthew Ahrens */
4115d7b4d43SMatthew Ahrens for (i = 0; i < psize; i++) {
4125d7b4d43SMatthew Ahrens if (i % sizeof (w) == 0) {
4135d7b4d43SMatthew Ahrens /* beginning of a word */
4145d7b4d43SMatthew Ahrens w = *bp64;
4155d7b4d43SMatthew Ahrens bp64++;
4165d7b4d43SMatthew Ahrens if (!BPE_IS_PAYLOADWORD(bp, bp64))
4175d7b4d43SMatthew Ahrens bp64++;
4185d7b4d43SMatthew Ahrens }
4195d7b4d43SMatthew Ahrens buf8[i] = BF64_GET(w, (i % sizeof (w)) * NBBY, NBBY);
4205d7b4d43SMatthew Ahrens }
4215d7b4d43SMatthew Ahrens }
4225d7b4d43SMatthew Ahrens
4235d7b4d43SMatthew Ahrens /*
4245d7b4d43SMatthew Ahrens * Fill in the buffer with the (decompressed) payload of the embedded
4255d7b4d43SMatthew Ahrens * blkptr_t. Takes into account compression and byteorder (the payload is
4265d7b4d43SMatthew Ahrens * treated as a stream of bytes).
4275d7b4d43SMatthew Ahrens * Return 0 on success, or ENOSPC if it won't fit in the buffer.
4285d7b4d43SMatthew Ahrens */
4295d7b4d43SMatthew Ahrens static int
decode_embedded_bp(const blkptr_t * bp,void * buf)4305d7b4d43SMatthew Ahrens decode_embedded_bp(const blkptr_t *bp, void *buf)
4315d7b4d43SMatthew Ahrens {
4325d7b4d43SMatthew Ahrens int comp;
4335d7b4d43SMatthew Ahrens int lsize, psize;
4345d7b4d43SMatthew Ahrens uint8_t *dst = buf;
4355d7b4d43SMatthew Ahrens uint64_t w = 0;
4365d7b4d43SMatthew Ahrens
4375d7b4d43SMatthew Ahrens lsize = BPE_GET_LSIZE(bp);
4385d7b4d43SMatthew Ahrens psize = BPE_GET_PSIZE(bp);
4395d7b4d43SMatthew Ahrens comp = BP_GET_COMPRESS(bp);
4405d7b4d43SMatthew Ahrens
4415d7b4d43SMatthew Ahrens if (comp != ZIO_COMPRESS_OFF) {
4425d7b4d43SMatthew Ahrens uint8_t dstbuf[BPE_PAYLOAD_SIZE];
4435d7b4d43SMatthew Ahrens
4445d7b4d43SMatthew Ahrens if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
4455d7b4d43SMatthew Ahrens decomp_table[comp].decomp_func == NULL) {
4465d7b4d43SMatthew Ahrens grub_printf("compression algorithm not supported\n");
4475d7b4d43SMatthew Ahrens return (ERR_FSYS_CORRUPT);
4485d7b4d43SMatthew Ahrens }
4495d7b4d43SMatthew Ahrens
4505d7b4d43SMatthew Ahrens decode_embedded_bp_compressed(bp, dstbuf);
4515d7b4d43SMatthew Ahrens decomp_table[comp].decomp_func(dstbuf, buf, psize, lsize);
4525d7b4d43SMatthew Ahrens } else {
4535d7b4d43SMatthew Ahrens decode_embedded_bp_compressed(bp, buf);
4545d7b4d43SMatthew Ahrens }
4555d7b4d43SMatthew Ahrens
4565d7b4d43SMatthew Ahrens return (0);
4575d7b4d43SMatthew Ahrens }
4585d7b4d43SMatthew Ahrens
459cd9c78d9SLin Ling /*
460cd9c78d9SLin Ling * Read in a block of data, verify its checksum, decompress if needed,
461cd9c78d9SLin Ling * and put the uncompressed data in buf.
462cd9c78d9SLin Ling *
463cd9c78d9SLin Ling * Return:
464cd9c78d9SLin Ling * 0 - success
465cd9c78d9SLin Ling * errnum - failure
466cd9c78d9SLin Ling */
467cd9c78d9SLin Ling static int
zio_read(blkptr_t * bp,void * buf,char * stack)468cd9c78d9SLin Ling zio_read(blkptr_t *bp, void *buf, char *stack)
469cd9c78d9SLin Ling {
470cd9c78d9SLin Ling int lsize, psize, comp;
471cd9c78d9SLin Ling char *retbuf;
472cd9c78d9SLin Ling
4735d7b4d43SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) {
4745d7b4d43SMatthew Ahrens if (BPE_GET_ETYPE(bp) != BP_EMBEDDED_TYPE_DATA) {
4755d7b4d43SMatthew Ahrens grub_printf("unsupported embedded BP (type=%u)\n",
4765d7b4d43SMatthew Ahrens (int)BPE_GET_ETYPE(bp));
4775d7b4d43SMatthew Ahrens return (ERR_FSYS_CORRUPT);
4785d7b4d43SMatthew Ahrens }
4795d7b4d43SMatthew Ahrens return (decode_embedded_bp(bp, buf));
4805d7b4d43SMatthew Ahrens }
4815d7b4d43SMatthew Ahrens
482cd9c78d9SLin Ling comp = BP_GET_COMPRESS(bp);
483cd9c78d9SLin Ling lsize = BP_GET_LSIZE(bp);
484cd9c78d9SLin Ling psize = BP_GET_PSIZE(bp);
485cd9c78d9SLin Ling
486cd9c78d9SLin Ling if ((unsigned int)comp >= ZIO_COMPRESS_FUNCTIONS ||
487cd9c78d9SLin Ling (comp != ZIO_COMPRESS_OFF &&
488cd9c78d9SLin Ling decomp_table[comp].decomp_func == NULL)) {
489cd9c78d9SLin Ling grub_printf("compression algorithm not supported\n");
490cd9c78d9SLin Ling return (ERR_FSYS_CORRUPT);
491cd9c78d9SLin Ling }
492cd9c78d9SLin Ling
493cd9c78d9SLin Ling if ((char *)buf < stack && ((char *)buf) + lsize > stack) {
4945d7b4d43SMatthew Ahrens grub_printf("not enough memory to fit %u bytes on stack\n",
4955d7b4d43SMatthew Ahrens lsize);
496cd9c78d9SLin Ling return (ERR_WONT_FIT);
497cd9c78d9SLin Ling }
498cd9c78d9SLin Ling
499cd9c78d9SLin Ling retbuf = buf;
500cd9c78d9SLin Ling if (comp != ZIO_COMPRESS_OFF) {
501cd9c78d9SLin Ling buf = stack;
502cd9c78d9SLin Ling stack += psize;
503cd9c78d9SLin Ling }
504cd9c78d9SLin Ling
505ad135b5dSChristopher Siden if (zio_read_data(bp, buf, stack) != 0) {
506cd9c78d9SLin Ling grub_printf("zio_read_data failed\n");
507cd9c78d9SLin Ling return (ERR_FSYS_CORRUPT);
508cd9c78d9SLin Ling }
509cd9c78d9SLin Ling
510a6f561b4SSašo Kiselkov if (comp != ZIO_COMPRESS_OFF) {
511a6f561b4SSašo Kiselkov if (decomp_table[comp].decomp_func(buf, retbuf, psize,
512a6f561b4SSašo Kiselkov lsize) != 0) {
513a6f561b4SSašo Kiselkov grub_printf("zio_read decompression failed\n");
514a6f561b4SSašo Kiselkov return (ERR_FSYS_CORRUPT);
515a6f561b4SSašo Kiselkov }
516a6f561b4SSašo Kiselkov }
517cd9c78d9SLin Ling
518cd9c78d9SLin Ling return (0);
519b1b8ab34Slling }
520b1b8ab34Slling
521b1b8ab34Slling /*
522b1b8ab34Slling * Get the block from a block id.
523b1b8ab34Slling * push the block onto the stack.
524b1b8ab34Slling *
525b1b8ab34Slling * Return:
526b1b8ab34Slling * 0 - success
527b1b8ab34Slling * errnum - failure
528b1b8ab34Slling */
529b1b8ab34Slling static int
dmu_read(dnode_phys_t * dn,uint64_t blkid,void * buf,char * stack)530b1b8ab34Slling dmu_read(dnode_phys_t *dn, uint64_t blkid, void *buf, char *stack)
531b1b8ab34Slling {
532b1b8ab34Slling int idx, level;
533b1b8ab34Slling blkptr_t *bp_array = dn->dn_blkptr;
534b1b8ab34Slling int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
535b1b8ab34Slling blkptr_t *bp, *tmpbuf;
536b1b8ab34Slling
537b1b8ab34Slling bp = (blkptr_t *)stack;
538b1b8ab34Slling stack += sizeof (blkptr_t);
539b1b8ab34Slling
540b1b8ab34Slling tmpbuf = (blkptr_t *)stack;
541b1b8ab34Slling stack += 1<<dn->dn_indblkshift;
542b1b8ab34Slling
543b1b8ab34Slling for (level = dn->dn_nlevels - 1; level >= 0; level--) {
544b1b8ab34Slling idx = (blkid >> (epbs * level)) & ((1<<epbs)-1);
545b1b8ab34Slling *bp = bp_array[idx];
546b1b8ab34Slling if (level == 0)
547b1b8ab34Slling tmpbuf = buf;
548ae8180dbSlling if (BP_IS_HOLE(bp)) {
549ae8180dbSlling grub_memset(buf, 0,
550ae8180dbSlling dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
551ae8180dbSlling break;
552ae8180dbSlling } else if (errnum = zio_read(bp, tmpbuf, stack)) {
553b1b8ab34Slling return (errnum);
554ae8180dbSlling }
555b1b8ab34Slling
556b1b8ab34Slling bp_array = tmpbuf;
557b1b8ab34Slling }
558b1b8ab34Slling
559b1b8ab34Slling return (0);
560b1b8ab34Slling }
561b1b8ab34Slling
562b1b8ab34Slling /*
563b1b8ab34Slling * mzap_lookup: Looks up property described by "name" and returns the value
564b1b8ab34Slling * in "value".
565b1b8ab34Slling *
566b1b8ab34Slling * Return:
567b1b8ab34Slling * 0 - success
568b1b8ab34Slling * errnum - failure
569b1b8ab34Slling */
570b1b8ab34Slling static int
mzap_lookup(mzap_phys_t * zapobj,int objsize,const char * name,uint64_t * value)571ad135b5dSChristopher Siden mzap_lookup(mzap_phys_t *zapobj, int objsize, const char *name,
5729a686fbcSPaul Dagnelie uint64_t *value)
573b1b8ab34Slling {
574b1b8ab34Slling int i, chunks;
575b1b8ab34Slling mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk;
576b1b8ab34Slling
577ad135b5dSChristopher Siden chunks = objsize / MZAP_ENT_LEN - 1;
578b1b8ab34Slling for (i = 0; i < chunks; i++) {
579b1b8ab34Slling if (grub_strcmp(mzap_ent[i].mze_name, name) == 0) {
580b1b8ab34Slling *value = mzap_ent[i].mze_value;
581b1b8ab34Slling return (0);
582b1b8ab34Slling }
583b1b8ab34Slling }
584b1b8ab34Slling
585b1b8ab34Slling return (ERR_FSYS_CORRUPT);
586b1b8ab34Slling }
587b1b8ab34Slling
588b1b8ab34Slling static uint64_t
zap_hash(uint64_t salt,const char * name)589b1b8ab34Slling zap_hash(uint64_t salt, const char *name)
590b1b8ab34Slling {
591b1b8ab34Slling static uint64_t table[256];
592b1b8ab34Slling const uint8_t *cp;
593b1b8ab34Slling uint8_t c;
594b1b8ab34Slling uint64_t crc = salt;
595b1b8ab34Slling
596b1b8ab34Slling if (table[128] == 0) {
597b1b8ab34Slling uint64_t *ct;
598b1b8ab34Slling int i, j;
599b1b8ab34Slling for (i = 0; i < 256; i++) {
600b1b8ab34Slling for (ct = table + i, *ct = i, j = 8; j > 0; j--)
601b1b8ab34Slling *ct = (*ct >> 1) ^ (-(*ct & 1) &
602b1b8ab34Slling ZFS_CRC64_POLY);
603b1b8ab34Slling }
604b1b8ab34Slling }
605b1b8ab34Slling
606b1b8ab34Slling if (crc == 0 || table[128] != ZFS_CRC64_POLY) {
607b1b8ab34Slling errnum = ERR_FSYS_CORRUPT;
608b1b8ab34Slling return (0);
609b1b8ab34Slling }
610b1b8ab34Slling
611b1b8ab34Slling for (cp = (const uint8_t *)name; (c = *cp) != '\0'; cp++)
612b1b8ab34Slling crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF];
613b1b8ab34Slling
614b1b8ab34Slling /*
615b1b8ab34Slling * Only use 28 bits, since we need 4 bits in the cookie for the
616b1b8ab34Slling * collision differentiator. We MUST use the high bits, since
617ad135b5dSChristopher Siden * those are the ones that we first pay attention to when
618ad135b5dSChristopher Siden * choosing the bucket.
619b1b8ab34Slling */
620b24ab676SJeff Bonwick crc &= ~((1ULL << (64 - 28)) - 1);
621b1b8ab34Slling
622b1b8ab34Slling return (crc);
623b1b8ab34Slling }
624b1b8ab34Slling
625b1b8ab34Slling /*
626b1b8ab34Slling * Only to be used on 8-bit arrays.
627b1b8ab34Slling * array_len is actual len in bytes (not encoded le_value_length).
628b1b8ab34Slling * buf is null-terminated.
629b1b8ab34Slling */
630b1b8ab34Slling static int
zap_leaf_array_equal(zap_leaf_phys_t * l,int blksft,int chunk,int array_len,const char * buf)631b1b8ab34Slling zap_leaf_array_equal(zap_leaf_phys_t *l, int blksft, int chunk,
632b1b8ab34Slling int array_len, const char *buf)
633b1b8ab34Slling {
634b1b8ab34Slling int bseen = 0;
635b1b8ab34Slling
636b1b8ab34Slling while (bseen < array_len) {
637b1b8ab34Slling struct zap_leaf_array *la =
638b1b8ab34Slling &ZAP_LEAF_CHUNK(l, blksft, chunk).l_array;
639b1b8ab34Slling int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES);
640b1b8ab34Slling
641b1b8ab34Slling if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
642b1b8ab34Slling return (0);
643b1b8ab34Slling
644b1b8ab34Slling if (zfs_bcmp(la->la_array, buf + bseen, toread) != 0)
645b1b8ab34Slling break;
646b1b8ab34Slling chunk = la->la_next;
647b1b8ab34Slling bseen += toread;
648b1b8ab34Slling }
649b1b8ab34Slling return (bseen == array_len);
650b1b8ab34Slling }
651b1b8ab34Slling
652b1b8ab34Slling /*
653b1b8ab34Slling * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the
654b1b8ab34Slling * value for the property "name".
655b1b8ab34Slling *
656b1b8ab34Slling * Return:
657b1b8ab34Slling * 0 - success
658b1b8ab34Slling * errnum - failure
659b1b8ab34Slling */
660e7cbe64fSgw static int
zap_leaf_lookup(zap_leaf_phys_t * l,int blksft,uint64_t h,const char * name,uint64_t * value)661b1b8ab34Slling zap_leaf_lookup(zap_leaf_phys_t *l, int blksft, uint64_t h,
662b1b8ab34Slling const char *name, uint64_t *value)
663b1b8ab34Slling {
664b1b8ab34Slling uint16_t chunk;
665b1b8ab34Slling struct zap_leaf_entry *le;
666b1b8ab34Slling
667b1b8ab34Slling /* Verify if this is a valid leaf block */
668b1b8ab34Slling if (l->l_hdr.lh_block_type != ZBT_LEAF)
669b1b8ab34Slling return (ERR_FSYS_CORRUPT);
670b1b8ab34Slling if (l->l_hdr.lh_magic != ZAP_LEAF_MAGIC)
671b1b8ab34Slling return (ERR_FSYS_CORRUPT);
672b1b8ab34Slling
673b1b8ab34Slling for (chunk = l->l_hash[LEAF_HASH(blksft, h)];
674b1b8ab34Slling chunk != CHAIN_END; chunk = le->le_next) {
675b1b8ab34Slling
676b1b8ab34Slling if (chunk >= ZAP_LEAF_NUMCHUNKS(blksft))
677b1b8ab34Slling return (ERR_FSYS_CORRUPT);
678b1b8ab34Slling
679b1b8ab34Slling le = ZAP_LEAF_ENTRY(l, blksft, chunk);
680b1b8ab34Slling
681b1b8ab34Slling /* Verify the chunk entry */
682b1b8ab34Slling if (le->le_type != ZAP_CHUNK_ENTRY)
683b1b8ab34Slling return (ERR_FSYS_CORRUPT);
684b1b8ab34Slling
685b1b8ab34Slling if (le->le_hash != h)
686b1b8ab34Slling continue;
687b1b8ab34Slling
688b1b8ab34Slling if (zap_leaf_array_equal(l, blksft, le->le_name_chunk,
689b1b8ab34Slling le->le_name_length, name)) {
690b1b8ab34Slling
691b1b8ab34Slling struct zap_leaf_array *la;
692b1b8ab34Slling uint8_t *ip;
693b1b8ab34Slling
694b1b8ab34Slling if (le->le_int_size != 8 || le->le_value_length != 1)
695e37b211cStaylor return (ERR_FSYS_CORRUPT);
696b1b8ab34Slling
697b1b8ab34Slling /* get the uint64_t property value */
698b1b8ab34Slling la = &ZAP_LEAF_CHUNK(l, blksft,
699b1b8ab34Slling le->le_value_chunk).l_array;
700b1b8ab34Slling ip = la->la_array;
701b1b8ab34Slling
702b1b8ab34Slling *value = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 |
703b1b8ab34Slling (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 |
704b1b8ab34Slling (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 |
705b1b8ab34Slling (uint64_t)ip[6] << 8 | (uint64_t)ip[7];
706b1b8ab34Slling
707b1b8ab34Slling return (0);
708b1b8ab34Slling }
709b1b8ab34Slling }
710b1b8ab34Slling
711b1b8ab34Slling return (ERR_FSYS_CORRUPT);
712b1b8ab34Slling }
713b1b8ab34Slling
714b1b8ab34Slling /*
715b1b8ab34Slling * Fat ZAP lookup
716b1b8ab34Slling *
717b1b8ab34Slling * Return:
718b1b8ab34Slling * 0 - success
719b1b8ab34Slling * errnum - failure
720b1b8ab34Slling */
721e7cbe64fSgw static int
fzap_lookup(dnode_phys_t * zap_dnode,zap_phys_t * zap,const char * name,uint64_t * value,char * stack)722b1b8ab34Slling fzap_lookup(dnode_phys_t *zap_dnode, zap_phys_t *zap,
723ad135b5dSChristopher Siden const char *name, uint64_t *value, char *stack)
724b1b8ab34Slling {
725b1b8ab34Slling zap_leaf_phys_t *l;
726b1b8ab34Slling uint64_t hash, idx, blkid;
727b1b8ab34Slling int blksft = zfs_log2(zap_dnode->dn_datablkszsec << DNODE_SHIFT);
728b1b8ab34Slling
729b1b8ab34Slling /* Verify if this is a fat zap header block */
730b24ab676SJeff Bonwick if (zap->zap_magic != (uint64_t)ZAP_MAGIC ||
731b24ab676SJeff Bonwick zap->zap_flags != 0)
732b1b8ab34Slling return (ERR_FSYS_CORRUPT);
733b1b8ab34Slling
734b1b8ab34Slling hash = zap_hash(zap->zap_salt, name);
735b1b8ab34Slling if (errnum)
736b1b8ab34Slling return (errnum);
737b1b8ab34Slling
738b1b8ab34Slling /* get block id from index */
739b1b8ab34Slling if (zap->zap_ptrtbl.zt_numblks != 0) {
740b1b8ab34Slling /* external pointer tables not supported */
741b1b8ab34Slling return (ERR_FSYS_CORRUPT);
742b1b8ab34Slling }
743b1b8ab34Slling idx = ZAP_HASH_IDX(hash, zap->zap_ptrtbl.zt_shift);
744b1b8ab34Slling blkid = ((uint64_t *)zap)[idx + (1<<(blksft-3-1))];
745b1b8ab34Slling
746b1b8ab34Slling /* Get the leaf block */
747b1b8ab34Slling l = (zap_leaf_phys_t *)stack;
748b1b8ab34Slling stack += 1<<blksft;
749051aabe6Staylor if ((1<<blksft) < sizeof (zap_leaf_phys_t))
750e37b211cStaylor return (ERR_FSYS_CORRUPT);
751b1b8ab34Slling if (errnum = dmu_read(zap_dnode, blkid, l, stack))
752b1b8ab34Slling return (errnum);
753b1b8ab34Slling
754b1b8ab34Slling return (zap_leaf_lookup(l, blksft, hash, name, value));
755b1b8ab34Slling }
756b1b8ab34Slling
757b1b8ab34Slling /*
758b1b8ab34Slling * Read in the data of a zap object and find the value for a matching
759b1b8ab34Slling * property name.
760b1b8ab34Slling *
761b1b8ab34Slling * Return:
762b1b8ab34Slling * 0 - success
763b1b8ab34Slling * errnum - failure
764b1b8ab34Slling */
765b1b8ab34Slling static int
zap_lookup(dnode_phys_t * zap_dnode,const char * name,uint64_t * val,char * stack)766ad135b5dSChristopher Siden zap_lookup(dnode_phys_t *zap_dnode, const char *name, uint64_t *val,
767ad135b5dSChristopher Siden char *stack)
768b1b8ab34Slling {
769b1b8ab34Slling uint64_t block_type;
770b1b8ab34Slling int size;
771b1b8ab34Slling void *zapbuf;
772b1b8ab34Slling
773b1b8ab34Slling /* Read in the first block of the zap object data. */
774b1b8ab34Slling zapbuf = stack;
775b1b8ab34Slling size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
776b1b8ab34Slling stack += size;
7770a586ceaSMark Shellenbaum
778ad135b5dSChristopher Siden if ((errnum = dmu_read(zap_dnode, 0, zapbuf, stack)) != 0)
779b1b8ab34Slling return (errnum);
780b1b8ab34Slling
781b1b8ab34Slling block_type = *((uint64_t *)zapbuf);
782b1b8ab34Slling
783b1b8ab34Slling if (block_type == ZBT_MICRO) {
784b1b8ab34Slling return (mzap_lookup(zapbuf, size, name, val));
785b1b8ab34Slling } else if (block_type == ZBT_HEADER) {
786b1b8ab34Slling /* this is a fat zap */
787b1b8ab34Slling return (fzap_lookup(zap_dnode, zapbuf, name,
788b1b8ab34Slling val, stack));
789b1b8ab34Slling }
790b1b8ab34Slling
791b1b8ab34Slling return (ERR_FSYS_CORRUPT);
792b1b8ab34Slling }
793b1b8ab34Slling
794ad135b5dSChristopher Siden typedef struct zap_attribute {
795ad135b5dSChristopher Siden int za_integer_length;
796ad135b5dSChristopher Siden uint64_t za_num_integers;
797ad135b5dSChristopher Siden uint64_t za_first_integer;
798ad135b5dSChristopher Siden char *za_name;
799ad135b5dSChristopher Siden } zap_attribute_t;
800ad135b5dSChristopher Siden
801ad135b5dSChristopher Siden typedef int (zap_cb_t)(zap_attribute_t *za, void *arg, char *stack);
802ad135b5dSChristopher Siden
803ad135b5dSChristopher Siden static int
zap_iterate(dnode_phys_t * zap_dnode,zap_cb_t * cb,void * arg,char * stack)804ad135b5dSChristopher Siden zap_iterate(dnode_phys_t *zap_dnode, zap_cb_t *cb, void *arg, char *stack)
805ad135b5dSChristopher Siden {
806ad135b5dSChristopher Siden uint32_t size = zap_dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT;
807ad135b5dSChristopher Siden zap_attribute_t za;
808ad135b5dSChristopher Siden int i;
809ad135b5dSChristopher Siden mzap_phys_t *mzp = (mzap_phys_t *)stack;
810ad135b5dSChristopher Siden stack += size;
811ad135b5dSChristopher Siden
812ad135b5dSChristopher Siden if ((errnum = dmu_read(zap_dnode, 0, mzp, stack)) != 0)
813ad135b5dSChristopher Siden return (errnum);
814ad135b5dSChristopher Siden
815ad135b5dSChristopher Siden /*
816ad135b5dSChristopher Siden * Iteration over fatzap objects has not yet been implemented.
817ad135b5dSChristopher Siden * If we encounter a pool in which there are more features for
818ad135b5dSChristopher Siden * read than can fit inside a microzap (i.e., more than 2048
819ad135b5dSChristopher Siden * features for read), we can add support for fatzap iteration.
820ad135b5dSChristopher Siden * For now, fail.
821ad135b5dSChristopher Siden */
822ad135b5dSChristopher Siden if (mzp->mz_block_type != ZBT_MICRO) {
823ad135b5dSChristopher Siden grub_printf("feature information stored in fatzap, pool "
824ad135b5dSChristopher Siden "version not supported\n");
825ad135b5dSChristopher Siden return (1);
826ad135b5dSChristopher Siden }
827ad135b5dSChristopher Siden
828ad135b5dSChristopher Siden za.za_integer_length = 8;
829ad135b5dSChristopher Siden za.za_num_integers = 1;
830ad135b5dSChristopher Siden for (i = 0; i < size / MZAP_ENT_LEN - 1; i++) {
831ad135b5dSChristopher Siden mzap_ent_phys_t *mzep = &mzp->mz_chunk[i];
832ad135b5dSChristopher Siden int err;
833ad135b5dSChristopher Siden
834ad135b5dSChristopher Siden za.za_first_integer = mzep->mze_value;
835ad135b5dSChristopher Siden za.za_name = mzep->mze_name;
836ad135b5dSChristopher Siden err = cb(&za, arg, stack);
837ad135b5dSChristopher Siden if (err != 0)
838ad135b5dSChristopher Siden return (err);
839ad135b5dSChristopher Siden }
840ad135b5dSChristopher Siden
841ad135b5dSChristopher Siden return (0);
842ad135b5dSChristopher Siden }
843ad135b5dSChristopher Siden
844b1b8ab34Slling /*
845b1b8ab34Slling * Get the dnode of an object number from the metadnode of an object set.
846b1b8ab34Slling *
847b1b8ab34Slling * Input
848b1b8ab34Slling * mdn - metadnode to get the object dnode
849b1b8ab34Slling * objnum - object number for the object dnode
8505d7b4d43SMatthew Ahrens * type - if nonzero, object must be of this type
851b1b8ab34Slling * buf - data buffer that holds the returning dnode
852b1b8ab34Slling * stack - scratch area
853b1b8ab34Slling *
854b1b8ab34Slling * Return:
855b1b8ab34Slling * 0 - success
856b1b8ab34Slling * errnum - failure
857b1b8ab34Slling */
858b1b8ab34Slling static int
dnode_get(dnode_phys_t * mdn,uint64_t objnum,uint8_t type,dnode_phys_t * buf,char * stack)859b1b8ab34Slling dnode_get(dnode_phys_t *mdn, uint64_t objnum, uint8_t type, dnode_phys_t *buf,
8609a686fbcSPaul Dagnelie char *stack)
861b1b8ab34Slling {
862b1b8ab34Slling uint64_t blkid, blksz; /* the block id this object dnode is in */
863b1b8ab34Slling int epbs; /* shift of number of dnodes in a block */
864b1b8ab34Slling int idx; /* index within a block */
865b1b8ab34Slling dnode_phys_t *dnbuf;
866b1b8ab34Slling
867b1b8ab34Slling blksz = mdn->dn_datablkszsec << SPA_MINBLOCKSHIFT;
868b1b8ab34Slling epbs = zfs_log2(blksz) - DNODE_SHIFT;
869b1b8ab34Slling blkid = objnum >> epbs;
870b1b8ab34Slling idx = objnum & ((1<<epbs)-1);
871b1b8ab34Slling
872b1b8ab34Slling if (dnode_buf != NULL && dnode_mdn == mdn &&
873b1b8ab34Slling objnum >= dnode_start && objnum < dnode_end) {
874b1b8ab34Slling grub_memmove(buf, &dnode_buf[idx], DNODE_SIZE);
875b1b8ab34Slling VERIFY_DN_TYPE(buf, type);
876b1b8ab34Slling return (0);
877b1b8ab34Slling }
878b1b8ab34Slling
879b1b8ab34Slling if (dnode_buf && blksz == 1<<DNODE_BLOCK_SHIFT) {
880b1b8ab34Slling dnbuf = dnode_buf;
881b1b8ab34Slling dnode_mdn = mdn;
882b1b8ab34Slling dnode_start = blkid << epbs;
883b1b8ab34Slling dnode_end = (blkid + 1) << epbs;
884b1b8ab34Slling } else {
885b1b8ab34Slling dnbuf = (dnode_phys_t *)stack;
886b1b8ab34Slling stack += blksz;
887b1b8ab34Slling }
888b1b8ab34Slling
889b1b8ab34Slling if (errnum = dmu_read(mdn, blkid, (char *)dnbuf, stack))
890b1b8ab34Slling return (errnum);
891b1b8ab34Slling
892b1b8ab34Slling grub_memmove(buf, &dnbuf[idx], DNODE_SIZE);
893b1b8ab34Slling VERIFY_DN_TYPE(buf, type);
894b1b8ab34Slling
895b1b8ab34Slling return (0);
896b1b8ab34Slling }
897b1b8ab34Slling
898b1b8ab34Slling /*
899eb2bd662Svikram * Check if this is a special file that resides at the top
900eb2bd662Svikram * dataset of the pool. Currently this is the GRUB menu,
901eb2bd662Svikram * boot signature and boot signature backup.
902b1b8ab34Slling * str starts with '/'.
903b1b8ab34Slling */
904b1b8ab34Slling static int
is_top_dataset_file(char * str)905eb2bd662Svikram is_top_dataset_file(char *str)
906b1b8ab34Slling {
907b1b8ab34Slling char *tptr;
908b1b8ab34Slling
909b1b8ab34Slling if ((tptr = grub_strstr(str, "menu.lst")) &&
910b1b8ab34Slling (tptr[8] == '\0' || tptr[8] == ' ') &&
911b1b8ab34Slling *(tptr-1) == '/')
912b1b8ab34Slling return (1);
913b1b8ab34Slling
914eb2bd662Svikram if (grub_strncmp(str, BOOTSIGN_DIR"/",
9151183b401Svikram grub_strlen(BOOTSIGN_DIR) + 1) == 0)
916eb2bd662Svikram return (1);
917eb2bd662Svikram
918eb2bd662Svikram if (grub_strcmp(str, BOOTSIGN_BACKUP) == 0)
919eb2bd662Svikram return (1);
920eb2bd662Svikram
921b1b8ab34Slling return (0);
922b1b8ab34Slling }
923b1b8ab34Slling
924ad135b5dSChristopher Siden static int
check_feature(zap_attribute_t * za,void * arg,char * stack)925ad135b5dSChristopher Siden check_feature(zap_attribute_t *za, void *arg, char *stack)
926ad135b5dSChristopher Siden {
927ad135b5dSChristopher Siden const char **names = arg;
928ad135b5dSChristopher Siden int i;
929ad135b5dSChristopher Siden
930ad135b5dSChristopher Siden if (za->za_first_integer == 0)
931ad135b5dSChristopher Siden return (0);
932ad135b5dSChristopher Siden
933ad135b5dSChristopher Siden for (i = 0; names[i] != NULL; i++) {
934ad135b5dSChristopher Siden if (grub_strcmp(za->za_name, names[i]) == 0) {
935ad135b5dSChristopher Siden return (0);
936ad135b5dSChristopher Siden }
937ad135b5dSChristopher Siden }
938ad135b5dSChristopher Siden grub_printf("missing feature for read '%s'\n", za->za_name);
939ad135b5dSChristopher Siden return (ERR_NEWER_VERSION);
940ad135b5dSChristopher Siden }
941ad135b5dSChristopher Siden
942b1b8ab34Slling /*
943b1b8ab34Slling * Get the file dnode for a given file name where mdn is the meta dnode
944b1b8ab34Slling * for this ZFS object set. When found, place the file dnode in dn.
945b1b8ab34Slling * The 'path' argument will be mangled.
946b1b8ab34Slling *
947b1b8ab34Slling * Return:
948b1b8ab34Slling * 0 - success
949b1b8ab34Slling * errnum - failure
950b1b8ab34Slling */
951b1b8ab34Slling static int
dnode_get_path(dnode_phys_t * mdn,char * path,dnode_phys_t * dn,char * stack)952b1b8ab34Slling dnode_get_path(dnode_phys_t *mdn, char *path, dnode_phys_t *dn,
953b1b8ab34Slling char *stack)
954b1b8ab34Slling {
955e7437265Sahrens uint64_t objnum, version;
956b1b8ab34Slling char *cname, ch;
957b1b8ab34Slling
958b1b8ab34Slling if (errnum = dnode_get(mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE,
959b1b8ab34Slling dn, stack))
960b1b8ab34Slling return (errnum);
961b1b8ab34Slling
962e7437265Sahrens if (errnum = zap_lookup(dn, ZPL_VERSION_STR, &version, stack))
963e7437265Sahrens return (errnum);
964e7437265Sahrens if (version > ZPL_VERSION)
965e7437265Sahrens return (-1);
966e7437265Sahrens
967b1b8ab34Slling if (errnum = zap_lookup(dn, ZFS_ROOT_OBJ, &objnum, stack))
968b1b8ab34Slling return (errnum);
969b1b8ab34Slling
970b1b8ab34Slling if (errnum = dnode_get(mdn, objnum, DMU_OT_DIRECTORY_CONTENTS,
971b1b8ab34Slling dn, stack))
972b1b8ab34Slling return (errnum);
973b1b8ab34Slling
974b1b8ab34Slling /* skip leading slashes */
975b1b8ab34Slling while (*path == '/')
976b1b8ab34Slling path++;
977b1b8ab34Slling
978ad135b5dSChristopher Siden while (*path && !grub_isspace(*path)) {
979b1b8ab34Slling
980b1b8ab34Slling /* get the next component name */
981b1b8ab34Slling cname = path;
982ad135b5dSChristopher Siden while (*path && !grub_isspace(*path) && *path != '/')
983b1b8ab34Slling path++;
984b1b8ab34Slling ch = *path;
985b1b8ab34Slling *path = 0; /* ensure null termination */
986b1b8ab34Slling
987b1b8ab34Slling if (errnum = zap_lookup(dn, cname, &objnum, stack))
988b1b8ab34Slling return (errnum);
989b1b8ab34Slling
990e7437265Sahrens objnum = ZFS_DIRENT_OBJ(objnum);
991b1b8ab34Slling if (errnum = dnode_get(mdn, objnum, 0, dn, stack))
992b1b8ab34Slling return (errnum);
993b1b8ab34Slling
994b1b8ab34Slling *path = ch;
995b1b8ab34Slling while (*path == '/')
996b1b8ab34Slling path++;
997b1b8ab34Slling }
998b1b8ab34Slling
999b1b8ab34Slling /* We found the dnode for this file. Verify if it is a plain file. */
1000b1b8ab34Slling VERIFY_DN_TYPE(dn, DMU_OT_PLAIN_FILE_CONTENTS);
1001b1b8ab34Slling
1002b1b8ab34Slling return (0);
1003b1b8ab34Slling }
1004b1b8ab34Slling
1005b1b8ab34Slling /*
1006b1b8ab34Slling * Get the default 'bootfs' property value from the rootpool.
1007b1b8ab34Slling *
1008b1b8ab34Slling * Return:
1009b1b8ab34Slling * 0 - success
1010b1b8ab34Slling * errnum -failure
1011b1b8ab34Slling */
1012b1b8ab34Slling static int
get_default_bootfsobj(dnode_phys_t * mosmdn,uint64_t * obj,char * stack)1013b1b8ab34Slling get_default_bootfsobj(dnode_phys_t *mosmdn, uint64_t *obj, char *stack)
1014b1b8ab34Slling {
1015b1b8ab34Slling uint64_t objnum = 0;
1016b1b8ab34Slling dnode_phys_t *dn = (dnode_phys_t *)stack;
1017b1b8ab34Slling stack += DNODE_SIZE;
1018b1b8ab34Slling
1019ae8180dbSlling if (errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1020b1b8ab34Slling DMU_OT_OBJECT_DIRECTORY, dn, stack))
1021ae8180dbSlling return (errnum);
1022b1b8ab34Slling
1023b1b8ab34Slling /*
1024b1b8ab34Slling * find the object number for 'pool_props', and get the dnode
1025b1b8ab34Slling * of the 'pool_props'.
1026b1b8ab34Slling */
1027b1b8ab34Slling if (zap_lookup(dn, DMU_POOL_PROPS, &objnum, stack))
1028b1b8ab34Slling return (ERR_FILESYSTEM_NOT_FOUND);
1029b1b8ab34Slling
1030ae8180dbSlling if (errnum = dnode_get(mosmdn, objnum, DMU_OT_POOL_PROPS, dn, stack))
1031ae8180dbSlling return (errnum);
1032b1b8ab34Slling
1033b1b8ab34Slling if (zap_lookup(dn, ZPOOL_PROP_BOOTFS, &objnum, stack))
1034b1b8ab34Slling return (ERR_FILESYSTEM_NOT_FOUND);
1035b1b8ab34Slling
1036b1b8ab34Slling if (!objnum)
1037b1b8ab34Slling return (ERR_FILESYSTEM_NOT_FOUND);
1038b1b8ab34Slling
1039b1b8ab34Slling *obj = objnum;
1040b1b8ab34Slling return (0);
1041b1b8ab34Slling }
1042b1b8ab34Slling
1043ad135b5dSChristopher Siden /*
1044ad135b5dSChristopher Siden * List of pool features that the grub implementation of ZFS supports for
1045ad135b5dSChristopher Siden * read. Note that features that are only required for write do not need
1046ad135b5dSChristopher Siden * to be listed here since grub opens pools in read-only mode.
104733915f34SRichard Lowe *
104833915f34SRichard Lowe * When this list is updated the version number in usr/src/grub/capability
104933915f34SRichard Lowe * must be incremented to ensure the new grub gets installed.
1050ad135b5dSChristopher Siden */
1051ad135b5dSChristopher Siden static const char *spa_feature_names[] = {
1052a6f561b4SSašo Kiselkov "org.illumos:lz4_compress",
105343466aaeSMax Grossman "com.delphix:hole_birth",
10542acef22dSMatthew Ahrens "com.delphix:extensible_dataset",
10555d7b4d43SMatthew Ahrens "com.delphix:embedded_data",
1056b5152584SMatthew Ahrens "org.open-zfs:large_blocks",
105745818ee1SMatthew Ahrens "org.illumos:sha512",
1058ad135b5dSChristopher Siden NULL
1059ad135b5dSChristopher Siden };
1060ad135b5dSChristopher Siden
1061ad135b5dSChristopher Siden /*
1062ad135b5dSChristopher Siden * Checks whether the MOS features that are active are supported by this
1063ad135b5dSChristopher Siden * (GRUB's) implementation of ZFS.
1064ad135b5dSChristopher Siden *
1065ad135b5dSChristopher Siden * Return:
1066ad135b5dSChristopher Siden * 0: Success.
1067ad135b5dSChristopher Siden * errnum: Failure.
1068ad135b5dSChristopher Siden */
1069ad135b5dSChristopher Siden static int
check_mos_features(dnode_phys_t * mosmdn,char * stack)1070ad135b5dSChristopher Siden check_mos_features(dnode_phys_t *mosmdn, char *stack)
1071ad135b5dSChristopher Siden {
1072ad135b5dSChristopher Siden uint64_t objnum;
1073ad135b5dSChristopher Siden dnode_phys_t *dn;
1074ad135b5dSChristopher Siden uint8_t error = 0;
1075ad135b5dSChristopher Siden
1076ad135b5dSChristopher Siden dn = (dnode_phys_t *)stack;
1077ad135b5dSChristopher Siden stack += DNODE_SIZE;
1078ad135b5dSChristopher Siden
1079ad135b5dSChristopher Siden if ((errnum = dnode_get(mosmdn, DMU_POOL_DIRECTORY_OBJECT,
1080ad135b5dSChristopher Siden DMU_OT_OBJECT_DIRECTORY, dn, stack)) != 0)
1081ad135b5dSChristopher Siden return (errnum);
1082ad135b5dSChristopher Siden
1083ad135b5dSChristopher Siden /*
1084ad135b5dSChristopher Siden * Find the object number for 'features_for_read' and retrieve its
1085ad135b5dSChristopher Siden * corresponding dnode. Note that we don't check features_for_write
1086ad135b5dSChristopher Siden * because GRUB is not opening the pool for write.
1087ad135b5dSChristopher Siden */
1088ad135b5dSChristopher Siden if ((errnum = zap_lookup(dn, DMU_POOL_FEATURES_FOR_READ, &objnum,
1089ad135b5dSChristopher Siden stack)) != 0)
1090ad135b5dSChristopher Siden return (errnum);
1091ad135b5dSChristopher Siden
1092ad135b5dSChristopher Siden if ((errnum = dnode_get(mosmdn, objnum, DMU_OTN_ZAP_METADATA,
1093ad135b5dSChristopher Siden dn, stack)) != 0)
1094ad135b5dSChristopher Siden return (errnum);
1095ad135b5dSChristopher Siden
1096ad135b5dSChristopher Siden return (zap_iterate(dn, check_feature, spa_feature_names, stack));
1097ad135b5dSChristopher Siden }
1098ad135b5dSChristopher Siden
1099b1b8ab34Slling /*
1100b1b8ab34Slling * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname),
1101b1b8ab34Slling * e.g. pool/rootfs, or a given object number (obj), e.g. the object number
1102b1b8ab34Slling * of pool/rootfs.
1103b1b8ab34Slling *
1104b1b8ab34Slling * If no fsname and no obj are given, return the DSL_DIR metadnode.
1105b1b8ab34Slling * If fsname is given, return its metadnode and its matching object number.
1106b1b8ab34Slling * If only obj is given, return the metadnode for this object number.
1107b1b8ab34Slling *
1108b1b8ab34Slling * Return:
1109b1b8ab34Slling * 0 - success
1110b1b8ab34Slling * errnum - failure
1111b1b8ab34Slling */
1112b1b8ab34Slling static int
get_objset_mdn(dnode_phys_t * mosmdn,char * fsname,uint64_t * obj,dnode_phys_t * mdn,char * stack)