4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
23 * All rights reserved.
26 #include <sys/zfs_context.h>
27 #include <sys/param.h>
28 #include <sys/kernel.h>
32 #include <sys/spa_impl.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/fs/zfs.h>
36 #include <geom/geom.h>
37 #include <geom/geom_int.h>
40 * Virtual device vector for GEOM.
43 struct g_class zfs_vdev_class = {
48 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
51 vdev_geom_orphan(struct g_consumer *cp)
61 error = cp->provider->error;
63 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
64 if (cp->acr + cp->acw + cp->ace > 0)
65 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
66 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
68 g_destroy_consumer(cp);
69 /* Destroy geom if there are no consumers left. */
70 if (LIST_EMPTY(&gp->consumer)) {
71 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
72 g_wither_geom(gp, error);
75 vd->vdev_remove_wanted = B_TRUE;
76 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
79 static struct g_consumer *
80 vdev_geom_attach(struct g_provider *pp)
83 struct g_consumer *cp;
87 ZFS_LOG(1, "Attaching to %s.", pp->name);
88 /* Do we have geom already? No? Create one. */
89 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
90 if (gp->flags & G_GEOM_WITHER)
92 if (strcmp(gp->name, "zfs::vdev") != 0)
97 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
98 gp->orphan = vdev_geom_orphan;
99 cp = g_new_consumer(gp);
100 if (g_attach(cp, pp) != 0) {
101 g_wither_geom(gp, ENXIO);
104 if (g_access(cp, 1, 0, 1) != 0) {
105 g_wither_geom(gp, ENXIO);
108 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
110 /* Check if we are already connected to this provider. */
111 LIST_FOREACH(cp, &gp->consumer, consumer) {
112 if (cp->provider == pp) {
113 ZFS_LOG(1, "Found consumer for %s.", pp->name);
118 cp = g_new_consumer(gp);
119 if (g_attach(cp, pp) != 0) {
120 g_destroy_consumer(cp);
123 if (g_access(cp, 1, 0, 1) != 0) {
125 g_destroy_consumer(cp);
128 ZFS_LOG(1, "Created consumer for %s.", pp->name);
130 if (g_access(cp, 1, 0, 1) != 0)
132 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
139 vdev_geom_detach(void *arg, int flag __unused)
142 struct g_consumer *cp;
148 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
149 g_access(cp, -1, 0, -1);
150 /* Destroy consumer on last close. */
151 if (cp->acr == 0 && cp->ace == 0) {
152 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
154 g_access(cp, 0, -cp->acw, 0);
156 g_destroy_consumer(cp);
158 /* Destroy geom if there are no consumers left. */
159 if (LIST_EMPTY(&gp->consumer)) {
160 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
161 g_wither_geom(gp, ENXIO);
166 nvlist_get_guid(nvlist_t *list)
168 nvpair_t *elem = NULL;
171 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
172 if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
173 strcmp(nvpair_name(elem), "guid") == 0) {
174 VERIFY(nvpair_value_uint64(elem, &value) == 0);
182 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
189 ASSERT((offset % cp->provider->sectorsize) == 0);
190 ASSERT((size % cp->provider->sectorsize) == 0);
196 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
199 for (; off < offset; off += maxio, p += maxio, size -= maxio) {
200 bzero(bp, sizeof(*bp));
203 bp->bio_offset = off;
204 bp->bio_length = MIN(size, maxio);
206 g_io_request(bp, cp);
207 error = biowait(bp, "vdev_geom_io");
217 vdev_geom_read_guid(struct g_consumer *cp)
219 struct g_provider *pp;
226 int error, l, len, iszvol;
228 g_topology_assert_not();
231 ZFS_LOG(1, "Reading guid from %s...", pp->name);
232 if (g_getattr("ZFS::iszvol", cp, &iszvol) == 0 && iszvol) {
233 ZFS_LOG(1, "Skipping ZVOL-based provider %s.", pp->name);
237 psize = pp->mediasize;
238 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
240 size = sizeof(*label) + pp->sectorsize -
241 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
244 label = kmem_alloc(size, KM_SLEEP);
245 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
247 for (l = 0; l < VDEV_LABELS; l++) {
248 nvlist_t *config = NULL;
250 offset = vdev_label_offset(psize, l, 0);
251 if ((offset % pp->sectorsize) != 0)
254 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
256 buf = label->vl_vdev_phys.vp_nvlist;
258 if (nvlist_unpack(buf, buflen, &config, 0) != 0)
261 guid = nvlist_get_guid(config);
267 kmem_free(label, size);
269 ZFS_LOG(1, "guid for %s is %ju", pp->name, (uintmax_t)guid);
273 struct vdev_geom_find {
275 struct g_consumer *cp;
279 vdev_geom_taste_orphan(struct g_consumer *cp)
282 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
283 cp->provider->name));
287 vdev_geom_attach_by_guid_event(void *arg, int flags __unused)
289 struct vdev_geom_find *ap;
291 struct g_geom *gp, *zgp;
292 struct g_provider *pp;
293 struct g_consumer *zcp;
300 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
301 /* This orphan function should be never called. */
302 zgp->orphan = vdev_geom_taste_orphan;
303 zcp = g_new_consumer(zgp);
305 LIST_FOREACH(mp, &g_classes, class) {
306 if (mp == &zfs_vdev_class)
308 LIST_FOREACH(gp, &mp->geom, geom) {
309 if (gp->flags & G_GEOM_WITHER)
311 LIST_FOREACH(pp, &gp->provider, provider) {
312 if (pp->flags & G_PF_WITHER)
315 if (g_access(zcp, 1, 0, 0) != 0) {
320 guid = vdev_geom_read_guid(zcp);
322 g_access(zcp, -1, 0, 0);
324 if (guid != ap->guid)
326 ap->cp = vdev_geom_attach(pp);
327 if (ap->cp == NULL) {
328 printf("ZFS WARNING: Unable to attach to %s.\n",
338 g_destroy_consumer(zcp);
342 static struct g_consumer *
343 vdev_geom_attach_by_guid(uint64_t guid)
345 struct vdev_geom_find *ap;
346 struct g_consumer *cp;
348 ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
350 g_waitfor_event(vdev_geom_attach_by_guid_event, ap, M_WAITOK, NULL);
352 kmem_free(ap, sizeof(*ap));
356 static struct g_consumer *
357 vdev_geom_open_by_guid(vdev_t *vd)
359 struct g_consumer *cp;
363 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
364 cp = vdev_geom_attach_by_guid(vd->vdev_guid);
366 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
367 buf = kmem_alloc(len, KM_SLEEP);
369 snprintf(buf, len, "/dev/%s", cp->provider->name);
370 spa_strfree(vd->vdev_path);
373 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
374 (uintmax_t)vd->vdev_guid, vd->vdev_path);
376 ZFS_LOG(1, "Search by guid [%ju] failed.",
377 (uintmax_t)vd->vdev_guid);
383 static struct g_consumer *
384 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
386 struct g_provider *pp;
387 struct g_consumer *cp;
392 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
394 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
395 cp = vdev_geom_attach(pp);
396 if (cp != NULL && check_guid) {
398 guid = vdev_geom_read_guid(cp);
400 if (guid != vd->vdev_guid) {
401 vdev_geom_detach(cp, 0);
403 ZFS_LOG(1, "guid mismatch for provider %s: "
404 "%ju != %ju.", vd->vdev_path,
405 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
407 ZFS_LOG(1, "guid match for provider %s.",
418 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
420 struct g_provider *pp;
421 struct g_consumer *cp;
425 * We must have a pathname, and it must be absolute.
427 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
428 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
434 if ((owned = mtx_owned(&Giant)))
439 * If we're creating pool, just find GEOM provider by its name
440 * and ignore GUID mismatches.
442 if (vd->vdev_spa->spa_load_state == SPA_LOAD_NONE)
443 cp = vdev_geom_open_by_path(vd, 0);
445 cp = vdev_geom_open_by_path(vd, 1);
448 * The device at vd->vdev_path doesn't have the
449 * expected guid. The disks might have merely
450 * moved around so try all other GEOM providers
451 * to find one with the right guid.
453 cp = vdev_geom_open_by_guid(vd);
458 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
460 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
464 for (i = 0; i < 5; i++) {
465 error = g_access(cp, 0, 1, 0);
469 tsleep(vd, 0, "vdev", hz / 2);
473 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
474 vd->vdev_path, error);
475 vdev_geom_detach(cp, 0);
483 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
492 * Determine the actual size of the device.
494 *psize = pp->mediasize;
497 * Determine the device's minimum transfer size.
499 *ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
502 * Clear the nowritecache bit, so that on a vdev_reopen() we will
505 vd->vdev_nowritecache = B_FALSE;
511 vdev_geom_close(vdev_t *vd)
513 struct g_consumer *cp;
519 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
523 vdev_geom_io_intr(struct bio *bp)
527 zio = bp->bio_caller1;
528 zio->io_error = bp->bio_error;
529 if (zio->io_error == 0 && bp->bio_resid != 0)
531 if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
535 * If we get ENOTSUP, we know that no future
536 * attempts will ever succeed. In this case we
537 * set a persistent bit so that we don't bother
538 * with the ioctl in the future.
541 vd->vdev_nowritecache = B_TRUE;
548 vdev_geom_io_start(zio_t *zio)
551 struct g_consumer *cp;
557 if (zio->io_type == ZIO_TYPE_IOCTL) {
559 if (!vdev_readable(vd)) {
560 zio->io_error = ENXIO;
561 return (ZIO_PIPELINE_CONTINUE);
564 switch (zio->io_cmd) {
566 case DKIOCFLUSHWRITECACHE:
568 if (zfs_nocacheflush)
571 if (vd->vdev_nowritecache) {
572 zio->io_error = ENOTSUP;
578 zio->io_error = ENOTSUP;
581 return (ZIO_PIPELINE_CONTINUE);
586 zio->io_error = ENXIO;
587 return (ZIO_PIPELINE_CONTINUE);
590 bp->bio_caller1 = zio;
591 switch (zio->io_type) {
594 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
595 bp->bio_data = zio->io_data;
596 bp->bio_offset = zio->io_offset;
597 bp->bio_length = zio->io_size;
600 bp->bio_cmd = BIO_FLUSH;
601 bp->bio_flags |= BIO_ORDERED;
603 bp->bio_offset = cp->provider->mediasize;
607 bp->bio_done = vdev_geom_io_intr;
609 g_io_request(bp, cp);
611 return (ZIO_PIPELINE_STOP);
615 vdev_geom_io_done(zio_t *zio)
619 vdev_ops_t vdev_geom_ops = {
626 VDEV_TYPE_DISK, /* name of this vdev type */
627 B_TRUE /* leaf vdev */