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 */ 35 36#include <sys/cdefs.h>
| 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 */ 35 36#include <sys/cdefs.h>
|
37__FBSDID("$FreeBSD: head/sys/geom/geom_disk.c 219950 2011-03-24 08:37:48Z mav $");
| 37__FBSDID("$FreeBSD: head/sys/geom/geom_disk.c 219970 2011-03-24 19:23:42Z mav $");
|
38 39#include "opt_geom.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/sysctl.h> 45#include <sys/bio.h> 46#include <sys/ctype.h> 47#include <sys/fcntl.h> 48#include <sys/malloc.h> 49#include <sys/sysctl.h> 50#include <sys/devicestat.h> 51#include <machine/md_var.h> 52 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <geom/geom.h> 56#include <geom/geom_disk.h> 57#include <geom/geom_int.h> 58
| 38 39#include "opt_geom.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/sysctl.h> 45#include <sys/bio.h> 46#include <sys/ctype.h> 47#include <sys/fcntl.h> 48#include <sys/malloc.h> 49#include <sys/sysctl.h> 50#include <sys/devicestat.h> 51#include <machine/md_var.h> 52 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <geom/geom.h> 56#include <geom/geom_disk.h> 57#include <geom/geom_int.h> 58
|
| 59#include <dev/led/led.h> 60 61struct g_disk_softc { 62 struct disk *dp; 63 struct sysctl_ctx_list sysctl_ctx; 64 struct sysctl_oid *sysctl_tree; 65 char led[64]; 66 uint32_t state; 67}; 68
|
59static struct mtx g_disk_done_mtx; 60 61static g_access_t g_disk_access; 62static g_init_t g_disk_init; 63static g_fini_t g_disk_fini; 64static g_start_t g_disk_start; 65static g_ioctl_t g_disk_ioctl; 66static g_dumpconf_t g_disk_dumpconf; 67 68static struct g_class g_disk_class = { 69 .name = "DISK", 70 .version = G_VERSION, 71 .init = g_disk_init, 72 .fini = g_disk_fini, 73 .start = g_disk_start, 74 .access = g_disk_access, 75 .ioctl = g_disk_ioctl, 76 .dumpconf = g_disk_dumpconf, 77}; 78
| 69static struct mtx g_disk_done_mtx; 70 71static g_access_t g_disk_access; 72static g_init_t g_disk_init; 73static g_fini_t g_disk_fini; 74static g_start_t g_disk_start; 75static g_ioctl_t g_disk_ioctl; 76static g_dumpconf_t g_disk_dumpconf; 77 78static struct g_class g_disk_class = { 79 .name = "DISK", 80 .version = G_VERSION, 81 .init = g_disk_init, 82 .fini = g_disk_fini, 83 .start = g_disk_start, 84 .access = g_disk_access, 85 .ioctl = g_disk_ioctl, 86 .dumpconf = g_disk_dumpconf, 87}; 88
|
| 89SYSCTL_DECL(_kern_geom); 90SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0, "GEOM_DISK stuff"); 91
|
79static void 80g_disk_init(struct g_class *mp __unused) 81{ 82 83 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF); 84} 85 86static void 87g_disk_fini(struct g_class *mp __unused) 88{ 89 90 mtx_destroy(&g_disk_done_mtx); 91} 92 93DECLARE_GEOM_CLASS(g_disk_class, g_disk); 94 95static void __inline 96g_disk_lock_giant(struct disk *dp) 97{ 98 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 99 mtx_lock(&Giant); 100} 101 102static void __inline 103g_disk_unlock_giant(struct disk *dp) 104{ 105 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 106 mtx_unlock(&Giant); 107} 108 109static int 110g_disk_access(struct g_provider *pp, int r, int w, int e) 111{ 112 struct disk *dp;
| 92static void 93g_disk_init(struct g_class *mp __unused) 94{ 95 96 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF); 97} 98 99static void 100g_disk_fini(struct g_class *mp __unused) 101{ 102 103 mtx_destroy(&g_disk_done_mtx); 104} 105 106DECLARE_GEOM_CLASS(g_disk_class, g_disk); 107 108static void __inline 109g_disk_lock_giant(struct disk *dp) 110{ 111 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 112 mtx_lock(&Giant); 113} 114 115static void __inline 116g_disk_unlock_giant(struct disk *dp) 117{ 118 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 119 mtx_unlock(&Giant); 120} 121 122static int 123g_disk_access(struct g_provider *pp, int r, int w, int e) 124{ 125 struct disk *dp;
|
| 126 struct g_disk_softc *sc;
|
113 int error; 114 115 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 116 pp->name, r, w, e); 117 g_topology_assert();
| 127 int error; 128 129 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 130 pp->name, r, w, e); 131 g_topology_assert();
|
118 dp = pp->geom->softc; 119 if (dp == NULL || dp->d_destroyed) {
| 132 sc = pp->geom->softc; 133 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
|
120 /* 121 * Allow decreasing access count even if disk is not 122 * avaliable anymore. 123 */ 124 if (r <= 0 && w <= 0 && e <= 0) 125 return (0); 126 return (ENXIO); 127 } 128 r += pp->acr; 129 w += pp->acw; 130 e += pp->ace; 131 error = 0; 132 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 133 if (dp->d_open != NULL) { 134 g_disk_lock_giant(dp); 135 error = dp->d_open(dp); 136 if (bootverbose && error != 0) 137 printf("Opened disk %s -> %d\n", 138 pp->name, error); 139 g_disk_unlock_giant(dp); 140 } 141 pp->mediasize = dp->d_mediasize; 142 pp->sectorsize = dp->d_sectorsize; 143 dp->d_flags |= DISKFLAG_OPEN; 144 if (dp->d_maxsize == 0) { 145 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 146 dp->d_name, dp->d_unit); 147 dp->d_maxsize = DFLTPHYS; 148 } 149 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 150 if (dp->d_close != NULL) { 151 g_disk_lock_giant(dp); 152 error = dp->d_close(dp); 153 if (error != 0) 154 printf("Closed disk %s -> %d\n", 155 pp->name, error); 156 g_disk_unlock_giant(dp); 157 }
| 134 /* 135 * Allow decreasing access count even if disk is not 136 * avaliable anymore. 137 */ 138 if (r <= 0 && w <= 0 && e <= 0) 139 return (0); 140 return (ENXIO); 141 } 142 r += pp->acr; 143 w += pp->acw; 144 e += pp->ace; 145 error = 0; 146 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 147 if (dp->d_open != NULL) { 148 g_disk_lock_giant(dp); 149 error = dp->d_open(dp); 150 if (bootverbose && error != 0) 151 printf("Opened disk %s -> %d\n", 152 pp->name, error); 153 g_disk_unlock_giant(dp); 154 } 155 pp->mediasize = dp->d_mediasize; 156 pp->sectorsize = dp->d_sectorsize; 157 dp->d_flags |= DISKFLAG_OPEN; 158 if (dp->d_maxsize == 0) { 159 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 160 dp->d_name, dp->d_unit); 161 dp->d_maxsize = DFLTPHYS; 162 } 163 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 164 if (dp->d_close != NULL) { 165 g_disk_lock_giant(dp); 166 error = dp->d_close(dp); 167 if (error != 0) 168 printf("Closed disk %s -> %d\n", 169 pp->name, error); 170 g_disk_unlock_giant(dp); 171 }
|
| 172 sc->state = G_STATE_ACTIVE; 173 if (sc->led[0] != 0) 174 led_set(sc->led, "0");
|
158 dp->d_flags &= ~DISKFLAG_OPEN; 159 } 160 return (error); 161} 162 163static void 164g_disk_kerneldump(struct bio *bp, struct disk *dp) 165{ 166 struct g_kerneldump *gkd; 167 struct g_geom *gp; 168 169 gkd = (struct g_kerneldump*)bp->bio_data; 170 gp = bp->bio_to->geom; 171 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 172 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 173 if (dp->d_dump == NULL) { 174 g_io_deliver(bp, ENODEV); 175 return; 176 } 177 gkd->di.dumper = dp->d_dump; 178 gkd->di.priv = dp; 179 gkd->di.blocksize = dp->d_sectorsize; 180 gkd->di.maxiosize = dp->d_maxsize; 181 gkd->di.mediaoffset = gkd->offset; 182 if ((gkd->offset + gkd->length) > dp->d_mediasize) 183 gkd->length = dp->d_mediasize - gkd->offset; 184 gkd->di.mediasize = gkd->length; 185 g_io_deliver(bp, 0); 186} 187 188static void
| 175 dp->d_flags &= ~DISKFLAG_OPEN; 176 } 177 return (error); 178} 179 180static void 181g_disk_kerneldump(struct bio *bp, struct disk *dp) 182{ 183 struct g_kerneldump *gkd; 184 struct g_geom *gp; 185 186 gkd = (struct g_kerneldump*)bp->bio_data; 187 gp = bp->bio_to->geom; 188 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 189 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 190 if (dp->d_dump == NULL) { 191 g_io_deliver(bp, ENODEV); 192 return; 193 } 194 gkd->di.dumper = dp->d_dump; 195 gkd->di.priv = dp; 196 gkd->di.blocksize = dp->d_sectorsize; 197 gkd->di.maxiosize = dp->d_maxsize; 198 gkd->di.mediaoffset = gkd->offset; 199 if ((gkd->offset + gkd->length) > dp->d_mediasize) 200 gkd->length = dp->d_mediasize - gkd->offset; 201 gkd->di.mediasize = gkd->length; 202 g_io_deliver(bp, 0); 203} 204 205static void
|
| 206g_disk_setstate(struct bio *bp, struct g_disk_softc *sc) 207{ 208 const char *cmd; 209 210 memcpy(&sc->state, bp->bio_data, sizeof(sc->state)); 211 if (sc->led[0] != 0) { 212 switch (sc->state) { 213 case G_STATE_FAILED: 214 cmd = "1"; 215 break; 216 case G_STATE_REBUILD: 217 cmd = "f5"; 218 break; 219 case G_STATE_RESYNC: 220 cmd = "f1"; 221 break; 222 default: 223 cmd = "0"; 224 break; 225 } 226 led_set(sc->led, cmd); 227 } 228 g_io_deliver(bp, 0); 229} 230 231static void
|
189g_disk_done(struct bio *bp) 190{ 191 struct bio *bp2; 192 struct disk *dp;
| 232g_disk_done(struct bio *bp) 233{ 234 struct bio *bp2; 235 struct disk *dp;
|
| 236 struct g_disk_softc *sc;
|
193 194 /* See "notes" for why we need a mutex here */ 195 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 196 mtx_lock(&g_disk_done_mtx); 197 bp->bio_completed = bp->bio_length - bp->bio_resid; 198 199 bp2 = bp->bio_parent; 200 if (bp2->bio_error == 0) 201 bp2->bio_error = bp->bio_error; 202 bp2->bio_completed += bp->bio_completed; 203 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) &&
| 237 238 /* See "notes" for why we need a mutex here */ 239 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 240 mtx_lock(&g_disk_done_mtx); 241 bp->bio_completed = bp->bio_length - bp->bio_resid; 242 243 bp2 = bp->bio_parent; 244 if (bp2->bio_error == 0) 245 bp2->bio_error = bp->bio_error; 246 bp2->bio_completed += bp->bio_completed; 247 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) &&
|
204 (dp = bp2->bio_to->geom->softc)) {
| 248 (sc = bp2->bio_to->geom->softc) && 249 (dp = sc->dp)) {
|
205 devstat_end_transaction_bio(dp->d_devstat, bp); 206 } 207 g_destroy_bio(bp); 208 bp2->bio_inbed++; 209 if (bp2->bio_children == bp2->bio_inbed) { 210 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 211 g_io_deliver(bp2, bp2->bio_error); 212 } 213 mtx_unlock(&g_disk_done_mtx); 214} 215 216static int 217g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 218{ 219 struct g_geom *gp; 220 struct disk *dp;
| 250 devstat_end_transaction_bio(dp->d_devstat, bp); 251 } 252 g_destroy_bio(bp); 253 bp2->bio_inbed++; 254 if (bp2->bio_children == bp2->bio_inbed) { 255 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 256 g_io_deliver(bp2, bp2->bio_error); 257 } 258 mtx_unlock(&g_disk_done_mtx); 259} 260 261static int 262g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 263{ 264 struct g_geom *gp; 265 struct disk *dp;
|
| 266 struct g_disk_softc *sc;
|
221 int error; 222 223 gp = pp->geom;
| 267 int error; 268 269 gp = pp->geom;
|
224 dp = gp->softc;
| 270 sc = gp->softc; 271 dp = sc->dp;
|
225 226 if (dp->d_ioctl == NULL) 227 return (ENOIOCTL); 228 g_disk_lock_giant(dp); 229 error = dp->d_ioctl(dp, cmd, data, fflag, td); 230 g_disk_unlock_giant(dp); 231 return(error); 232} 233 234static void 235g_disk_start(struct bio *bp) 236{ 237 struct bio *bp2, *bp3; 238 struct disk *dp;
| 272 273 if (dp->d_ioctl == NULL) 274 return (ENOIOCTL); 275 g_disk_lock_giant(dp); 276 error = dp->d_ioctl(dp, cmd, data, fflag, td); 277 g_disk_unlock_giant(dp); 278 return(error); 279} 280 281static void 282g_disk_start(struct bio *bp) 283{ 284 struct bio *bp2, *bp3; 285 struct disk *dp;
|
| 286 struct g_disk_softc *sc;
|
239 int error; 240 off_t off; 241
| 287 int error; 288 off_t off; 289
|
242 dp = bp->bio_to->geom->softc; 243 if (dp == NULL || dp->d_destroyed) {
| 290 sc = bp->bio_to->geom->softc; 291 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
|
244 g_io_deliver(bp, ENXIO); 245 return; 246 } 247 error = EJUSTRETURN; 248 switch(bp->bio_cmd) { 249 case BIO_DELETE: 250 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 251 error = 0; 252 break; 253 } 254 /* fall-through */ 255 case BIO_READ: 256 case BIO_WRITE: 257 off = 0; 258 bp3 = NULL; 259 bp2 = g_clone_bio(bp); 260 if (bp2 == NULL) { 261 error = ENOMEM; 262 break; 263 } 264 do { 265 bp2->bio_offset += off; 266 bp2->bio_length -= off; 267 bp2->bio_data += off; 268 if (bp2->bio_length > dp->d_maxsize) { 269 /* 270 * XXX: If we have a stripesize we should really 271 * use it here. 272 */ 273 bp2->bio_length = dp->d_maxsize; 274 off += dp->d_maxsize; 275 /* 276 * To avoid a race, we need to grab the next bio 277 * before we schedule this one. See "notes". 278 */ 279 bp3 = g_clone_bio(bp); 280 if (bp3 == NULL) 281 bp->bio_error = ENOMEM; 282 } 283 bp2->bio_done = g_disk_done; 284 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 285 bp2->bio_bcount = bp2->bio_length; 286 bp2->bio_disk = dp; 287 devstat_start_transaction_bio(dp->d_devstat, bp2); 288 g_disk_lock_giant(dp); 289 dp->d_strategy(bp2); 290 g_disk_unlock_giant(dp); 291 bp2 = bp3; 292 bp3 = NULL; 293 } while (bp2 != NULL); 294 break; 295 case BIO_GETATTR: 296 if (g_handleattr_int(bp, "GEOM::candelete", 297 (dp->d_flags & DISKFLAG_CANDELETE) != 0)) 298 break; 299 else if (g_handleattr_int(bp, "GEOM::fwsectors", 300 dp->d_fwsectors)) 301 break; 302 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 303 break; 304 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 305 break; 306 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident)) 307 break; 308 else if (g_handleattr(bp, "GEOM::hba_vendor", 309 &dp->d_hba_vendor, 2)) 310 break; 311 else if (g_handleattr(bp, "GEOM::hba_device", 312 &dp->d_hba_device, 2)) 313 break; 314 else if (g_handleattr(bp, "GEOM::hba_subvendor", 315 &dp->d_hba_subvendor, 2)) 316 break; 317 else if (g_handleattr(bp, "GEOM::hba_subdevice", 318 &dp->d_hba_subdevice, 2)) 319 break; 320 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 321 g_disk_kerneldump(bp, dp);
| 292 g_io_deliver(bp, ENXIO); 293 return; 294 } 295 error = EJUSTRETURN; 296 switch(bp->bio_cmd) { 297 case BIO_DELETE: 298 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 299 error = 0; 300 break; 301 } 302 /* fall-through */ 303 case BIO_READ: 304 case BIO_WRITE: 305 off = 0; 306 bp3 = NULL; 307 bp2 = g_clone_bio(bp); 308 if (bp2 == NULL) { 309 error = ENOMEM; 310 break; 311 } 312 do { 313 bp2->bio_offset += off; 314 bp2->bio_length -= off; 315 bp2->bio_data += off; 316 if (bp2->bio_length > dp->d_maxsize) { 317 /* 318 * XXX: If we have a stripesize we should really 319 * use it here. 320 */ 321 bp2->bio_length = dp->d_maxsize; 322 off += dp->d_maxsize; 323 /* 324 * To avoid a race, we need to grab the next bio 325 * before we schedule this one. See "notes". 326 */ 327 bp3 = g_clone_bio(bp); 328 if (bp3 == NULL) 329 bp->bio_error = ENOMEM; 330 } 331 bp2->bio_done = g_disk_done; 332 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 333 bp2->bio_bcount = bp2->bio_length; 334 bp2->bio_disk = dp; 335 devstat_start_transaction_bio(dp->d_devstat, bp2); 336 g_disk_lock_giant(dp); 337 dp->d_strategy(bp2); 338 g_disk_unlock_giant(dp); 339 bp2 = bp3; 340 bp3 = NULL; 341 } while (bp2 != NULL); 342 break; 343 case BIO_GETATTR: 344 if (g_handleattr_int(bp, "GEOM::candelete", 345 (dp->d_flags & DISKFLAG_CANDELETE) != 0)) 346 break; 347 else if (g_handleattr_int(bp, "GEOM::fwsectors", 348 dp->d_fwsectors)) 349 break; 350 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 351 break; 352 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 353 break; 354 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident)) 355 break; 356 else if (g_handleattr(bp, "GEOM::hba_vendor", 357 &dp->d_hba_vendor, 2)) 358 break; 359 else if (g_handleattr(bp, "GEOM::hba_device", 360 &dp->d_hba_device, 2)) 361 break; 362 else if (g_handleattr(bp, "GEOM::hba_subvendor", 363 &dp->d_hba_subvendor, 2)) 364 break; 365 else if (g_handleattr(bp, "GEOM::hba_subdevice", 366 &dp->d_hba_subdevice, 2)) 367 break; 368 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 369 g_disk_kerneldump(bp, dp);
|
| 370 else if (!strcmp(bp->bio_attribute, "GEOM::setstate")) 371 g_disk_setstate(bp, sc);
|
322 else 323 error = ENOIOCTL; 324 break; 325 case BIO_FLUSH: 326 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)", 327 bp->bio_to->name); 328 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 329 g_io_deliver(bp, ENODEV); 330 return; 331 } 332 bp2 = g_clone_bio(bp); 333 if (bp2 == NULL) { 334 g_io_deliver(bp, ENOMEM); 335 return; 336 } 337 bp2->bio_done = g_disk_done; 338 bp2->bio_disk = dp; 339 g_disk_lock_giant(dp); 340 dp->d_strategy(bp2); 341 g_disk_unlock_giant(dp); 342 break; 343 default: 344 error = EOPNOTSUPP; 345 break; 346 } 347 if (error != EJUSTRETURN) 348 g_io_deliver(bp, error); 349 return; 350} 351 352static void 353g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 354{ 355 struct disk *dp;
| 372 else 373 error = ENOIOCTL; 374 break; 375 case BIO_FLUSH: 376 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)", 377 bp->bio_to->name); 378 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 379 g_io_deliver(bp, ENODEV); 380 return; 381 } 382 bp2 = g_clone_bio(bp); 383 if (bp2 == NULL) { 384 g_io_deliver(bp, ENOMEM); 385 return; 386 } 387 bp2->bio_done = g_disk_done; 388 bp2->bio_disk = dp; 389 g_disk_lock_giant(dp); 390 dp->d_strategy(bp2); 391 g_disk_unlock_giant(dp); 392 break; 393 default: 394 error = EOPNOTSUPP; 395 break; 396 } 397 if (error != EJUSTRETURN) 398 g_io_deliver(bp, error); 399 return; 400} 401 402static void 403g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 404{ 405 struct disk *dp;
|
| 406 struct g_disk_softc *sc;
|
356
| 407
|
357 dp = gp->softc; 358 if (dp == NULL)
| 408 sc = gp->softc; 409 if (sc == NULL || (dp = sc->dp) == NULL)
|
359 return; 360 if (indent == NULL) { 361 sbuf_printf(sb, " hd %u", dp->d_fwheads); 362 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 363 return; 364 } 365 if (pp != NULL) { 366 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 367 indent, dp->d_fwheads); 368 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 369 indent, dp->d_fwsectors); 370 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident); 371 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr); 372 } 373} 374 375static void 376g_disk_create(void *arg, int flag) 377{ 378 struct g_geom *gp; 379 struct g_provider *pp; 380 struct disk *dp;
| 410 return; 411 if (indent == NULL) { 412 sbuf_printf(sb, " hd %u", dp->d_fwheads); 413 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 414 return; 415 } 416 if (pp != NULL) { 417 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 418 indent, dp->d_fwheads); 419 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 420 indent, dp->d_fwsectors); 421 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident); 422 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr); 423 } 424} 425 426static void 427g_disk_create(void *arg, int flag) 428{ 429 struct g_geom *gp; 430 struct g_provider *pp; 431 struct disk *dp;
|
| 432 struct g_disk_softc *sc; 433 char tmpstr[80];
|
381 382 if (flag == EV_CANCEL) 383 return; 384 g_topology_assert(); 385 dp = arg;
| 434 435 if (flag == EV_CANCEL) 436 return; 437 g_topology_assert(); 438 dp = arg;
|
| 439 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 440 sc->dp = dp;
|
386 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
| 441 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
|
387 gp->softc = dp;
| 442 gp->softc = sc;
|
388 pp = g_new_providerf(gp, "%s", gp->name); 389 pp->mediasize = dp->d_mediasize; 390 pp->sectorsize = dp->d_sectorsize; 391 if (dp->d_flags & DISKFLAG_CANDELETE) 392 pp->flags |= G_PF_CANDELETE; 393 pp->stripeoffset = dp->d_stripeoffset; 394 pp->stripesize = dp->d_stripesize; 395 if (bootverbose) 396 printf("GEOM: new disk %s\n", gp->name);
| 443 pp = g_new_providerf(gp, "%s", gp->name); 444 pp->mediasize = dp->d_mediasize; 445 pp->sectorsize = dp->d_sectorsize; 446 if (dp->d_flags & DISKFLAG_CANDELETE) 447 pp->flags |= G_PF_CANDELETE; 448 pp->stripeoffset = dp->d_stripeoffset; 449 pp->stripesize = dp->d_stripesize; 450 if (bootverbose) 451 printf("GEOM: new disk %s\n", gp->name);
|
| 452 sysctl_ctx_init(&sc->sysctl_ctx); 453 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name); 454 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx, 455 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name, 456 CTLFLAG_RD, 0, tmpstr); 457 if (sc->sysctl_tree != NULL) { 458 snprintf(tmpstr, sizeof(tmpstr), 459 "kern.geom.disk.%s.led", gp->name); 460 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led)); 461 SYSCTL_ADD_STRING(&sc->sysctl_ctx, 462 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led", 463 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led), 464 "LED name"); 465 } 466 pp->private = sc;
|
397 dp->d_geom = gp; 398 g_error_provider(pp, 0); 399} 400 401static void 402g_disk_destroy(void *ptr, int flag) 403{ 404 struct disk *dp; 405 struct g_geom *gp;
| 467 dp->d_geom = gp; 468 g_error_provider(pp, 0); 469} 470 471static void 472g_disk_destroy(void *ptr, int flag) 473{ 474 struct disk *dp; 475 struct g_geom *gp;
|
| 476 struct g_disk_softc *sc;
|
406 407 g_topology_assert(); 408 dp = ptr; 409 gp = dp->d_geom; 410 if (gp != NULL) {
| 477 478 g_topology_assert(); 479 dp = ptr; 480 gp = dp->d_geom; 481 if (gp != NULL) {
|
| 482 sc = gp->softc; 483 if (sc->sysctl_tree != NULL) { 484 sysctl_ctx_free(&sc->sysctl_ctx); 485 sc->sysctl_tree = NULL; 486 } 487 if (sc->led[0] != 0) { 488 led_set(sc->led, "0"); 489 sc->led[0] = 0; 490 } 491 g_free(sc);
|
411 gp->softc = NULL; 412 g_wither_geom(gp, ENXIO); 413 } 414 g_free(dp); 415} 416 417/* 418 * We only allow printable characters in disk ident, 419 * the rest is converted to 'x<HH>'. 420 */ 421static void 422g_disk_ident_adjust(char *ident, size_t size) 423{ 424 char *p, tmp[4], newid[DISK_IDENT_SIZE]; 425 426 newid[0] = '\0'; 427 for (p = ident; *p != '\0'; p++) { 428 if (isprint(*p)) { 429 tmp[0] = *p; 430 tmp[1] = '\0'; 431 } else { 432 snprintf(tmp, sizeof(tmp), "x%02hhx", 433 *(unsigned char *)p); 434 } 435 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid)) 436 break; 437 } 438 bzero(ident, size); 439 strlcpy(ident, newid, size); 440} 441 442struct disk * 443disk_alloc() 444{ 445 struct disk *dp; 446 447 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO); 448 return (dp); 449} 450 451void 452disk_create(struct disk *dp, int version) 453{ 454 if (version != DISK_VERSION_00 && version != DISK_VERSION_01) { 455 printf("WARNING: Attempt to add disk %s%d %s", 456 dp->d_name, dp->d_unit, 457 " using incompatible ABI version of disk(9)\n"); 458 printf("WARNING: Ignoring disk %s%d\n", 459 dp->d_name, dp->d_unit); 460 return; 461 } 462 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 463 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 464 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 465 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 466 if (dp->d_devstat == NULL) 467 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 468 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 469 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 470 dp->d_geom = NULL; 471 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident)); 472 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 473} 474 475void 476disk_destroy(struct disk *dp) 477{ 478 479 g_cancel_event(dp); 480 dp->d_destroyed = 1; 481 if (dp->d_devstat != NULL) 482 devstat_remove_entry(dp->d_devstat); 483 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 484} 485 486void 487disk_gone(struct disk *dp) 488{ 489 struct g_geom *gp; 490 struct g_provider *pp; 491 492 gp = dp->d_geom; 493 if (gp != NULL) 494 LIST_FOREACH(pp, &gp->provider, provider) 495 g_wither_provider(pp, ENXIO); 496} 497 498static void 499g_kern_disks(void *p, int flag __unused) 500{ 501 struct sbuf *sb; 502 struct g_geom *gp; 503 char *sp; 504 505 sb = p; 506 sp = ""; 507 g_topology_assert(); 508 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 509 sbuf_printf(sb, "%s%s", sp, gp->name); 510 sp = " "; 511 } 512 sbuf_finish(sb); 513} 514 515static int 516sysctl_disks(SYSCTL_HANDLER_ARGS) 517{ 518 int error; 519 struct sbuf *sb; 520 521 sb = sbuf_new_auto(); 522 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 523 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 524 sbuf_delete(sb); 525 return error; 526} 527 528SYSCTL_PROC(_kern, OID_AUTO, disks, 529 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 530 sysctl_disks, "A", "names of available disks"); 531
| 492 gp->softc = NULL; 493 g_wither_geom(gp, ENXIO); 494 } 495 g_free(dp); 496} 497 498/* 499 * We only allow printable characters in disk ident, 500 * the rest is converted to 'x<HH>'. 501 */ 502static void 503g_disk_ident_adjust(char *ident, size_t size) 504{ 505 char *p, tmp[4], newid[DISK_IDENT_SIZE]; 506 507 newid[0] = '\0'; 508 for (p = ident; *p != '\0'; p++) { 509 if (isprint(*p)) { 510 tmp[0] = *p; 511 tmp[1] = '\0'; 512 } else { 513 snprintf(tmp, sizeof(tmp), "x%02hhx", 514 *(unsigned char *)p); 515 } 516 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid)) 517 break; 518 } 519 bzero(ident, size); 520 strlcpy(ident, newid, size); 521} 522 523struct disk * 524disk_alloc() 525{ 526 struct disk *dp; 527 528 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO); 529 return (dp); 530} 531 532void 533disk_create(struct disk *dp, int version) 534{ 535 if (version != DISK_VERSION_00 && version != DISK_VERSION_01) { 536 printf("WARNING: Attempt to add disk %s%d %s", 537 dp->d_name, dp->d_unit, 538 " using incompatible ABI version of disk(9)\n"); 539 printf("WARNING: Ignoring disk %s%d\n", 540 dp->d_name, dp->d_unit); 541 return; 542 } 543 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 544 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 545 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 546 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 547 if (dp->d_devstat == NULL) 548 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 549 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 550 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 551 dp->d_geom = NULL; 552 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident)); 553 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 554} 555 556void 557disk_destroy(struct disk *dp) 558{ 559 560 g_cancel_event(dp); 561 dp->d_destroyed = 1; 562 if (dp->d_devstat != NULL) 563 devstat_remove_entry(dp->d_devstat); 564 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 565} 566 567void 568disk_gone(struct disk *dp) 569{ 570 struct g_geom *gp; 571 struct g_provider *pp; 572 573 gp = dp->d_geom; 574 if (gp != NULL) 575 LIST_FOREACH(pp, &gp->provider, provider) 576 g_wither_provider(pp, ENXIO); 577} 578 579static void 580g_kern_disks(void *p, int flag __unused) 581{ 582 struct sbuf *sb; 583 struct g_geom *gp; 584 char *sp; 585 586 sb = p; 587 sp = ""; 588 g_topology_assert(); 589 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 590 sbuf_printf(sb, "%s%s", sp, gp->name); 591 sp = " "; 592 } 593 sbuf_finish(sb); 594} 595 596static int 597sysctl_disks(SYSCTL_HANDLER_ARGS) 598{ 599 int error; 600 struct sbuf *sb; 601 602 sb = sbuf_new_auto(); 603 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 604 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 605 sbuf_delete(sb); 606 return error; 607} 608 609SYSCTL_PROC(_kern, OID_AUTO, disks, 610 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 611 sysctl_disks, "A", "names of available disks"); 612
|