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