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