1/*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 *
| 1/*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 *
|
48#include <sys/malloc.h> 49#include <sys/sysctl.h> 50#include <sys/stdint.h> 51#include <machine/md_var.h> 52 53 54#include <sys/lock.h> 55#include <sys/mutex.h> 56#include <geom/geom.h> 57 58static g_access_t g_disk_access; 59 60struct g_class g_disk_class = { 61 "DISK", 62 NULL, 63 NULL, 64 G_CLASS_INITIALIZER 65}; 66 67DECLARE_GEOM_CLASS(g_disk_class, g_disk); 68 69static void __inline 70g_disk_lock_giant(struct disk *dp) 71{ 72 if (!(dp->d_devsw->d_flags & D_NOGIANT)) 73 mtx_lock(&Giant); 74} 75 76static void __inline 77g_disk_unlock_giant(struct disk *dp) 78{ 79 if (!(dp->d_devsw->d_flags & D_NOGIANT)) 80 mtx_unlock(&Giant); 81} 82 83static int 84g_disk_access(struct g_provider *pp, int r, int w, int e) 85{ 86 struct disk *dp; 87 dev_t dev; 88 int error; 89 90 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 91 pp->name, r, w, e); 92 g_topology_assert(); 93 r += pp->acr; 94 w += pp->acw; 95 e += pp->ace; 96 dp = pp->geom->softc; 97 dev = dp->d_dev; 98 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 99 g_disk_lock_giant(dp); 100 error = devsw(dev)->d_open(dev, 3, 0, NULL); 101 if (error != 0) 102 printf("Opened disk %s -> %d\n", pp->name, error); 103 g_disk_unlock_giant(dp); 104 pp->mediasize = dp->d_mediasize; 105 pp->sectorsize = dp->d_sectorsize; 106 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 107 g_disk_lock_giant(dp); 108 error = devsw(dev)->d_close(dev, 3, 0, NULL); 109 if (error != 0) 110 printf("Closed disk %s -> %d\n", pp->name, error); 111 g_disk_unlock_giant(dp); 112 } else { 113 error = 0; 114 } 115 return (error); 116} 117 118static void 119g_disk_kerneldump(struct bio *bp, struct disk *dp) 120{ 121 int error; 122 struct g_kerneldump *gkd; 123 struct dumperinfo di; 124 struct g_geom *gp; 125 126 gkd = (struct g_kerneldump*)bp->bio_data; 127 gp = bp->bio_to->geom; 128 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 129 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 130 di.dumper = (dumper_t *)dp->d_devsw->d_dump; 131 di.priv = dp->d_dev; 132 di.blocksize = dp->d_sectorsize; 133 di.mediaoffset = gkd->offset; 134 di.mediasize = gkd->length; 135 error = set_dumper(&di); 136 g_io_deliver(bp, error); 137} 138 139static void 140g_disk_done(struct bio *bp) 141{ 142 struct disk *dp; 143 144 dp = bp->bio_caller1; 145 bp->bio_completed = bp->bio_length - bp->bio_resid; 146 if (!(dp->d_devsw->d_flags & D_NOGIANT)) { 147 DROP_GIANT(); 148 g_std_done(bp); 149 PICKUP_GIANT(); 150 } else { 151 g_std_done(bp); 152 } 153} 154 155static void 156g_disk_start(struct bio *bp) 157{ 158 struct bio *bp2; 159 dev_t dev; 160 struct disk *dp; 161 struct g_ioctl *gio; 162 int error; 163 164 dp = bp->bio_to->geom->softc; 165 dev = dp->d_dev; 166 error = EJUSTRETURN; 167 switch(bp->bio_cmd) { 168 case BIO_DELETE: 169 if (!(devsw(dev)->d_flags & D_CANFREE)) { 170 error = 0; 171 break; 172 } 173 /* fall-through */ 174 case BIO_READ: 175 case BIO_WRITE: 176 bp2 = g_clone_bio(bp); 177 bp2->bio_done = g_disk_done; 178 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT; 179 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 180 bp2->bio_bcount = bp2->bio_length; 181 bp2->bio_dev = dev; 182 bp2->bio_caller1 = dp; 183 g_disk_lock_giant(dp); 184 devsw(dev)->d_strategy(bp2); 185 g_disk_unlock_giant(dp); 186 break; 187 case BIO_GETATTR: 188 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 189 break; 190 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 191 break; 192 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 193 break; 194 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 195 g_disk_kerneldump(bp, dp); 196 else if (!strcmp(bp->bio_attribute, "GEOM::ioctl") && 197 bp->bio_length == sizeof *gio) { 198 gio = (struct g_ioctl *)bp->bio_data; 199 gio->func = devsw(dp->d_dev)->d_ioctl; 200 gio->dev = dp->d_dev; 201 error = EDIRIOCTL; 202 } else 203 error = ENOIOCTL; 204 break; 205 case BIO_SETATTR: 206 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") && 207 bp->bio_length == sizeof *gio) { 208 gio = (struct g_ioctl *)bp->bio_data; 209 gio->func = devsw(dp->d_dev)->d_ioctl; 210 gio->dev = dp->d_dev; 211 error = EDIRIOCTL; 212 } else 213 error = ENOIOCTL; 214 break; 215 default: 216 error = EOPNOTSUPP; 217 break; 218 } 219 if (error != EJUSTRETURN) 220 g_io_deliver(bp, error); 221 return; 222} 223 224static void 225g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 226{ 227 struct disk *dp; 228 229 dp = gp->softc; 230 if (indent == NULL) { 231 sbuf_printf(sb, " hd %u", dp->d_fwheads); 232 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 233 return; 234 } 235 if (pp != NULL) { 236 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 237 indent, dp->d_fwheads); 238 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 239 indent, dp->d_fwsectors); 240 } 241} 242 243static void 244g_disk_create(void *arg) 245{ 246 struct g_geom *gp; 247 struct g_provider *pp; 248 dev_t dev; 249 250 g_topology_assert(); 251 dev = arg; 252 gp = g_new_geomf(&g_disk_class, dev->si_name); 253 gp->start = g_disk_start; 254 gp->access = g_disk_access; 255 gp->softc = dev->si_disk; 256 gp->dumpconf = g_disk_dumpconf; 257 dev->si_disk->d_softc = gp; 258 pp = g_new_providerf(gp, "%s", gp->name); 259 pp->mediasize = dev->si_disk->d_mediasize; 260 pp->sectorsize = dev->si_disk->d_sectorsize; 261 g_error_provider(pp, 0); 262 if (bootverbose) 263 printf("GEOM: new disk %s\n", gp->name); 264} 265 266 267 268dev_t 269disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, void *unused) 270{ 271 dev_t dev; 272 273 dev = g_malloc(sizeof *dev, M_ZERO); 274 dp->d_dev = dev; 275 dp->d_devsw = cdevsw; 276 dev->si_devsw = cdevsw; 277 dev->si_disk = dp;
| 47#include <sys/malloc.h> 48#include <sys/sysctl.h> 49#include <sys/stdint.h> 50#include <machine/md_var.h> 51 52 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <geom/geom.h> 56 57static g_access_t g_disk_access; 58 59struct g_class g_disk_class = { 60 "DISK", 61 NULL, 62 NULL, 63 G_CLASS_INITIALIZER 64}; 65 66DECLARE_GEOM_CLASS(g_disk_class, g_disk); 67 68static void __inline 69g_disk_lock_giant(struct disk *dp) 70{ 71 if (!(dp->d_devsw->d_flags & D_NOGIANT)) 72 mtx_lock(&Giant); 73} 74 75static void __inline 76g_disk_unlock_giant(struct disk *dp) 77{ 78 if (!(dp->d_devsw->d_flags & D_NOGIANT)) 79 mtx_unlock(&Giant); 80} 81 82static int 83g_disk_access(struct g_provider *pp, int r, int w, int e) 84{ 85 struct disk *dp; 86 dev_t dev; 87 int error; 88 89 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 90 pp->name, r, w, e); 91 g_topology_assert(); 92 r += pp->acr; 93 w += pp->acw; 94 e += pp->ace; 95 dp = pp->geom->softc; 96 dev = dp->d_dev; 97 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 98 g_disk_lock_giant(dp); 99 error = devsw(dev)->d_open(dev, 3, 0, NULL); 100 if (error != 0) 101 printf("Opened disk %s -> %d\n", pp->name, error); 102 g_disk_unlock_giant(dp); 103 pp->mediasize = dp->d_mediasize; 104 pp->sectorsize = dp->d_sectorsize; 105 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 106 g_disk_lock_giant(dp); 107 error = devsw(dev)->d_close(dev, 3, 0, NULL); 108 if (error != 0) 109 printf("Closed disk %s -> %d\n", pp->name, error); 110 g_disk_unlock_giant(dp); 111 } else { 112 error = 0; 113 } 114 return (error); 115} 116 117static void 118g_disk_kerneldump(struct bio *bp, struct disk *dp) 119{ 120 int error; 121 struct g_kerneldump *gkd; 122 struct dumperinfo di; 123 struct g_geom *gp; 124 125 gkd = (struct g_kerneldump*)bp->bio_data; 126 gp = bp->bio_to->geom; 127 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 128 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 129 di.dumper = (dumper_t *)dp->d_devsw->d_dump; 130 di.priv = dp->d_dev; 131 di.blocksize = dp->d_sectorsize; 132 di.mediaoffset = gkd->offset; 133 di.mediasize = gkd->length; 134 error = set_dumper(&di); 135 g_io_deliver(bp, error); 136} 137 138static void 139g_disk_done(struct bio *bp) 140{ 141 struct disk *dp; 142 143 dp = bp->bio_caller1; 144 bp->bio_completed = bp->bio_length - bp->bio_resid; 145 if (!(dp->d_devsw->d_flags & D_NOGIANT)) { 146 DROP_GIANT(); 147 g_std_done(bp); 148 PICKUP_GIANT(); 149 } else { 150 g_std_done(bp); 151 } 152} 153 154static void 155g_disk_start(struct bio *bp) 156{ 157 struct bio *bp2; 158 dev_t dev; 159 struct disk *dp; 160 struct g_ioctl *gio; 161 int error; 162 163 dp = bp->bio_to->geom->softc; 164 dev = dp->d_dev; 165 error = EJUSTRETURN; 166 switch(bp->bio_cmd) { 167 case BIO_DELETE: 168 if (!(devsw(dev)->d_flags & D_CANFREE)) { 169 error = 0; 170 break; 171 } 172 /* fall-through */ 173 case BIO_READ: 174 case BIO_WRITE: 175 bp2 = g_clone_bio(bp); 176 bp2->bio_done = g_disk_done; 177 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT; 178 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 179 bp2->bio_bcount = bp2->bio_length; 180 bp2->bio_dev = dev; 181 bp2->bio_caller1 = dp; 182 g_disk_lock_giant(dp); 183 devsw(dev)->d_strategy(bp2); 184 g_disk_unlock_giant(dp); 185 break; 186 case BIO_GETATTR: 187 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 188 break; 189 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 190 break; 191 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 192 break; 193 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 194 g_disk_kerneldump(bp, dp); 195 else if (!strcmp(bp->bio_attribute, "GEOM::ioctl") && 196 bp->bio_length == sizeof *gio) { 197 gio = (struct g_ioctl *)bp->bio_data; 198 gio->func = devsw(dp->d_dev)->d_ioctl; 199 gio->dev = dp->d_dev; 200 error = EDIRIOCTL; 201 } else 202 error = ENOIOCTL; 203 break; 204 case BIO_SETATTR: 205 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") && 206 bp->bio_length == sizeof *gio) { 207 gio = (struct g_ioctl *)bp->bio_data; 208 gio->func = devsw(dp->d_dev)->d_ioctl; 209 gio->dev = dp->d_dev; 210 error = EDIRIOCTL; 211 } else 212 error = ENOIOCTL; 213 break; 214 default: 215 error = EOPNOTSUPP; 216 break; 217 } 218 if (error != EJUSTRETURN) 219 g_io_deliver(bp, error); 220 return; 221} 222 223static void 224g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 225{ 226 struct disk *dp; 227 228 dp = gp->softc; 229 if (indent == NULL) { 230 sbuf_printf(sb, " hd %u", dp->d_fwheads); 231 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 232 return; 233 } 234 if (pp != NULL) { 235 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 236 indent, dp->d_fwheads); 237 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 238 indent, dp->d_fwsectors); 239 } 240} 241 242static void 243g_disk_create(void *arg) 244{ 245 struct g_geom *gp; 246 struct g_provider *pp; 247 dev_t dev; 248 249 g_topology_assert(); 250 dev = arg; 251 gp = g_new_geomf(&g_disk_class, dev->si_name); 252 gp->start = g_disk_start; 253 gp->access = g_disk_access; 254 gp->softc = dev->si_disk; 255 gp->dumpconf = g_disk_dumpconf; 256 dev->si_disk->d_softc = gp; 257 pp = g_new_providerf(gp, "%s", gp->name); 258 pp->mediasize = dev->si_disk->d_mediasize; 259 pp->sectorsize = dev->si_disk->d_sectorsize; 260 g_error_provider(pp, 0); 261 if (bootverbose) 262 printf("GEOM: new disk %s\n", gp->name); 263} 264 265 266 267dev_t 268disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, void *unused) 269{ 270 dev_t dev; 271 272 dev = g_malloc(sizeof *dev, M_ZERO); 273 dp->d_dev = dev; 274 dp->d_devsw = cdevsw; 275 dev->si_devsw = cdevsw; 276 dev->si_disk = dp;
|
279 sprintf(dev->si_name, "%s%d", cdevsw->d_name, unit); 280 g_call_me(g_disk_create, dev); 281 return (dev); 282} 283 284void 285disk_destroy(dev_t dev) 286{ 287 struct disk *dp; 288 struct g_geom *gp; 289 290 dp = dev->si_disk; 291 gp = dp->d_softc; 292 g_free(dev); 293 gp->flags |= G_GEOM_WITHER; 294 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO); 295} 296 297static void 298g_kern_disks(void *p) 299{ 300 struct sbuf *sb; 301 struct g_geom *gp; 302 char *sp; 303 304 sb = p; 305 sp = ""; 306 g_topology_assert(); 307 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 308 sbuf_printf(sb, "%s%s", sp, gp->name); 309 sp = " "; 310 } 311 sbuf_finish(sb); 312 wakeup(sb); 313} 314 315static int 316sysctl_disks(SYSCTL_HANDLER_ARGS) 317{ 318 int error; 319 struct sbuf *sb; 320 321 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 322 sbuf_clear(sb); 323 g_call_me(g_kern_disks, sb); 324 do { 325 tsleep(sb, PZERO, "kern.disks", hz); 326 } while(!sbuf_done(sb)); 327 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 328 sbuf_delete(sb); 329 return error; 330} 331 332SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 333 sysctl_disks, "A", "names of available disks"); 334
| 278 sprintf(dev->si_name, "%s%d", cdevsw->d_name, unit); 279 g_call_me(g_disk_create, dev); 280 return (dev); 281} 282 283void 284disk_destroy(dev_t dev) 285{ 286 struct disk *dp; 287 struct g_geom *gp; 288 289 dp = dev->si_disk; 290 gp = dp->d_softc; 291 g_free(dev); 292 gp->flags |= G_GEOM_WITHER; 293 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO); 294} 295 296static void 297g_kern_disks(void *p) 298{ 299 struct sbuf *sb; 300 struct g_geom *gp; 301 char *sp; 302 303 sb = p; 304 sp = ""; 305 g_topology_assert(); 306 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 307 sbuf_printf(sb, "%s%s", sp, gp->name); 308 sp = " "; 309 } 310 sbuf_finish(sb); 311 wakeup(sb); 312} 313 314static int 315sysctl_disks(SYSCTL_HANDLER_ARGS) 316{ 317 int error; 318 struct sbuf *sb; 319 320 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 321 sbuf_clear(sb); 322 g_call_me(g_kern_disks, sb); 323 do { 324 tsleep(sb, PZERO, "kern.disks", hz); 325 } while(!sbuf_done(sb)); 326 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 327 sbuf_delete(sb); 328 return error; 329} 330 331SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 332 sysctl_disks, "A", "names of available disks"); 333
|