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/vdev_impl.h>
33 #include <sys/fs/zfs.h>
35 #include <geom/geom.h>
36 #include <geom/geom_int.h>
39 * Virtual device vector for GEOM.
42 struct g_class zfs_vdev_class = {
47 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
49 typedef struct vdev_geom_ctx {
50 struct g_consumer *gc_consumer;
52 struct bio_queue_head gc_queue;
53 struct mtx gc_queue_mtx;
57 vdev_geom_release(vdev_t *vd)
64 mtx_lock(&ctx->gc_queue_mtx);
66 wakeup_one(&ctx->gc_queue);
67 while (ctx->gc_state != 2)
68 msleep(&ctx->gc_state, &ctx->gc_queue_mtx, 0, "vgeom:w", 0);
69 mtx_unlock(&ctx->gc_queue_mtx);
70 mtx_destroy(&ctx->gc_queue_mtx);
71 kmem_free(ctx, sizeof(*ctx));
75 vdev_geom_orphan(struct g_consumer *cp)
85 error = cp->provider->error;
87 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
88 if (cp->acr + cp->acw + cp->ace > 0)
89 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
90 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
92 g_destroy_consumer(cp);
93 /* Destroy geom if there are no consumers left. */
94 if (LIST_EMPTY(&gp->consumer)) {
95 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
96 g_wither_geom(gp, error);
98 vdev_geom_release(vd);
100 vd->vdev_remove_wanted = B_TRUE;
101 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
104 static struct g_consumer *
105 vdev_geom_attach(struct g_provider *pp, int write)
108 struct g_consumer *cp;
112 ZFS_LOG(1, "Attaching to %s.", pp->name);
113 /* Do we have geom already? No? Create one. */
114 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
115 if (gp->flags & G_GEOM_WITHER)
117 if (strcmp(gp->name, "zfs::vdev") != 0)
122 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
123 gp->orphan = vdev_geom_orphan;
124 cp = g_new_consumer(gp);
125 if (g_attach(cp, pp) != 0) {
126 g_wither_geom(gp, ENXIO);
129 if (g_access(cp, 1, write, 1) != 0) {
130 g_wither_geom(gp, ENXIO);
133 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
135 /* Check if we are already connected to this provider. */
136 LIST_FOREACH(cp, &gp->consumer, consumer) {
137 if (cp->provider == pp) {
138 ZFS_LOG(1, "Found consumer for %s.", pp->name);
143 cp = g_new_consumer(gp);
144 if (g_attach(cp, pp) != 0) {
145 g_destroy_consumer(cp);
148 if (g_access(cp, 1, write, 1) != 0) {
150 g_destroy_consumer(cp);
153 ZFS_LOG(1, "Created consumer for %s.", pp->name);
155 if (g_access(cp, 1, cp->acw > 0 ? 0 : write, 1) != 0)
157 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
164 vdev_geom_detach(void *arg, int flag __unused)
167 struct g_consumer *cp;
173 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
174 g_access(cp, -1, 0, -1);
175 /* Destroy consumer on last close. */
176 if (cp->acr == 0 && cp->ace == 0) {
177 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
179 g_access(cp, 0, -cp->acw, 0);
181 g_destroy_consumer(cp);
183 /* Destroy geom if there are no consumers left. */
184 if (LIST_EMPTY(&gp->consumer)) {
185 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
186 g_wither_geom(gp, ENXIO);
191 vdev_geom_worker(void *arg)
193 vdev_geom_ctx_t *ctx;
197 thread_lock(curthread);
198 sched_prio(curthread, PRIBIO);
199 thread_unlock(curthread);
203 mtx_lock(&ctx->gc_queue_mtx);
204 bp = bioq_takefirst(&ctx->gc_queue);
206 if (ctx->gc_state == 1) {
208 wakeup_one(&ctx->gc_state);
209 mtx_unlock(&ctx->gc_queue_mtx);
212 msleep(&ctx->gc_queue, &ctx->gc_queue_mtx,
213 PRIBIO | PDROP, "vgeom:io", 0);
216 mtx_unlock(&ctx->gc_queue_mtx);
217 zio = bp->bio_caller1;
218 zio->io_error = bp->bio_error;
219 if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
223 * If we get ENOTSUP, we know that no future
224 * attempts will ever succeed. In this case we
225 * set a persistent bit so that we don't bother
226 * with the ioctl in the future.
229 vd->vdev_nowritecache = B_TRUE;
237 nvlist_get_guid(nvlist_t *list)
239 nvpair_t *elem = NULL;
242 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
243 if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
244 strcmp(nvpair_name(elem), "guid") == 0) {
245 VERIFY(nvpair_value_uint64(elem, &value) == 0);
253 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
260 ASSERT((offset % cp->provider->sectorsize) == 0);
261 ASSERT((size % cp->provider->sectorsize) == 0);
269 for (; off < offset; off += MAXPHYS, p += MAXPHYS, size -= MAXPHYS) {
270 bzero(bp, sizeof(*bp));
273 bp->bio_offset = off;
274 bp->bio_length = MIN(size, MAXPHYS);
276 g_io_request(bp, cp);
277 error = biowait(bp, "vdev_geom_io");
287 vdev_geom_read_guid(struct g_consumer *cp)
289 struct g_provider *pp;
296 int error, l, len, iszvol;
298 g_topology_assert_not();
301 ZFS_LOG(1, "Reading guid from %s...", pp->name);
302 if (g_getattr("ZFS::iszvol", cp, &iszvol) == 0 && iszvol) {
303 ZFS_LOG(1, "Skipping ZVOL-based provider %s.", pp->name);
307 psize = pp->mediasize;
308 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
310 size = sizeof(*label) + pp->sectorsize -
311 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
314 label = kmem_alloc(size, KM_SLEEP);
315 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
317 for (l = 0; l < VDEV_LABELS; l++) {
318 nvlist_t *config = NULL;
320 offset = vdev_label_offset(psize, l, 0);
321 if ((offset % pp->sectorsize) != 0)
324 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
326 buf = label->vl_vdev_phys.vp_nvlist;
328 if (nvlist_unpack(buf, buflen, &config, 0) != 0)
331 guid = nvlist_get_guid(config);
337 kmem_free(label, size);
339 ZFS_LOG(1, "guid for %s is %ju", pp->name, (uintmax_t)guid);
343 struct vdev_geom_find {
346 struct g_consumer *cp;
350 vdev_geom_taste_orphan(struct g_consumer *cp)
353 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
354 cp->provider->name));
358 vdev_geom_attach_by_guid_event(void *arg, int flags __unused)
360 struct vdev_geom_find *ap;
362 struct g_geom *gp, *zgp;
363 struct g_provider *pp;
364 struct g_consumer *zcp;
371 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
372 /* This orphan function should be never called. */
373 zgp->orphan = vdev_geom_taste_orphan;
374 zcp = g_new_consumer(zgp);
376 LIST_FOREACH(mp, &g_classes, class) {
377 if (mp == &zfs_vdev_class)
379 LIST_FOREACH(gp, &mp->geom, geom) {
380 if (gp->flags & G_GEOM_WITHER)
382 LIST_FOREACH(pp, &gp->provider, provider) {
383 if (pp->flags & G_PF_WITHER)
386 if (g_access(zcp, 1, 0, 0) != 0) {
391 guid = vdev_geom_read_guid(zcp);
393 g_access(zcp, -1, 0, 0);
395 if (guid != ap->guid)
397 ap->cp = vdev_geom_attach(pp, ap->write);
398 if (ap->cp == NULL) {
399 printf("ZFS WARNING: Cannot open %s "
400 "for writting.\n", pp->name);
409 g_destroy_consumer(zcp);
413 static struct g_consumer *
414 vdev_geom_attach_by_guid(uint64_t guid, int write)
416 struct vdev_geom_find *ap;
417 struct g_consumer *cp;
419 ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
422 g_waitfor_event(vdev_geom_attach_by_guid_event, ap, M_WAITOK, NULL);
424 kmem_free(ap, sizeof(*ap));
428 static struct g_consumer *
429 vdev_geom_open_by_guid(vdev_t *vd)
431 struct g_consumer *cp;
435 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
436 cp = vdev_geom_attach_by_guid(vd->vdev_guid, !!(spa_mode & FWRITE));
438 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
439 buf = kmem_alloc(len, KM_SLEEP);
441 snprintf(buf, len, "/dev/%s", cp->provider->name);
442 spa_strfree(vd->vdev_path);
445 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
446 (uintmax_t)vd->vdev_guid, vd->vdev_path);
448 ZFS_LOG(1, "Search by guid [%ju] failed.",
449 (uintmax_t)vd->vdev_guid);
455 static struct g_consumer *
456 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
458 struct g_provider *pp;
459 struct g_consumer *cp;
464 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
466 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
467 cp = vdev_geom_attach(pp, !!(spa_mode & FWRITE));
468 if (cp != NULL && check_guid) {
470 guid = vdev_geom_read_guid(cp);
472 if (guid != vd->vdev_guid) {
473 vdev_geom_detach(cp, 0);
475 ZFS_LOG(1, "guid mismatch for provider %s: "
476 "%ju != %ju.", vd->vdev_path,
477 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
479 ZFS_LOG(1, "guid match for provider %s.",
490 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
492 vdev_geom_ctx_t *ctx;
493 struct g_provider *pp;
494 struct g_consumer *cp;
498 * We must have a pathname, and it must be absolute.
500 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
501 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
507 if ((owned = mtx_owned(&Giant)))
509 cp = vdev_geom_open_by_path(vd, 1);
512 * The device at vd->vdev_path doesn't have the expected guid.
513 * The disks might have merely moved around so try all other
514 * geom providers to find one with the right guid.
516 cp = vdev_geom_open_by_guid(vd);
519 cp = vdev_geom_open_by_path(vd, 0);
521 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
522 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
532 ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP);
533 bioq_init(&ctx->gc_queue);
534 mtx_init(&ctx->gc_queue_mtx, "zfs:vdev:geom:queue", NULL, MTX_DEF);
535 ctx->gc_consumer = cp;
541 kproc_kthread_add(vdev_geom_worker, ctx, &zfsproc, NULL, 0, 0,
542 "zfskern", "vdev %s", pp->name);
545 * Determine the actual size of the device.
547 *psize = pp->mediasize;
550 * Determine the device's minimum transfer size.
552 *ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
555 * Clear the nowritecache bit, so that on a vdev_reopen() we will
558 vd->vdev_nowritecache = B_FALSE;
564 vdev_geom_close(vdev_t *vd)
566 vdev_geom_ctx_t *ctx;
567 struct g_consumer *cp;
569 if ((ctx = vd->vdev_tsd) == NULL)
571 if ((cp = ctx->gc_consumer) == NULL)
573 vdev_geom_release(vd);
574 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
578 vdev_geom_io_intr(struct bio *bp)
580 vdev_geom_ctx_t *ctx;
583 zio = bp->bio_caller1;
584 ctx = zio->io_vd->vdev_tsd;
586 if ((zio->io_error = bp->bio_error) == 0 && bp->bio_resid != 0)
589 mtx_lock(&ctx->gc_queue_mtx);
590 bioq_insert_tail(&ctx->gc_queue, bp);
591 wakeup_one(&ctx->gc_queue);
592 mtx_unlock(&ctx->gc_queue_mtx);
596 vdev_geom_io_start(zio_t *zio)
599 vdev_geom_ctx_t *ctx;
600 struct g_consumer *cp;
609 cp = ctx->gc_consumer;
611 if (zio->io_type == ZIO_TYPE_IOCTL) {
613 if (!vdev_readable(vd)) {
614 zio->io_error = ENXIO;
615 return (ZIO_PIPELINE_CONTINUE);
618 switch (zio->io_cmd) {
620 case DKIOCFLUSHWRITECACHE:
622 if (zfs_nocacheflush)
625 if (vd->vdev_nowritecache) {
626 zio->io_error = ENOTSUP;
632 zio->io_error = ENOTSUP;
635 return (ZIO_PIPELINE_CONTINUE);
639 zio->io_error = ENXIO;
640 return (ZIO_PIPELINE_CONTINUE);
643 bp->bio_caller1 = zio;
644 switch (zio->io_type) {
647 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
648 bp->bio_data = zio->io_data;
649 bp->bio_offset = zio->io_offset;
650 bp->bio_length = zio->io_size;
653 bp->bio_cmd = BIO_FLUSH;
655 bp->bio_offset = cp->provider->mediasize;
659 bp->bio_done = vdev_geom_io_intr;
661 g_io_request(bp, cp);
663 return (ZIO_PIPELINE_STOP);
667 vdev_geom_io_done(zio_t *zio)
671 vdev_ops_t vdev_geom_ops = {
678 VDEV_TYPE_DISK, /* name of this vdev type */
679 B_TRUE /* leaf vdev */