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 --- 20 unchanged lines hidden (view full) --- 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 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> --- 5 unchanged lines hidden (view full) --- 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 |
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; --- 4 unchanged lines hidden (view full) --- 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 |
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 --- 18 unchanged lines hidden (view full) --- 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; |
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(); |
132 sc = pp->geom->softc; 133 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) { |
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 } --- 22 unchanged lines hidden (view full) --- 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"); |
175 dp->d_flags &= ~DISKFLAG_OPEN; 176 } 177 return (error); 178} 179 180static void 181g_disk_kerneldump(struct bio *bp, struct disk *dp) 182{ --- 15 unchanged lines hidden (view full) --- 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 |
232g_disk_done(struct bio *bp) 233{ 234 struct bio *bp2; 235 struct disk *dp; |
236 struct g_disk_softc *sc; |
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)) && |
248 (sc = bp2->bio_to->geom->softc) && 249 (dp = sc->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; |
267 int error; 268 269 gp = pp->geom; |
270 sc = gp->softc; 271 dp = sc->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; |
287 int error; 288 off_t off; 289 |
290 sc = bp->bio_to->geom->softc; 291 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) { |
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; --- 62 unchanged lines hidden (view full) --- 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); |
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); --- 18 unchanged lines hidden (view full) --- 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; |
407 |
408 sc = gp->softc; 409 if (sc == NULL || (dp = sc->dp) == NULL) |
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", --- 6 unchanged lines hidden (view full) --- 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]; |
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; |
441 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); |
442 gp->softc = sc; |
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; |
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; |
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); |
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, --- 113 unchanged lines hidden --- |