| 1 | /* $NetBSD: dk.c,v 1.91 2016/05/29 13:11:21 mlelstv Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to The NetBSD Foundation |
| 8 | * by Jason R. Thorpe. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include <sys/cdefs.h> |
| 33 | __KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.91 2016/05/29 13:11:21 mlelstv Exp $" ); |
| 34 | |
| 35 | #ifdef _KERNEL_OPT |
| 36 | #include "opt_dkwedge.h" |
| 37 | #endif |
| 38 | |
| 39 | #include <sys/param.h> |
| 40 | #include <sys/systm.h> |
| 41 | #include <sys/proc.h> |
| 42 | #include <sys/errno.h> |
| 43 | #include <sys/pool.h> |
| 44 | #include <sys/ioctl.h> |
| 45 | #include <sys/disklabel.h> |
| 46 | #include <sys/disk.h> |
| 47 | #include <sys/fcntl.h> |
| 48 | #include <sys/buf.h> |
| 49 | #include <sys/bufq.h> |
| 50 | #include <sys/vnode.h> |
| 51 | #include <sys/stat.h> |
| 52 | #include <sys/conf.h> |
| 53 | #include <sys/callout.h> |
| 54 | #include <sys/kernel.h> |
| 55 | #include <sys/malloc.h> |
| 56 | #include <sys/device.h> |
| 57 | #include <sys/kauth.h> |
| 58 | |
| 59 | #include <miscfs/specfs/specdev.h> |
| 60 | |
| 61 | MALLOC_DEFINE(M_DKWEDGE, "dkwedge" , "Disk wedge structures" ); |
| 62 | |
| 63 | typedef enum { |
| 64 | DKW_STATE_LARVAL = 0, |
| 65 | DKW_STATE_RUNNING = 1, |
| 66 | DKW_STATE_DYING = 2, |
| 67 | DKW_STATE_DEAD = 666 |
| 68 | } dkwedge_state_t; |
| 69 | |
| 70 | struct dkwedge_softc { |
| 71 | device_t sc_dev; /* pointer to our pseudo-device */ |
| 72 | struct cfdata sc_cfdata; /* our cfdata structure */ |
| 73 | uint8_t sc_wname[128]; /* wedge name (Unicode, UTF-8) */ |
| 74 | |
| 75 | dkwedge_state_t sc_state; /* state this wedge is in */ |
| 76 | |
| 77 | struct disk *sc_parent; /* parent disk */ |
| 78 | daddr_t sc_offset; /* LBA offset of wedge in parent */ |
| 79 | uint64_t sc_size; /* size of wedge in blocks */ |
| 80 | char sc_ptype[32]; /* partition type */ |
| 81 | dev_t sc_pdev; /* cached parent's dev_t */ |
| 82 | /* link on parent's wedge list */ |
| 83 | LIST_ENTRY(dkwedge_softc) sc_plink; |
| 84 | |
| 85 | struct disk sc_dk; /* our own disk structure */ |
| 86 | struct bufq_state *sc_bufq; /* buffer queue */ |
| 87 | struct callout sc_restart_ch; /* callout to restart I/O */ |
| 88 | |
| 89 | u_int sc_iopend; /* I/Os pending */ |
| 90 | int sc_flags; /* flags (splbio) */ |
| 91 | }; |
| 92 | |
| 93 | #define DK_F_WAIT_DRAIN 0x0001 /* waiting for I/O to drain */ |
| 94 | |
| 95 | static void dkstart(struct dkwedge_softc *); |
| 96 | static void dkiodone(struct buf *); |
| 97 | static void dkrestart(void *); |
| 98 | static void dkminphys(struct buf *); |
| 99 | |
| 100 | static int dklastclose(struct dkwedge_softc *); |
| 101 | static int dkwedge_cleanup_parent(struct dkwedge_softc *, int); |
| 102 | static int dkwedge_detach(device_t, int); |
| 103 | static void dkwedge_delall1(struct disk *, bool); |
| 104 | static int dkwedge_del1(struct dkwedge_info *, int); |
| 105 | static int dk_open_parent(dev_t, int, struct vnode **); |
| 106 | static int dk_close_parent(struct vnode *, int); |
| 107 | |
| 108 | static dev_type_open(dkopen); |
| 109 | static dev_type_close(dkclose); |
| 110 | static dev_type_read(dkread); |
| 111 | static dev_type_write(dkwrite); |
| 112 | static dev_type_ioctl(dkioctl); |
| 113 | static dev_type_strategy(dkstrategy); |
| 114 | static dev_type_dump(dkdump); |
| 115 | static dev_type_size(dksize); |
| 116 | static dev_type_discard(dkdiscard); |
| 117 | |
| 118 | const struct bdevsw dk_bdevsw = { |
| 119 | .d_open = dkopen, |
| 120 | .d_close = dkclose, |
| 121 | .d_strategy = dkstrategy, |
| 122 | .d_ioctl = dkioctl, |
| 123 | .d_dump = dkdump, |
| 124 | .d_psize = dksize, |
| 125 | .d_discard = dkdiscard, |
| 126 | .d_flag = D_DISK |
| 127 | }; |
| 128 | |
| 129 | const struct cdevsw dk_cdevsw = { |
| 130 | .d_open = dkopen, |
| 131 | .d_close = dkclose, |
| 132 | .d_read = dkread, |
| 133 | .d_write = dkwrite, |
| 134 | .d_ioctl = dkioctl, |
| 135 | .d_stop = nostop, |
| 136 | .d_tty = notty, |
| 137 | .d_poll = nopoll, |
| 138 | .d_mmap = nommap, |
| 139 | .d_kqfilter = nokqfilter, |
| 140 | .d_discard = dkdiscard, |
| 141 | .d_flag = D_DISK |
| 142 | }; |
| 143 | |
| 144 | static struct dkwedge_softc **dkwedges; |
| 145 | static u_int ndkwedges; |
| 146 | static krwlock_t dkwedges_lock; |
| 147 | |
| 148 | static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods; |
| 149 | static krwlock_t dkwedge_discovery_methods_lock; |
| 150 | |
| 151 | /* |
| 152 | * dkwedge_match: |
| 153 | * |
| 154 | * Autoconfiguration match function for pseudo-device glue. |
| 155 | */ |
| 156 | static int |
| 157 | dkwedge_match(device_t parent, cfdata_t match, |
| 158 | void *aux) |
| 159 | { |
| 160 | |
| 161 | /* Pseudo-device; always present. */ |
| 162 | return (1); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * dkwedge_attach: |
| 167 | * |
| 168 | * Autoconfiguration attach function for pseudo-device glue. |
| 169 | */ |
| 170 | static void |
| 171 | dkwedge_attach(device_t parent, device_t self, |
| 172 | void *aux) |
| 173 | { |
| 174 | |
| 175 | if (!pmf_device_register(self, NULL, NULL)) |
| 176 | aprint_error_dev(self, "couldn't establish power handler\n" ); |
| 177 | } |
| 178 | |
| 179 | CFDRIVER_DECL(dk, DV_DISK, NULL); |
| 180 | CFATTACH_DECL3_NEW(dk, 0, |
| 181 | dkwedge_match, dkwedge_attach, dkwedge_detach, NULL, NULL, NULL, |
| 182 | DVF_DETACH_SHUTDOWN); |
| 183 | |
| 184 | /* |
| 185 | * dkwedge_wait_drain: |
| 186 | * |
| 187 | * Wait for I/O on the wedge to drain. |
| 188 | * NOTE: Must be called at splbio()! |
| 189 | */ |
| 190 | static void |
| 191 | dkwedge_wait_drain(struct dkwedge_softc *sc) |
| 192 | { |
| 193 | |
| 194 | while (sc->sc_iopend != 0) { |
| 195 | sc->sc_flags |= DK_F_WAIT_DRAIN; |
| 196 | (void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn" , 0); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * dkwedge_compute_pdev: |
| 202 | * |
| 203 | * Compute the parent disk's dev_t. |
| 204 | */ |
| 205 | static int |
| 206 | dkwedge_compute_pdev(const char *pname, dev_t *pdevp, enum vtype type) |
| 207 | { |
| 208 | const char *name, *cp; |
| 209 | devmajor_t pmaj; |
| 210 | int punit; |
| 211 | char devname[16]; |
| 212 | |
| 213 | name = pname; |
| 214 | switch (type) { |
| 215 | case VBLK: |
| 216 | pmaj = devsw_name2blk(name, devname, sizeof(devname)); |
| 217 | break; |
| 218 | case VCHR: |
| 219 | pmaj = devsw_name2chr(name, devname, sizeof(devname)); |
| 220 | break; |
| 221 | default: |
| 222 | pmaj = NODEVMAJOR; |
| 223 | break; |
| 224 | } |
| 225 | if (pmaj == NODEVMAJOR) |
| 226 | return (ENODEV); |
| 227 | |
| 228 | name += strlen(devname); |
| 229 | for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 230 | punit = (punit * 10) + (*cp - '0'); |
| 231 | if (cp == name) { |
| 232 | /* Invalid parent disk name. */ |
| 233 | return (ENODEV); |
| 234 | } |
| 235 | |
| 236 | *pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART); |
| 237 | |
| 238 | return (0); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * dkwedge_array_expand: |
| 243 | * |
| 244 | * Expand the dkwedges array. |
| 245 | */ |
| 246 | static void |
| 247 | dkwedge_array_expand(void) |
| 248 | { |
| 249 | int newcnt = ndkwedges + 16; |
| 250 | struct dkwedge_softc **newarray, **oldarray; |
| 251 | |
| 252 | newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE, |
| 253 | M_WAITOK|M_ZERO); |
| 254 | if ((oldarray = dkwedges) != NULL) |
| 255 | memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray)); |
| 256 | dkwedges = newarray; |
| 257 | ndkwedges = newcnt; |
| 258 | if (oldarray != NULL) |
| 259 | free(oldarray, M_DKWEDGE); |
| 260 | } |
| 261 | |
| 262 | static void |
| 263 | dk_set_geometry(struct dkwedge_softc *sc, struct disk *pdk) |
| 264 | { |
| 265 | struct disk *dk = &sc->sc_dk; |
| 266 | struct disk_geom *dg = &dk->dk_geom; |
| 267 | |
| 268 | memset(dg, 0, sizeof(*dg)); |
| 269 | |
| 270 | dg->dg_secperunit = sc->sc_size; |
| 271 | dg->dg_secsize = DEV_BSIZE << pdk->dk_blkshift; |
| 272 | |
| 273 | /* fake numbers, 1 cylinder is 1 MB with default sector size */ |
| 274 | dg->dg_nsectors = 32; |
| 275 | dg->dg_ntracks = 64; |
| 276 | dg->dg_ncylinders = dg->dg_secperunit / (dg->dg_nsectors * dg->dg_ntracks); |
| 277 | |
| 278 | disk_set_info(sc->sc_dev, dk, NULL); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * dkwedge_add: [exported function] |
| 283 | * |
| 284 | * Add a disk wedge based on the provided information. |
| 285 | * |
| 286 | * The incoming dkw_devname[] is ignored, instead being |
| 287 | * filled in and returned to the caller. |
| 288 | */ |
| 289 | int |
| 290 | dkwedge_add(struct dkwedge_info *dkw) |
| 291 | { |
| 292 | struct dkwedge_softc *sc, *lsc; |
| 293 | struct disk *pdk; |
| 294 | u_int unit; |
| 295 | int error; |
| 296 | dev_t pdev; |
| 297 | |
| 298 | dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0'; |
| 299 | pdk = disk_find(dkw->dkw_parent); |
| 300 | if (pdk == NULL) |
| 301 | return (ENODEV); |
| 302 | |
| 303 | error = dkwedge_compute_pdev(pdk->dk_name, &pdev, VBLK); |
| 304 | if (error) |
| 305 | return (error); |
| 306 | |
| 307 | if (dkw->dkw_offset < 0) |
| 308 | return (EINVAL); |
| 309 | |
| 310 | sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO); |
| 311 | sc->sc_state = DKW_STATE_LARVAL; |
| 312 | sc->sc_parent = pdk; |
| 313 | sc->sc_pdev = pdev; |
| 314 | sc->sc_offset = dkw->dkw_offset; |
| 315 | sc->sc_size = dkw->dkw_size; |
| 316 | |
| 317 | memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname)); |
| 318 | sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0'; |
| 319 | |
| 320 | memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype)); |
| 321 | sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0'; |
| 322 | |
| 323 | bufq_alloc(&sc->sc_bufq, "fcfs" , 0); |
| 324 | |
| 325 | callout_init(&sc->sc_restart_ch, 0); |
| 326 | callout_setfunc(&sc->sc_restart_ch, dkrestart, sc); |
| 327 | |
| 328 | /* |
| 329 | * Wedge will be added; increment the wedge count for the parent. |
| 330 | * Only allow this to happend if RAW_PART is the only thing open. |
| 331 | */ |
| 332 | mutex_enter(&pdk->dk_openlock); |
| 333 | if (pdk->dk_openmask & ~(1 << RAW_PART)) |
| 334 | error = EBUSY; |
| 335 | else { |
| 336 | /* Check for wedge overlap. */ |
| 337 | LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) { |
| 338 | daddr_t lastblk = sc->sc_offset + sc->sc_size - 1; |
| 339 | daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1; |
| 340 | |
| 341 | if (sc->sc_offset >= lsc->sc_offset && |
| 342 | sc->sc_offset <= llastblk) { |
| 343 | /* Overlaps the tail of the existing wedge. */ |
| 344 | break; |
| 345 | } |
| 346 | if (lastblk >= lsc->sc_offset && |
| 347 | lastblk <= llastblk) { |
| 348 | /* Overlaps the head of the existing wedge. */ |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | if (lsc != NULL) { |
| 353 | if (sc->sc_offset == lsc->sc_offset && |
| 354 | sc->sc_size == lsc->sc_size && |
| 355 | strcmp(sc->sc_wname, lsc->sc_wname) == 0) |
| 356 | error = EEXIST; |
| 357 | else |
| 358 | error = EINVAL; |
| 359 | } else { |
| 360 | pdk->dk_nwedges++; |
| 361 | LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink); |
| 362 | } |
| 363 | } |
| 364 | mutex_exit(&pdk->dk_openlock); |
| 365 | if (error) { |
| 366 | bufq_free(sc->sc_bufq); |
| 367 | free(sc, M_DKWEDGE); |
| 368 | return (error); |
| 369 | } |
| 370 | |
| 371 | /* Fill in our cfdata for the pseudo-device glue. */ |
| 372 | sc->sc_cfdata.cf_name = dk_cd.cd_name; |
| 373 | sc->sc_cfdata.cf_atname = dk_ca.ca_name; |
| 374 | /* sc->sc_cfdata.cf_unit set below */ |
| 375 | sc->sc_cfdata.cf_fstate = FSTATE_STAR; |
| 376 | |
| 377 | /* Insert the larval wedge into the array. */ |
| 378 | rw_enter(&dkwedges_lock, RW_WRITER); |
| 379 | for (error = 0;;) { |
| 380 | struct dkwedge_softc **scpp; |
| 381 | |
| 382 | /* |
| 383 | * Check for a duplicate wname while searching for |
| 384 | * a slot. |
| 385 | */ |
| 386 | for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) { |
| 387 | if (dkwedges[unit] == NULL) { |
| 388 | if (scpp == NULL) { |
| 389 | scpp = &dkwedges[unit]; |
| 390 | sc->sc_cfdata.cf_unit = unit; |
| 391 | } |
| 392 | } else { |
| 393 | /* XXX Unicode. */ |
| 394 | if (strcmp(dkwedges[unit]->sc_wname, |
| 395 | sc->sc_wname) == 0) { |
| 396 | error = EEXIST; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | if (error) |
| 402 | break; |
| 403 | KASSERT(unit == ndkwedges); |
| 404 | if (scpp == NULL) |
| 405 | dkwedge_array_expand(); |
| 406 | else { |
| 407 | KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]); |
| 408 | *scpp = sc; |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | rw_exit(&dkwedges_lock); |
| 413 | if (error) { |
| 414 | mutex_enter(&pdk->dk_openlock); |
| 415 | pdk->dk_nwedges--; |
| 416 | LIST_REMOVE(sc, sc_plink); |
| 417 | mutex_exit(&pdk->dk_openlock); |
| 418 | |
| 419 | bufq_free(sc->sc_bufq); |
| 420 | free(sc, M_DKWEDGE); |
| 421 | return (error); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Now that we know the unit #, attach a pseudo-device for |
| 426 | * this wedge instance. This will provide us with the |
| 427 | * device_t necessary for glue to other parts of the system. |
| 428 | * |
| 429 | * This should never fail, unless we're almost totally out of |
| 430 | * memory. |
| 431 | */ |
| 432 | if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) { |
| 433 | aprint_error("%s%u: unable to attach pseudo-device\n" , |
| 434 | sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit); |
| 435 | |
| 436 | rw_enter(&dkwedges_lock, RW_WRITER); |
| 437 | dkwedges[sc->sc_cfdata.cf_unit] = NULL; |
| 438 | rw_exit(&dkwedges_lock); |
| 439 | |
| 440 | mutex_enter(&pdk->dk_openlock); |
| 441 | pdk->dk_nwedges--; |
| 442 | LIST_REMOVE(sc, sc_plink); |
| 443 | mutex_exit(&pdk->dk_openlock); |
| 444 | |
| 445 | bufq_free(sc->sc_bufq); |
| 446 | free(sc, M_DKWEDGE); |
| 447 | return (ENOMEM); |
| 448 | } |
| 449 | |
| 450 | /* Return the devname to the caller. */ |
| 451 | strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev), |
| 452 | sizeof(dkw->dkw_devname)); |
| 453 | |
| 454 | /* |
| 455 | * XXX Really ought to make the disk_attach() and the changing |
| 456 | * of state to RUNNING atomic. |
| 457 | */ |
| 458 | |
| 459 | disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL); |
| 460 | dk_set_geometry(sc, pdk); |
| 461 | disk_attach(&sc->sc_dk); |
| 462 | |
| 463 | /* Disk wedge is ready for use! */ |
| 464 | sc->sc_state = DKW_STATE_RUNNING; |
| 465 | |
| 466 | /* Announce our arrival. */ |
| 467 | aprint_normal( |
| 468 | "%s at %s: \"%s\", %" PRIu64" blocks at %" PRId64", type: %s\n" , |
| 469 | device_xname(sc->sc_dev), pdk->dk_name, |
| 470 | sc->sc_wname, /* XXX Unicode */ |
| 471 | sc->sc_size, sc->sc_offset, |
| 472 | sc->sc_ptype[0] == '\0' ? "<unknown>" : sc->sc_ptype); |
| 473 | |
| 474 | return (0); |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * dkwedge_find: |
| 479 | * |
| 480 | * Lookup a disk wedge based on the provided information. |
| 481 | * NOTE: We look up the wedge based on the wedge devname, |
| 482 | * not wname. |
| 483 | * |
| 484 | * Return NULL if the wedge is not found, otherwise return |
| 485 | * the wedge's softc. Assign the wedge's unit number to unitp |
| 486 | * if unitp is not NULL. |
| 487 | */ |
| 488 | static struct dkwedge_softc * |
| 489 | dkwedge_find(struct dkwedge_info *dkw, u_int *unitp) |
| 490 | { |
| 491 | struct dkwedge_softc *sc = NULL; |
| 492 | u_int unit; |
| 493 | |
| 494 | /* Find our softc. */ |
| 495 | dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0'; |
| 496 | rw_enter(&dkwedges_lock, RW_READER); |
| 497 | for (unit = 0; unit < ndkwedges; unit++) { |
| 498 | if ((sc = dkwedges[unit]) != NULL && |
| 499 | strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 && |
| 500 | strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) { |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | rw_exit(&dkwedges_lock); |
| 505 | if (unit == ndkwedges) |
| 506 | return NULL; |
| 507 | |
| 508 | if (unitp != NULL) |
| 509 | *unitp = unit; |
| 510 | |
| 511 | return sc; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * dkwedge_del: [exported function] |
| 516 | * |
| 517 | * Delete a disk wedge based on the provided information. |
| 518 | * NOTE: We look up the wedge based on the wedge devname, |
| 519 | * not wname. |
| 520 | */ |
| 521 | int |
| 522 | dkwedge_del(struct dkwedge_info *dkw) |
| 523 | { |
| 524 | return dkwedge_del1(dkw, 0); |
| 525 | } |
| 526 | |
| 527 | int |
| 528 | dkwedge_del1(struct dkwedge_info *dkw, int flags) |
| 529 | { |
| 530 | struct dkwedge_softc *sc = NULL; |
| 531 | |
| 532 | /* Find our softc. */ |
| 533 | if ((sc = dkwedge_find(dkw, NULL)) == NULL) |
| 534 | return (ESRCH); |
| 535 | |
| 536 | return config_detach(sc->sc_dev, flags); |
| 537 | } |
| 538 | |
| 539 | static int |
| 540 | dkwedge_cleanup_parent(struct dkwedge_softc *sc, int flags) |
| 541 | { |
| 542 | struct disk *dk = &sc->sc_dk; |
| 543 | int rc; |
| 544 | |
| 545 | rc = 0; |
| 546 | mutex_enter(&dk->dk_openlock); |
| 547 | if (dk->dk_openmask == 0) |
| 548 | /* nothing to do */ |
| 549 | mutex_exit(&dk->dk_openlock); |
| 550 | else if ((flags & DETACH_FORCE) == 0) { |
| 551 | rc = EBUSY; |
| 552 | mutex_exit(&dk->dk_openlock); |
| 553 | } else { |
| 554 | mutex_enter(&sc->sc_parent->dk_rawlock); |
| 555 | rc = dklastclose(sc); /* releases locks */ |
| 556 | } |
| 557 | |
| 558 | return rc; |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * dkwedge_detach: |
| 563 | * |
| 564 | * Autoconfiguration detach function for pseudo-device glue. |
| 565 | */ |
| 566 | static int |
| 567 | dkwedge_detach(device_t self, int flags) |
| 568 | { |
| 569 | struct dkwedge_softc *sc = NULL; |
| 570 | u_int unit; |
| 571 | int bmaj, cmaj, rc, s; |
| 572 | |
| 573 | rw_enter(&dkwedges_lock, RW_WRITER); |
| 574 | for (unit = 0; unit < ndkwedges; unit++) { |
| 575 | if ((sc = dkwedges[unit]) != NULL && sc->sc_dev == self) |
| 576 | break; |
| 577 | } |
| 578 | if (unit == ndkwedges) |
| 579 | rc = ENXIO; |
| 580 | else if ((rc = dkwedge_cleanup_parent(sc, flags)) == 0) { |
| 581 | /* Mark the wedge as dying. */ |
| 582 | sc->sc_state = DKW_STATE_DYING; |
| 583 | } |
| 584 | rw_exit(&dkwedges_lock); |
| 585 | |
| 586 | if (rc != 0) |
| 587 | return rc; |
| 588 | |
| 589 | pmf_device_deregister(self); |
| 590 | |
| 591 | /* Locate the wedge major numbers. */ |
| 592 | bmaj = bdevsw_lookup_major(&dk_bdevsw); |
| 593 | cmaj = cdevsw_lookup_major(&dk_cdevsw); |
| 594 | |
| 595 | /* Kill any pending restart. */ |
| 596 | callout_stop(&sc->sc_restart_ch); |
| 597 | |
| 598 | /* |
| 599 | * dkstart() will kill any queued buffers now that the |
| 600 | * state of the wedge is not RUNNING. Once we've done |
| 601 | * that, wait for any other pending I/O to complete. |
| 602 | */ |
| 603 | s = splbio(); |
| 604 | dkstart(sc); |
| 605 | dkwedge_wait_drain(sc); |
| 606 | splx(s); |
| 607 | |
| 608 | /* Nuke the vnodes for any open instances. */ |
| 609 | vdevgone(bmaj, unit, unit, VBLK); |
| 610 | vdevgone(cmaj, unit, unit, VCHR); |
| 611 | |
| 612 | /* Clean up the parent. */ |
| 613 | dkwedge_cleanup_parent(sc, flags | DETACH_FORCE); |
| 614 | |
| 615 | /* Announce our departure. */ |
| 616 | aprint_normal("%s at %s (%s) deleted\n" , device_xname(sc->sc_dev), |
| 617 | sc->sc_parent->dk_name, |
| 618 | sc->sc_wname); /* XXX Unicode */ |
| 619 | |
| 620 | mutex_enter(&sc->sc_parent->dk_openlock); |
| 621 | sc->sc_parent->dk_nwedges--; |
| 622 | LIST_REMOVE(sc, sc_plink); |
| 623 | mutex_exit(&sc->sc_parent->dk_openlock); |
| 624 | |
| 625 | /* Delete our buffer queue. */ |
| 626 | bufq_free(sc->sc_bufq); |
| 627 | |
| 628 | /* Detach from the disk list. */ |
| 629 | disk_detach(&sc->sc_dk); |
| 630 | disk_destroy(&sc->sc_dk); |
| 631 | |
| 632 | /* Poof. */ |
| 633 | rw_enter(&dkwedges_lock, RW_WRITER); |
| 634 | dkwedges[unit] = NULL; |
| 635 | sc->sc_state = DKW_STATE_DEAD; |
| 636 | rw_exit(&dkwedges_lock); |
| 637 | |
| 638 | free(sc, M_DKWEDGE); |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * dkwedge_delall: [exported function] |
| 645 | * |
| 646 | * Delete all of the wedges on the specified disk. Used when |
| 647 | * a disk is being detached. |
| 648 | */ |
| 649 | void |
| 650 | dkwedge_delall(struct disk *pdk) |
| 651 | { |
| 652 | dkwedge_delall1(pdk, false); |
| 653 | } |
| 654 | |
| 655 | static void |
| 656 | dkwedge_delall1(struct disk *pdk, bool idleonly) |
| 657 | { |
| 658 | struct dkwedge_info dkw; |
| 659 | struct dkwedge_softc *sc; |
| 660 | int flags; |
| 661 | |
| 662 | flags = DETACH_QUIET; |
| 663 | if (!idleonly) flags |= DETACH_FORCE; |
| 664 | |
| 665 | for (;;) { |
| 666 | mutex_enter(&pdk->dk_openlock); |
| 667 | LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) { |
| 668 | if (!idleonly || sc->sc_dk.dk_openmask == 0) |
| 669 | break; |
| 670 | } |
| 671 | if (sc == NULL) { |
| 672 | KASSERT(idleonly || pdk->dk_nwedges == 0); |
| 673 | mutex_exit(&pdk->dk_openlock); |
| 674 | return; |
| 675 | } |
| 676 | strcpy(dkw.dkw_parent, pdk->dk_name); |
| 677 | strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev), |
| 678 | sizeof(dkw.dkw_devname)); |
| 679 | mutex_exit(&pdk->dk_openlock); |
| 680 | (void) dkwedge_del1(&dkw, flags); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * dkwedge_list: [exported function] |
| 686 | * |
| 687 | * List all of the wedges on a particular disk. |
| 688 | */ |
| 689 | int |
| 690 | dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l) |
| 691 | { |
| 692 | struct uio uio; |
| 693 | struct iovec iov; |
| 694 | struct dkwedge_softc *sc; |
| 695 | struct dkwedge_info dkw; |
| 696 | int error = 0; |
| 697 | |
| 698 | iov.iov_base = dkwl->dkwl_buf; |
| 699 | iov.iov_len = dkwl->dkwl_bufsize; |
| 700 | |
| 701 | uio.uio_iov = &iov; |
| 702 | uio.uio_iovcnt = 1; |
| 703 | uio.uio_offset = 0; |
| 704 | uio.uio_resid = dkwl->dkwl_bufsize; |
| 705 | uio.uio_rw = UIO_READ; |
| 706 | KASSERT(l == curlwp); |
| 707 | uio.uio_vmspace = l->l_proc->p_vmspace; |
| 708 | |
| 709 | dkwl->dkwl_ncopied = 0; |
| 710 | |
| 711 | mutex_enter(&pdk->dk_openlock); |
| 712 | LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) { |
| 713 | if (uio.uio_resid < sizeof(dkw)) |
| 714 | break; |
| 715 | |
| 716 | if (sc->sc_state != DKW_STATE_RUNNING) |
| 717 | continue; |
| 718 | |
| 719 | strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev), |
| 720 | sizeof(dkw.dkw_devname)); |
| 721 | memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname)); |
| 722 | dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0'; |
| 723 | strcpy(dkw.dkw_parent, sc->sc_parent->dk_name); |
| 724 | dkw.dkw_offset = sc->sc_offset; |
| 725 | dkw.dkw_size = sc->sc_size; |
| 726 | strcpy(dkw.dkw_ptype, sc->sc_ptype); |
| 727 | |
| 728 | error = uiomove(&dkw, sizeof(dkw), &uio); |
| 729 | if (error) |
| 730 | break; |
| 731 | dkwl->dkwl_ncopied++; |
| 732 | } |
| 733 | dkwl->dkwl_nwedges = pdk->dk_nwedges; |
| 734 | mutex_exit(&pdk->dk_openlock); |
| 735 | |
| 736 | return (error); |
| 737 | } |
| 738 | |
| 739 | device_t |
| 740 | dkwedge_find_by_wname(const char *wname) |
| 741 | { |
| 742 | device_t dv = NULL; |
| 743 | struct dkwedge_softc *sc; |
| 744 | int i; |
| 745 | |
| 746 | rw_enter(&dkwedges_lock, RW_WRITER); |
| 747 | for (i = 0; i < ndkwedges; i++) { |
| 748 | if ((sc = dkwedges[i]) == NULL) |
| 749 | continue; |
| 750 | if (strcmp(sc->sc_wname, wname) == 0) { |
| 751 | if (dv != NULL) { |
| 752 | printf( |
| 753 | "WARNING: double match for wedge name %s " |
| 754 | "(%s, %s)\n" , wname, device_xname(dv), |
| 755 | device_xname(sc->sc_dev)); |
| 756 | continue; |
| 757 | } |
| 758 | dv = sc->sc_dev; |
| 759 | } |
| 760 | } |
| 761 | rw_exit(&dkwedges_lock); |
| 762 | return dv; |
| 763 | } |
| 764 | |
| 765 | device_t |
| 766 | dkwedge_find_by_parent(const char *name, size_t *i) |
| 767 | { |
| 768 | rw_enter(&dkwedges_lock, RW_WRITER); |
| 769 | for (; *i < (size_t)ndkwedges; (*i)++) { |
| 770 | struct dkwedge_softc *sc; |
| 771 | if ((sc = dkwedges[*i]) == NULL) |
| 772 | continue; |
| 773 | if (strcmp(sc->sc_parent->dk_name, name) != 0) |
| 774 | continue; |
| 775 | rw_exit(&dkwedges_lock); |
| 776 | return sc->sc_dev; |
| 777 | } |
| 778 | rw_exit(&dkwedges_lock); |
| 779 | return NULL; |
| 780 | } |
| 781 | |
| 782 | void |
| 783 | dkwedge_print_wnames(void) |
| 784 | { |
| 785 | struct dkwedge_softc *sc; |
| 786 | int i; |
| 787 | |
| 788 | rw_enter(&dkwedges_lock, RW_WRITER); |
| 789 | for (i = 0; i < ndkwedges; i++) { |
| 790 | if ((sc = dkwedges[i]) == NULL) |
| 791 | continue; |
| 792 | printf(" wedge:%s" , sc->sc_wname); |
| 793 | } |
| 794 | rw_exit(&dkwedges_lock); |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * We need a dummy object to stuff into the dkwedge discovery method link |
| 799 | * set to ensure that there is always at least one object in the set. |
| 800 | */ |
| 801 | static struct dkwedge_discovery_method dummy_discovery_method; |
| 802 | __link_set_add_bss(dkwedge_methods, dummy_discovery_method); |
| 803 | |
| 804 | /* |
| 805 | * dkwedge_init: |
| 806 | * |
| 807 | * Initialize the disk wedge subsystem. |
| 808 | */ |
| 809 | void |
| 810 | dkwedge_init(void) |
| 811 | { |
| 812 | __link_set_decl(dkwedge_methods, struct dkwedge_discovery_method); |
| 813 | struct dkwedge_discovery_method * const *ddmp; |
| 814 | struct dkwedge_discovery_method *lddm, *ddm; |
| 815 | |
| 816 | rw_init(&dkwedges_lock); |
| 817 | rw_init(&dkwedge_discovery_methods_lock); |
| 818 | |
| 819 | if (config_cfdriver_attach(&dk_cd) != 0) |
| 820 | panic("dkwedge: unable to attach cfdriver" ); |
| 821 | if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0) |
| 822 | panic("dkwedge: unable to attach cfattach" ); |
| 823 | |
| 824 | rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER); |
| 825 | |
| 826 | LIST_INIT(&dkwedge_discovery_methods); |
| 827 | |
| 828 | __link_set_foreach(ddmp, dkwedge_methods) { |
| 829 | ddm = *ddmp; |
| 830 | if (ddm == &dummy_discovery_method) |
| 831 | continue; |
| 832 | if (LIST_EMPTY(&dkwedge_discovery_methods)) { |
| 833 | LIST_INSERT_HEAD(&dkwedge_discovery_methods, |
| 834 | ddm, ddm_list); |
| 835 | continue; |
| 836 | } |
| 837 | LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) { |
| 838 | if (ddm->ddm_priority == lddm->ddm_priority) { |
| 839 | aprint_error("dk-method-%s: method \"%s\" " |
| 840 | "already exists at priority %d\n" , |
| 841 | ddm->ddm_name, lddm->ddm_name, |
| 842 | lddm->ddm_priority); |
| 843 | /* Not inserted. */ |
| 844 | break; |
| 845 | } |
| 846 | if (ddm->ddm_priority < lddm->ddm_priority) { |
| 847 | /* Higher priority; insert before. */ |
| 848 | LIST_INSERT_BEFORE(lddm, ddm, ddm_list); |
| 849 | break; |
| 850 | } |
| 851 | if (LIST_NEXT(lddm, ddm_list) == NULL) { |
| 852 | /* Last one; insert after. */ |
| 853 | KASSERT(lddm->ddm_priority < ddm->ddm_priority); |
| 854 | LIST_INSERT_AFTER(lddm, ddm, ddm_list); |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | rw_exit(&dkwedge_discovery_methods_lock); |
| 861 | } |
| 862 | |
| 863 | #ifdef DKWEDGE_AUTODISCOVER |
| 864 | int dkwedge_autodiscover = 1; |
| 865 | #else |
| 866 | int dkwedge_autodiscover = 0; |
| 867 | #endif |
| 868 | |
| 869 | /* |
| 870 | * dkwedge_discover: [exported function] |
| 871 | * |
| 872 | * Discover the wedges on a newly attached disk. |
| 873 | * Remove all unused wedges on the disk first. |
| 874 | */ |
| 875 | void |
| 876 | dkwedge_discover(struct disk *pdk) |
| 877 | { |
| 878 | struct dkwedge_discovery_method *ddm; |
| 879 | struct vnode *vp; |
| 880 | int error; |
| 881 | dev_t pdev; |
| 882 | |
| 883 | /* |
| 884 | * Require people playing with wedges to enable this explicitly. |
| 885 | */ |
| 886 | if (dkwedge_autodiscover == 0) |
| 887 | return; |
| 888 | |
| 889 | rw_enter(&dkwedge_discovery_methods_lock, RW_READER); |
| 890 | |
| 891 | /* |
| 892 | * Use the character device for scanning, the block device |
| 893 | * is busy if there are already wedges attached. |
| 894 | */ |
| 895 | error = dkwedge_compute_pdev(pdk->dk_name, &pdev, VCHR); |
| 896 | if (error) { |
| 897 | aprint_error("%s: unable to compute pdev, error = %d\n" , |
| 898 | pdk->dk_name, error); |
| 899 | goto out; |
| 900 | } |
| 901 | |
| 902 | error = cdevvp(pdev, &vp); |
| 903 | if (error) { |
| 904 | aprint_error("%s: unable to find vnode for pdev, error = %d\n" , |
| 905 | pdk->dk_name, error); |
| 906 | goto out; |
| 907 | } |
| 908 | |
| 909 | error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 910 | if (error) { |
| 911 | aprint_error("%s: unable to lock vnode for pdev, error = %d\n" , |
| 912 | pdk->dk_name, error); |
| 913 | vrele(vp); |
| 914 | goto out; |
| 915 | } |
| 916 | |
| 917 | error = VOP_OPEN(vp, FREAD | FSILENT, NOCRED); |
| 918 | if (error) { |
| 919 | if (error != ENODEV) |
| 920 | aprint_error("%s: unable to open device, error = %d\n" , |
| 921 | pdk->dk_name, error); |
| 922 | vput(vp); |
| 923 | goto out; |
| 924 | } |
| 925 | VOP_UNLOCK(vp); |
| 926 | |
| 927 | /* |
| 928 | * Remove unused wedges |
| 929 | */ |
| 930 | dkwedge_delall1(pdk, true); |
| 931 | |
| 932 | /* |
| 933 | * For each supported partition map type, look to see if |
| 934 | * this map type exists. If so, parse it and add the |
| 935 | * corresponding wedges. |
| 936 | */ |
| 937 | LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) { |
| 938 | error = (*ddm->ddm_discover)(pdk, vp); |
| 939 | if (error == 0) { |
| 940 | /* Successfully created wedges; we're done. */ |
| 941 | break; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | error = vn_close(vp, FREAD, NOCRED); |
| 946 | if (error) { |
| 947 | aprint_error("%s: unable to close device, error = %d\n" , |
| 948 | pdk->dk_name, error); |
| 949 | /* We'll just assume the vnode has been cleaned up. */ |
| 950 | } |
| 951 | |
| 952 | out: |
| 953 | rw_exit(&dkwedge_discovery_methods_lock); |
| 954 | } |
| 955 | |
| 956 | /* |
| 957 | * dkwedge_read: |
| 958 | * |
| 959 | * Read some data from the specified disk, used for |
| 960 | * partition discovery. |
| 961 | */ |
| 962 | int |
| 963 | dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno, |
| 964 | void *tbuf, size_t len) |
| 965 | { |
| 966 | buf_t *bp; |
| 967 | int error; |
| 968 | bool isopen; |
| 969 | dev_t bdev; |
| 970 | struct vnode *bdvp; |
| 971 | |
| 972 | /* |
| 973 | * The kernel cannot read from a character device vnode |
| 974 | * as physio() only handles user memory. |
| 975 | * |
| 976 | * If the block device has already been opened by a wedge |
| 977 | * use that vnode and temporarily bump the open counter. |
| 978 | * |
| 979 | * Otherwise try to open the block device. |
| 980 | */ |
| 981 | |
| 982 | bdev = devsw_chr2blk(vp->v_rdev); |
| 983 | |
| 984 | mutex_enter(&pdk->dk_rawlock); |
| 985 | if (pdk->dk_rawopens != 0) { |
| 986 | KASSERT(pdk->dk_rawvp != NULL); |
| 987 | isopen = true; |
| 988 | ++pdk->dk_rawopens; |
| 989 | bdvp = pdk->dk_rawvp; |
| 990 | error = 0; |
| 991 | } else { |
| 992 | isopen = false; |
| 993 | error = dk_open_parent(bdev, FREAD, &bdvp); |
| 994 | } |
| 995 | mutex_exit(&pdk->dk_rawlock); |
| 996 | |
| 997 | if (error) |
| 998 | return error; |
| 999 | |
| 1000 | bp = getiobuf(bdvp, true); |
| 1001 | bp->b_flags = B_READ; |
| 1002 | bp->b_cflags = BC_BUSY; |
| 1003 | bp->b_dev = bdev; |
| 1004 | bp->b_data = tbuf; |
| 1005 | bp->b_bufsize = bp->b_bcount = len; |
| 1006 | bp->b_blkno = blkno; |
| 1007 | bp->b_cylinder = 0; |
| 1008 | bp->b_error = 0; |
| 1009 | |
| 1010 | VOP_STRATEGY(bdvp, bp); |
| 1011 | error = biowait(bp); |
| 1012 | putiobuf(bp); |
| 1013 | |
| 1014 | mutex_enter(&pdk->dk_rawlock); |
| 1015 | if (isopen) { |
| 1016 | --pdk->dk_rawopens; |
| 1017 | } else { |
| 1018 | dk_close_parent(bdvp, FREAD); |
| 1019 | } |
| 1020 | mutex_exit(&pdk->dk_rawlock); |
| 1021 | |
| 1022 | return error; |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * dkwedge_lookup: |
| 1027 | * |
| 1028 | * Look up a dkwedge_softc based on the provided dev_t. |
| 1029 | */ |
| 1030 | static struct dkwedge_softc * |
| 1031 | dkwedge_lookup(dev_t dev) |
| 1032 | { |
| 1033 | int unit = minor(dev); |
| 1034 | |
| 1035 | if (unit >= ndkwedges) |
| 1036 | return (NULL); |
| 1037 | |
| 1038 | KASSERT(dkwedges != NULL); |
| 1039 | |
| 1040 | return (dkwedges[unit]); |
| 1041 | } |
| 1042 | |
| 1043 | static int |
| 1044 | dk_open_parent(dev_t dev, int mode, struct vnode **vpp) |
| 1045 | { |
| 1046 | struct vnode *vp; |
| 1047 | int error; |
| 1048 | |
| 1049 | error = bdevvp(dev, &vp); |
| 1050 | if (error) |
| 1051 | return error; |
| 1052 | |
| 1053 | error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 1054 | if (error) { |
| 1055 | vrele(vp); |
| 1056 | return error; |
| 1057 | } |
| 1058 | error = VOP_OPEN(vp, mode, NOCRED); |
| 1059 | if (error) { |
| 1060 | vput(vp); |
| 1061 | return error; |
| 1062 | } |
| 1063 | |
| 1064 | /* VOP_OPEN() doesn't do this for us. */ |
| 1065 | if (mode & FWRITE) { |
| 1066 | mutex_enter(vp->v_interlock); |
| 1067 | vp->v_writecount++; |
| 1068 | mutex_exit(vp->v_interlock); |
| 1069 | } |
| 1070 | |
| 1071 | VOP_UNLOCK(vp); |
| 1072 | |
| 1073 | *vpp = vp; |
| 1074 | |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | static int |
| 1079 | dk_close_parent(struct vnode *vp, int mode) |
| 1080 | { |
| 1081 | int error; |
| 1082 | |
| 1083 | error = vn_close(vp, mode, NOCRED); |
| 1084 | return error; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * dkopen: [devsw entry point] |
| 1089 | * |
| 1090 | * Open a wedge. |
| 1091 | */ |
| 1092 | static int |
| 1093 | dkopen(dev_t dev, int flags, int fmt, struct lwp *l) |
| 1094 | { |
| 1095 | struct dkwedge_softc *sc = dkwedge_lookup(dev); |
| 1096 | struct vnode *vp; |
| 1097 | int error = 0; |
| 1098 | |
| 1099 | if (sc == NULL) |
| 1100 | return (ENODEV); |
| 1101 | if (sc->sc_state != DKW_STATE_RUNNING) |
| 1102 | return (ENXIO); |
| 1103 | |
| 1104 | /* |
| 1105 | * We go through a complicated little dance to only open the parent |
| 1106 | * vnode once per wedge, no matter how many times the wedge is |
| 1107 | * opened. The reason? We see one dkopen() per open call, but |
| 1108 | * only dkclose() on the last close. |
| 1109 | */ |
| 1110 | mutex_enter(&sc->sc_dk.dk_openlock); |
| 1111 | mutex_enter(&sc->sc_parent->dk_rawlock); |
| 1112 | if (sc->sc_dk.dk_openmask == 0) { |
| 1113 | if (sc->sc_parent->dk_rawopens == 0) { |
| 1114 | KASSERT(sc->sc_parent->dk_rawvp == NULL); |
| 1115 | error = dk_open_parent(sc->sc_pdev, FREAD | FWRITE, &vp); |
| 1116 | if (error) |
| 1117 | goto popen_fail; |
| 1118 | sc->sc_parent->dk_rawvp = vp; |
| 1119 | } |
| 1120 | sc->sc_parent->dk_rawopens++; |
| 1121 | } |
| 1122 | if (fmt == S_IFCHR) |
| 1123 | sc->sc_dk.dk_copenmask |= 1; |
| 1124 | else |
| 1125 | sc->sc_dk.dk_bopenmask |= 1; |
| 1126 | sc->sc_dk.dk_openmask = |
| 1127 | sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; |
| 1128 | |
| 1129 | popen_fail: |
| 1130 | mutex_exit(&sc->sc_parent->dk_rawlock); |
| 1131 | mutex_exit(&sc->sc_dk.dk_openlock); |
| 1132 | return (error); |
| 1133 | } |
| 1134 | |
| 1135 | /* |
| 1136 | * Caller must hold sc->sc_dk.dk_openlock and sc->sc_parent->dk_rawlock. |
| 1137 | */ |
| 1138 | static int |
| 1139 | dklastclose(struct dkwedge_softc *sc) |
| 1140 | { |
| 1141 | int error = 0, doclose; |
| 1142 | |
| 1143 | doclose = 0; |
| 1144 | if (sc->sc_parent->dk_rawopens > 0) { |
| 1145 | if (--sc->sc_parent->dk_rawopens == 0) |
| 1146 | doclose = 1; |
| 1147 | } |
| 1148 | |
| 1149 | mutex_exit(&sc->sc_parent->dk_rawlock); |
| 1150 | mutex_exit(&sc->sc_dk.dk_openlock); |
| 1151 | |
| 1152 | if (doclose) { |
| 1153 | KASSERT(sc->sc_parent->dk_rawvp != NULL); |
| 1154 | dk_close_parent(sc->sc_parent->dk_rawvp, FREAD | FWRITE); |
| 1155 | sc->sc_parent->dk_rawvp = NULL; |
| 1156 | } |
| 1157 | |
| 1158 | return error; |
| 1159 | } |
| 1160 | |
| 1161 | /* |
| 1162 | * dkclose: [devsw entry point] |
| 1163 | * |
| 1164 | * Close a wedge. |
| 1165 | */ |
| 1166 | static int |
| 1167 | dkclose(dev_t dev, int flags, int fmt, struct lwp *l) |
| 1168 | { |
| 1169 | struct dkwedge_softc *sc = dkwedge_lookup(dev); |
| 1170 | int error = 0; |
| 1171 | |
| 1172 | if (sc == NULL) |
| 1173 | return (ENODEV); |
| 1174 | if (sc->sc_state != DKW_STATE_RUNNING) |
| 1175 | return (ENXIO); |
| 1176 | |
| 1177 | KASSERT(sc->sc_dk.dk_openmask != 0); |
| 1178 | |
| 1179 | mutex_enter(&sc->sc_dk.dk_openlock); |
| 1180 | mutex_enter(&sc->sc_parent->dk_rawlock); |
| 1181 | |
| 1182 | if (fmt == S_IFCHR) |
| 1183 | sc->sc_dk.dk_copenmask &= ~1; |
| 1184 | else |
| 1185 | sc->sc_dk.dk_bopenmask &= ~1; |
| 1186 | sc->sc_dk.dk_openmask = |
| 1187 | sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; |
| 1188 | |
| 1189 | if (sc->sc_dk.dk_openmask == 0) |
| 1190 | error = dklastclose(sc); /* releases locks */ |
| 1191 | else { |
| 1192 | mutex_exit(&sc->sc_parent->dk_rawlock); |
| 1193 | mutex_exit(&sc->sc_dk.dk_openlock); |
| 1194 | } |
| 1195 | |
| 1196 | return (error); |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * dkstragegy: [devsw entry point] |
| 1201 | * |
| 1202 | * Perform I/O based on the wedge I/O strategy. |
| 1203 | */ |
| 1204 | static void |
| 1205 | dkstrategy(struct buf *bp) |
| 1206 | { |
| 1207 | struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev); |
| 1208 | uint64_t p_size, p_offset; |
| 1209 | int s; |
| 1210 | |
| 1211 | if (sc == NULL) { |
| 1212 | bp->b_error = ENODEV; |
| 1213 | goto done; |
| 1214 | } |
| 1215 | |
| 1216 | if (sc->sc_state != DKW_STATE_RUNNING || |
| 1217 | sc->sc_parent->dk_rawvp == NULL) { |
| 1218 | bp->b_error = ENXIO; |
| 1219 | goto done; |
| 1220 | } |
| 1221 | |
| 1222 | /* If it's an empty transfer, wake up the top half now. */ |
| 1223 | if (bp->b_bcount == 0) |
| 1224 | goto done; |
| 1225 | |
| 1226 | p_offset = sc->sc_offset << sc->sc_parent->dk_blkshift; |
| 1227 | p_size = sc->sc_size << sc->sc_parent->dk_blkshift; |
| 1228 | |
| 1229 | /* Make sure it's in-range. */ |
| 1230 | if (bounds_check_with_mediasize(bp, DEV_BSIZE, p_size) <= 0) |
| 1231 | goto done; |
| 1232 | |
| 1233 | /* Translate it to the parent's raw LBA. */ |
| 1234 | bp->b_rawblkno = bp->b_blkno + p_offset; |
| 1235 | |
| 1236 | /* Place it in the queue and start I/O on the unit. */ |
| 1237 | s = splbio(); |
| 1238 | sc->sc_iopend++; |
| 1239 | bufq_put(sc->sc_bufq, bp); |
| 1240 | dkstart(sc); |
| 1241 | splx(s); |
| 1242 | return; |
| 1243 | |
| 1244 | done: |
| 1245 | bp->b_resid = bp->b_bcount; |
| 1246 | biodone(bp); |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * dkstart: |
| 1251 | * |
| 1252 | * Start I/O that has been enqueued on the wedge. |
| 1253 | * NOTE: Must be called at splbio()! |
| 1254 | */ |
| 1255 | static void |
| 1256 | dkstart(struct dkwedge_softc *sc) |
| 1257 | { |
| 1258 | struct vnode *vp; |
| 1259 | struct buf *bp, *nbp; |
| 1260 | |
| 1261 | /* Do as much work as has been enqueued. */ |
| 1262 | while ((bp = bufq_peek(sc->sc_bufq)) != NULL) { |
| 1263 | if (sc->sc_state != DKW_STATE_RUNNING) { |
| 1264 | (void) bufq_get(sc->sc_bufq); |
| 1265 | if (sc->sc_iopend-- == 1 && |
| 1266 | (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) { |
| 1267 | sc->sc_flags &= ~DK_F_WAIT_DRAIN; |
| 1268 | wakeup(&sc->sc_iopend); |
| 1269 | } |
| 1270 | bp->b_error = ENXIO; |
| 1271 | bp->b_resid = bp->b_bcount; |
| 1272 | biodone(bp); |
| 1273 | } |
| 1274 | |
| 1275 | /* Instrumentation. */ |
| 1276 | disk_busy(&sc->sc_dk); |
| 1277 | |
| 1278 | nbp = getiobuf(sc->sc_parent->dk_rawvp, false); |
| 1279 | if (nbp == NULL) { |
| 1280 | /* |
| 1281 | * No resources to run this request; leave the |
| 1282 | * buffer queued up, and schedule a timer to |
| 1283 | * restart the queue in 1/2 a second. |
| 1284 | */ |
| 1285 | disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ); |
| 1286 | callout_schedule(&sc->sc_restart_ch, hz / 2); |
| 1287 | return; |
| 1288 | } |
| 1289 | |
| 1290 | (void) bufq_get(sc->sc_bufq); |
| 1291 | |
| 1292 | nbp->b_data = bp->b_data; |
| 1293 | nbp->b_flags = bp->b_flags; |
| 1294 | nbp->b_oflags = bp->b_oflags; |
| 1295 | nbp->b_cflags = bp->b_cflags; |
| 1296 | nbp->b_iodone = dkiodone; |
| 1297 | nbp->b_proc = bp->b_proc; |
| 1298 | nbp->b_blkno = bp->b_rawblkno; |
| 1299 | nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev; |
| 1300 | nbp->b_bcount = bp->b_bcount; |
| 1301 | nbp->b_private = bp; |
| 1302 | BIO_COPYPRIO(nbp, bp); |
| 1303 | |
| 1304 | vp = nbp->b_vp; |
| 1305 | if ((nbp->b_flags & B_READ) == 0) { |
| 1306 | mutex_enter(vp->v_interlock); |
| 1307 | vp->v_numoutput++; |
| 1308 | mutex_exit(vp->v_interlock); |
| 1309 | } |
| 1310 | VOP_STRATEGY(vp, nbp); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | /* |
| 1315 | * dkiodone: |
| 1316 | * |
| 1317 | * I/O to a wedge has completed; alert the top half. |
| 1318 | */ |
| 1319 | static void |
| 1320 | dkiodone(struct buf *bp) |
| 1321 | { |
| 1322 | struct buf *obp = bp->b_private; |
| 1323 | struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev); |
| 1324 | |
| 1325 | int s = splbio(); |
| 1326 | |
| 1327 | if (bp->b_error != 0) |
| 1328 | obp->b_error = bp->b_error; |
| 1329 | obp->b_resid = bp->b_resid; |
| 1330 | putiobuf(bp); |
| 1331 | |
| 1332 | if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) { |
| 1333 | sc->sc_flags &= ~DK_F_WAIT_DRAIN; |
| 1334 | wakeup(&sc->sc_iopend); |
| 1335 | } |
| 1336 | |
| 1337 | disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid, |
| 1338 | obp->b_flags & B_READ); |
| 1339 | |
| 1340 | biodone(obp); |
| 1341 | |
| 1342 | /* Kick the queue in case there is more work we can do. */ |
| 1343 | dkstart(sc); |
| 1344 | splx(s); |
| 1345 | } |
| 1346 | |
| 1347 | /* |
| 1348 | * dkrestart: |
| 1349 | * |
| 1350 | * Restart the work queue after it was stalled due to |
| 1351 | * a resource shortage. Invoked via a callout. |
| 1352 | */ |
| 1353 | static void |
| 1354 | dkrestart(void *v) |
| 1355 | { |
| 1356 | struct dkwedge_softc *sc = v; |
| 1357 | int s; |
| 1358 | |
| 1359 | s = splbio(); |
| 1360 | dkstart(sc); |
| 1361 | splx(s); |
| 1362 | } |
| 1363 | |
| 1364 | /* |
| 1365 | * dkminphys: |
| 1366 | * |
| 1367 | * Call parent's minphys function. |
| 1368 | */ |
| 1369 | static void |
| 1370 | dkminphys(struct buf *bp) |
| 1371 | { |
| 1372 | struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev); |
| 1373 | dev_t dev; |
| 1374 | |
| 1375 | dev = bp->b_dev; |
| 1376 | bp->b_dev = sc->sc_pdev; |
| 1377 | (*sc->sc_parent->dk_driver->d_minphys)(bp); |
| 1378 | bp->b_dev = dev; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * dkread: [devsw entry point] |
| 1383 | * |
| 1384 | * Read from a wedge. |
| 1385 | */ |
| 1386 | static int |
| 1387 | dkread(dev_t dev, struct uio *uio, int flags) |
| 1388 | { |
| 1389 | struct dkwedge_softc *sc = dkwedge_lookup(dev); |
| 1390 | |
| 1391 | if (sc == NULL) |
| 1392 | return (ENODEV); |
| 1393 | if (sc->sc_state != DKW_STATE_RUNNING) |
| 1394 | return (ENXIO); |
| 1395 | |
| 1396 | return (physio(dkstrategy, NULL, dev, B_READ, dkminphys, uio)); |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * dkwrite: [devsw entry point] |
| 1401 | * |
| 1402 | * Write to a wedge. |
| 1403 | */ |
| 1404 | static int |
| 1405 | dkwrite(dev_t dev, struct uio *uio, int flags) |
| 1406 | { |
| 1407 | struct dkwedge_softc *sc = dkwedge_lookup(dev); |
| 1408 | |
| 1409 | if (sc == NULL) |
| 1410 | return (ENODEV); |
| 1411 | if (sc->sc_state != DKW_STATE_RUNNING) |
| 1412 | return (ENXIO); |
| 1413 | |
| 1414 | return (physio(dkstrategy, NULL, dev, B_WRITE, dkminphys, uio)); |
| 1415 | } |
| 1416 | |
| 1417 | /* |
| 1418 | * dkioctl: [devsw entry point] |
| 1419 | * |
| 1420 | * Perform an ioctl request on a wedge. |
| 1421 | */ |
| 1422 | static int |
| 1423 | dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) |
| 1424 | { |
| 1425 | struct dkwedge_softc *sc = dkwedge_lookup(dev); |
| 1426 | int error = 0; |
| 1427 | |
| 1428 | if (sc == NULL) |
| 1429 | return (ENODEV); |
| 1430 | if (sc->sc_state != DKW_STATE_RUNNING) |
| 1431 | return (ENXIO); |
| 1432 | if (sc->sc_parent->dk_rawvp == NULL) |
| 1433 | return (ENXIO); |
| 1434 | |
| 1435 | /* |
| 1436 | * We pass NODEV instead of our device to indicate we don't |
| 1437 | * want to handle disklabel ioctls |
| 1438 | */ |
| 1439 | error = disk_ioctl(&sc->sc_dk, NODEV, cmd, data, flag, l); |
| 1440 | if (error != EPASSTHROUGH) |
| 1441 | return (error); |
| 1442 | |
| 1443 | error = 0; |
| 1444 | |
| 1445 | switch (cmd) { |
| 1446 | case DIOCCACHESYNC: |
| 1447 | /* |
| 1448 | * XXX Do we really need to care about having a writable |
| 1449 | * file descriptor here? |
| 1450 | */ |
| 1451 | if ((flag & FWRITE) == 0) |
| 1452 | error = EBADF; |
| 1453 | else |
| 1454 | error = VOP_IOCTL(sc->sc_parent->dk_rawvp, |
| 1455 | cmd, data, flag, |
| 1456 | l != NULL ? l->l_cred : NOCRED); |
| 1457 | break; |
| 1458 | case DIOCGWEDGEINFO: |
| 1459 | { |
| 1460 | struct dkwedge_info *dkw = (void *) data; |
| 1461 | |
| 1462 | strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev), |
| 1463 | sizeof(dkw->dkw_devname)); |
| 1464 | memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname)); |
| 1465 | dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0'; |
| 1466 | strcpy(dkw->dkw_parent, sc->sc_parent->dk_name); |
| 1467 | dkw->dkw_offset = sc->sc_offset; |
| 1468 | dkw->dkw_size = sc->sc_size; |
| 1469 | strcpy(dkw->dkw_ptype, sc->sc_ptype); |
| 1470 | |
| 1471 | break; |
| 1472 | } |
| 1473 | |
| 1474 | default: |
| 1475 | error = ENOTTY; |
| 1476 | } |
| 1477 | |
| 1478 | return (error); |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * dkdiscard: [devsw entry point] |
| 1483 | * |
| 1484 | * Perform a discard-range request on a wedge. |
| 1485 | */ |
| 1486 | static int |
| 1487 | dkdiscard(dev_t dev, off_t pos, off_t len) |
| 1488 | { |
| 1489 | struct dkwedge_softc *sc = dkwedge_lookup(dev); |
| 1490 | unsigned shift; |
| 1491 | off_t offset, maxlen; |
| 1492 | |
| 1493 | if (sc == NULL) |
| 1494 | return (ENODEV); |
| 1495 | if (sc->sc_state != DKW_STATE_RUNNING) |
| 1496 | return (ENXIO); |
| 1497 | if (sc->sc_parent->dk_rawvp == NULL) |
| 1498 | return (ENXIO); |
| 1499 | |
| 1500 | shift = (sc->sc_parent->dk_blkshift + DEV_BSHIFT); |
| 1501 | KASSERT(__type_fit(off_t, sc->sc_size)); |
| 1502 | KASSERT(__type_fit(off_t, sc->sc_offset)); |
| 1503 | KASSERT(0 <= sc->sc_offset); |
| 1504 | KASSERT(sc->sc_size <= (__type_max(off_t) >> shift)); |
| 1505 | KASSERT(sc->sc_offset <= ((__type_max(off_t) >> shift) - sc->sc_size)); |
| 1506 | offset = ((off_t)sc->sc_offset << shift); |
| 1507 | maxlen = ((off_t)sc->sc_size << shift); |
| 1508 | |
| 1509 | if (len > maxlen) |
| 1510 | return (EINVAL); |
| 1511 | if (pos > (maxlen - len)) |
| 1512 | return (EINVAL); |
| 1513 | |
| 1514 | pos += offset; |
| 1515 | return VOP_FDISCARD(sc->sc_parent->dk_rawvp, pos, len); |
| 1516 | } |
| 1517 | |
| 1518 | /* |
| 1519 | * dksize: [devsw entry point] |
| 1520 | * |
| 1521 | * Query the size of a wedge for the purpose of performing a dump |
| 1522 | * or for swapping to. |
| 1523 | */ |
| 1524 | static int |
| 1525 | dksize(dev_t dev) |
| 1526 | { |
| 1527 | struct dkwedge_softc *sc = dkwedge_lookup(dev); |
| 1528 | int rv = -1; |
| 1529 | |
| 1530 | if (sc == NULL) |
| 1531 | return (-1); |
| 1532 | if (sc->sc_state != DKW_STATE_RUNNING) |
| 1533 | return (-1); |
| 1534 | |
| 1535 | mutex_enter(&sc->sc_dk.dk_openlock); |
| 1536 | mutex_enter(&sc->sc_parent->dk_rawlock); |
| 1537 | |
| 1538 | /* Our content type is static, no need to open the device. */ |
| 1539 | |
| 1540 | if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) { |
| 1541 | /* Saturate if we are larger than INT_MAX. */ |
| 1542 | if (sc->sc_size > INT_MAX) |
| 1543 | rv = INT_MAX; |
| 1544 | else |
| 1545 | rv = (int) sc->sc_size; |
| 1546 | } |
| 1547 | |
| 1548 | mutex_exit(&sc->sc_parent->dk_rawlock); |
| 1549 | mutex_exit(&sc->sc_dk.dk_openlock); |
| 1550 | |
| 1551 | return (rv); |
| 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * dkdump: [devsw entry point] |
| 1556 | * |
| 1557 | * Perform a crash dump to a wedge. |
| 1558 | */ |
| 1559 | static int |
| 1560 | dkdump(dev_t dev, daddr_t blkno, void *va, size_t size) |
| 1561 | { |
| 1562 | struct dkwedge_softc *sc = dkwedge_lookup(dev); |
| 1563 | const struct bdevsw *bdev; |
| 1564 | int rv = 0; |
| 1565 | |
| 1566 | if (sc == NULL) |
| 1567 | return (ENODEV); |
| 1568 | if (sc->sc_state != DKW_STATE_RUNNING) |
| 1569 | return (ENXIO); |
| 1570 | |
| 1571 | mutex_enter(&sc->sc_dk.dk_openlock); |
| 1572 | mutex_enter(&sc->sc_parent->dk_rawlock); |
| 1573 | |
| 1574 | /* Our content type is static, no need to open the device. */ |
| 1575 | |
| 1576 | if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0 && |
| 1577 | strcmp(sc->sc_ptype, DKW_PTYPE_RAID) != 0) { |
| 1578 | rv = ENXIO; |
| 1579 | goto out; |
| 1580 | } |
| 1581 | if (size % DEV_BSIZE != 0) { |
| 1582 | rv = EINVAL; |
| 1583 | goto out; |
| 1584 | } |
| 1585 | if (blkno + size / DEV_BSIZE > sc->sc_size) { |
| 1586 | printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > " |
| 1587 | "sc->sc_size (%" PRIu64 ")\n" , __func__, blkno, |
| 1588 | size / DEV_BSIZE, sc->sc_size); |
| 1589 | rv = EINVAL; |
| 1590 | goto out; |
| 1591 | } |
| 1592 | |
| 1593 | bdev = bdevsw_lookup(sc->sc_pdev); |
| 1594 | rv = (*bdev->d_dump)(sc->sc_pdev, blkno + sc->sc_offset, va, size); |
| 1595 | |
| 1596 | out: |
| 1597 | mutex_exit(&sc->sc_parent->dk_rawlock); |
| 1598 | mutex_exit(&sc->sc_dk.dk_openlock); |
| 1599 | |
| 1600 | return rv; |
| 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | * config glue |
| 1605 | */ |
| 1606 | |
| 1607 | /* |
| 1608 | * dkwedge_find_partition |
| 1609 | * |
| 1610 | * Find wedge corresponding to the specified parent name |
| 1611 | * and offset/length. |
| 1612 | */ |
| 1613 | device_t |
| 1614 | dkwedge_find_partition(device_t parent, daddr_t startblk, uint64_t nblks) |
| 1615 | { |
| 1616 | struct dkwedge_softc *sc; |
| 1617 | int i; |
| 1618 | device_t wedge = NULL; |
| 1619 | |
| 1620 | rw_enter(&dkwedges_lock, RW_READER); |
| 1621 | for (i = 0; i < ndkwedges; i++) { |
| 1622 | if ((sc = dkwedges[i]) == NULL) |
| 1623 | continue; |
| 1624 | if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 && |
| 1625 | sc->sc_offset == startblk && |
| 1626 | sc->sc_size == nblks) { |
| 1627 | if (wedge) { |
| 1628 | printf("WARNING: double match for boot wedge " |
| 1629 | "(%s, %s)\n" , |
| 1630 | device_xname(wedge), |
| 1631 | device_xname(sc->sc_dev)); |
| 1632 | continue; |
| 1633 | } |
| 1634 | wedge = sc->sc_dev; |
| 1635 | } |
| 1636 | } |
| 1637 | rw_exit(&dkwedges_lock); |
| 1638 | |
| 1639 | return wedge; |
| 1640 | } |
| 1641 | |
| 1642 | const char * |
| 1643 | dkwedge_get_parent_name(dev_t dev) |
| 1644 | { |
| 1645 | /* XXX: perhaps do this in lookup? */ |
| 1646 | int bmaj = bdevsw_lookup_major(&dk_bdevsw); |
| 1647 | int cmaj = cdevsw_lookup_major(&dk_cdevsw); |
| 1648 | if (major(dev) != bmaj && major(dev) != cmaj) |
| 1649 | return NULL; |
| 1650 | struct dkwedge_softc *sc = dkwedge_lookup(dev); |
| 1651 | if (sc == NULL) |
| 1652 | return NULL; |
| 1653 | return sc->sc_parent->dk_name; |
| 1654 | } |
| 1655 | |
| 1656 | |