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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* All rights reserved.
*/
#include <sys/zfs_context.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/bio.h>
#include <sys/disk.h>
#include <sys/spa.h>
#include <sys/vdev_impl.h>
#include <sys/fs/zfs.h>
#include <sys/zio.h>
#include <geom/geom.h>
#include <geom/geom_int.h>
/*
* Virtual device vector for GEOM.
*/
struct g_class zfs_vdev_class = {
.name = "ZFS::VDEV",
.version = G_VERSION,
};
DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
typedef struct vdev_geom_ctx {
struct g_consumer *gc_consumer;
int gc_state;
struct bio_queue_head gc_queue;
struct mtx gc_queue_mtx;
} vdev_geom_ctx_t;
static void
vdev_geom_release(vdev_t *vd)
{
vdev_geom_ctx_t *ctx;
ctx = vd->vdev_tsd;
vd->vdev_tsd = NULL;
mtx_lock(&ctx->gc_queue_mtx);
ctx->gc_state = 1;
wakeup_one(&ctx->gc_queue);
while (ctx->gc_state != 2)
msleep(&ctx->gc_state, &ctx->gc_queue_mtx, 0, "vgeom:w", 0);
mtx_unlock(&ctx->gc_queue_mtx);
mtx_destroy(&ctx->gc_queue_mtx);
kmem_free(ctx, sizeof(*ctx));
}
static void
vdev_geom_orphan(struct g_consumer *cp)
{
struct g_geom *gp;
vdev_t *vd;
int error;
g_topology_assert();
vd = cp->private;
gp = cp->geom;
error = cp->provider->error;
ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
if (cp->acr + cp->acw + cp->ace > 0)
g_access(cp, -cp->acr, -cp->acw, -cp->ace);
ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
g_detach(cp);
g_destroy_consumer(cp);
/* Destroy geom if there are no consumers left. */
if (LIST_EMPTY(&gp->consumer)) {
ZFS_LOG(1, "Destroyed geom %s.", gp->name);
g_wither_geom(gp, error);
}
vdev_geom_release(vd);
vd->vdev_remove_wanted = B_TRUE;
spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
}
static struct g_consumer *
vdev_geom_attach(struct g_provider *pp, int write)
{
struct g_geom *gp;
struct g_consumer *cp;
g_topology_assert();
ZFS_LOG(1, "Attaching to %s.", pp->name);
/* Do we have geom already? No? Create one. */
LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
if (gp->flags & G_GEOM_WITHER)
continue;
if (strcmp(gp->name, "zfs::vdev") != 0)
continue;
break;
}
if (gp == NULL) {
gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
gp->orphan = vdev_geom_orphan;
cp = g_new_consumer(gp);
if (g_attach(cp, pp) != 0) {
g_wither_geom(gp, ENXIO);
return (NULL);
}
if (g_access(cp, 1, write, 1) != 0) {
g_wither_geom(gp, ENXIO);
return (NULL);
}
ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
} else {
/* Check if we are already connected to this provider. */
LIST_FOREACH(cp, &gp->consumer, consumer) {
if (cp->provider == pp) {
ZFS_LOG(1, "Found consumer for %s.", pp->name);
break;
}
}
if (cp == NULL) {
cp = g_new_consumer(gp);
if (g_attach(cp, pp) != 0) {
g_destroy_consumer(cp);
return (NULL);
}
if (g_access(cp, 1, write, 1) != 0) {
g_detach(cp);
g_destroy_consumer(cp);
return (NULL);
}
ZFS_LOG(1, "Created consumer for %s.", pp->name);
} else {
if (g_access(cp, 1, cp->acw > 0 ? 0 : write, 1) != 0)
return (NULL);
ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
}
}
return (cp);
}
static void
vdev_geom_detach(void *arg, int flag __unused)
{
struct g_geom *gp;
struct g_consumer *cp;
g_topology_assert();
cp = arg;
gp = cp->geom;
ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
g_access(cp, -1, 0, -1);
/* Destroy consumer on last close. */
if (cp->acr == 0 && cp->ace == 0) {
ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
if (cp->acw > 0)
g_access(cp, 0, -cp->acw, 0);
g_detach(cp);
g_destroy_consumer(cp);
}
/* Destroy geom if there are no consumers left. */
if (LIST_EMPTY(&gp->consumer)) {
ZFS_LOG(1, "Destroyed geom %s.", gp->name);
g_wither_geom(gp, ENXIO);
}
}
static void
vdev_geom_worker(void *arg)
{
vdev_geom_ctx_t *ctx;
zio_t *zio;
struct bio *bp;
ctx = arg;
for (;;) {
mtx_lock(&ctx->gc_queue_mtx);
bp = bioq_takefirst(&ctx->gc_queue);
if (bp == NULL) {
if (ctx->gc_state == 1) {
ctx->gc_state = 2;
wakeup_one(&ctx->gc_state);
mtx_unlock(&ctx->gc_queue_mtx);
kproc_exit(0);
}
msleep(&ctx->gc_queue, &ctx->gc_queue_mtx,
PRIBIO | PDROP, "vgeom:io", 0);
continue;
}
mtx_unlock(&ctx->gc_queue_mtx);
zio = bp->bio_caller1;
zio->io_error = bp->bio_error;
if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
vdev_t *vd;
/*
* If we get ENOTSUP, we know that no future
* attempts will ever succeed. In this case we
* set a persistent bit so that we don't bother
* with the ioctl in the future.
*/
vd = zio->io_vd;
vd->vdev_nowritecache = B_TRUE;
}
g_destroy_bio(bp);
zio_interrupt(zio);
}
}
static char *
vdev_geom_get_id(struct g_consumer *cp)
{
char *id;
int len;
g_topology_assert_not();
len = DISK_IDENT_SIZE;
id = kmem_zalloc(len, KM_SLEEP);
if (g_io_getattr("GEOM::ident", cp, &len, id) != 0) {
kmem_free(id, DISK_IDENT_SIZE);
return (NULL);
}
return (id);
}
static uint64_t
nvlist_get_guid(nvlist_t *list)
{
nvpair_t *elem = NULL;
uint64_t value;
while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
strcmp(nvpair_name(elem), "guid") == 0) {
VERIFY(nvpair_value_uint64(elem, &value) == 0);
return (value);
}
}
return (0);
}
static char *
nvlist_get_devid(nvlist_t *list, uint64_t guid)
{
nvpair_t *elem = NULL;
int progress;
char *id;
progress = 0;
id = NULL;
while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
switch (nvpair_type(elem)) {
case DATA_TYPE_STRING:
{
char *value;
VERIFY(nvpair_value_string(elem, &value) == 0);
if (strcmp(nvpair_name(elem), "type") == 0 &&
strcmp(value, "disk") == 0) {
progress |= 0x01;
} else if (strcmp(nvpair_name(elem), "devid") == 0) {
progress |= 0x02;
id = value;
}
break;
}
case DATA_TYPE_UINT64:
{
uint64_t value;
VERIFY(nvpair_value_uint64(elem, &value) == 0);
if (strcmp(nvpair_name(elem), "guid") == 0 &&
value == guid) {
progress |= 0x04;
}
break;
}
case DATA_TYPE_NVLIST:
{
nvlist_t *value;
char *lid;
VERIFY(nvpair_value_nvlist(elem, &value) == 0);
lid = nvlist_get_devid(value, guid);
if (lid != NULL)
return (lid);
break;
}
case DATA_TYPE_NVLIST_ARRAY:
{
nvlist_t **value;
u_int c, count;
char *lid;
VERIFY(nvpair_value_nvlist_array(elem, &value,
&count) == 0);
for (c = 0; c < count; c++) {
lid = nvlist_get_devid(value[c], guid);
if (lid != NULL)
return (lid);
}
break;
}
}
if (progress == 0x07)
break;
}
if (progress != 0x07)
id = NULL;
return (id);
}
static int
vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
{
struct bio *bp;
u_char *p;
off_t off;
int error;
ASSERT((offset % cp->provider->sectorsize) == 0);
ASSERT((size % cp->provider->sectorsize) == 0);
bp = g_alloc_bio();
off = offset;
offset += size;
p = data;
error = 0;
for (; off < offset; off += MAXPHYS, p += MAXPHYS, size -= MAXPHYS) {
bzero(bp, sizeof(*bp));
bp->bio_cmd = cmd;
bp->bio_done = NULL;
bp->bio_offset = off;
bp->bio_length = MIN(size, MAXPHYS);
bp->bio_data = p;
g_io_request(bp, cp);
error = biowait(bp, "vdev_geom_io");
if (error != 0)
break;
}
g_destroy_bio(bp);
return (error);
}
static char *
vdev_geom_read_id(struct g_consumer *cp)
{
struct g_provider *pp;
vdev_label_t *label;
char *p, *buf;
size_t buflen;
uint64_t psize;
off_t offset, size;
char *id;
int error, l, len;
g_topology_assert_not();
pp = cp->provider;
psize = pp->mediasize;
psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
size = sizeof(*label) + pp->sectorsize -
((sizeof(*label) - 1) % pp->sectorsize) - 1;
id = NULL;
label = kmem_alloc(size, KM_SLEEP);
buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
for (l = 0; l < VDEV_LABELS && id == NULL; l++) {
nvlist_t *config = NULL;
uint64_t guid;
offset = vdev_label_offset(psize, l, 0);
if ((offset % pp->sectorsize) != 0)
continue;
error = vdev_geom_io(cp, BIO_READ, label, offset, size);
if (error != 0)
continue;
buf = label->vl_vdev_phys.vp_nvlist;
if (nvlist_unpack(buf, buflen, &config, 0) != 0)
continue;
guid = nvlist_get_guid(config);
if (guid == 0) {
nvlist_free(config);
continue;
}
id = nvlist_get_devid(config, guid);
if (id != NULL) {
char *tmp;
tmp = kmem_zalloc(DISK_IDENT_SIZE, KM_SLEEP);
strlcpy(tmp, id, DISK_IDENT_SIZE);
id = tmp;
}
nvlist_free(config);
}
kmem_free(label, size);
if (id != NULL)
ZFS_LOG(1, "ID of %s: %s", pp->name, id);
return (id);
}
static void
vdev_geom_free_id(char *id)
{
if (id != NULL)
kmem_free(id, DISK_IDENT_SIZE);
}
struct vdev_geom_find {
const char *id;
int write;
struct g_consumer *cp;
};
static void
vdev_geom_taste_orphan(struct g_consumer *cp)
{
KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
cp->provider->name));
}
static void
vdev_geom_attach_by_id_event(void *arg, int flags __unused)
{
struct vdev_geom_find *ap;
struct g_class *mp;
struct g_geom *gp, *zgp;
struct g_provider *pp;
struct g_consumer *zcp;
char *id;
g_topology_assert();
ap = arg;
zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
/* This orphan function should be never called. */
zgp->orphan = vdev_geom_taste_orphan;
zcp = g_new_consumer(zgp);
/* First round tries to get provider's ID without reading metadata. */
LIST_FOREACH(mp, &g_classes, class) {
if (mp == &zfs_vdev_class)
continue;
LIST_FOREACH(gp, &mp->geom, geom) {
if (gp->flags & G_GEOM_WITHER)
continue;
LIST_FOREACH(pp, &gp->provider, provider) {
if (pp->flags & G_PF_WITHER)
continue;
g_attach(zcp, pp);
if (g_access(zcp, 1, 0, 0) != 0) {
g_detach(zcp);
continue;
}
g_topology_unlock();
id = vdev_geom_get_id(zcp);
g_topology_lock();
g_access(zcp, -1, 0, 0);
g_detach(zcp);
if (id == NULL || strcmp(id, ap->id) != 0) {
vdev_geom_free_id(id);
continue;
}
vdev_geom_free_id(id);
ap->cp = vdev_geom_attach(pp, ap->write);
if (ap->cp == NULL) {
printf("ZFS WARNING: Cannot open %s "
"for writting.\n", pp->name);
continue;
}
goto end;
}
}
}
/* Second round looks for ID by reading ZFS metadata. */
LIST_FOREACH(mp, &g_classes, class) {
if (mp == &zfs_vdev_class)
continue;
LIST_FOREACH(gp, &mp->geom, geom) {
if (gp->flags & G_GEOM_WITHER)
continue;
LIST_FOREACH(pp, &gp->provider, provider) {
if (pp->flags & G_PF_WITHER)
continue;
g_attach(zcp, pp);
if (g_access(zcp, 1, 0, 0) != 0) {
g_detach(zcp);
continue;
}
g_topology_unlock();
id = vdev_geom_read_id(zcp);
g_topology_lock();
g_access(zcp, -1, 0, 0);
g_detach(zcp);
if (id == NULL || strcmp(id, ap->id) != 0) {
vdev_geom_free_id(id);
continue;
}
vdev_geom_free_id(id);
ap->cp = vdev_geom_attach(pp, ap->write);
if (ap->cp == NULL) {
printf("ZFS WARNING: Cannot open %s "
"for writting.\n", pp->name);
continue;
}
goto end;
}
}
}
ap->cp = NULL;
end:
g_destroy_consumer(zcp);
g_destroy_geom(zgp);
}
static struct g_consumer *
vdev_geom_attach_by_id(const char *id, int write)
{
struct vdev_geom_find *ap;
struct g_consumer *cp;
ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
ap->id = id;
ap->write = write;
g_waitfor_event(vdev_geom_attach_by_id_event, ap, M_WAITOK, NULL);
cp = ap->cp;
kmem_free(ap, sizeof(*ap));
return (cp);
}
static struct g_consumer *
vdev_geom_open_by_path_and_devid(vdev_t *vd)
{
struct g_provider *pp;
struct g_consumer *cp;
char *id;
cp = NULL;
g_topology_lock();
pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
if (pp != NULL) {
ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
cp = vdev_geom_attach(pp, !!(spa_mode & FWRITE));
if (cp != NULL && vd->vdev_devid != NULL) {
g_topology_unlock();
id = vdev_geom_get_id(cp);
g_topology_lock();
if (id == NULL || strcmp(id, vd->vdev_devid) != 0) {
vdev_geom_detach(cp, 0);
cp = NULL;
ZFS_LOG(1, "ID mismatch for provider %s: "
"[%s]!=[%s].", vd->vdev_path,
vd->vdev_devid, id);
} else
ZFS_LOG(1, "ID match for provider %s.",
vd->vdev_path);
vdev_geom_free_id(id);
}
}
g_topology_unlock();
return (cp);
}
static struct g_consumer *
vdev_geom_open_by_devid(vdev_t *vd)
{
struct g_consumer *cp;
char *buf;
size_t len;
/*
* We can't search by devid if it's missing.
*/
if (vd->vdev_devid == NULL)
return (NULL);
ZFS_LOG(1, "Searching by ID [%s].", vd->vdev_devid);
cp = vdev_geom_attach_by_id(vd->vdev_devid, !!(spa_mode & FWRITE));
if (cp != NULL) {
len = strlen(cp->provider->name) + strlen("/dev/") + 1;
buf = kmem_alloc(len, KM_SLEEP);
snprintf(buf, len, "/dev/%s", cp->provider->name);
spa_strfree(vd->vdev_path);
vd->vdev_path = buf;
ZFS_LOG(1, "Attach by ID [%s] succeeded, provider %s.",
vd->vdev_devid, vd->vdev_path);
} else
ZFS_LOG(1, "Search by ID [%s] failed.", vd->vdev_devid);
return (cp);
}
static int
vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
{
vdev_geom_ctx_t *ctx;
struct g_provider *pp;
struct g_consumer *cp;
int owned;
/*
* We must have a pathname, and it must be absolute.
*/
if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
return (EINVAL);
}
vd->vdev_tsd = NULL;
if ((owned = mtx_owned(&Giant)))
mtx_unlock(&Giant);
cp = vdev_geom_open_by_path_and_devid(vd);
if (cp == NULL) {
/*
* The device at vd->vdev_path doesn't have the right devid.
* The disks might have merely moved around so try all other
* geom providers to find one with the right devid.
*/
cp = vdev_geom_open_by_devid(vd);
if (cp == NULL) {
ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
if (owned)
mtx_lock(&Giant);
return (EACCES);
}
}
if (owned)
mtx_lock(&Giant);
cp->private = vd;
ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP);
bioq_init(&ctx->gc_queue);
mtx_init(&ctx->gc_queue_mtx, "zfs:vdev:geom:queue", NULL, MTX_DEF);
ctx->gc_consumer = cp;
ctx->gc_state = 0;
vd->vdev_tsd = ctx;
pp = cp->provider;
kproc_create(vdev_geom_worker, ctx, NULL, 0, 0, "vdev:worker %s",
pp->name);
/*
* Determine the actual size of the device.
*/
*psize = pp->mediasize;
/*
* Determine the device's minimum transfer size.
*/
*ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
/*
* Clear the nowritecache bit, so that on a vdev_reopen() we will
* try again.
*/
vd->vdev_nowritecache = B_FALSE;
return (0);
}
static void
vdev_geom_close(vdev_t *vd)
{
vdev_geom_ctx_t *ctx;
struct g_consumer *cp;
if ((ctx = vd->vdev_tsd) == NULL)
return;
if ((cp = ctx->gc_consumer) == NULL)
return;
vdev_geom_release(vd);
g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
}
static void
vdev_geom_io_intr(struct bio *bp)
{
vdev_geom_ctx_t *ctx;
zio_t *zio;
zio = bp->bio_caller1;
ctx = zio->io_vd->vdev_tsd;
if ((zio->io_error = bp->bio_error) == 0 && bp->bio_resid != 0)
zio->io_error = EIO;
mtx_lock(&ctx->gc_queue_mtx);
bioq_insert_tail(&ctx->gc_queue, bp);
wakeup_one(&ctx->gc_queue);
mtx_unlock(&ctx->gc_queue_mtx);
}
static int
vdev_geom_io_start(zio_t *zio)
{
vdev_t *vd;
vdev_geom_ctx_t *ctx;
struct g_consumer *cp;
struct bio *bp;
int error;
cp = NULL;
vd = zio->io_vd;
ctx = vd->vdev_tsd;
if (ctx != NULL)
cp = ctx->gc_consumer;
if (zio->io_type == ZIO_TYPE_IOCTL) {
/* XXPOLICY */
if (!vdev_readable(vd)) {
zio->io_error = ENXIO;
return (ZIO_PIPELINE_CONTINUE);
}
switch (zio->io_cmd) {
case DKIOCFLUSHWRITECACHE:
if (zfs_nocacheflush)
break;
if (vd->vdev_nowritecache) {
zio->io_error = ENOTSUP;
break;
}
goto sendreq;
default:
zio->io_error = ENOTSUP;
}
return (ZIO_PIPELINE_CONTINUE);
}
sendreq:
if (cp == NULL) {
zio->io_error = ENXIO;
return (ZIO_PIPELINE_CONTINUE);
}
bp = g_alloc_bio();
bp->bio_caller1 = zio;
switch (zio->io_type) {
case ZIO_TYPE_READ:
case ZIO_TYPE_WRITE:
bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
bp->bio_data = zio->io_data;
bp->bio_offset = zio->io_offset;
bp->bio_length = zio->io_size;
break;
case ZIO_TYPE_IOCTL:
bp->bio_cmd = BIO_FLUSH;
bp->bio_data = NULL;
bp->bio_offset = cp->provider->mediasize;
bp->bio_length = 0;
break;
}
bp->bio_done = vdev_geom_io_intr;
g_io_request(bp, cp);
return (ZIO_PIPELINE_STOP);
}
static void
vdev_geom_io_done(zio_t *zio)
{
/*
* If the device returned ENXIO, then attempt we should verify if GEOM
* provider has been removed. If this is the case, then we trigger an
* asynchronous removal of the device.
*/
if (zio->io_error == ENXIO) {
vdev_t *vd = zio->io_vd;
vdev_geom_ctx_t *ctx;
struct g_provider *pp = NULL;
ctx = vd->vdev_tsd;
if (ctx != NULL && ctx->gc_consumer != NULL)
pp = ctx->gc_consumer->provider;
if (pp == NULL || (pp->flags & G_PF_ORPHAN)) {
vd->vdev_remove_wanted = B_TRUE;
spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
}
}
}
vdev_ops_t vdev_geom_ops = {
vdev_geom_open,
vdev_geom_close,
vdev_default_asize,
vdev_geom_io_start,
vdev_geom_io_done,
NULL,
VDEV_TYPE_DISK, /* name of this vdev type */
B_TRUE /* leaf vdev */
};
|