]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_dev.c
MFV r328251: 8652 Tautological comparisons with ZPROP_INVAL
[FreeBSD/FreeBSD.git] / sys / geom / geom_dev.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_compat.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/conf.h>
48 #include <sys/ctype.h>
49 #include <sys/bio.h>
50 #include <sys/bus.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/proc.h>
54 #include <sys/errno.h>
55 #include <sys/time.h>
56 #include <sys/disk.h>
57 #include <sys/fcntl.h>
58 #include <sys/limits.h>
59 #include <sys/sysctl.h>
60 #include <geom/geom.h>
61 #include <geom/geom_int.h>
62 #include <machine/stdarg.h>
63
64 struct g_dev_softc {
65         struct mtx       sc_mtx;
66         struct cdev     *sc_dev;
67         struct cdev     *sc_alias;
68         int              sc_open;
69         int              sc_active;
70 };
71
72 static d_open_t         g_dev_open;
73 static d_close_t        g_dev_close;
74 static d_strategy_t     g_dev_strategy;
75 static d_ioctl_t        g_dev_ioctl;
76
77 static struct cdevsw g_dev_cdevsw = {
78         .d_version =    D_VERSION,
79         .d_open =       g_dev_open,
80         .d_close =      g_dev_close,
81         .d_read =       physread,
82         .d_write =      physwrite,
83         .d_ioctl =      g_dev_ioctl,
84         .d_strategy =   g_dev_strategy,
85         .d_name =       "g_dev",
86         .d_flags =      D_DISK | D_TRACKCLOSE,
87 };
88
89 static g_init_t g_dev_init;
90 static g_fini_t g_dev_fini;
91 static g_taste_t g_dev_taste;
92 static g_orphan_t g_dev_orphan;
93 static g_attrchanged_t g_dev_attrchanged;
94
95 static struct g_class g_dev_class       = {
96         .name = "DEV",
97         .version = G_VERSION,
98         .init = g_dev_init,
99         .fini = g_dev_fini,
100         .taste = g_dev_taste,
101         .orphan = g_dev_orphan,
102         .attrchanged = g_dev_attrchanged
103 };
104
105 /*
106  * We target 262144 (8 x 32768) sectors by default as this significantly
107  * increases the throughput on commonly used SSD's with a marginal
108  * increase in non-interruptible request latency.
109  */
110 static uint64_t g_dev_del_max_sectors = 262144;
111 SYSCTL_DECL(_kern_geom);
112 SYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW, 0, "GEOM_DEV stuff");
113 SYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW,
114     &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single "
115     "delete request sent to the provider. Larger requests are chunked "
116     "so they can be interrupted. (0 = disable chunking)");
117
118 static char *dumpdev = NULL;
119 static void
120 g_dev_init(struct g_class *mp)
121 {
122
123         dumpdev = kern_getenv("dumpdev");
124 }
125
126 static void
127 g_dev_fini(struct g_class *mp)
128 {
129
130         freeenv(dumpdev);
131         dumpdev = NULL;
132 }
133
134 static int
135 g_dev_setdumpdev(struct cdev *dev, struct diocskerneldump_arg *kda,
136     struct thread *td)
137 {
138         struct g_kerneldump kd;
139         struct g_consumer *cp;
140         int error, len;
141
142         if (dev == NULL || kda == NULL)
143                 return (set_dumper(NULL, NULL, td, 0, 0, NULL, 0, NULL));
144
145         cp = dev->si_drv2;
146         len = sizeof(kd);
147         kd.offset = 0;
148         kd.length = OFF_MAX;
149         error = g_io_getattr("GEOM::kerneldump", cp, &len, &kd);
150         if (error != 0)
151                 return (error);
152
153         error = set_dumper(&kd.di, devtoname(dev), td, kda->kda_compression,
154             kda->kda_encryption, kda->kda_key, kda->kda_encryptedkeysize,
155             kda->kda_encryptedkey);
156         if (error == 0)
157                 dev->si_flags |= SI_DUMPDEV;
158
159         return (error);
160 }
161
162 static int
163 init_dumpdev(struct cdev *dev)
164 {
165         struct diocskerneldump_arg kda;
166         struct g_consumer *cp;
167         const char *devprefix = "/dev/", *devname;
168         int error;
169         size_t len;
170
171         bzero(&kda, sizeof(kda));
172         kda.kda_enable = 1;
173
174         if (dumpdev == NULL)
175                 return (0);
176
177         len = strlen(devprefix);
178         devname = devtoname(dev);
179         if (strcmp(devname, dumpdev) != 0 &&
180            (strncmp(dumpdev, devprefix, len) != 0 ||
181             strcmp(devname, dumpdev + len) != 0))
182                 return (0);
183
184         cp = (struct g_consumer *)dev->si_drv2;
185         error = g_access(cp, 1, 0, 0);
186         if (error != 0)
187                 return (error);
188
189         error = g_dev_setdumpdev(dev, &kda, curthread);
190         if (error == 0) {
191                 freeenv(dumpdev);
192                 dumpdev = NULL;
193         }
194
195         (void)g_access(cp, -1, 0, 0);
196
197         return (error);
198 }
199
200 static void
201 g_dev_destroy(void *arg, int flags __unused)
202 {
203         struct g_consumer *cp;
204         struct g_geom *gp;
205         struct g_dev_softc *sc;
206         char buf[SPECNAMELEN + 6];
207
208         g_topology_assert();
209         cp = arg;
210         gp = cp->geom;
211         sc = cp->private;
212         g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name);
213         snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
214         devctl_notify_f("GEOM", "DEV", "DESTROY", buf, M_WAITOK);
215         if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
216                 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
217         g_detach(cp);
218         g_destroy_consumer(cp);
219         g_destroy_geom(gp);
220         mtx_destroy(&sc->sc_mtx);
221         g_free(sc);
222 }
223
224 void
225 g_dev_print(void)
226 {
227         struct g_geom *gp;
228         char const *p = "";
229
230         LIST_FOREACH(gp, &g_dev_class.geom, geom) {
231                 printf("%s%s", p, gp->name);
232                 p = " ";
233         }
234         printf("\n");
235 }
236
237 static void
238 g_dev_set_physpath(struct g_consumer *cp)
239 {
240         struct g_dev_softc *sc;
241         char *physpath;
242         int error, physpath_len;
243
244         if (g_access(cp, 1, 0, 0) != 0)
245                 return;
246
247         sc = cp->private;
248         physpath_len = MAXPATHLEN;
249         physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
250         error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
251         g_access(cp, -1, 0, 0);
252         if (error == 0 && strlen(physpath) != 0) {
253                 struct cdev *dev, *old_alias_dev;
254                 struct cdev **alias_devp;
255
256                 dev = sc->sc_dev;
257                 old_alias_dev = sc->sc_alias;
258                 alias_devp = (struct cdev **)&sc->sc_alias;
259                 make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp, dev,
260                     old_alias_dev, physpath);
261         } else if (sc->sc_alias) {
262                 destroy_dev((struct cdev *)sc->sc_alias);
263                 sc->sc_alias = NULL;
264         }
265         g_free(physpath);
266 }
267
268 static void
269 g_dev_set_media(struct g_consumer *cp)
270 {
271         struct g_dev_softc *sc;
272         struct cdev *dev;
273         char buf[SPECNAMELEN + 6];
274
275         sc = cp->private;
276         dev = sc->sc_dev;
277         snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
278         devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
279         devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
280         dev = sc->sc_alias;
281         if (dev != NULL) {
282                 snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
283                 devctl_notify_f("DEVFS", "CDEV", "MEDIACHANGE", buf, M_WAITOK);
284                 devctl_notify_f("GEOM", "DEV", "MEDIACHANGE", buf, M_WAITOK);
285         }
286 }
287
288 static void
289 g_dev_attrchanged(struct g_consumer *cp, const char *attr)
290 {
291
292         if (strcmp(attr, "GEOM::media") == 0) {
293                 g_dev_set_media(cp);
294                 return;
295         }
296
297         if (strcmp(attr, "GEOM::physpath") == 0) {
298                 g_dev_set_physpath(cp);
299                 return;
300         }
301 }
302
303 struct g_provider *
304 g_dev_getprovider(struct cdev *dev)
305 {
306         struct g_consumer *cp;
307
308         g_topology_assert();
309         if (dev == NULL)
310                 return (NULL);
311         if (dev->si_devsw != &g_dev_cdevsw)
312                 return (NULL);
313         cp = dev->si_drv2;
314         return (cp->provider);
315 }
316
317 static struct g_geom *
318 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
319 {
320         struct g_geom *gp;
321         struct g_geom_alias *gap;
322         struct g_consumer *cp;
323         struct g_dev_softc *sc;
324         int error;
325         struct cdev *dev, *adev;
326         char buf[SPECNAMELEN + 6];
327
328         g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
329         g_topology_assert();
330         gp = g_new_geomf(mp, "%s", pp->name);
331         sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
332         mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF);
333         cp = g_new_consumer(gp);
334         cp->private = sc;
335         cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
336         error = g_attach(cp, pp);
337         KASSERT(error == 0,
338             ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
339         error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
340             &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
341         if (error != 0) {
342                 printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
343                     __func__, gp->name, error);
344                 g_detach(cp);
345                 g_destroy_consumer(cp);
346                 g_destroy_geom(gp);
347                 mtx_destroy(&sc->sc_mtx);
348                 g_free(sc);
349                 return (NULL);
350         }
351         dev->si_flags |= SI_UNMAPPED;
352         sc->sc_dev = dev;
353
354         dev->si_iosize_max = MAXPHYS;
355         dev->si_drv2 = cp;
356         error = init_dumpdev(dev);
357         if (error != 0)
358                 printf("%s: init_dumpdev() failed (gp->name=%s, error=%d)\n",
359                     __func__, gp->name, error);
360
361         g_dev_attrchanged(cp, "GEOM::physpath");
362         snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
363         devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
364         /*
365          * Now add all the aliases for this drive
366          */
367         LIST_FOREACH(gap, &pp->geom->aliases, ga_next) {
368                 error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &adev, dev,
369                     "%s", gap->ga_alias);
370                 if (error) {
371                         printf("%s: make_dev_alias_p() failed (name=%s, error=%d)\n",
372                             __func__, gap->ga_alias, error);
373                         continue;
374                 }
375                 snprintf(buf, sizeof(buf), "cdev=%s", gap->ga_alias);
376                 devctl_notify_f("GEOM", "DEV", "CREATE", buf, M_WAITOK);
377         }
378
379         return (gp);
380 }
381
382 static int
383 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
384 {
385         struct g_consumer *cp;
386         struct g_dev_softc *sc;
387         int error, r, w, e;
388
389         cp = dev->si_drv2;
390         if (cp == NULL)
391                 return (ENXIO);         /* g_dev_taste() not done yet */
392         g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
393             cp->geom->name, flags, fmt, td);
394
395         r = flags & FREAD ? 1 : 0;
396         w = flags & FWRITE ? 1 : 0;
397 #ifdef notyet
398         e = flags & O_EXCL ? 1 : 0;
399 #else
400         e = 0;
401 #endif
402
403         /*
404          * This happens on attempt to open a device node with O_EXEC.
405          */
406         if (r + w + e == 0)
407                 return (EINVAL);
408
409         if (w) {
410                 /*
411                  * When running in very secure mode, do not allow
412                  * opens for writing of any disks.
413                  */
414                 error = securelevel_ge(td->td_ucred, 2);
415                 if (error)
416                         return (error);
417         }
418         g_topology_lock();
419         error = g_access(cp, r, w, e);
420         g_topology_unlock();
421         if (error == 0) {
422                 sc = cp->private;
423                 mtx_lock(&sc->sc_mtx);
424                 if (sc->sc_open == 0 && sc->sc_active != 0)
425                         wakeup(&sc->sc_active);
426                 sc->sc_open += r + w + e;
427                 mtx_unlock(&sc->sc_mtx);
428         }
429         return (error);
430 }
431
432 static int
433 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
434 {
435         struct g_consumer *cp;
436         struct g_dev_softc *sc;
437         int error, r, w, e;
438
439         cp = dev->si_drv2;
440         if (cp == NULL)
441                 return (ENXIO);
442         g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
443             cp->geom->name, flags, fmt, td);
444
445         r = flags & FREAD ? -1 : 0;
446         w = flags & FWRITE ? -1 : 0;
447 #ifdef notyet
448         e = flags & O_EXCL ? -1 : 0;
449 #else
450         e = 0;
451 #endif
452
453         /*
454          * The vgonel(9) - caused by eg. forced unmount of devfs - calls
455          * VOP_CLOSE(9) on devfs vnode without any FREAD or FWRITE flags,
456          * which would result in zero deltas, which in turn would cause
457          * panic in g_access(9).
458          *
459          * Note that we cannot zero the counters (ie. do "r = cp->acr"
460          * etc) instead, because the consumer might be opened in another
461          * devfs instance.
462          */
463         if (r + w + e == 0)
464                 return (EINVAL);
465
466         sc = cp->private;
467         mtx_lock(&sc->sc_mtx);
468         sc->sc_open += r + w + e;
469         while (sc->sc_open == 0 && sc->sc_active != 0)
470                 msleep(&sc->sc_active, &sc->sc_mtx, 0, "PRIBIO", 0);
471         mtx_unlock(&sc->sc_mtx);
472         g_topology_lock();
473         error = g_access(cp, r, w, e);
474         g_topology_unlock();
475         return (error);
476 }
477
478 /*
479  * XXX: Until we have unmessed the ioctl situation, there is a race against
480  * XXX: a concurrent orphanization.  We cannot close it by holding topology
481  * XXX: since that would prevent us from doing our job, and stalling events
482  * XXX: will break (actually: stall) the BSD disklabel hacks.
483  */
484 static int
485 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
486 {
487         struct g_consumer *cp;
488         struct g_provider *pp;
489         off_t offset, length, chunk, odd;
490         int i, error;
491
492         cp = dev->si_drv2;
493         pp = cp->provider;
494
495         error = 0;
496         KASSERT(cp->acr || cp->acw,
497             ("Consumer with zero access count in g_dev_ioctl"));
498
499         i = IOCPARM_LEN(cmd);
500         switch (cmd) {
501         case DIOCGSECTORSIZE:
502                 *(u_int *)data = cp->provider->sectorsize;
503                 if (*(u_int *)data == 0)
504                         error = ENOENT;
505                 break;
506         case DIOCGMEDIASIZE:
507                 *(off_t *)data = cp->provider->mediasize;
508                 if (*(off_t *)data == 0)
509                         error = ENOENT;
510                 break;
511         case DIOCGFWSECTORS:
512                 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
513                 if (error == 0 && *(u_int *)data == 0)
514                         error = ENOENT;
515                 break;
516         case DIOCGFWHEADS:
517                 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
518                 if (error == 0 && *(u_int *)data == 0)
519                         error = ENOENT;
520                 break;
521         case DIOCGFRONTSTUFF:
522                 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
523                 break;
524 #ifdef COMPAT_FREEBSD11
525         case DIOCSKERNELDUMP_FREEBSD11:
526             {
527                 struct diocskerneldump_arg kda;
528
529                 bzero(&kda, sizeof(kda));
530                 kda.kda_encryption = KERNELDUMP_ENC_NONE;
531                 kda.kda_enable = (uint8_t)*(u_int *)data;
532                 if (kda.kda_enable == 0)
533                         error = g_dev_setdumpdev(NULL, NULL, td);
534                 else
535                         error = g_dev_setdumpdev(dev, &kda, td);
536                 break;
537             }
538 #endif
539         case DIOCSKERNELDUMP:
540             {
541                 struct diocskerneldump_arg *kda;
542                 uint8_t *encryptedkey;
543
544                 kda = (struct diocskerneldump_arg *)data;
545                 if (kda->kda_enable == 0) {
546                         error = g_dev_setdumpdev(NULL, NULL, td);
547                         break;
548                 }
549
550                 if (kda->kda_encryption != KERNELDUMP_ENC_NONE) {
551                         if (kda->kda_encryptedkeysize <= 0 ||
552                             kda->kda_encryptedkeysize >
553                             KERNELDUMP_ENCKEY_MAX_SIZE) {
554                                 return (EINVAL);
555                         }
556                         encryptedkey = malloc(kda->kda_encryptedkeysize, M_TEMP,
557                             M_WAITOK);
558                         error = copyin(kda->kda_encryptedkey, encryptedkey,
559                             kda->kda_encryptedkeysize);
560                 } else {
561                         encryptedkey = NULL;
562                 }
563                 if (error == 0) {
564                         kda->kda_encryptedkey = encryptedkey;
565                         error = g_dev_setdumpdev(dev, kda, td);
566                 }
567                 if (encryptedkey != NULL) {
568                         explicit_bzero(encryptedkey, kda->kda_encryptedkeysize);
569                         free(encryptedkey, M_TEMP);
570                 }
571                 explicit_bzero(kda, sizeof(*kda));
572                 break;
573             }
574         case DIOCGFLUSH:
575                 error = g_io_flush(cp);
576                 break;
577         case DIOCGDELETE:
578                 offset = ((off_t *)data)[0];
579                 length = ((off_t *)data)[1];
580                 if ((offset % cp->provider->sectorsize) != 0 ||
581                     (length % cp->provider->sectorsize) != 0 || length <= 0) {
582                         printf("%s: offset=%jd length=%jd\n", __func__, offset,
583                             length);
584                         error = EINVAL;
585                         break;
586                 }
587                 while (length > 0) {
588                         chunk = length;
589                         if (g_dev_del_max_sectors != 0 && chunk >
590                             g_dev_del_max_sectors * cp->provider->sectorsize) {
591                                 chunk = g_dev_del_max_sectors *
592                                     cp->provider->sectorsize;
593                                 if (cp->provider->stripesize > 0) {
594                                         odd = (offset + chunk +
595                                             cp->provider->stripeoffset) %
596                                             cp->provider->stripesize;
597                                         if (chunk > odd)
598                                                 chunk -= odd;
599                                 }
600                         }
601                         error = g_delete_data(cp, offset, chunk);
602                         length -= chunk;
603                         offset += chunk;
604                         if (error)
605                                 break;
606                         /*
607                          * Since the request size can be large, the service
608                          * time can be is likewise.  We make this ioctl
609                          * interruptible by checking for signals for each bio.
610                          */
611                         if (SIGPENDING(td))
612                                 break;
613                 }
614                 break;
615         case DIOCGIDENT:
616                 error = g_io_getattr("GEOM::ident", cp, &i, data);
617                 break;
618         case DIOCGPROVIDERNAME:
619                 if (pp == NULL)
620                         return (ENOENT);
621                 strlcpy(data, pp->name, i);
622                 break;
623         case DIOCGSTRIPESIZE:
624                 *(off_t *)data = cp->provider->stripesize;
625                 break;
626         case DIOCGSTRIPEOFFSET:
627                 *(off_t *)data = cp->provider->stripeoffset;
628                 break;
629         case DIOCGPHYSPATH:
630                 error = g_io_getattr("GEOM::physpath", cp, &i, data);
631                 if (error == 0 && *(char *)data == '\0')
632                         error = ENOENT;
633                 break;
634         case DIOCGATTR: {
635                 struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
636
637                 if (arg->len > sizeof(arg->value)) {
638                         error = EINVAL;
639                         break;
640                 }
641                 error = g_io_getattr(arg->name, cp, &arg->len, &arg->value);
642                 break;
643         }
644         case DIOCZONECMD: {
645                 struct disk_zone_args *zone_args =(struct disk_zone_args *)data;
646                 struct disk_zone_rep_entry *new_entries, *old_entries;
647                 struct disk_zone_report *rep;
648                 size_t alloc_size;
649
650                 old_entries = NULL;
651                 new_entries = NULL;
652                 rep = NULL;
653                 alloc_size = 0;
654
655                 if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES) {
656
657                         rep = &zone_args->zone_params.report;
658                         alloc_size = rep->entries_allocated *
659                             sizeof(struct disk_zone_rep_entry);
660                         if (alloc_size != 0)
661                                 new_entries = g_malloc(alloc_size,
662                                     M_WAITOK| M_ZERO);
663                         old_entries = rep->entries;
664                         rep->entries = new_entries;
665                 }
666                 error = g_io_zonecmd(zone_args, cp);
667                 if ((zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES)
668                  && (alloc_size != 0)
669                  && (error == 0)) {
670                         error = copyout(new_entries, old_entries, alloc_size);
671                 }
672                 if ((old_entries != NULL)
673                  && (rep != NULL))
674                         rep->entries = old_entries;
675
676                 if (new_entries != NULL)
677                         g_free(new_entries);
678                 break;
679         }
680         default:
681                 if (cp->provider->geom->ioctl != NULL) {
682                         error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
683                 } else {
684                         error = ENOIOCTL;
685                 }
686         }
687
688         return (error);
689 }
690
691 static void
692 g_dev_done(struct bio *bp2)
693 {
694         struct g_consumer *cp;
695         struct g_dev_softc *sc;
696         struct bio *bp;
697         int destroy;
698
699         cp = bp2->bio_from;
700         sc = cp->private;
701         bp = bp2->bio_parent;
702         bp->bio_error = bp2->bio_error;
703         bp->bio_completed = bp2->bio_completed;
704         bp->bio_resid = bp->bio_length - bp2->bio_completed;
705         if (bp2->bio_cmd == BIO_ZONE)
706                 bcopy(&bp2->bio_zone, &bp->bio_zone, sizeof(bp->bio_zone));
707
708         if (bp2->bio_error != 0) {
709                 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
710                     bp2, bp2->bio_error);
711                 bp->bio_flags |= BIO_ERROR;
712         } else {
713                 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
714                     bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
715         }
716         g_destroy_bio(bp2);
717         destroy = 0;
718         mtx_lock(&sc->sc_mtx);
719         if ((--sc->sc_active) == 0) {
720                 if (sc->sc_open == 0)
721                         wakeup(&sc->sc_active);
722                 if (sc->sc_dev == NULL)
723                         destroy = 1;
724         }
725         mtx_unlock(&sc->sc_mtx);
726         if (destroy)
727                 g_post_event(g_dev_destroy, cp, M_NOWAIT, NULL);
728         biodone(bp);
729 }
730
731 static void
732 g_dev_strategy(struct bio *bp)
733 {
734         struct g_consumer *cp;
735         struct bio *bp2;
736         struct cdev *dev;
737         struct g_dev_softc *sc;
738
739         KASSERT(bp->bio_cmd == BIO_READ ||
740                 bp->bio_cmd == BIO_WRITE ||
741                 bp->bio_cmd == BIO_DELETE ||
742                 bp->bio_cmd == BIO_FLUSH ||
743                 bp->bio_cmd == BIO_ZONE,
744                 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
745         dev = bp->bio_dev;
746         cp = dev->si_drv2;
747         sc = cp->private;
748         KASSERT(cp->acr || cp->acw,
749             ("Consumer with zero access count in g_dev_strategy"));
750         biotrack(bp, __func__);
751 #ifdef INVARIANTS
752         if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
753             (bp->bio_bcount % cp->provider->sectorsize) != 0) {
754                 bp->bio_resid = bp->bio_bcount;
755                 biofinish(bp, NULL, EINVAL);
756                 return;
757         }
758 #endif
759         mtx_lock(&sc->sc_mtx);
760         KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
761         sc->sc_active++;
762         mtx_unlock(&sc->sc_mtx);
763
764         for (;;) {
765                 /*
766                  * XXX: This is not an ideal solution, but I believe it to
767                  * XXX: deadlock safely, all things considered.
768                  */
769                 bp2 = g_clone_bio(bp);
770                 if (bp2 != NULL)
771                         break;
772                 pause("gdstrat", hz / 10);
773         }
774         KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
775         bp2->bio_done = g_dev_done;
776         g_trace(G_T_BIO,
777             "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
778             bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
779             bp2->bio_data, bp2->bio_cmd);
780         g_io_request(bp2, cp);
781         KASSERT(cp->acr || cp->acw,
782             ("g_dev_strategy raced with g_dev_close and lost"));
783
784 }
785
786 /*
787  * g_dev_callback()
788  *
789  * Called by devfs when asynchronous device destruction is completed.
790  * - Mark that we have no attached device any more.
791  * - If there are no outstanding requests, schedule geom destruction.
792  *   Otherwise destruction will be scheduled later by g_dev_done().
793  */
794
795 static void
796 g_dev_callback(void *arg)
797 {
798         struct g_consumer *cp;
799         struct g_dev_softc *sc;
800         int destroy;
801
802         cp = arg;
803         sc = cp->private;
804         g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
805
806         mtx_lock(&sc->sc_mtx);
807         sc->sc_dev = NULL;
808         sc->sc_alias = NULL;
809         destroy = (sc->sc_active == 0);
810         mtx_unlock(&sc->sc_mtx);
811         if (destroy)
812                 g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
813 }
814
815 /*
816  * g_dev_orphan()
817  *
818  * Called from below when the provider orphaned us.
819  * - Clear any dump settings.
820  * - Request asynchronous device destruction to prevent any more requests
821  *   from coming in.  The provider is already marked with an error, so
822  *   anything which comes in the interim will be returned immediately.
823  */
824
825 static void
826 g_dev_orphan(struct g_consumer *cp)
827 {
828         struct cdev *dev;
829         struct g_dev_softc *sc;
830
831         g_topology_assert();
832         sc = cp->private;
833         dev = sc->sc_dev;
834         g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
835
836         /* Reset any dump-area set on this device */
837         if (dev->si_flags & SI_DUMPDEV)
838                 (void)set_dumper(NULL, NULL, curthread, 0, 0, NULL, 0, NULL);
839
840         /* Destroy the struct cdev *so we get no more requests */
841         destroy_dev_sched_cb(dev, g_dev_callback, cp);
842 }
843
844 DECLARE_GEOM_CLASS(g_dev_class, g_dev);