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.
25 * Portions Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>
28 #include <sys/zfs_context.h>
29 #include <sys/param.h>
30 #include <sys/kernel.h>
34 #include <sys/spa_impl.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/fs/zfs.h>
38 #include <geom/geom.h>
39 #include <geom/geom_int.h>
42 * Virtual device vector for GEOM.
45 static g_attrchanged_t vdev_geom_attrchanged;
46 struct g_class zfs_vdev_class = {
49 .attrchanged = vdev_geom_attrchanged,
52 struct consumer_vdev_elem {
53 SLIST_ENTRY(consumer_vdev_elem) elems;
57 SLIST_HEAD(consumer_priv_t, consumer_vdev_elem);
58 _Static_assert(sizeof(((struct g_consumer*)NULL)->private)
59 == sizeof(struct consumer_priv_t*),
60 "consumer_priv_t* can't be stored in g_consumer.private");
62 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
64 SYSCTL_DECL(_vfs_zfs_vdev);
65 /* Don't send BIO_FLUSH. */
66 static int vdev_geom_bio_flush_disable;
67 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_flush_disable, CTLFLAG_RWTUN,
68 &vdev_geom_bio_flush_disable, 0, "Disable BIO_FLUSH");
69 /* Don't send BIO_DELETE. */
70 static int vdev_geom_bio_delete_disable;
71 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_delete_disable, CTLFLAG_RWTUN,
72 &vdev_geom_bio_delete_disable, 0, "Disable BIO_DELETE");
74 /* Declare local functions */
75 static void vdev_geom_detach(struct g_consumer *cp, boolean_t open_for_read);
78 * Thread local storage used to indicate when a thread is probing geoms
79 * for their guids. If NULL, this thread is not tasting geoms. If non NULL,
80 * it is looking for a replacement for the vdev_t* that is its value.
82 uint_t zfs_geom_probe_vdev_key;
85 vdev_geom_set_rotation_rate(vdev_t *vd, struct g_consumer *cp)
90 error = g_getattr("GEOM::rotation_rate", cp, &rate);
92 vd->vdev_rotation_rate = rate;
94 vd->vdev_rotation_rate = VDEV_RATE_UNKNOWN;
98 vdev_geom_set_physpath(vdev_t *vd, struct g_consumer *cp,
99 boolean_t do_null_update)
101 boolean_t needs_update = B_FALSE;
103 int error, physpath_len;
105 physpath_len = MAXPATHLEN;
106 physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
107 error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
111 /* g_topology lock ensures that vdev has not been closed */
113 old_physpath = vd->vdev_physpath;
114 vd->vdev_physpath = spa_strdup(physpath);
116 if (old_physpath != NULL) {
117 needs_update = (strcmp(old_physpath,
118 vd->vdev_physpath) != 0);
119 spa_strfree(old_physpath);
121 needs_update = do_null_update;
126 * If the physical path changed, update the config.
127 * Only request an update for previously unset physpaths if
128 * requested by the caller.
131 spa_async_request(vd->vdev_spa, SPA_ASYNC_CONFIG_UPDATE);
136 vdev_geom_attrchanged(struct g_consumer *cp, const char *attr)
139 struct consumer_priv_t *priv;
140 struct consumer_vdev_elem *elem;
143 priv = (struct consumer_priv_t*)&cp->private;
144 if (SLIST_EMPTY(priv))
147 SLIST_FOREACH(elem, priv, elems) {
148 vdev_t *vd = elem->vd;
149 if (strcmp(attr, "GEOM::rotation_rate") == 0) {
150 vdev_geom_set_rotation_rate(vd, cp);
153 if (strcmp(attr, "GEOM::physpath") == 0) {
154 vdev_geom_set_physpath(vd, cp, /*null_update*/B_TRUE);
161 vdev_geom_orphan(struct g_consumer *cp)
163 struct consumer_priv_t *priv;
164 struct consumer_vdev_elem *elem;
168 priv = (struct consumer_priv_t*)&cp->private;
169 if (SLIST_EMPTY(priv))
170 /* Vdev close in progress. Ignore the event. */
174 * Orphan callbacks occur from the GEOM event thread.
175 * Concurrent with this call, new I/O requests may be
176 * working their way through GEOM about to find out
177 * (only once executed by the g_down thread) that we've
178 * been orphaned from our disk provider. These I/Os
179 * must be retired before we can detach our consumer.
180 * This is most easily achieved by acquiring the
181 * SPA ZIO configuration lock as a writer, but doing
182 * so with the GEOM topology lock held would cause
183 * a lock order reversal. Instead, rely on the SPA's
184 * async removal support to invoke a close on this
185 * vdev once it is safe to do so.
187 SLIST_FOREACH(elem, priv, elems) {
188 vdev_t *vd = elem->vd;
190 vd->vdev_remove_wanted = B_TRUE;
191 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
195 static struct g_consumer *
196 vdev_geom_attach(struct g_provider *pp, vdev_t *vd)
199 struct g_consumer *cp;
204 ZFS_LOG(1, "Attaching to %s.", pp->name);
206 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize)) {
207 ZFS_LOG(1, "Failing attach of %s. Incompatible sectorsize %d\n",
208 pp->name, pp->sectorsize);
210 } else if (pp->mediasize < SPA_MINDEVSIZE) {
211 ZFS_LOG(1, "Failing attach of %s. Incompatible mediasize %ju\n",
212 pp->name, pp->mediasize);
216 /* Do we have geom already? No? Create one. */
217 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
218 if (gp->flags & G_GEOM_WITHER)
220 if (strcmp(gp->name, "zfs::vdev") != 0)
225 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
226 gp->orphan = vdev_geom_orphan;
227 gp->attrchanged = vdev_geom_attrchanged;
228 cp = g_new_consumer(gp);
229 error = g_attach(cp, pp);
231 ZFS_LOG(1, "%s(%d): g_attach failed: %d\n", __func__,
233 vdev_geom_detach(cp, B_FALSE);
236 error = g_access(cp, 1, 0, 1);
238 ZFS_LOG(1, "%s(%d): g_access failed: %d", __func__,
240 vdev_geom_detach(cp, B_FALSE);
243 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
245 /* Check if we are already connected to this provider. */
246 LIST_FOREACH(cp, &gp->consumer, consumer) {
247 if (cp->provider == pp) {
248 ZFS_LOG(1, "Found consumer for %s.", pp->name);
253 cp = g_new_consumer(gp);
254 error = g_attach(cp, pp);
256 ZFS_LOG(1, "%s(%d): g_attach failed: %d\n",
257 __func__, __LINE__, error);
258 vdev_geom_detach(cp, B_FALSE);
261 error = g_access(cp, 1, 0, 1);
263 ZFS_LOG(1, "%s(%d): g_access failed: %d\n",
264 __func__, __LINE__, error);
265 vdev_geom_detach(cp, B_FALSE);
268 ZFS_LOG(1, "Created consumer for %s.", pp->name);
270 error = g_access(cp, 1, 0, 1);
272 ZFS_LOG(1, "%s(%d): g_access failed: %d\n",
273 __func__, __LINE__, error);
276 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
283 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
288 vdev_geom_detach(struct g_consumer *cp, boolean_t open_for_read)
294 ZFS_LOG(1, "Detaching from %s.",
295 cp->provider && cp->provider->name ? cp->provider->name : "NULL");
299 g_access(cp, -1, 0, -1);
300 /* Destroy consumer on last close. */
301 if (cp->acr == 0 && cp->ace == 0) {
303 g_access(cp, 0, -cp->acw, 0);
304 if (cp->provider != NULL) {
305 ZFS_LOG(1, "Destroying consumer for %s.",
306 cp->provider->name ? cp->provider->name : "NULL");
309 g_destroy_consumer(cp);
311 /* Destroy geom if there are no consumers left. */
312 if (LIST_EMPTY(&gp->consumer)) {
313 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
314 g_wither_geom(gp, ENXIO);
319 vdev_geom_close_locked(vdev_t *vd)
321 struct g_consumer *cp;
322 struct consumer_priv_t *priv;
323 struct consumer_vdev_elem *elem, *elem_temp;
328 vd->vdev_delayed_close = B_FALSE;
332 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
333 KASSERT(cp->private != NULL, ("%s: cp->private is NULL", __func__));
334 priv = (struct consumer_priv_t*)&cp->private;
336 SLIST_FOREACH_SAFE(elem, priv, elems, elem_temp) {
337 if (elem->vd == vd) {
338 SLIST_REMOVE(priv, elem, consumer_vdev_elem, elems);
343 vdev_geom_detach(cp, B_TRUE);
347 * Issue one or more bios to the vdev in parallel
348 * cmds, datas, offsets, errors, and sizes are arrays of length ncmds. Each IO
349 * operation is described by parallel entries from each array. There may be
350 * more bios actually issued than entries in the array
353 vdev_geom_io(struct g_consumer *cp, int *cmds, void **datas, off_t *offsets,
354 off_t *sizes, int *errors, int ncmds)
358 off_t off, maxio, s, end;
362 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
365 /* How many bios are required for all commands ? */
366 for (i = 0; i < ncmds; i++)
367 n_bios += (sizes[i] + maxio - 1) / maxio;
369 /* Allocate memory for the bios */
370 bios_size = n_bios * sizeof(struct bio*);
371 bios = kmem_zalloc(bios_size, KM_SLEEP);
373 /* Prepare and issue all of the bios */
374 for (i = j = 0; i < ncmds; i++) {
379 ASSERT((off % cp->provider->sectorsize) == 0);
380 ASSERT((s % cp->provider->sectorsize) == 0);
382 for (; off < end; off += maxio, p += maxio, s -= maxio, j++) {
383 bios[j] = g_alloc_bio();
384 bios[j]->bio_cmd = cmds[i];
385 bios[j]->bio_done = NULL;
386 bios[j]->bio_offset = off;
387 bios[j]->bio_length = MIN(s, maxio);
388 bios[j]->bio_data = p;
389 g_io_request(bios[j], cp);
394 /* Wait for all of the bios to complete, and clean them up */
395 for (i = j = 0; i < ncmds; i++) {
400 for (; off < end; off += maxio, s -= maxio, j++) {
401 errors[i] = biowait(bios[j], "vdev_geom_io") || errors[i];
402 g_destroy_bio(bios[j]);
405 kmem_free(bios, bios_size);
409 * Read the vdev config from a device. Return the number of valid labels that
410 * were found. The vdev config will be returned in config if and only if at
411 * least one valid label was found.
414 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **config)
416 struct g_provider *pp;
417 vdev_phys_t *vdev_lists[VDEV_LABELS];
420 uint64_t psize, state, txg;
421 off_t offsets[VDEV_LABELS];
423 off_t sizes[VDEV_LABELS];
424 int cmds[VDEV_LABELS];
425 int errors[VDEV_LABELS];
428 g_topology_assert_not();
431 ZFS_LOG(1, "Reading config from %s...", pp->name);
433 psize = pp->mediasize;
434 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
436 size = sizeof(*vdev_lists[0]) + pp->sectorsize -
437 ((sizeof(*vdev_lists[0]) - 1) % pp->sectorsize) - 1;
439 buflen = sizeof(vdev_lists[0]->vp_nvlist);
442 /* Create all of the IO requests */
443 for (l = 0; l < VDEV_LABELS; l++) {
445 vdev_lists[l] = kmem_alloc(size, KM_SLEEP);
446 offsets[l] = vdev_label_offset(psize, l, 0) + VDEV_SKIP_SIZE;
449 ASSERT(offsets[l] % pp->sectorsize == 0);
452 /* Issue the IO requests */
453 vdev_geom_io(cp, cmds, (void**)vdev_lists, offsets, sizes, errors,
456 /* Parse the labels */
458 for (l = 0; l < VDEV_LABELS; l++) {
462 buf = vdev_lists[l]->vp_nvlist;
464 if (nvlist_unpack(buf, buflen, config, 0) != 0)
467 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
468 &state) != 0 || state > POOL_STATE_L2CACHE) {
469 nvlist_free(*config);
474 if (state != POOL_STATE_SPARE &&
475 state != POOL_STATE_L2CACHE &&
476 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
477 &txg) != 0 || txg == 0)) {
478 nvlist_free(*config);
486 /* Free the label storage */
487 for (l = 0; l < VDEV_LABELS; l++)
488 kmem_free(vdev_lists[l], size);
494 resize_configs(nvlist_t ***configs, uint64_t *count, uint64_t id)
496 nvlist_t **new_configs;
501 new_configs = kmem_zalloc((id + 1) * sizeof(nvlist_t *),
503 for (i = 0; i < *count; i++)
504 new_configs[i] = (*configs)[i];
505 if (*configs != NULL)
506 kmem_free(*configs, *count * sizeof(void *));
507 *configs = new_configs;
512 process_vdev_config(nvlist_t ***configs, uint64_t *count, nvlist_t *cfg,
513 const char *name, uint64_t* known_pool_guid)
517 uint64_t vdev_guid, known_guid;
518 uint64_t id, txg, known_txg;
522 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
523 strcmp(pname, name) != 0)
526 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
529 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_TOP_GUID, &vdev_guid) != 0)
532 if (nvlist_lookup_nvlist(cfg, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0)
535 if (nvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ID, &id) != 0)
538 VERIFY(nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
540 if (*known_pool_guid != 0) {
541 if (pool_guid != *known_pool_guid)
544 *known_pool_guid = pool_guid;
546 resize_configs(configs, count, id);
548 if ((*configs)[id] != NULL) {
549 VERIFY(nvlist_lookup_uint64((*configs)[id],
550 ZPOOL_CONFIG_POOL_TXG, &known_txg) == 0);
551 if (txg <= known_txg)
553 nvlist_free((*configs)[id]);
556 (*configs)[id] = cfg;
564 vdev_geom_read_pool_label(const char *name,
565 nvlist_t ***configs, uint64_t *count)
569 struct g_provider *pp;
570 struct g_consumer *zcp;
581 LIST_FOREACH(mp, &g_classes, class) {
582 if (mp == &zfs_vdev_class)
584 LIST_FOREACH(gp, &mp->geom, geom) {
585 if (gp->flags & G_GEOM_WITHER)
587 LIST_FOREACH(pp, &gp->provider, provider) {
588 if (pp->flags & G_PF_WITHER)
590 zcp = vdev_geom_attach(pp, NULL);
594 nlabels = vdev_geom_read_config(zcp, &vdev_cfg);
596 vdev_geom_detach(zcp, B_TRUE);
599 ZFS_LOG(1, "successfully read vdev config");
601 process_vdev_config(configs, count,
602 vdev_cfg, name, &pool_guid);
609 return (*count > 0 ? 0 : ENOENT);
613 NO_MATCH = 0, /* No matching labels found */
614 TOPGUID_MATCH = 1, /* Labels match top guid, not vdev guid*/
615 ZERO_MATCH = 1, /* Should never be returned */
616 ONE_MATCH = 2, /* 1 label matching the vdev_guid */
617 TWO_MATCH = 3, /* 2 label matching the vdev_guid */
618 THREE_MATCH = 4, /* 3 label matching the vdev_guid */
619 FULL_MATCH = 5 /* all labels match the vdev_guid */
623 vdev_attach_ok(vdev_t *vd, struct g_provider *pp)
626 uint64_t pool_guid, top_guid, vdev_guid;
627 struct g_consumer *cp;
630 cp = vdev_geom_attach(pp, NULL);
632 ZFS_LOG(1, "Unable to attach tasting instance to %s.",
637 nlabels = vdev_geom_read_config(cp, &config);
640 vdev_geom_detach(cp, B_TRUE);
641 ZFS_LOG(1, "Unable to read config from %s.", pp->name);
645 vdev_geom_detach(cp, B_TRUE);
648 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid);
650 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, &top_guid);
652 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid);
656 * Check that the label's pool guid matches the desired guid.
657 * Inactive spares and L2ARCs do not have any pool guid in the label.
659 if (pool_guid != 0 && pool_guid != spa_guid(vd->vdev_spa)) {
660 ZFS_LOG(1, "pool guid mismatch for provider %s: %ju != %ju.",
662 (uintmax_t)spa_guid(vd->vdev_spa), (uintmax_t)pool_guid);
667 * Check that the label's vdev guid matches the desired guid.
668 * The second condition handles possible race on vdev detach, when
669 * remaining vdev receives GUID of destroyed top level mirror vdev.
671 if (vdev_guid == vd->vdev_guid) {
672 ZFS_LOG(1, "guids match for provider %s.", pp->name);
673 return (ZERO_MATCH + nlabels);
674 } else if (top_guid == vd->vdev_guid && vd == vd->vdev_top) {
675 ZFS_LOG(1, "top vdev guid match for provider %s.", pp->name);
676 return (TOPGUID_MATCH);
678 ZFS_LOG(1, "vdev guid mismatch for provider %s: %ju != %ju.",
679 pp->name, (uintmax_t)vd->vdev_guid, (uintmax_t)vdev_guid);
683 static struct g_consumer *
684 vdev_geom_attach_by_guids(vdev_t *vd)
688 struct g_provider *pp, *best_pp;
689 struct g_consumer *cp;
690 enum match match, best_match;
696 best_match = NO_MATCH;
697 LIST_FOREACH(mp, &g_classes, class) {
698 if (mp == &zfs_vdev_class)
700 LIST_FOREACH(gp, &mp->geom, geom) {
701 if (gp->flags & G_GEOM_WITHER)
703 LIST_FOREACH(pp, &gp->provider, provider) {
704 match = vdev_attach_ok(vd, pp);
705 if (match > best_match) {
709 if (match == FULL_MATCH)
717 cp = vdev_geom_attach(best_pp, vd);
719 printf("ZFS WARNING: Unable to attach to %s.\n",
726 static struct g_consumer *
727 vdev_geom_open_by_guids(vdev_t *vd)
729 struct g_consumer *cp;
735 ZFS_LOG(1, "Searching by guids [%ju:%ju].",
736 (uintmax_t)spa_guid(vd->vdev_spa), (uintmax_t)vd->vdev_guid);
737 cp = vdev_geom_attach_by_guids(vd);
739 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
740 buf = kmem_alloc(len, KM_SLEEP);
742 snprintf(buf, len, "/dev/%s", cp->provider->name);
743 spa_strfree(vd->vdev_path);
746 ZFS_LOG(1, "Attach by guid [%ju:%ju] succeeded, provider %s.",
747 (uintmax_t)spa_guid(vd->vdev_spa),
748 (uintmax_t)vd->vdev_guid, cp->provider->name);
750 ZFS_LOG(1, "Search by guid [%ju:%ju] failed.",
751 (uintmax_t)spa_guid(vd->vdev_spa),
752 (uintmax_t)vd->vdev_guid);
758 static struct g_consumer *
759 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
761 struct g_provider *pp;
762 struct g_consumer *cp;
767 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
769 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
770 if (!check_guid || vdev_attach_ok(vd, pp) == FULL_MATCH)
771 cp = vdev_geom_attach(pp, vd);
778 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
779 uint64_t *logical_ashift, uint64_t *physical_ashift)
781 struct g_provider *pp;
782 struct g_consumer *cp;
786 /* Set the TLS to indicate downstack that we should not access zvols*/
787 VERIFY(tsd_set(zfs_geom_probe_vdev_key, vd) == 0);
790 * We must have a pathname, and it must be absolute.
792 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
793 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
798 * Reopen the device if it's not currently open. Otherwise,
799 * just update the physical size of the device.
801 if ((cp = vd->vdev_tsd) != NULL) {
802 ASSERT(vd->vdev_reopening);
810 if (vd->vdev_spa->spa_splitting_newspa ||
811 (vd->vdev_prevstate == VDEV_STATE_UNKNOWN &&
812 vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
813 vd->vdev_spa->spa_load_state == SPA_LOAD_CREATE)) {
815 * We are dealing with a vdev that hasn't been previously
816 * opened (since boot), and we are not loading an
817 * existing pool configuration. This looks like a
818 * vdev add operation to a new or existing pool.
819 * Assume the user knows what he/she is doing and find
820 * GEOM provider by its name, ignoring GUID mismatches.
822 * XXPOLICY: It would be safer to only allow a device
823 * that is unlabeled or labeled but missing
824 * GUID information to be opened in this fashion,
825 * unless we are doing a split, in which case we
826 * should allow any guid.
828 cp = vdev_geom_open_by_path(vd, 0);
831 * Try using the recorded path for this device, but only
832 * accept it if its label data contains the expected GUIDs.
834 cp = vdev_geom_open_by_path(vd, 1);
837 * The device at vd->vdev_path doesn't have the
838 * expected GUIDs. The disks might have merely
839 * moved around so try all other GEOM providers
840 * to find one with the right GUIDs.
842 cp = vdev_geom_open_by_guids(vd);
846 /* Clear the TLS now that tasting is done */
847 VERIFY(tsd_set(zfs_geom_probe_vdev_key, NULL) == 0);
850 ZFS_LOG(1, "Vdev %s not found.", vd->vdev_path);
852 } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
853 !ISP2(cp->provider->sectorsize)) {
854 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
857 vdev_geom_close_locked(vd);
860 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
863 for (i = 0; i < 5; i++) {
864 error = g_access(cp, 0, 1, 0);
868 tsleep(vd, 0, "vdev", hz / 2);
872 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
873 cp->provider->name, error);
874 vdev_geom_close_locked(vd);
879 struct consumer_priv_t *priv;
880 struct consumer_vdev_elem *elem;
882 priv = (struct consumer_priv_t*)&cp->private;
883 if (cp->private == NULL)
885 elem = g_malloc(sizeof(*elem), M_WAITOK|M_ZERO);
887 SLIST_INSERT_HEAD(priv, elem, elems);
890 /* Fetch initial physical path information for this device. */
892 vdev_geom_attrchanged(cp, "GEOM::physpath");
894 /* Set other GEOM characteristics */
895 vdev_geom_set_physpath(vd, cp, /*do_null_update*/B_FALSE);
896 vdev_geom_set_rotation_rate(vd, cp);
902 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
909 * Determine the actual size of the device.
911 *max_psize = *psize = pp->mediasize;
914 * Determine the device's minimum transfer size and preferred
917 *logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
918 *physical_ashift = 0;
919 if (pp->stripesize > (1 << *logical_ashift) && ISP2(pp->stripesize) &&
920 pp->stripesize <= (1 << SPA_MAXASHIFT) && pp->stripeoffset == 0)
921 *physical_ashift = highbit(pp->stripesize) - 1;
924 * Clear the nowritecache settings, so that on a vdev_reopen()
927 vd->vdev_nowritecache = B_FALSE;
933 vdev_geom_close(vdev_t *vd)
936 if (vd->vdev_reopening)
941 vdev_geom_close_locked(vd);
947 vdev_geom_io_intr(struct bio *bp)
952 zio = bp->bio_caller1;
954 zio->io_error = bp->bio_error;
955 if (zio->io_error == 0 && bp->bio_resid != 0)
956 zio->io_error = SET_ERROR(EIO);
958 switch(zio->io_error) {
961 * If we get ENOTSUP for BIO_FLUSH or BIO_DELETE we know
962 * that future attempts will never succeed. In this case
963 * we set a persistent flag so that we don't bother with
964 * requests in the future.
966 switch(bp->bio_cmd) {
968 vd->vdev_nowritecache = B_TRUE;
971 vd->vdev_notrim = B_TRUE;
976 if (!vd->vdev_remove_wanted) {
978 * If provider's error is set we assume it is being
981 if (bp->bio_to->error != 0) {
982 vd->vdev_remove_wanted = B_TRUE;
983 spa_async_request(zio->io_spa,
985 } else if (!vd->vdev_delayed_close) {
986 vd->vdev_delayed_close = B_TRUE;
993 * We have to split bio freeing into two parts, because the ABD code
994 * cannot be called in this context and vdev_op_io_done is not called
995 * for ZIO_TYPE_IOCTL zio-s.
997 if (zio->io_type != ZIO_TYPE_READ && zio->io_type != ZIO_TYPE_WRITE) {
1001 zio_delay_interrupt(zio);
1005 vdev_geom_io_start(zio_t *zio)
1008 struct g_consumer *cp;
1014 switch (zio->io_type) {
1015 case ZIO_TYPE_IOCTL:
1017 if (!vdev_readable(vd)) {
1018 zio->io_error = SET_ERROR(ENXIO);
1022 switch (zio->io_cmd) {
1023 case DKIOCFLUSHWRITECACHE:
1024 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
1026 if (vd->vdev_nowritecache) {
1027 zio->io_error = SET_ERROR(ENOTSUP);
1032 zio->io_error = SET_ERROR(ENOTSUP);
1039 if (vd->vdev_notrim) {
1040 zio->io_error = SET_ERROR(ENOTSUP);
1041 } else if (!vdev_geom_bio_delete_disable) {
1048 ASSERT(zio->io_type == ZIO_TYPE_READ ||
1049 zio->io_type == ZIO_TYPE_WRITE ||
1050 zio->io_type == ZIO_TYPE_FREE ||
1051 zio->io_type == ZIO_TYPE_IOCTL);
1055 zio->io_error = SET_ERROR(ENXIO);
1060 bp->bio_caller1 = zio;
1061 switch (zio->io_type) {
1063 case ZIO_TYPE_WRITE:
1064 zio->io_target_timestamp = zio_handle_io_delay(zio);
1065 bp->bio_offset = zio->io_offset;
1066 bp->bio_length = zio->io_size;
1067 if (zio->io_type == ZIO_TYPE_READ) {
1068 bp->bio_cmd = BIO_READ;
1070 abd_borrow_buf(zio->io_abd, zio->io_size);
1072 bp->bio_cmd = BIO_WRITE;
1074 abd_borrow_buf_copy(zio->io_abd, zio->io_size);
1078 bp->bio_cmd = BIO_DELETE;
1079 bp->bio_data = NULL;
1080 bp->bio_offset = zio->io_offset;
1081 bp->bio_length = zio->io_size;
1083 case ZIO_TYPE_IOCTL:
1084 bp->bio_cmd = BIO_FLUSH;
1085 bp->bio_flags |= BIO_ORDERED;
1086 bp->bio_data = NULL;
1087 bp->bio_offset = cp->provider->mediasize;
1091 bp->bio_done = vdev_geom_io_intr;
1094 g_io_request(bp, cp);
1098 vdev_geom_io_done(zio_t *zio)
1100 struct bio *bp = zio->io_bio;
1102 if (zio->io_type != ZIO_TYPE_READ && zio->io_type != ZIO_TYPE_WRITE) {
1108 ASSERT3S(zio->io_error, ==, ENXIO);
1112 if (zio->io_type == ZIO_TYPE_READ)
1113 abd_return_buf_copy(zio->io_abd, bp->bio_data, zio->io_size);
1115 abd_return_buf(zio->io_abd, bp->bio_data, zio->io_size);
1122 vdev_geom_hold(vdev_t *vd)
1127 vdev_geom_rele(vdev_t *vd)
1131 vdev_ops_t vdev_geom_ops = {
1140 VDEV_TYPE_DISK, /* name of this vdev type */
1141 B_TRUE /* leaf vdev */