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