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