This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
/
zfs.c
4378 lines (3845 loc) · 110 KB
/
zfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2009,2010,2011 Free Software Foundation, Inc.
* Copyright 2010 Sun Microsystems, Inc.
* Copyright (c) 2012 by Delphix. All rights reserved.
*
* GRUB is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* The zfs plug-in routines for GRUB are:
*
* zfs_mount() - locates a valid uberblock of the root pool and reads
* in its MOS at the memory address MOS.
*
* zfs_open() - locates a plain file object by following the MOS
* and places its dnode at the memory address DNODE.
*
* zfs_read() - read in the data blocks pointed by the DNODE.
*
*/
#include <grub/err.h>
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/disk.h>
#include <grub/partition.h>
#include <grub/dl.h>
#include <grub/types.h>
#include <grub/zfs/zfs.h>
#include <grub/zfs/zio.h>
#include <grub/zfs/dnode.h>
#include <grub/zfs/uberblock_impl.h>
#include <grub/zfs/vdev_impl.h>
#include <grub/zfs/zio_checksum.h>
#include <grub/zfs/zap_impl.h>
#include <grub/zfs/zap_leaf.h>
#include <grub/zfs/zfs_znode.h>
#include <grub/zfs/dmu.h>
#include <grub/zfs/dmu_objset.h>
#include <grub/zfs/sa_impl.h>
#include <grub/zfs/dsl_dir.h>
#include <grub/zfs/dsl_dataset.h>
#include <grub/deflate.h>
#include <grub/crypto.h>
#include <grub/i18n.h>
GRUB_MOD_LICENSE ("GPLv3+");
#define ZPOOL_PROP_BOOTFS "bootfs"
/*
* For nvlist manipulation. (from nvpair.h)
*/
#define NV_ENCODE_NATIVE 0
#define NV_ENCODE_XDR 1
#define NV_BIG_ENDIAN 0
#define NV_LITTLE_ENDIAN 1
#define DATA_TYPE_UINT64 8
#define DATA_TYPE_STRING 9
#define DATA_TYPE_NVLIST 19
#define DATA_TYPE_NVLIST_ARRAY 20
#ifndef GRUB_UTIL
static grub_dl_t my_mod;
#endif
#define P2PHASE(x, align) ((x) & ((align) - 1))
static inline grub_disk_addr_t
DVA_OFFSET_TO_PHYS_SECTOR (grub_disk_addr_t offset)
{
return ((offset + VDEV_LABEL_START_SIZE) >> SPA_MINBLOCKSHIFT);
}
/*
* FAT ZAP data structures
*/
#define ZFS_CRC64_POLY 0xC96C5795D7870F42ULL /* ECMA-182, reflected form */
static inline grub_uint64_t
ZAP_HASH_IDX (grub_uint64_t hash, grub_uint64_t n)
{
return (((n) == 0) ? 0 : ((hash) >> (64 - (n))));
}
#define CHAIN_END 0xffff /* end of the chunk chain */
/*
* The amount of space within the chunk available for the array is:
* chunk size - space for type (1) - space for next pointer (2)
*/
#define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3)
static inline int
ZAP_LEAF_HASH_SHIFT (int bs)
{
return bs - 5;
}
static inline int
ZAP_LEAF_HASH_NUMENTRIES (int bs)
{
return 1 << ZAP_LEAF_HASH_SHIFT(bs);
}
static inline grub_size_t
LEAF_HASH (int bs, grub_uint64_t h, zap_leaf_phys_t *l)
{
return ((ZAP_LEAF_HASH_NUMENTRIES (bs)-1)
& ((h) >> (64 - ZAP_LEAF_HASH_SHIFT (bs) - l->l_hdr.lh_prefix_len)));
}
/*
* The amount of space available for chunks is:
* block size shift - hash entry size (2) * number of hash
* entries - header space (2*chunksize)
*/
static inline int
ZAP_LEAF_NUMCHUNKS (int bs)
{
return (((1U << bs) - 2 * ZAP_LEAF_HASH_NUMENTRIES (bs)) /
ZAP_LEAF_CHUNKSIZE - 2);
}
/*
* The chunks start immediately after the hash table. The end of the
* hash table is at l_hash + HASH_NUMENTRIES, which we simply cast to a
* chunk_t.
*/
static inline zap_leaf_chunk_t *
ZAP_LEAF_CHUNK (zap_leaf_phys_t *l, int bs, int idx)
{
return &((zap_leaf_chunk_t *) (l->l_entries
+ (ZAP_LEAF_HASH_NUMENTRIES(bs) * 2)
/ sizeof (grub_properly_aligned_t)))[idx];
}
static inline struct zap_leaf_entry *
ZAP_LEAF_ENTRY(zap_leaf_phys_t *l, int bs, int idx)
{
return &ZAP_LEAF_CHUNK(l, bs, idx)->l_entry;
}
/*
* Decompression Entry - lzjb & lz4
*/
extern grub_err_t lzjb_decompress (void *, void *, grub_size_t, grub_size_t);
extern grub_err_t lz4_decompress (void *, void *, grub_size_t, grub_size_t);
typedef grub_err_t zfs_decomp_func_t (void *s_start, void *d_start,
grub_size_t s_len, grub_size_t d_len);
typedef struct decomp_entry
{
const char *name;
zfs_decomp_func_t *decomp_func;
} decomp_entry_t;
/*
* Signature for checksum functions.
*/
typedef void zio_checksum_t(const void *data, grub_uint64_t size,
grub_zfs_endian_t endian, zio_cksum_t *zcp);
/*
* Information about each checksum function.
*/
typedef struct zio_checksum_info {
zio_checksum_t *ci_func; /* checksum function for each byteorder */
int ci_correctable; /* number of correctable bits */
int ci_eck; /* uses zio embedded checksum? */
const char *ci_name; /* descriptive name */
} zio_checksum_info_t;
typedef struct dnode_end
{
dnode_phys_t dn;
grub_zfs_endian_t endian;
} dnode_end_t;
struct grub_zfs_device_desc
{
enum { DEVICE_LEAF, DEVICE_MIRROR, DEVICE_RAIDZ } type;
grub_uint64_t id;
grub_uint64_t guid;
unsigned ashift;
unsigned max_children_ashift;
/* Valid only for non-leafs. */
unsigned n_children;
struct grub_zfs_device_desc *children;
/* Valid only for RAIDZ. */
unsigned nparity;
/* Valid only for leaf devices. */
grub_device_t dev;
grub_disk_addr_t vdev_phys_sector;
uberblock_t current_uberblock;
int original;
};
struct subvolume
{
dnode_end_t mdn;
grub_uint64_t obj;
grub_uint64_t case_insensitive;
grub_size_t nkeys;
struct
{
grub_crypto_cipher_handle_t cipher;
grub_uint64_t txg;
grub_uint64_t algo;
} *keyring;
};
struct grub_zfs_data
{
/* cache for a file block of the currently zfs_open()-ed file */
char *file_buf;
grub_uint64_t file_start;
grub_uint64_t file_end;
/* cache for a dnode block */
dnode_phys_t *dnode_buf;
dnode_phys_t *dnode_mdn;
grub_uint64_t dnode_start;
grub_uint64_t dnode_end;
grub_zfs_endian_t dnode_endian;
dnode_end_t mos;
dnode_end_t dnode;
struct subvolume subvol;
struct grub_zfs_device_desc *devices_attached;
unsigned n_devices_attached;
unsigned n_devices_allocated;
struct grub_zfs_device_desc *device_original;
uberblock_t current_uberblock;
grub_uint64_t guid;
};
/* Context for grub_zfs_dir. */
struct grub_zfs_dir_ctx
{
grub_fs_dir_hook_t hook;
void *hook_data;
struct grub_zfs_data *data;
};
grub_err_t (*grub_zfs_decrypt) (grub_crypto_cipher_handle_t cipher,
grub_uint64_t algo,
void *nonce,
char *buf, grub_size_t size,
const grub_uint32_t *expected_mac,
grub_zfs_endian_t endian) = NULL;
grub_crypto_cipher_handle_t (*grub_zfs_load_key) (const struct grub_zfs_key *key,
grub_size_t keysize,
grub_uint64_t salt,
grub_uint64_t algo) = NULL;
/*
* List of pool features that the grub implementation of ZFS supports for
* read. Note that features that are only required for write do not need
* to be listed here since grub opens pools in read-only mode.
*/
#define MAX_SUPPORTED_FEATURE_STRLEN 50
static const char *spa_feature_names[] = {
"org.illumos:lz4_compress",
"com.delphix:hole_birth",
"com.delphix:embedded_data",
"com.delphix:extensible_dataset",
"org.open-zfs:large_blocks",
NULL
};
static int
check_feature(const char *name, grub_uint64_t val, struct grub_zfs_dir_ctx *ctx);
static grub_err_t
check_mos_features(dnode_phys_t *mosmdn_phys,grub_zfs_endian_t endian,struct grub_zfs_data* data );
static grub_err_t
zlib_decompress (void *s, void *d,
grub_size_t slen, grub_size_t dlen)
{
if (grub_zlib_decompress (s, slen, 0, d, dlen) == (grub_ssize_t) dlen)
return GRUB_ERR_NONE;
if (!grub_errno)
grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
"premature end of compressed");
return grub_errno;
}
static grub_err_t
zle_decompress (void *s, void *d,
grub_size_t slen, grub_size_t dlen)
{
grub_uint8_t *iptr, *optr;
grub_size_t clen;
for (iptr = s, optr = d; iptr < (grub_uint8_t *) s + slen
&& optr < (grub_uint8_t *) d + dlen;)
{
if (*iptr & 0x80)
clen = ((*iptr) & 0x7f) + 0x41;
else
clen = ((*iptr) & 0x3f) + 1;
if ((grub_ssize_t) clen > (grub_uint8_t *) d + dlen - optr)
clen = (grub_uint8_t *) d + dlen - optr;
if (*iptr & 0x40 || *iptr & 0x80)
{
grub_memset (optr, 0, clen);
iptr++;
optr += clen;
continue;
}
if ((grub_ssize_t) clen > (grub_uint8_t *) s + slen - iptr - 1)
clen = (grub_uint8_t *) s + slen - iptr - 1;
grub_memcpy (optr, iptr + 1, clen);
optr += clen;
iptr += clen + 1;
}
if (optr < (grub_uint8_t *) d + dlen)
grub_memset (optr, 0, (grub_uint8_t *) d + dlen - optr);
return GRUB_ERR_NONE;
}
static decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] = {
{"inherit", NULL}, /* ZIO_COMPRESS_INHERIT */
{"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */
{"off", NULL}, /* ZIO_COMPRESS_OFF */
{"lzjb", lzjb_decompress}, /* ZIO_COMPRESS_LZJB */
{"empty", NULL}, /* ZIO_COMPRESS_EMPTY */
{"gzip-1", zlib_decompress}, /* ZIO_COMPRESS_GZIP1 */
{"gzip-2", zlib_decompress}, /* ZIO_COMPRESS_GZIP2 */
{"gzip-3", zlib_decompress}, /* ZIO_COMPRESS_GZIP3 */
{"gzip-4", zlib_decompress}, /* ZIO_COMPRESS_GZIP4 */
{"gzip-5", zlib_decompress}, /* ZIO_COMPRESS_GZIP5 */
{"gzip-6", zlib_decompress}, /* ZIO_COMPRESS_GZIP6 */
{"gzip-7", zlib_decompress}, /* ZIO_COMPRESS_GZIP7 */
{"gzip-8", zlib_decompress}, /* ZIO_COMPRESS_GZIP8 */
{"gzip-9", zlib_decompress}, /* ZIO_COMPRESS_GZIP9 */
{"zle", zle_decompress}, /* ZIO_COMPRESS_ZLE */
{"lz4", lz4_decompress}, /* ZIO_COMPRESS_LZ4 */
};
static grub_err_t zio_read_data (blkptr_t * bp, grub_zfs_endian_t endian,
void *buf, struct grub_zfs_data *data);
/*
* Our own version of log2(). Same thing as highbit()-1.
*/
static int
zfs_log2 (grub_uint64_t num)
{
int i = 0;
while (num > 1)
{
i++;
num = num >> 1;
}
return i;
}
/* Checksum Functions */
static void
zio_checksum_off (const void *buf __attribute__ ((unused)),
grub_uint64_t size __attribute__ ((unused)),
grub_zfs_endian_t endian __attribute__ ((unused)),
zio_cksum_t * zcp)
{
ZIO_SET_CHECKSUM (zcp, 0, 0, 0, 0);
}
/* Checksum Table and Values */
static zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
{NULL, 0, 0, "inherit"},
{NULL, 0, 0, "on"},
{zio_checksum_off, 0, 0, "off"},
{zio_checksum_SHA256, 1, 1, "label"},
{zio_checksum_SHA256, 1, 1, "gang_header"},
{NULL, 0, 0, "zilog"},
{fletcher_2, 0, 0, "fletcher2"},
{fletcher_4, 1, 0, "fletcher4"},
{zio_checksum_SHA256, 1, 0, "SHA256"},
{NULL, 0, 0, "zilog2"},
{zio_checksum_SHA256, 1, 0, "SHA256+MAC"},
};
/*
* zio_checksum_verify: Provides support for checksum verification.
*
* Fletcher2, Fletcher4, and SHA256 are supported.
*
*/
static grub_err_t
zio_checksum_verify (zio_cksum_t zc, grub_uint32_t checksum,
grub_zfs_endian_t endian,
char *buf, grub_size_t size)
{
zio_eck_t *zec = (zio_eck_t *) (buf + size) - 1;
zio_checksum_info_t *ci = &zio_checksum_table[checksum];
zio_cksum_t actual_cksum, expected_cksum;
if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func == NULL)
{
grub_dprintf ("zfs", "unknown checksum function %d\n", checksum);
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
"unknown checksum function %d", checksum);
}
if (ci->ci_eck)
{
expected_cksum = zec->zec_cksum;
zec->zec_cksum = zc;
ci->ci_func (buf, size, endian, &actual_cksum);
zec->zec_cksum = expected_cksum;
zc = expected_cksum;
}
else
ci->ci_func (buf, size, endian, &actual_cksum);
if (grub_memcmp (&actual_cksum, &zc,
checksum != ZIO_CHECKSUM_SHA256_MAC ? 32 : 20) != 0)
{
grub_dprintf ("zfs", "checksum %s verification failed\n", ci->ci_name);
grub_dprintf ("zfs", "actual checksum %016llx %016llx %016llx %016llx\n",
(unsigned long long) actual_cksum.zc_word[0],
(unsigned long long) actual_cksum.zc_word[1],
(unsigned long long) actual_cksum.zc_word[2],
(unsigned long long) actual_cksum.zc_word[3]);
grub_dprintf ("zfs", "expected checksum %016llx %016llx %016llx %016llx\n",
(unsigned long long) zc.zc_word[0],
(unsigned long long) zc.zc_word[1],
(unsigned long long) zc.zc_word[2],
(unsigned long long) zc.zc_word[3]);
return grub_error (GRUB_ERR_BAD_FS, N_("checksum verification failed"));
}
return GRUB_ERR_NONE;
}
/*
* vdev_uberblock_compare takes two uberblock structures and returns an integer
* indicating the more recent of the two.
* Return Value = 1 if ub2 is more recent
* Return Value = -1 if ub1 is more recent
* The most recent uberblock is determined using its transaction number and
* timestamp. The uberblock with the highest transaction number is
* considered "newer". If the transaction numbers of the two blocks match, the
* timestamps are compared to determine the "newer" of the two.
*/
static int
vdev_uberblock_compare (uberblock_t * ub1, uberblock_t * ub2)
{
grub_zfs_endian_t ub1_endian, ub2_endian;
if (grub_zfs_to_cpu64 (ub1->ub_magic, GRUB_ZFS_LITTLE_ENDIAN)
== UBERBLOCK_MAGIC)
ub1_endian = GRUB_ZFS_LITTLE_ENDIAN;
else
ub1_endian = GRUB_ZFS_BIG_ENDIAN;
if (grub_zfs_to_cpu64 (ub2->ub_magic, GRUB_ZFS_LITTLE_ENDIAN)
== UBERBLOCK_MAGIC)
ub2_endian = GRUB_ZFS_LITTLE_ENDIAN;
else
ub2_endian = GRUB_ZFS_BIG_ENDIAN;
if (grub_zfs_to_cpu64 (ub1->ub_txg, ub1_endian)
< grub_zfs_to_cpu64 (ub2->ub_txg, ub2_endian))
return -1;
if (grub_zfs_to_cpu64 (ub1->ub_txg, ub1_endian)
> grub_zfs_to_cpu64 (ub2->ub_txg, ub2_endian))
return 1;
if (grub_zfs_to_cpu64 (ub1->ub_timestamp, ub1_endian)
< grub_zfs_to_cpu64 (ub2->ub_timestamp, ub2_endian))
return -1;
if (grub_zfs_to_cpu64 (ub1->ub_timestamp, ub1_endian)
> grub_zfs_to_cpu64 (ub2->ub_timestamp, ub2_endian))
return 1;
return 0;
}
/*
* Three pieces of information are needed to verify an uberblock: the magic
* number, the version number, and the checksum.
*
* Currently Implemented: version number, magic number, checksum
*
*/
static grub_err_t
uberblock_verify (uberblock_phys_t * ub, grub_uint64_t offset,
grub_size_t s)
{
uberblock_t *uber = &ub->ubp_uberblock;
grub_err_t err;
grub_zfs_endian_t endian = GRUB_ZFS_UNKNOWN_ENDIAN;
zio_cksum_t zc;
if (grub_zfs_to_cpu64 (uber->ub_magic, GRUB_ZFS_LITTLE_ENDIAN)
== UBERBLOCK_MAGIC
&& SPA_VERSION_IS_SUPPORTED(grub_zfs_to_cpu64 (uber->ub_version, GRUB_ZFS_LITTLE_ENDIAN)))
endian = GRUB_ZFS_LITTLE_ENDIAN;
if (grub_zfs_to_cpu64 (uber->ub_magic, GRUB_ZFS_BIG_ENDIAN) == UBERBLOCK_MAGIC
&& SPA_VERSION_IS_SUPPORTED(grub_zfs_to_cpu64 (uber->ub_version, GRUB_ZFS_BIG_ENDIAN)))
endian = GRUB_ZFS_BIG_ENDIAN;
if (endian == GRUB_ZFS_UNKNOWN_ENDIAN)
return grub_error (GRUB_ERR_BAD_FS, "invalid uberblock magic");
grub_memset (&zc, 0, sizeof (zc));
zc.zc_word[0] = grub_cpu_to_zfs64 (offset, endian);
err = zio_checksum_verify (zc, ZIO_CHECKSUM_LABEL, endian,
(char *) ub, s);
return err;
}
/*
* Find the best uberblock.
* Return:
* Success - Pointer to the best uberblock.
* Failure - NULL
*/
static uberblock_phys_t *
find_bestub (uberblock_phys_t * ub_array,
const struct grub_zfs_device_desc *desc)
{
uberblock_phys_t *ubbest = NULL, *ubptr;
int i;
grub_disk_addr_t offset;
grub_err_t err = GRUB_ERR_NONE;
int ub_shift;
ub_shift = desc->ashift;
if (ub_shift < VDEV_UBERBLOCK_SHIFT)
ub_shift = VDEV_UBERBLOCK_SHIFT;
for (i = 0; i < (VDEV_UBERBLOCK_RING >> ub_shift); i++)
{
offset = (desc->vdev_phys_sector << SPA_MINBLOCKSHIFT) + VDEV_PHYS_SIZE
+ (i << ub_shift);
ubptr = (uberblock_phys_t *) ((grub_properly_aligned_t *) ub_array
+ ((i << ub_shift)
/ sizeof (grub_properly_aligned_t)));
err = uberblock_verify (ubptr, offset, 1 << ub_shift);
if (err)
{
grub_errno = GRUB_ERR_NONE;
continue;
}
if (ubbest == NULL
|| vdev_uberblock_compare (&(ubptr->ubp_uberblock),
&(ubbest->ubp_uberblock)) > 0)
ubbest = ubptr;
}
if (!ubbest)
grub_errno = err;
return ubbest;
}
static inline grub_size_t
get_psize (blkptr_t * bp, grub_zfs_endian_t endian)
{
return ((((grub_zfs_to_cpu64 ((bp)->blk_prop, endian) >> 16) & 0xffff) + 1)
<< SPA_MINBLOCKSHIFT);
}
static grub_uint64_t
dva_get_offset (const dva_t *dva, grub_zfs_endian_t endian)
{
grub_dprintf ("zfs", "dva=%llx, %llx\n",
(unsigned long long) dva->dva_word[0],
(unsigned long long) dva->dva_word[1]);
return grub_zfs_to_cpu64 ((dva)->dva_word[1],
endian) << SPA_MINBLOCKSHIFT;
}
static grub_err_t
zfs_fetch_nvlist (struct grub_zfs_device_desc *diskdesc, char **nvlist)
{
grub_err_t err;
*nvlist = 0;
if (!diskdesc->dev)
return grub_error (GRUB_ERR_BUG, "member drive unknown");
*nvlist = grub_malloc (VDEV_PHYS_SIZE);
/* Read in the vdev name-value pair list (112K). */
err = grub_disk_read (diskdesc->dev->disk, diskdesc->vdev_phys_sector, 0,
VDEV_PHYS_SIZE, *nvlist);
if (err)
{
grub_free (*nvlist);
*nvlist = 0;
return err;
}
return GRUB_ERR_NONE;
}
static grub_err_t
fill_vdev_info_real (struct grub_zfs_data *data,
const char *nvlist,
struct grub_zfs_device_desc *fill,
struct grub_zfs_device_desc *insert,
int *inserted,
unsigned ashift)
{
char *type;
type = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_TYPE);
if (!type)
return grub_errno;
if (!grub_zfs_nvlist_lookup_uint64 (nvlist, "id", &(fill->id)))
{
grub_free (type);
return grub_error (GRUB_ERR_BAD_FS, "couldn't find vdev id");
}
if (!grub_zfs_nvlist_lookup_uint64 (nvlist, "guid", &(fill->guid)))
{
grub_free (type);
return grub_error (GRUB_ERR_BAD_FS, "couldn't find vdev id");
}
{
grub_uint64_t par;
if (grub_zfs_nvlist_lookup_uint64 (nvlist, "ashift", &par))
fill->ashift = par;
else if (ashift != 0xffffffff)
fill->ashift = ashift;
else
{
grub_free (type);
return grub_error (GRUB_ERR_BAD_FS, "couldn't find ashift");
}
}
fill->max_children_ashift = 0;
if (grub_strcmp (type, VDEV_TYPE_DISK) == 0
|| grub_strcmp (type, VDEV_TYPE_FILE) == 0)
{
fill->type = DEVICE_LEAF;
if (!fill->dev && fill->guid == insert->guid)
{
fill->dev = insert->dev;
fill->vdev_phys_sector = insert->vdev_phys_sector;
fill->current_uberblock = insert->current_uberblock;
fill->original = insert->original;
if (!data->device_original)
data->device_original = fill;
insert->ashift = fill->ashift;
*inserted = 1;
}
grub_free (type);
return GRUB_ERR_NONE;
}
if (grub_strcmp (type, VDEV_TYPE_MIRROR) == 0
|| grub_strcmp (type, VDEV_TYPE_RAIDZ) == 0)
{
int nelm, i;
if (grub_strcmp (type, VDEV_TYPE_MIRROR) == 0)
fill->type = DEVICE_MIRROR;
else
{
grub_uint64_t par;
fill->type = DEVICE_RAIDZ;
if (!grub_zfs_nvlist_lookup_uint64 (nvlist, "nparity", &par))
{
grub_free (type);
return grub_error (GRUB_ERR_BAD_FS, "couldn't find raidz parity");
}
fill->nparity = par;
}
nelm = grub_zfs_nvlist_lookup_nvlist_array_get_nelm (nvlist,
ZPOOL_CONFIG_CHILDREN);
if (nelm <= 0)
{
grub_free (type);
return grub_error (GRUB_ERR_BAD_FS, "incorrect mirror VDEV");
}
if (!fill->children)
{
fill->n_children = nelm;
fill->children = grub_zalloc (fill->n_children
* sizeof (fill->children[0]));
}
for (i = 0; i < nelm; i++)
{
char *child;
grub_err_t err;
child = grub_zfs_nvlist_lookup_nvlist_array
(nvlist, ZPOOL_CONFIG_CHILDREN, i);
err = fill_vdev_info_real (data, child, &fill->children[i], insert,
inserted, fill->ashift);
grub_free (child);
if (err)
{
grub_free (type);
return err;
}
if (fill->children[i].ashift > fill->max_children_ashift)
fill->max_children_ashift = fill->children[i].ashift;
}
grub_free (type);
return GRUB_ERR_NONE;
}
grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "vdev %s isn't supported", type);
grub_free (type);
return grub_errno;
}
static grub_err_t
fill_vdev_info (struct grub_zfs_data *data,
char *nvlist, struct grub_zfs_device_desc *diskdesc,
int *inserted)
{
grub_uint64_t id;
unsigned i;
*inserted = 0;
if (!grub_zfs_nvlist_lookup_uint64 (nvlist, "id", &id))
return grub_error (GRUB_ERR_BAD_FS, "couldn't find vdev id");
for (i = 0; i < data->n_devices_attached; i++)
if (data->devices_attached[i].id == id)
return fill_vdev_info_real (data, nvlist, &data->devices_attached[i],
diskdesc, inserted, 0xffffffff);
data->n_devices_attached++;
if (data->n_devices_attached > data->n_devices_allocated)
{
void *tmp;
data->n_devices_allocated = 2 * data->n_devices_attached + 1;
data->devices_attached
= grub_realloc (tmp = data->devices_attached,
data->n_devices_allocated
* sizeof (data->devices_attached[0]));
if (!data->devices_attached)
{
data->devices_attached = tmp;
return grub_errno;
}
}
grub_memset (&data->devices_attached[data->n_devices_attached - 1],
0, sizeof (data->devices_attached[data->n_devices_attached - 1]));
return fill_vdev_info_real (data, nvlist,
&data->devices_attached[data->n_devices_attached - 1],
diskdesc, inserted, 0xffffffff);
}
/*
* For a given XDR packed nvlist, verify the first 4 bytes and move on.
*
* An XDR packed nvlist is encoded as (comments from nvs_xdr_create) :
*
* encoding method/host endian (4 bytes)
* nvl_version (4 bytes)
* nvl_nvflag (4 bytes)
* encoded nvpairs:
* encoded size of the nvpair (4 bytes)
* decoded size of the nvpair (4 bytes)
* name string size (4 bytes)
* name string data (sizeof(NV_ALIGN4(string))
* data type (4 bytes)
* # of elements in the nvpair (4 bytes)
* data
* 2 zero's for the last nvpair
* (end of the entire list) (8 bytes)
*
*/
/*
* The nvlist_next_nvpair() function returns a handle to the next nvpair in the
* list following nvpair. If nvpair is NULL, the first pair is returned. If
* nvpair is the last pair in the nvlist, NULL is returned.
*/
static const char *
nvlist_next_nvpair (const char *nvl, const char *nvpair)
{
const char *nvp;
int encode_size;
int name_len;
if (nvl == NULL)
return NULL;
if (nvpair == NULL)
{
/* skip over header, nvl_version and nvl_nvflag */
nvpair = nvl + 4 * 3;
}
else
{
/* skip to the next nvpair */
encode_size = grub_be_to_cpu32 (grub_get_unaligned32(nvpair));
nvpair += encode_size;
/*If encode_size equals 0 nvlist_next_nvpair would return
* the same pair received in input, leading to an infinite loop.
* If encode_size is less than 0, this will move the pointer
* backwards, *possibly* examinining two times the same nvpair
* and potentially getting into an infinite loop. */
if(encode_size <= 0)
{
grub_dprintf ("zfs", "nvpair with size <= 0\n");
grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist");
return NULL;
}
}
/* 8 bytes of 0 marks the end of the list */
if (grub_get_unaligned64 (nvpair) == 0)
return NULL;
/*consistency checks*/
if (nvpair + 4 * 3 >= nvl + VDEV_PHYS_SIZE)
{
grub_dprintf ("zfs", "nvlist overflow\n");
grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist");
return NULL;
}
encode_size = grub_be_to_cpu32 (grub_get_unaligned32(nvpair));
nvp = nvpair + 4*2;
name_len = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
nvp += 4;
nvp = nvp + ((name_len + 3) & ~3); // align
if (nvp + 4 >= nvl + VDEV_PHYS_SIZE
|| encode_size < 0
|| nvp + 4 + encode_size > nvl + VDEV_PHYS_SIZE)
{
grub_dprintf ("zfs", "nvlist overflow\n");
grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist");
return NULL;
}
/* end consistency checks */
return nvpair;
}
/*
* This function returns 0 on success and 1 on failure. On success, a string
* containing the name of nvpair is saved in buf.
*/
static int
nvpair_name (const char *nvp, char **buf, grub_size_t *buflen)
{
/* skip over encode/decode size */
nvp += 4 * 2;
*buf = (char *) (nvp + 4);
*buflen = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
return 0;
}
/*
* This function retrieves the value of the nvpair in the form of enumerated
* type data_type_t.
*/
static int
nvpair_type (const char *nvp)
{
int name_len, type;
/* skip over encode/decode size */
nvp += 4 * 2;
/* skip over name_len */
name_len = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
nvp += 4;
/* skip over name */
nvp = nvp + ((name_len + 3) & ~3); /* align */
type = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
return type;
}
static int
nvpair_value (const char *nvp,char **val,
grub_size_t *size_out, grub_size_t *nelm_out)
{
int name_len,nelm,encode_size;
/* skip over encode/decode size */
encode_size = grub_be_to_cpu32 (grub_get_unaligned32(nvp));
nvp += 8;
/* skip over name_len */
name_len = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
nvp += 4;
/* skip over name */
nvp = nvp + ((name_len + 3) & ~3); /* align */
/* skip over type */
nvp += 4;
nelm = grub_be_to_cpu32 (grub_get_unaligned32 (nvp));
nvp +=4;
if (nelm < 1)
{
grub_error (GRUB_ERR_BAD_FS, "empty nvpair");
return 0;
}
*val = (char *) nvp;
*size_out = encode_size;
if (nelm_out)
*nelm_out = nelm;
return 1;
}
/*
* Check the disk label information and retrieve needed vdev name-value pairs.
*
*/
static grub_err_t
check_pool_label (struct grub_zfs_data *data,
struct grub_zfs_device_desc *diskdesc,
int *inserted, int original)
{
grub_uint64_t pool_state, txg = 0;
char *nvlist,*features;
#if 0
char *nv;
#endif
grub_uint64_t poolguid;
grub_uint64_t version;
int found;
grub_err_t err;
grub_zfs_endian_t endian;
vdev_phys_t *phys;
zio_cksum_t emptycksum;
*inserted = 0;
err = zfs_fetch_nvlist (diskdesc, &nvlist);
if (err)
return err;
phys = (vdev_phys_t*) nvlist;
if (grub_zfs_to_cpu64 (phys->vp_zbt.zec_magic,
GRUB_ZFS_LITTLE_ENDIAN)
== ZEC_MAGIC)
endian = GRUB_ZFS_LITTLE_ENDIAN;
else if (grub_zfs_to_cpu64 (phys->vp_zbt.zec_magic,
GRUB_ZFS_BIG_ENDIAN)
== ZEC_MAGIC)
endian = GRUB_ZFS_BIG_ENDIAN;
else
{
grub_free (nvlist);
return grub_error (GRUB_ERR_BAD_FS,
"bad vdev_phys_t.vp_zbt.zec_magic number");
}
/* Now check the integrity of the vdev_phys_t structure though checksum. */