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