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