]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/vdev_geom.c
s/dettach/detach/g
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / vdev_geom.c
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
23  * All rights reserved.
24  *
25  * Portions Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>
26  */
27
28 #include <sys/zfs_context.h>
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/bio.h>
32 #include <sys/disk.h>
33 #include <sys/spa.h>
34 #include <sys/spa_impl.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/fs/zfs.h>
37 #include <sys/zio.h>
38 #include <geom/geom.h>
39 #include <geom/geom_int.h>
40
41 /*
42  * Virtual device vector for GEOM.
43  */
44
45 struct g_class zfs_vdev_class = {
46         .name = "ZFS::VDEV",
47         .version = G_VERSION,
48 };
49
50 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
51
52 SYSCTL_DECL(_vfs_zfs_vdev);
53 /* Don't send BIO_FLUSH. */
54 static int vdev_geom_bio_flush_disable = 0;
55 TUNABLE_INT("vfs.zfs.vdev.bio_flush_disable", &vdev_geom_bio_flush_disable);
56 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_flush_disable, CTLFLAG_RW,
57     &vdev_geom_bio_flush_disable, 0, "Disable BIO_FLUSH");
58 /* Don't send BIO_DELETE. */
59 static int vdev_geom_bio_delete_disable = 0;
60 TUNABLE_INT("vfs.zfs.vdev.bio_delete_disable", &vdev_geom_bio_delete_disable);
61 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_delete_disable, CTLFLAG_RW,
62     &vdev_geom_bio_delete_disable, 0, "Disable BIO_DELETE");
63
64 static void
65 vdev_geom_orphan(struct g_consumer *cp)
66 {
67         vdev_t *vd;
68
69         g_topology_assert();
70
71         vd = cp->private;
72
73         /*
74          * Orphan callbacks occur from the GEOM event thread.
75          * Concurrent with this call, new I/O requests may be
76          * working their way through GEOM about to find out
77          * (only once executed by the g_down thread) that we've
78          * been orphaned from our disk provider.  These I/Os
79          * must be retired before we can detach our consumer.
80          * This is most easily achieved by acquiring the
81          * SPA ZIO configuration lock as a writer, but doing
82          * so with the GEOM topology lock held would cause
83          * a lock order reversal.  Instead, rely on the SPA's
84          * async removal support to invoke a close on this
85          * vdev once it is safe to do so.
86          */
87         zfs_post_remove(vd->vdev_spa, vd);
88         vd->vdev_remove_wanted = B_TRUE;
89         spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
90 }
91
92 static struct g_consumer *
93 vdev_geom_attach(struct g_provider *pp)
94 {
95         struct g_geom *gp;
96         struct g_consumer *cp;
97
98         g_topology_assert();
99
100         ZFS_LOG(1, "Attaching to %s.", pp->name);
101         /* Do we have geom already? No? Create one. */
102         LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
103                 if (gp->flags & G_GEOM_WITHER)
104                         continue;
105                 if (strcmp(gp->name, "zfs::vdev") != 0)
106                         continue;
107                 break;
108         }
109         if (gp == NULL) {
110                 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
111                 gp->orphan = vdev_geom_orphan;
112                 cp = g_new_consumer(gp);
113                 if (g_attach(cp, pp) != 0) {
114                         g_wither_geom(gp, ENXIO);
115                         return (NULL);
116                 }
117                 if (g_access(cp, 1, 0, 1) != 0) {
118                         g_wither_geom(gp, ENXIO);
119                         return (NULL);
120                 }
121                 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
122         } else {
123                 /* Check if we are already connected to this provider. */
124                 LIST_FOREACH(cp, &gp->consumer, consumer) {
125                         if (cp->provider == pp) {
126                                 ZFS_LOG(1, "Found consumer for %s.", pp->name);
127                                 break;
128                         }
129                 }
130                 if (cp == NULL) {
131                         cp = g_new_consumer(gp);
132                         if (g_attach(cp, pp) != 0) {
133                                 g_destroy_consumer(cp);
134                                 return (NULL);
135                         }
136                         if (g_access(cp, 1, 0, 1) != 0) {
137                                 g_detach(cp);
138                                 g_destroy_consumer(cp);
139                                 return (NULL);
140                         }
141                         ZFS_LOG(1, "Created consumer for %s.", pp->name);
142                 } else {
143                         if (g_access(cp, 1, 0, 1) != 0)
144                                 return (NULL);
145                         ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
146                 }
147         }
148         return (cp);
149 }
150
151 static void
152 vdev_geom_detach(void *arg, int flag __unused)
153 {
154         struct g_geom *gp;
155         struct g_consumer *cp;
156
157         g_topology_assert();
158         cp = arg;
159         gp = cp->geom;
160
161         ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
162         g_access(cp, -1, 0, -1);
163         /* Destroy consumer on last close. */
164         if (cp->acr == 0 && cp->ace == 0) {
165                 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
166                 if (cp->acw > 0)
167                         g_access(cp, 0, -cp->acw, 0);
168                 g_detach(cp);
169                 g_destroy_consumer(cp);
170         }
171         /* Destroy geom if there are no consumers left. */
172         if (LIST_EMPTY(&gp->consumer)) {
173                 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
174                 g_wither_geom(gp, ENXIO);
175         }
176 }
177
178 static uint64_t
179 nvlist_get_guid(nvlist_t *list)
180 {
181         uint64_t value;
182
183         value = 0;
184         nvlist_lookup_uint64(list, ZPOOL_CONFIG_GUID, &value);
185         return (value);
186 }
187
188 static int
189 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
190 {
191         struct bio *bp;
192         u_char *p;
193         off_t off, maxio;
194         int error;
195
196         ASSERT((offset % cp->provider->sectorsize) == 0);
197         ASSERT((size % cp->provider->sectorsize) == 0);
198
199         bp = g_alloc_bio();
200         off = offset;
201         offset += size;
202         p = data;
203         maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
204         error = 0;
205
206         for (; off < offset; off += maxio, p += maxio, size -= maxio) {
207                 bzero(bp, sizeof(*bp));
208                 bp->bio_cmd = cmd;
209                 bp->bio_done = NULL;
210                 bp->bio_offset = off;
211                 bp->bio_length = MIN(size, maxio);
212                 bp->bio_data = p;
213                 g_io_request(bp, cp);
214                 error = biowait(bp, "vdev_geom_io");
215                 if (error != 0)
216                         break;
217         }
218
219         g_destroy_bio(bp);
220         return (error);
221 }
222
223 static void
224 vdev_geom_taste_orphan(struct g_consumer *cp)
225 {
226
227         KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
228             cp->provider->name));
229 }
230
231 static int
232 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **config)
233 {
234         struct g_provider *pp;
235         vdev_label_t *label;
236         char *p, *buf;
237         size_t buflen;
238         uint64_t psize;
239         off_t offset, size;
240         uint64_t guid, state, txg;
241         int error, l, len;
242
243         g_topology_assert_not();
244
245         pp = cp->provider;
246         ZFS_LOG(1, "Reading config from %s...", pp->name);
247
248         psize = pp->mediasize;
249         psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
250
251         size = sizeof(*label) + pp->sectorsize -
252             ((sizeof(*label) - 1) % pp->sectorsize) - 1;
253
254         guid = 0;
255         label = kmem_alloc(size, KM_SLEEP);
256         buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
257
258         *config = NULL;
259         for (l = 0; l < VDEV_LABELS; l++) {
260
261                 offset = vdev_label_offset(psize, l, 0);
262                 if ((offset % pp->sectorsize) != 0)
263                         continue;
264
265                 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
266                         continue;
267                 buf = label->vl_vdev_phys.vp_nvlist;
268
269                 if (nvlist_unpack(buf, buflen, config, 0) != 0)
270                         continue;
271
272                 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
273                     &state) != 0 || state == POOL_STATE_DESTROYED ||
274                     state > POOL_STATE_L2CACHE) {
275                         nvlist_free(*config);
276                         *config = NULL;
277                         continue;
278                 }
279
280                 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
281                     (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
282                     &txg) != 0 || txg == 0)) {
283                         nvlist_free(*config);
284                         *config = NULL;
285                         continue;
286                 }
287
288                 break;
289         }
290
291         kmem_free(label, size);
292         return (*config == NULL ? ENOENT : 0);
293 }
294
295 static int
296 vdev_geom_check_config(nvlist_t *config, const char *name, uint64_t *best_txg)
297 {
298         uint64_t vdev_guid;
299         uint64_t txg;
300         char *pname;
301
302         if (nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
303             strcmp(pname, name) != 0)
304                 return (ENOENT);
305
306         ZFS_LOG(1, "found pool: %s", pname);
307
308         txg = 0;
309         nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg);
310         if (txg <= *best_txg)
311                 return (ENOENT);
312         *best_txg = txg;
313         ZFS_LOG(1, "txg: %ju", (uintmax_t)*best_txg);
314
315         return (0);
316 }
317
318 static int
319 vdev_geom_attach_taster(struct g_consumer *cp, struct g_provider *pp)
320 {
321         int error;
322
323         if (pp->flags & G_PF_WITHER)
324                 return (EINVAL);
325         if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize))
326                 return (EINVAL);
327         g_attach(cp, pp);
328         error = g_access(cp, 1, 0, 0);
329         if (error != 0)
330                 g_detach(cp);
331         return (error);
332 }
333
334 static void
335 vdev_geom_detach_taster(struct g_consumer *cp)
336 {
337         g_access(cp, -1, 0, 0);
338         g_detach(cp);
339 }
340
341 int
342 vdev_geom_read_pool_label(const char *name, nvlist_t **config)
343 {
344         struct g_class *mp;
345         struct g_geom *gp, *zgp;
346         struct g_provider *pp;
347         struct g_consumer *zcp;
348         nvlist_t *vdev_cfg;
349         uint64_t best_txg;
350         int error;
351
352         DROP_GIANT();
353         g_topology_lock();
354
355         zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
356         /* This orphan function should be never called. */
357         zgp->orphan = vdev_geom_taste_orphan;
358         zcp = g_new_consumer(zgp);
359
360         best_txg = 0;
361         *config = NULL;
362         LIST_FOREACH(mp, &g_classes, class) {
363                 if (mp == &zfs_vdev_class)
364                         continue;
365                 LIST_FOREACH(gp, &mp->geom, geom) {
366                         if (gp->flags & G_GEOM_WITHER)
367                                 continue;
368                         LIST_FOREACH(pp, &gp->provider, provider) {
369                                 if (pp->flags & G_PF_WITHER)
370                                         continue;
371                                 if (vdev_geom_attach_taster(zcp, pp) != 0)
372                                         continue;
373                                 g_topology_unlock();
374                                 error = vdev_geom_read_config(zcp, &vdev_cfg);
375                                 g_topology_lock();
376                                 vdev_geom_detach_taster(zcp);
377                                 if (error)
378                                         continue;
379                                 ZFS_LOG(1, "successfully read vdev config");
380
381                                 error = vdev_geom_check_config(vdev_cfg, name,
382                                     &best_txg);
383                                 if (error != 0) {
384                                         nvlist_free(vdev_cfg);
385                                         continue;
386                                 }
387                                 nvlist_free(*config);
388                                 *config = vdev_cfg;
389                         }
390                 }
391         }
392
393         g_destroy_consumer(zcp);
394         g_destroy_geom(zgp);
395         g_topology_unlock();
396         PICKUP_GIANT();
397         return (*config == NULL ? ENOENT : 0);
398 }
399
400 static uint64_t
401 vdev_geom_read_guid(struct g_consumer *cp)
402 {
403         nvlist_t *config;
404         uint64_t guid;
405
406         g_topology_assert_not();
407
408         guid = 0;
409         if (vdev_geom_read_config(cp, &config) == 0) {
410                 guid = nvlist_get_guid(config);
411                 nvlist_free(config);
412         }
413         return (guid);
414 }
415
416 static struct g_consumer *
417 vdev_geom_attach_by_guid(uint64_t guid)
418 {
419         struct g_class *mp;
420         struct g_geom *gp, *zgp;
421         struct g_provider *pp;
422         struct g_consumer *cp, *zcp;
423         uint64_t pguid;
424
425         g_topology_assert();
426
427         zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
428         /* This orphan function should be never called. */
429         zgp->orphan = vdev_geom_taste_orphan;
430         zcp = g_new_consumer(zgp);
431
432         cp = NULL;
433         LIST_FOREACH(mp, &g_classes, class) {
434                 if (mp == &zfs_vdev_class)
435                         continue;
436                 LIST_FOREACH(gp, &mp->geom, geom) {
437                         if (gp->flags & G_GEOM_WITHER)
438                                 continue;
439                         LIST_FOREACH(pp, &gp->provider, provider) {
440                                 if (vdev_geom_attach_taster(zcp, pp) != 0)
441                                         continue;
442                                 g_topology_unlock();
443                                 pguid = vdev_geom_read_guid(zcp);
444                                 g_topology_lock();
445                                 vdev_geom_detach_taster(zcp);
446                                 if (pguid != guid)
447                                         continue;
448                                 cp = vdev_geom_attach(pp);
449                                 if (cp == NULL) {
450                                         printf("ZFS WARNING: Unable to attach to %s.\n",
451                                             pp->name);
452                                         continue;
453                                 }
454                                 break;
455                         }
456                         if (cp != NULL)
457                                 break;
458                 }
459                 if (cp != NULL)
460                         break;
461         }
462 end:
463         g_destroy_consumer(zcp);
464         g_destroy_geom(zgp);
465         return (cp);
466 }
467
468 static struct g_consumer *
469 vdev_geom_open_by_guid(vdev_t *vd)
470 {
471         struct g_consumer *cp;
472         char *buf;
473         size_t len;
474
475         g_topology_assert();
476
477         ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
478         cp = vdev_geom_attach_by_guid(vd->vdev_guid);
479         if (cp != NULL) {
480                 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
481                 buf = kmem_alloc(len, KM_SLEEP);
482
483                 snprintf(buf, len, "/dev/%s", cp->provider->name);
484                 spa_strfree(vd->vdev_path);
485                 vd->vdev_path = buf;
486
487                 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
488                     (uintmax_t)vd->vdev_guid, vd->vdev_path);
489         } else {
490                 ZFS_LOG(1, "Search by guid [%ju] failed.",
491                     (uintmax_t)vd->vdev_guid);
492         }
493
494         return (cp);
495 }
496
497 static struct g_consumer *
498 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
499 {
500         struct g_provider *pp;
501         struct g_consumer *cp;
502         uint64_t guid;
503
504         g_topology_assert();
505
506         cp = NULL;
507         pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
508         if (pp != NULL) {
509                 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
510                 cp = vdev_geom_attach(pp);
511                 if (cp != NULL && check_guid && ISP2(pp->sectorsize) &&
512                     pp->sectorsize <= VDEV_PAD_SIZE) {
513                         g_topology_unlock();
514                         guid = vdev_geom_read_guid(cp);
515                         g_topology_lock();
516                         if (guid != vd->vdev_guid) {
517                                 vdev_geom_detach(cp, 0);
518                                 cp = NULL;
519                                 ZFS_LOG(1, "guid mismatch for provider %s: "
520                                     "%ju != %ju.", vd->vdev_path,
521                                     (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
522                         } else {
523                                 ZFS_LOG(1, "guid match for provider %s.",
524                                     vd->vdev_path);
525                         }
526                 }
527         }
528
529         return (cp);
530 }
531
532 static int
533 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
534     uint64_t *ashift)
535 {
536         struct g_provider *pp;
537         struct g_consumer *cp;
538         size_t bufsize;
539         int error;
540
541         /*
542          * We must have a pathname, and it must be absolute.
543          */
544         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
545                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
546                 return (EINVAL);
547         }
548
549         vd->vdev_tsd = NULL;
550
551         DROP_GIANT();
552         g_topology_lock();
553         error = 0;
554
555         /*
556          * If we're creating or splitting a pool, just find the GEOM provider
557          * by its name and ignore GUID mismatches.
558          */
559         if (vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
560             vd->vdev_spa->spa_splitting_newspa == B_TRUE)
561                 cp = vdev_geom_open_by_path(vd, 0);
562         else {
563                 cp = vdev_geom_open_by_path(vd, 1);
564                 if (cp == NULL) {
565                         /*
566                          * The device at vd->vdev_path doesn't have the
567                          * expected guid. The disks might have merely
568                          * moved around so try all other GEOM providers
569                          * to find one with the right guid.
570                          */
571                         cp = vdev_geom_open_by_guid(vd);
572                 }
573         }
574
575         if (cp == NULL) {
576                 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
577                 error = ENOENT;
578         } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
579             !ISP2(cp->provider->sectorsize)) {
580                 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
581                     vd->vdev_path);
582                 vdev_geom_detach(cp, 0);
583                 error = EINVAL;
584                 cp = NULL;
585         } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
586                 int i;
587
588                 for (i = 0; i < 5; i++) {
589                         error = g_access(cp, 0, 1, 0);
590                         if (error == 0)
591                                 break;
592                         g_topology_unlock();
593                         tsleep(vd, 0, "vdev", hz / 2);
594                         g_topology_lock();
595                 }
596                 if (error != 0) {
597                         printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
598                             vd->vdev_path, error);
599                         vdev_geom_detach(cp, 0);
600                         cp = NULL;
601                 }
602         }
603         g_topology_unlock();
604         PICKUP_GIANT();
605         if (cp == NULL) {
606                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
607                 return (error);
608         }
609
610         cp->private = vd;
611         vd->vdev_tsd = cp;
612         pp = cp->provider;
613
614         /*
615          * Determine the actual size of the device.
616          */
617         *max_psize = *psize = pp->mediasize;
618
619         /*
620          * Determine the device's minimum transfer size.
621          */
622         *ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
623
624         /*
625          * Clear the nowritecache settings, so that on a vdev_reopen()
626          * we will try again.
627          */
628         vd->vdev_nowritecache = B_FALSE;
629
630         if (vd->vdev_physpath != NULL)
631                 spa_strfree(vd->vdev_physpath);
632         bufsize = sizeof("/dev/") + strlen(pp->name);
633         vd->vdev_physpath = kmem_alloc(bufsize, KM_SLEEP);
634         snprintf(vd->vdev_physpath, bufsize, "/dev/%s", pp->name);
635
636         return (0);
637 }
638
639 static void
640 vdev_geom_close(vdev_t *vd)
641 {
642         struct g_consumer *cp;
643
644         cp = vd->vdev_tsd;
645         if (cp == NULL)
646                 return;
647         vd->vdev_tsd = NULL;
648         vd->vdev_delayed_close = B_FALSE;
649         g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
650 }
651
652 static void
653 vdev_geom_io_intr(struct bio *bp)
654 {
655         vdev_t *vd;
656         zio_t *zio;
657
658         zio = bp->bio_caller1;
659         vd = zio->io_vd;
660         zio->io_error = bp->bio_error;
661         if (zio->io_error == 0 && bp->bio_resid != 0)
662                 zio->io_error = EIO;
663         if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
664                 /*
665                  * If we get ENOTSUP, we know that no future
666                  * attempts will ever succeed.  In this case we
667                  * set a persistent bit so that we don't bother
668                  * with the ioctl in the future.
669                  */
670                 vd->vdev_nowritecache = B_TRUE;
671         }
672         if (bp->bio_cmd == BIO_DELETE && bp->bio_error == ENOTSUP) {
673                 /*
674                  * If we get ENOTSUP, we know that no future
675                  * attempts will ever succeed.  In this case we
676                  * set a persistent bit so that we don't bother
677                  * with the ioctl in the future.
678                  */
679                 vd->vdev_notrim = B_TRUE;
680         }
681         if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
682                 /*
683                  * If provider's error is set we assume it is being
684                  * removed.
685                  */
686                 if (bp->bio_to->error != 0) {
687                         /*
688                          * We post the resource as soon as possible, instead of
689                          * when the async removal actually happens, because the
690                          * DE is using this information to discard previous I/O
691                          * errors.
692                          */
693                         /* XXX: zfs_post_remove() can sleep. */
694                         zfs_post_remove(zio->io_spa, vd);
695                         vd->vdev_remove_wanted = B_TRUE;
696                         spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
697                 } else if (!vd->vdev_delayed_close) {
698                         vd->vdev_delayed_close = B_TRUE;
699                 }
700         }
701         g_destroy_bio(bp);
702         zio_interrupt(zio);
703 }
704
705 static int
706 vdev_geom_io_start(zio_t *zio)
707 {
708         vdev_t *vd;
709         struct g_consumer *cp;
710         struct bio *bp;
711         int error;
712
713         vd = zio->io_vd;
714
715         if (zio->io_type == ZIO_TYPE_IOCTL) {
716                 /* XXPOLICY */
717                 if (!vdev_readable(vd)) {
718                         zio->io_error = ENXIO;
719                         return (ZIO_PIPELINE_CONTINUE);
720                 }
721
722                 switch (zio->io_cmd) {
723                 case DKIOCFLUSHWRITECACHE:
724                         if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
725                                 break;
726                         if (vd->vdev_nowritecache) {
727                                 zio->io_error = ENOTSUP;
728                                 break;
729                         }
730                         goto sendreq;
731                 case DKIOCTRIM:
732                         if (vdev_geom_bio_delete_disable)
733                                 break;
734                         if (vd->vdev_notrim) {
735                                 zio->io_error = ENOTSUP;
736                                 break;
737                         }
738                         goto sendreq;
739                 default:
740                         zio->io_error = ENOTSUP;
741                 }
742
743                 return (ZIO_PIPELINE_CONTINUE);
744         }
745 sendreq:
746         cp = vd->vdev_tsd;
747         if (cp == NULL) {
748                 zio->io_error = ENXIO;
749                 return (ZIO_PIPELINE_CONTINUE);
750         }
751         bp = g_alloc_bio();
752         bp->bio_caller1 = zio;
753         switch (zio->io_type) {
754         case ZIO_TYPE_READ:
755         case ZIO_TYPE_WRITE:
756                 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
757                 bp->bio_data = zio->io_data;
758                 bp->bio_offset = zio->io_offset;
759                 bp->bio_length = zio->io_size;
760                 break;
761         case ZIO_TYPE_IOCTL:
762                 switch (zio->io_cmd) {
763                 case DKIOCFLUSHWRITECACHE:
764                         bp->bio_cmd = BIO_FLUSH;
765                         bp->bio_flags |= BIO_ORDERED;
766                         bp->bio_data = NULL;
767                         bp->bio_offset = cp->provider->mediasize;
768                         bp->bio_length = 0;
769                         break;
770                 case DKIOCTRIM:
771                         bp->bio_cmd = BIO_DELETE;
772                         bp->bio_data = NULL;
773                         bp->bio_offset = zio->io_offset;
774                         bp->bio_length = zio->io_size;
775                         break;
776                 }
777                 break;
778         }
779         bp->bio_done = vdev_geom_io_intr;
780
781         g_io_request(bp, cp);
782
783         return (ZIO_PIPELINE_STOP);
784 }
785
786 static void
787 vdev_geom_io_done(zio_t *zio)
788 {
789 }
790
791 static void
792 vdev_geom_hold(vdev_t *vd)
793 {
794 }
795
796 static void
797 vdev_geom_rele(vdev_t *vd)
798 {
799 }
800
801 vdev_ops_t vdev_geom_ops = {
802         vdev_geom_open,
803         vdev_geom_close,
804         vdev_default_asize,
805         vdev_geom_io_start,
806         vdev_geom_io_done,
807         NULL,
808         vdev_geom_hold,
809         vdev_geom_rele,
810         VDEV_TYPE_DISK,         /* name of this vdev type */
811         B_TRUE                  /* leaf vdev */
812 };