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