-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzfsimpl.c
2108 lines (1807 loc) · 46.2 KB
/
zfsimpl.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
/*-
* Copyright (c) 2007 Doug Rabson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: stable/9/sys/boot/zfs/zfsimpl.c 243674 2012-11-29 14:05:04Z mm $");
/*
* Stand-alone ZFS file reader.
*/
#include <sys/stat.h>
#include <sys/stdint.h>
#include "zfsimpl.h"
#include "zfssubr.c"
struct zfsmount {
const spa_t *spa;
objset_phys_t objset;
uint64_t rootobj;
};
/*
* List of all vdevs, chained through v_alllink.
*/
static vdev_list_t zfs_vdevs;
/*
* List of ZFS features supported for read
*/
static const char *features_for_read[] = {
NULL
};
/*
* List of all pools, chained through spa_link.
*/
static spa_list_t zfs_pools;
static uint64_t zfs_crc64_table[256];
static const dnode_phys_t *dnode_cache_obj = 0;
static uint64_t dnode_cache_bn;
static char *dnode_cache_buf;
static char *zap_scratch;
static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr;
#define TEMP_SIZE (1024 * 1024)
static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf);
static int zfs_get_root(const spa_t *spa, uint64_t *objid);
static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result);
static void
zfs_init(void)
{
STAILQ_INIT(&zfs_vdevs);
STAILQ_INIT(&zfs_pools);
zfs_temp_buf = malloc(TEMP_SIZE);
zfs_temp_end = zfs_temp_buf + TEMP_SIZE;
zfs_temp_ptr = zfs_temp_buf;
dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE);
zap_scratch = malloc(SPA_MAXBLOCKSIZE);
zfs_init_crc();
}
static void *
zfs_alloc(size_t size)
{
char *ptr;
if (zfs_temp_ptr + size > zfs_temp_end) {
printf("ZFS: out of temporary buffer space\n");
for (;;) ;
}
ptr = zfs_temp_ptr;
zfs_temp_ptr += size;
return (ptr);
}
static void
zfs_free(void *ptr, size_t size)
{
zfs_temp_ptr -= size;
if (zfs_temp_ptr != ptr) {
printf("ZFS: zfs_alloc()/zfs_free() mismatch\n");
for (;;) ;
}
}
static int
xdr_int(const unsigned char **xdr, int *ip)
{
*ip = ((*xdr)[0] << 24)
| ((*xdr)[1] << 16)
| ((*xdr)[2] << 8)
| ((*xdr)[3] << 0);
(*xdr) += 4;
return (0);
}
static int
xdr_u_int(const unsigned char **xdr, u_int *ip)
{
*ip = ((*xdr)[0] << 24)
| ((*xdr)[1] << 16)
| ((*xdr)[2] << 8)
| ((*xdr)[3] << 0);
(*xdr) += 4;
return (0);
}
static int
xdr_uint64_t(const unsigned char **xdr, uint64_t *lp)
{
u_int hi, lo;
xdr_u_int(xdr, &hi);
xdr_u_int(xdr, &lo);
*lp = (((uint64_t) hi) << 32) | lo;
return (0);
}
static int
nvlist_find(const unsigned char *nvlist, const char *name, int type,
int* elementsp, void *valuep)
{
const unsigned char *p, *pair;
int junk;
int encoded_size, decoded_size;
p = nvlist;
xdr_int(&p, &junk);
xdr_int(&p, &junk);
pair = p;
xdr_int(&p, &encoded_size);
xdr_int(&p, &decoded_size);
while (encoded_size && decoded_size) {
int namelen, pairtype, elements;
const char *pairname;
xdr_int(&p, &namelen);
pairname = (const char*) p;
p += roundup(namelen, 4);
xdr_int(&p, &pairtype);
if (!memcmp(name, pairname, namelen) && type == pairtype) {
xdr_int(&p, &elements);
if (elementsp)
*elementsp = elements;
if (type == DATA_TYPE_UINT64) {
xdr_uint64_t(&p, (uint64_t *) valuep);
return (0);
} else if (type == DATA_TYPE_STRING) {
int len;
xdr_int(&p, &len);
(*(const char**) valuep) = (const char*) p;
return (0);
} else if (type == DATA_TYPE_NVLIST
|| type == DATA_TYPE_NVLIST_ARRAY) {
(*(const unsigned char**) valuep) =
(const unsigned char*) p;
return (0);
} else {
return (EIO);
}
} else {
/*
* Not the pair we are looking for, skip to the next one.
*/
p = pair + encoded_size;
}
pair = p;
xdr_int(&p, &encoded_size);
xdr_int(&p, &decoded_size);
}
return (EIO);
}
static int
nvlist_check_features_for_read(const unsigned char *nvlist)
{
const unsigned char *p, *pair;
int junk;
int encoded_size, decoded_size;
int rc;
rc = 0;
p = nvlist;
xdr_int(&p, &junk);
xdr_int(&p, &junk);
pair = p;
xdr_int(&p, &encoded_size);
xdr_int(&p, &decoded_size);
while (encoded_size && decoded_size) {
int namelen, pairtype;
const char *pairname;
int i, found;
found = 0;
xdr_int(&p, &namelen);
pairname = (const char*) p;
p += roundup(namelen, 4);
xdr_int(&p, &pairtype);
for (i = 0; features_for_read[i] != NULL; i++) {
if (!memcmp(pairname, features_for_read[i], namelen)) {
found = 1;
break;
}
}
if (!found) {
printf("ZFS: unsupported feature: %s\n", pairname);
rc = EIO;
}
p = pair + encoded_size;
pair = p;
xdr_int(&p, &encoded_size);
xdr_int(&p, &decoded_size);
}
return (rc);
}
/*
* Return the next nvlist in an nvlist array.
*/
static const unsigned char *
nvlist_next(const unsigned char *nvlist)
{
const unsigned char *p, *pair;
int junk;
int encoded_size, decoded_size;
p = nvlist;
xdr_int(&p, &junk);
xdr_int(&p, &junk);
pair = p;
xdr_int(&p, &encoded_size);
xdr_int(&p, &decoded_size);
while (encoded_size && decoded_size) {
p = pair + encoded_size;
pair = p;
xdr_int(&p, &encoded_size);
xdr_int(&p, &decoded_size);
}
return p;
}
#ifdef TEST
static const unsigned char *
nvlist_print(const unsigned char *nvlist, unsigned int indent)
{
static const char* typenames[] = {
"DATA_TYPE_UNKNOWN",
"DATA_TYPE_BOOLEAN",
"DATA_TYPE_BYTE",
"DATA_TYPE_INT16",
"DATA_TYPE_UINT16",
"DATA_TYPE_INT32",
"DATA_TYPE_UINT32",
"DATA_TYPE_INT64",
"DATA_TYPE_UINT64",
"DATA_TYPE_STRING",
"DATA_TYPE_BYTE_ARRAY",
"DATA_TYPE_INT16_ARRAY",
"DATA_TYPE_UINT16_ARRAY",
"DATA_TYPE_INT32_ARRAY",
"DATA_TYPE_UINT32_ARRAY",
"DATA_TYPE_INT64_ARRAY",
"DATA_TYPE_UINT64_ARRAY",
"DATA_TYPE_STRING_ARRAY",
"DATA_TYPE_HRTIME",
"DATA_TYPE_NVLIST",
"DATA_TYPE_NVLIST_ARRAY",
"DATA_TYPE_BOOLEAN_VALUE",
"DATA_TYPE_INT8",
"DATA_TYPE_UINT8",
"DATA_TYPE_BOOLEAN_ARRAY",
"DATA_TYPE_INT8_ARRAY",
"DATA_TYPE_UINT8_ARRAY"
};
unsigned int i, j;
const unsigned char *p, *pair;
int junk;
int encoded_size, decoded_size;
p = nvlist;
xdr_int(&p, &junk);
xdr_int(&p, &junk);
pair = p;
xdr_int(&p, &encoded_size);
xdr_int(&p, &decoded_size);
while (encoded_size && decoded_size) {
int namelen, pairtype, elements;
const char *pairname;
xdr_int(&p, &namelen);
pairname = (const char*) p;
p += roundup(namelen, 4);
xdr_int(&p, &pairtype);
for (i = 0; i < indent; i++)
printf(" ");
printf("%s %s", typenames[pairtype], pairname);
xdr_int(&p, &elements);
switch (pairtype) {
case DATA_TYPE_UINT64: {
uint64_t val;
xdr_uint64_t(&p, &val);
printf(" = 0x%jx\n", (uintmax_t)val);
break;
}
case DATA_TYPE_STRING: {
int len;
xdr_int(&p, &len);
printf(" = \"%s\"\n", p);
break;
}
case DATA_TYPE_NVLIST:
printf("\n");
nvlist_print(p, indent + 1);
break;
case DATA_TYPE_NVLIST_ARRAY:
for (j = 0; j < elements; j++) {
printf("[%d]\n", j);
p = nvlist_print(p, indent + 1);
if (j != elements - 1) {
for (i = 0; i < indent; i++)
printf(" ");
printf("%s %s", typenames[pairtype], pairname);
}
}
break;
default:
printf("\n");
}
p = pair + encoded_size;
pair = p;
xdr_int(&p, &encoded_size);
xdr_int(&p, &decoded_size);
}
return p;
}
#endif
static int
vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf,
off_t offset, size_t size)
{
size_t psize;
int rc;
if (!vdev->v_phys_read)
return (EIO);
if (bp) {
psize = BP_GET_PSIZE(bp);
} else {
psize = size;
}
/*printf("ZFS: reading %d bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/
rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize);
if (rc)
return (rc);
if (bp && zio_checksum_verify(bp, buf))
return (EIO);
return (0);
}
static int
vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
off_t offset, size_t bytes)
{
return (vdev_read_phys(vdev, bp, buf,
offset + VDEV_LABEL_START_SIZE, bytes));
}
static int
vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
off_t offset, size_t bytes)
{
vdev_t *kid;
int rc;
rc = EIO;
STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
if (kid->v_state != VDEV_STATE_HEALTHY)
continue;
rc = kid->v_read(kid, bp, buf, offset, bytes);
if (!rc)
return (0);
}
return (rc);
}
static int
vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf,
off_t offset, size_t bytes)
{
vdev_t *kid;
/*
* Here we should have two kids:
* First one which is the one we are replacing and we can trust
* only this one to have valid data, but it might not be present.
* Second one is that one we are replacing with. It is most likely
* healthy, but we can't trust it has needed data, so we won't use it.
*/
kid = STAILQ_FIRST(&vdev->v_children);
if (kid == NULL)
return (EIO);
if (kid->v_state != VDEV_STATE_HEALTHY)
return (EIO);
return (kid->v_read(kid, bp, buf, offset, bytes));
}
static vdev_t *
vdev_find(uint64_t guid)
{
vdev_t *vdev;
STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink)
if (vdev->v_guid == guid)
return (vdev);
return (0);
}
static vdev_t *
vdev_create(uint64_t guid, vdev_read_t *read)
{
vdev_t *vdev;
vdev = malloc(sizeof(vdev_t));
memset(vdev, 0, sizeof(vdev_t));
STAILQ_INIT(&vdev->v_children);
vdev->v_guid = guid;
vdev->v_state = VDEV_STATE_OFFLINE;
vdev->v_read = read;
vdev->v_phys_read = 0;
vdev->v_read_priv = 0;
STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink);
return (vdev);
}
static int
vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev,
vdev_t **vdevp, int is_newer)
{
int rc;
uint64_t guid, id, ashift, nparity;
const char *type;
const char *path;
vdev_t *vdev, *kid;
const unsigned char *kids;
int nkids, i, is_new;
uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present;
if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID,
DATA_TYPE_UINT64, 0, &guid)
|| nvlist_find(nvlist, ZPOOL_CONFIG_ID,
DATA_TYPE_UINT64, 0, &id)
|| nvlist_find(nvlist, ZPOOL_CONFIG_TYPE,
DATA_TYPE_STRING, 0, &type)) {
printf("ZFS: can't find vdev details\n");
return (ENOENT);
}
if (strcmp(type, VDEV_TYPE_MIRROR)
&& strcmp(type, VDEV_TYPE_DISK)
#ifdef ZFS_TEST
&& strcmp(type, VDEV_TYPE_FILE)
#endif
&& strcmp(type, VDEV_TYPE_RAIDZ)
&& strcmp(type, VDEV_TYPE_REPLACING)) {
printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
return (EIO);
}
is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0;
nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, 0,
&is_offline);
nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, 0,
&is_removed);
nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, 0,
&is_faulted);
nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 0,
&is_degraded);
nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 0,
&isnt_present);
vdev = vdev_find(guid);
if (!vdev) {
is_new = 1;
if (!strcmp(type, VDEV_TYPE_MIRROR))
vdev = vdev_create(guid, vdev_mirror_read);
else if (!strcmp(type, VDEV_TYPE_RAIDZ))
vdev = vdev_create(guid, vdev_raidz_read);
else if (!strcmp(type, VDEV_TYPE_REPLACING))
vdev = vdev_create(guid, vdev_replacing_read);
else
vdev = vdev_create(guid, vdev_disk_read);
vdev->v_id = id;
vdev->v_top = pvdev != NULL ? pvdev : vdev;
if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT,
DATA_TYPE_UINT64, 0, &ashift) == 0)
vdev->v_ashift = ashift;
else
vdev->v_ashift = 0;
if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY,
DATA_TYPE_UINT64, 0, &nparity) == 0)
vdev->v_nparity = nparity;
else
vdev->v_nparity = 0;
if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH,
DATA_TYPE_STRING, 0, &path) == 0) {
if (strncmp(path, "/dev/", 5) == 0)
path += 5;
vdev->v_name = strdup(path);
} else {
if (!strcmp(type, "raidz")) {
if (vdev->v_nparity == 1)
vdev->v_name = "raidz1";
else if (vdev->v_nparity == 2)
vdev->v_name = "raidz2";
else if (vdev->v_nparity == 3)
vdev->v_name = "raidz3";
else {
printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n");
return (EIO);
}
} else {
vdev->v_name = strdup(type);
}
}
} else {
is_new = 0;
}
if (is_new || is_newer) {
/*
* This is either new vdev or we've already seen this vdev,
* but from an older vdev label, so let's refresh its state
* from the newer label.
*/
if (is_offline)
vdev->v_state = VDEV_STATE_OFFLINE;
else if (is_removed)
vdev->v_state = VDEV_STATE_REMOVED;
else if (is_faulted)
vdev->v_state = VDEV_STATE_FAULTED;
else if (is_degraded)
vdev->v_state = VDEV_STATE_DEGRADED;
else if (isnt_present)
vdev->v_state = VDEV_STATE_CANT_OPEN;
}
rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN,
DATA_TYPE_NVLIST_ARRAY, &nkids, &kids);
/*
* Its ok if we don't have any kids.
*/
if (rc == 0) {
vdev->v_nchildren = nkids;
for (i = 0; i < nkids; i++) {
rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer);
if (rc)
return (rc);
if (is_new)
STAILQ_INSERT_TAIL(&vdev->v_children, kid,
v_childlink);
kids = nvlist_next(kids);
}
} else {
vdev->v_nchildren = 0;
}
if (vdevp)
*vdevp = vdev;
return (0);
}
static void
vdev_set_state(vdev_t *vdev)
{
vdev_t *kid;
int good_kids;
int bad_kids;
/*
* A mirror or raidz is healthy if all its kids are healthy. A
* mirror is degraded if any of its kids is healthy; a raidz
* is degraded if at most nparity kids are offline.
*/
if (STAILQ_FIRST(&vdev->v_children)) {
good_kids = 0;
bad_kids = 0;
STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
if (kid->v_state == VDEV_STATE_HEALTHY)
good_kids++;
else
bad_kids++;
}
if (bad_kids == 0) {
vdev->v_state = VDEV_STATE_HEALTHY;
} else {
if (vdev->v_read == vdev_mirror_read) {
if (good_kids) {
vdev->v_state = VDEV_STATE_DEGRADED;
} else {
vdev->v_state = VDEV_STATE_OFFLINE;
}
} else if (vdev->v_read == vdev_raidz_read) {
if (bad_kids > vdev->v_nparity) {
vdev->v_state = VDEV_STATE_OFFLINE;
} else {
vdev->v_state = VDEV_STATE_DEGRADED;
}
}
}
}
}
static spa_t *
spa_find_by_guid(uint64_t guid)
{
spa_t *spa;
STAILQ_FOREACH(spa, &zfs_pools, spa_link)
if (spa->spa_guid == guid)
return (spa);
return (0);
}
static spa_t *
spa_find_by_name(const char *name)
{
spa_t *spa;
STAILQ_FOREACH(spa, &zfs_pools, spa_link)
if (!strcmp(spa->spa_name, name))
return (spa);
return (0);
}
#ifdef BOOT2
static spa_t *
spa_get_primary(void)
{
return (STAILQ_FIRST(&zfs_pools));
}
static vdev_t *
spa_get_primary_vdev(const spa_t *spa)
{
vdev_t *vdev;
vdev_t *kid;
if (spa == NULL)
spa = spa_get_primary();
if (spa == NULL)
return (NULL);
vdev = STAILQ_FIRST(&spa->spa_vdevs);
if (vdev == NULL)
return (NULL);
for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL;
kid = STAILQ_FIRST(&vdev->v_children))
vdev = kid;
return (vdev);
}
#endif
static spa_t *
spa_create(uint64_t guid)
{
spa_t *spa;
spa = malloc(sizeof(spa_t));
memset(spa, 0, sizeof(spa_t));
STAILQ_INIT(&spa->spa_vdevs);
spa->spa_guid = guid;
STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link);
return (spa);
}
static const char *
state_name(vdev_state_t state)
{
static const char* names[] = {
"UNKNOWN",
"CLOSED",
"OFFLINE",
"REMOVED",
"CANT_OPEN",
"FAULTED",
"DEGRADED",
"ONLINE"
};
return names[state];
}
#ifdef BOOT2
#define pager_printf printf
#else
static void
pager_printf(const char *fmt, ...)
{
char line[80];
va_list args;
va_start(args, fmt);
vsprintf(line, fmt, args);
va_end(args);
pager_output(line);
}
#endif
#define STATUS_FORMAT " %s %s\n"
static void
print_state(int indent, const char *name, vdev_state_t state)
{
int i;
char buf[512];
buf[0] = 0;
for (i = 0; i < indent; i++)
strcat(buf, " ");
strcat(buf, name);
pager_printf(STATUS_FORMAT, buf, state_name(state));
}
static void
vdev_status(vdev_t *vdev, int indent)
{
vdev_t *kid;
print_state(indent, vdev->v_name, vdev->v_state);
STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) {
vdev_status(kid, indent + 1);
}
}
static void
spa_status(spa_t *spa)
{
static char bootfs[ZFS_MAXNAMELEN];
uint64_t rootid;
vdev_t *vdev;
int good_kids, bad_kids, degraded_kids;
vdev_state_t state;
pager_printf(" pool: %s\n", spa->spa_name);
if (zfs_get_root(spa, &rootid) == 0 &&
zfs_rlookup(spa, rootid, bootfs) == 0) {
if (bootfs[0] == '\0')
pager_printf("bootfs: %s\n", spa->spa_name);
else
pager_printf("bootfs: %s/%s\n", spa->spa_name, bootfs);
}
pager_printf("config:\n\n");
pager_printf(STATUS_FORMAT, "NAME", "STATE");
good_kids = 0;
degraded_kids = 0;
bad_kids = 0;
STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
if (vdev->v_state == VDEV_STATE_HEALTHY)
good_kids++;
else if (vdev->v_state == VDEV_STATE_DEGRADED)
degraded_kids++;
else
bad_kids++;
}
state = VDEV_STATE_CLOSED;
if (good_kids > 0 && (degraded_kids + bad_kids) == 0)
state = VDEV_STATE_HEALTHY;
else if ((good_kids + degraded_kids) > 0)
state = VDEV_STATE_DEGRADED;
print_state(0, spa->spa_name, state);
STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) {
vdev_status(vdev, 1);
}
}
static void
spa_all_status(void)
{
spa_t *spa;
int first = 1;
STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
if (!first)
pager_printf("\n");
first = 0;
spa_status(spa);
}
}
static int
vdev_probe(vdev_phys_read_t *read, void *read_priv, spa_t **spap)
{
vdev_t vtmp;
vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch;
spa_t *spa;
vdev_t *vdev, *top_vdev, *pool_vdev;
off_t off;
blkptr_t bp;
const unsigned char *nvlist;
uint64_t val;
uint64_t guid;
uint64_t pool_txg, pool_guid;
uint64_t is_log;
const char *pool_name;
const unsigned char *vdevs;
const unsigned char *features;
int i, rc, is_newer;
char *upbuf;
const struct uberblock *up;
/*
* Load the vdev label and figure out which
* uberblock is most current.
*/
memset(&vtmp, 0, sizeof(vtmp));
vtmp.v_phys_read = read;
vtmp.v_read_priv = read_priv;
off = offsetof(vdev_label_t, vl_vdev_phys);
BP_ZERO(&bp);
BP_SET_LSIZE(&bp, sizeof(vdev_phys_t));
BP_SET_PSIZE(&bp, sizeof(vdev_phys_t));
BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL);
BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF);
DVA_SET_OFFSET(BP_IDENTITY(&bp), off);
ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0);
if (vdev_read_phys(&vtmp, &bp, vdev_label, off, 0))
return (EIO);
if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) {
return (EIO);
}
nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4;
if (nvlist_find(nvlist,
ZPOOL_CONFIG_VERSION,
DATA_TYPE_UINT64, 0, &val)) {
return (EIO);
}
if (!SPA_VERSION_IS_SUPPORTED(val)) {
printf("ZFS: unsupported ZFS version %u (should be %u)\n",
(unsigned) val, (unsigned) SPA_VERSION);
return (EIO);
}
/* Check ZFS features for read */
if (nvlist_find(nvlist,
ZPOOL_CONFIG_FEATURES_FOR_READ,
DATA_TYPE_NVLIST, 0, &features) == 0
&& nvlist_check_features_for_read(features) != 0)
return (EIO);
if (nvlist_find(nvlist,
ZPOOL_CONFIG_POOL_STATE,
DATA_TYPE_UINT64, 0, &val)) {
return (EIO);
}
if (val == POOL_STATE_DESTROYED) {
/* We don't boot only from destroyed pools. */
return (EIO);
}
if (nvlist_find(nvlist,
ZPOOL_CONFIG_POOL_TXG,
DATA_TYPE_UINT64, 0, &pool_txg)
|| nvlist_find(nvlist,
ZPOOL_CONFIG_POOL_GUID,
DATA_TYPE_UINT64, 0, &pool_guid)
|| nvlist_find(nvlist,
ZPOOL_CONFIG_POOL_NAME,
DATA_TYPE_STRING, 0, &pool_name)) {
/*
* Cache and spare devices end up here - just ignore
* them.
*/
/*printf("ZFS: can't find pool details\n");*/
return (EIO);
}
is_log = 0;
(void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 0,
&is_log);
if (is_log)
return (EIO);
/*
* Create the pool if this is the first time we've seen it.
*/
spa = spa_find_by_guid(pool_guid);
if (!spa) {
spa = spa_create(pool_guid);
spa->spa_name = strdup(pool_name);
}
if (pool_txg > spa->spa_txg) {
spa->spa_txg = pool_txg;
is_newer = 1;
} else
is_newer = 0;
/*
* Get the vdev tree and create our in-core copy of it.
* If we already have a vdev with this guid, this must
* be some kind of alias (overlapping slices, dangerously dedicated
* disks etc).
*/
if (nvlist_find(nvlist,
ZPOOL_CONFIG_GUID,
DATA_TYPE_UINT64, 0, &guid)) {
return (EIO);
}
vdev = vdev_find(guid);
if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */
return (EIO);
if (nvlist_find(nvlist,
ZPOOL_CONFIG_VDEV_TREE,