ld.c revision 1.59 1 1.59 tsutsui /* $NetBSD: ld.c,v 1.59 2008/06/11 12:41:22 tsutsui Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ad * by Andrew Doran and Charles M. Hannum.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad *
19 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ad */
31 1.1 ad
32 1.1 ad /*
33 1.1 ad * Disk driver for use by RAID controllers.
34 1.1 ad */
35 1.12 lukem
36 1.12 lukem #include <sys/cdefs.h>
37 1.59 tsutsui __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.59 2008/06/11 12:41:22 tsutsui Exp $");
38 1.1 ad
39 1.1 ad #include "rnd.h"
40 1.1 ad
41 1.1 ad #include <sys/param.h>
42 1.1 ad #include <sys/systm.h>
43 1.1 ad #include <sys/kernel.h>
44 1.1 ad #include <sys/device.h>
45 1.1 ad #include <sys/queue.h>
46 1.1 ad #include <sys/proc.h>
47 1.1 ad #include <sys/buf.h>
48 1.33 yamt #include <sys/bufq.h>
49 1.1 ad #include <sys/endian.h>
50 1.1 ad #include <sys/disklabel.h>
51 1.1 ad #include <sys/disk.h>
52 1.1 ad #include <sys/dkio.h>
53 1.1 ad #include <sys/stat.h>
54 1.1 ad #include <sys/conf.h>
55 1.1 ad #include <sys/fcntl.h>
56 1.2 ad #include <sys/vnode.h>
57 1.1 ad #include <sys/syslog.h>
58 1.44 ad #include <sys/mutex.h>
59 1.1 ad #if NRND > 0
60 1.1 ad #include <sys/rnd.h>
61 1.1 ad #endif
62 1.1 ad
63 1.1 ad #include <dev/ldvar.h>
64 1.1 ad
65 1.43 riz #include <prop/proplib.h>
66 1.43 riz
67 1.1 ad static void ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
68 1.1 ad static void ldgetdisklabel(struct ld_softc *);
69 1.1 ad static void ldminphys(struct buf *bp);
70 1.55 jmcneill static bool ld_shutdown(device_t, int);
71 1.44 ad static void ldstart(struct ld_softc *, struct buf *);
72 1.43 riz static void ld_set_properties(struct ld_softc *);
73 1.45 riz static void ld_config_interrupts (struct device *);
74 1.1 ad
75 1.1 ad extern struct cfdriver ld_cd;
76 1.1 ad
77 1.29 thorpej static dev_type_open(ldopen);
78 1.29 thorpej static dev_type_close(ldclose);
79 1.29 thorpej static dev_type_read(ldread);
80 1.29 thorpej static dev_type_write(ldwrite);
81 1.29 thorpej static dev_type_ioctl(ldioctl);
82 1.29 thorpej static dev_type_strategy(ldstrategy);
83 1.29 thorpej static dev_type_dump(lddump);
84 1.29 thorpej static dev_type_size(ldsize);
85 1.16 gehenna
86 1.16 gehenna const struct bdevsw ld_bdevsw = {
87 1.16 gehenna ldopen, ldclose, ldstrategy, ldioctl, lddump, ldsize, D_DISK
88 1.16 gehenna };
89 1.16 gehenna
90 1.16 gehenna const struct cdevsw ld_cdevsw = {
91 1.16 gehenna ldopen, ldclose, ldread, ldwrite, ldioctl,
92 1.17 jdolecek nostop, notty, nopoll, nommap, nokqfilter, D_DISK
93 1.16 gehenna };
94 1.16 gehenna
95 1.30 thorpej static struct dkdriver lddkdriver = { ldstrategy, ldminphys };
96 1.1 ad
97 1.1 ad void
98 1.1 ad ldattach(struct ld_softc *sc)
99 1.1 ad {
100 1.37 christos char tbuf[9];
101 1.1 ad
102 1.53 ad mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
103 1.44 ad
104 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0) {
105 1.57 cegger aprint_normal_dev(&sc->sc_dv, "disabled\n");
106 1.7 ad return;
107 1.7 ad }
108 1.7 ad
109 1.1 ad /* Initialise and attach the disk structure. */
110 1.57 cegger disk_init(&sc->sc_dk, device_xname(&sc->sc_dv), &lddkdriver);
111 1.1 ad disk_attach(&sc->sc_dk);
112 1.1 ad
113 1.1 ad if (sc->sc_maxxfer > MAXPHYS)
114 1.1 ad sc->sc_maxxfer = MAXPHYS;
115 1.9 ad
116 1.19 thorpej /* Build synthetic geometry if necessary. */
117 1.19 thorpej if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
118 1.19 thorpej sc->sc_ncylinders == 0) {
119 1.28 dbj uint64_t ncyl;
120 1.28 dbj
121 1.19 thorpej if (sc->sc_secperunit <= 528 * 2048) /* 528MB */
122 1.19 thorpej sc->sc_nheads = 16;
123 1.19 thorpej else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */
124 1.19 thorpej sc->sc_nheads = 32;
125 1.19 thorpej else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */
126 1.19 thorpej sc->sc_nheads = 64;
127 1.19 thorpej else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */
128 1.19 thorpej sc->sc_nheads = 128;
129 1.19 thorpej else
130 1.19 thorpej sc->sc_nheads = 255;
131 1.19 thorpej
132 1.19 thorpej sc->sc_nsectors = 63;
133 1.28 dbj sc->sc_ncylinders = INT_MAX;
134 1.35 perry ncyl = sc->sc_secperunit /
135 1.19 thorpej (sc->sc_nheads * sc->sc_nsectors);
136 1.28 dbj if (ncyl < INT_MAX)
137 1.28 dbj sc->sc_ncylinders = (int)ncyl;
138 1.19 thorpej }
139 1.1 ad
140 1.37 christos format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
141 1.1 ad sc->sc_secsize);
142 1.57 cegger aprint_normal_dev(&sc->sc_dv, "%s, %d cyl, %d head, %d sec, %d bytes/sect x %"PRIu64" sectors\n",
143 1.57 cegger tbuf, sc->sc_ncylinders, sc->sc_nheads,
144 1.1 ad sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
145 1.1 ad
146 1.43 riz ld_set_properties(sc);
147 1.43 riz
148 1.1 ad #if NRND > 0
149 1.1 ad /* Attach the device into the rnd source list. */
150 1.57 cegger rnd_attach_source(&sc->sc_rnd_source, device_xname(&sc->sc_dv),
151 1.1 ad RND_TYPE_DISK, 0);
152 1.1 ad #endif
153 1.1 ad
154 1.55 jmcneill /* Register with PMF */
155 1.55 jmcneill if (!pmf_device_register1(&sc->sc_dv, NULL, NULL, ld_shutdown))
156 1.55 jmcneill aprint_error_dev(&sc->sc_dv,
157 1.55 jmcneill "couldn't establish power handler\n");
158 1.55 jmcneill
159 1.38 yamt bufq_alloc(&sc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
160 1.30 thorpej
161 1.30 thorpej /* Discover wedges on this disk. */
162 1.45 riz config_interrupts(&sc->sc_dv, ld_config_interrupts);
163 1.1 ad }
164 1.1 ad
165 1.3 ad int
166 1.37 christos ldadjqparam(struct ld_softc *sc, int xmax)
167 1.3 ad {
168 1.24 thorpej int s;
169 1.7 ad
170 1.7 ad s = splbio();
171 1.37 christos sc->sc_maxqueuecnt = xmax;
172 1.7 ad splx(s);
173 1.7 ad
174 1.24 thorpej return (0);
175 1.7 ad }
176 1.7 ad
177 1.7 ad int
178 1.7 ad ldbegindetach(struct ld_softc *sc, int flags)
179 1.7 ad {
180 1.24 thorpej int s, rv = 0;
181 1.7 ad
182 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
183 1.7 ad return (0);
184 1.3 ad
185 1.3 ad if ((flags & DETACH_FORCE) == 0 && sc->sc_dk.dk_openmask != 0)
186 1.3 ad return (EBUSY);
187 1.3 ad
188 1.3 ad s = splbio();
189 1.24 thorpej sc->sc_maxqueuecnt = 0;
190 1.7 ad sc->sc_flags |= LDF_DETACH;
191 1.24 thorpej while (sc->sc_queuecnt > 0) {
192 1.24 thorpej sc->sc_flags |= LDF_DRAIN;
193 1.24 thorpej rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 0);
194 1.24 thorpej if (rv)
195 1.24 thorpej break;
196 1.24 thorpej }
197 1.3 ad splx(s);
198 1.7 ad
199 1.7 ad return (rv);
200 1.3 ad }
201 1.3 ad
202 1.2 ad void
203 1.7 ad ldenddetach(struct ld_softc *sc)
204 1.2 ad {
205 1.13 drochner int s, bmaj, cmaj, i, mn;
206 1.2 ad
207 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
208 1.7 ad return;
209 1.7 ad
210 1.2 ad /* Wait for commands queued with the hardware to complete. */
211 1.2 ad if (sc->sc_queuecnt != 0)
212 1.7 ad if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz))
213 1.57 cegger printf("%s: not drained\n", device_xname(&sc->sc_dv));
214 1.2 ad
215 1.2 ad /* Locate the major numbers. */
216 1.16 gehenna bmaj = bdevsw_lookup_major(&ld_bdevsw);
217 1.16 gehenna cmaj = cdevsw_lookup_major(&ld_cdevsw);
218 1.2 ad
219 1.2 ad /* Kill off any queued buffers. */
220 1.2 ad s = splbio();
221 1.38 yamt bufq_drain(sc->sc_bufq);
222 1.36 yamt splx(s);
223 1.36 yamt
224 1.38 yamt bufq_free(sc->sc_bufq);
225 1.2 ad
226 1.2 ad /* Nuke the vnodes for any open instances. */
227 1.13 drochner for (i = 0; i < MAXPARTITIONS; i++) {
228 1.40 thorpej mn = DISKMINOR(device_unit(&sc->sc_dv), i);
229 1.13 drochner vdevgone(bmaj, mn, mn, VBLK);
230 1.13 drochner vdevgone(cmaj, mn, mn, VCHR);
231 1.13 drochner }
232 1.13 drochner
233 1.30 thorpej /* Delete all of our wedges. */
234 1.30 thorpej dkwedge_delall(&sc->sc_dk);
235 1.30 thorpej
236 1.2 ad /* Detach from the disk list. */
237 1.2 ad disk_detach(&sc->sc_dk);
238 1.50 ad disk_destroy(&sc->sc_dk);
239 1.2 ad
240 1.2 ad #if NRND > 0
241 1.2 ad /* Unhook the entropy source. */
242 1.2 ad rnd_detach_source(&sc->sc_rnd_source);
243 1.2 ad #endif
244 1.2 ad
245 1.56 jmcneill /* Deregister with PMF */
246 1.56 jmcneill pmf_device_deregister(&sc->sc_dv);
247 1.56 jmcneill
248 1.24 thorpej /*
249 1.24 thorpej * XXX We can't really flush the cache here, beceause the
250 1.24 thorpej * XXX device may already be non-existent from the controller's
251 1.24 thorpej * XXX perspective.
252 1.24 thorpej */
253 1.24 thorpej #if 0
254 1.2 ad /* Flush the device's cache. */
255 1.2 ad if (sc->sc_flush != NULL)
256 1.2 ad if ((*sc->sc_flush)(sc) != 0)
257 1.57 cegger aprint_error_dev(&sc->sc_dv, "unable to flush cache\n");
258 1.24 thorpej #endif
259 1.2 ad }
260 1.2 ad
261 1.8 lukem /* ARGSUSED */
262 1.55 jmcneill static bool
263 1.55 jmcneill ld_shutdown(device_t dev, int flags)
264 1.1 ad {
265 1.55 jmcneill struct ld_softc *sc = device_private(dev);
266 1.1 ad
267 1.55 jmcneill if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0) {
268 1.55 jmcneill printf("%s: unable to flush cache\n", device_xname(dev));
269 1.55 jmcneill return false;
270 1.1 ad }
271 1.55 jmcneill
272 1.55 jmcneill return true;
273 1.1 ad }
274 1.1 ad
275 1.8 lukem /* ARGSUSED */
276 1.29 thorpej static int
277 1.42 christos ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
278 1.1 ad {
279 1.1 ad struct ld_softc *sc;
280 1.30 thorpej int error, unit, part;
281 1.1 ad
282 1.1 ad unit = DISKUNIT(dev);
283 1.59 tsutsui if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
284 1.1 ad return (ENXIO);
285 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
286 1.1 ad return (ENODEV);
287 1.1 ad part = DISKPART(dev);
288 1.30 thorpej
289 1.48 ad mutex_enter(&sc->sc_dk.dk_openlock);
290 1.1 ad
291 1.22 thorpej if (sc->sc_dk.dk_openmask == 0) {
292 1.22 thorpej /* Load the partition info if not already loaded. */
293 1.22 thorpej if ((sc->sc_flags & LDF_VLABEL) == 0)
294 1.22 thorpej ldgetdisklabel(sc);
295 1.22 thorpej }
296 1.1 ad
297 1.1 ad /* Check that the partition exists. */
298 1.1 ad if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
299 1.1 ad sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
300 1.30 thorpej error = ENXIO;
301 1.30 thorpej goto bad1;
302 1.1 ad }
303 1.1 ad
304 1.1 ad /* Ensure only one open at a time. */
305 1.1 ad switch (fmt) {
306 1.1 ad case S_IFCHR:
307 1.1 ad sc->sc_dk.dk_copenmask |= (1 << part);
308 1.1 ad break;
309 1.1 ad case S_IFBLK:
310 1.1 ad sc->sc_dk.dk_bopenmask |= (1 << part);
311 1.1 ad break;
312 1.1 ad }
313 1.1 ad sc->sc_dk.dk_openmask =
314 1.1 ad sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
315 1.1 ad
316 1.48 ad error = 0;
317 1.30 thorpej bad1:
318 1.48 ad mutex_exit(&sc->sc_dk.dk_openlock);
319 1.30 thorpej return (error);
320 1.1 ad }
321 1.1 ad
322 1.8 lukem /* ARGSUSED */
323 1.29 thorpej static int
324 1.42 christos ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
325 1.1 ad {
326 1.1 ad struct ld_softc *sc;
327 1.48 ad int part, unit;
328 1.1 ad
329 1.1 ad unit = DISKUNIT(dev);
330 1.1 ad part = DISKPART(dev);
331 1.59 tsutsui sc = device_lookup_private(&ld_cd, unit);
332 1.30 thorpej
333 1.48 ad mutex_enter(&sc->sc_dk.dk_openlock);
334 1.1 ad
335 1.1 ad switch (fmt) {
336 1.1 ad case S_IFCHR:
337 1.1 ad sc->sc_dk.dk_copenmask &= ~(1 << part);
338 1.1 ad break;
339 1.1 ad case S_IFBLK:
340 1.1 ad sc->sc_dk.dk_bopenmask &= ~(1 << part);
341 1.1 ad break;
342 1.1 ad }
343 1.1 ad sc->sc_dk.dk_openmask =
344 1.1 ad sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
345 1.1 ad
346 1.22 thorpej if (sc->sc_dk.dk_openmask == 0) {
347 1.22 thorpej if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
348 1.57 cegger aprint_error_dev(&sc->sc_dv, "unable to flush cache\n");
349 1.22 thorpej if ((sc->sc_flags & LDF_KLABEL) == 0)
350 1.22 thorpej sc->sc_flags &= ~LDF_VLABEL;
351 1.22 thorpej }
352 1.1 ad
353 1.48 ad mutex_exit(&sc->sc_dk.dk_openlock);
354 1.1 ad return (0);
355 1.1 ad }
356 1.1 ad
357 1.8 lukem /* ARGSUSED */
358 1.29 thorpej static int
359 1.42 christos ldread(dev_t dev, struct uio *uio, int ioflag)
360 1.1 ad {
361 1.1 ad
362 1.1 ad return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
363 1.1 ad }
364 1.1 ad
365 1.8 lukem /* ARGSUSED */
366 1.29 thorpej static int
367 1.42 christos ldwrite(dev_t dev, struct uio *uio, int ioflag)
368 1.1 ad {
369 1.1 ad
370 1.1 ad return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
371 1.1 ad }
372 1.1 ad
373 1.8 lukem /* ARGSUSED */
374 1.29 thorpej static int
375 1.46 christos ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
376 1.1 ad {
377 1.1 ad struct ld_softc *sc;
378 1.52 xtraeme int part, unit, error;
379 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
380 1.6 itojun struct disklabel newlabel;
381 1.4 fvdl #endif
382 1.6 itojun struct disklabel *lp;
383 1.1 ad
384 1.1 ad unit = DISKUNIT(dev);
385 1.1 ad part = DISKPART(dev);
386 1.59 tsutsui sc = device_lookup_private(&ld_cd, unit);
387 1.1 ad
388 1.43 riz error = disk_ioctl(&sc->sc_dk, cmd, addr, flag, l);
389 1.43 riz if (error != EPASSTHROUGH)
390 1.43 riz return (error);
391 1.43 riz
392 1.47 tron error = 0;
393 1.1 ad switch (cmd) {
394 1.1 ad case DIOCGDINFO:
395 1.1 ad memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
396 1.1 ad return (0);
397 1.1 ad
398 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
399 1.4 fvdl case ODIOCGDINFO:
400 1.4 fvdl newlabel = *(sc->sc_dk.dk_label);
401 1.4 fvdl if (newlabel.d_npartitions > OLDMAXPARTITIONS)
402 1.5 fvdl return ENOTTY;
403 1.4 fvdl memcpy(addr, &newlabel, sizeof(struct olddisklabel));
404 1.4 fvdl return (0);
405 1.4 fvdl #endif
406 1.4 fvdl
407 1.1 ad case DIOCGPART:
408 1.1 ad ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
409 1.1 ad ((struct partinfo *)addr)->part =
410 1.1 ad &sc->sc_dk.dk_label->d_partitions[part];
411 1.1 ad break;
412 1.1 ad
413 1.1 ad case DIOCWDINFO:
414 1.1 ad case DIOCSDINFO:
415 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
416 1.4 fvdl case ODIOCWDINFO:
417 1.4 fvdl case ODIOCSDINFO:
418 1.4 fvdl
419 1.4 fvdl if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
420 1.4 fvdl memset(&newlabel, 0, sizeof newlabel);
421 1.4 fvdl memcpy(&newlabel, addr, sizeof (struct olddisklabel));
422 1.4 fvdl lp = &newlabel;
423 1.4 fvdl } else
424 1.4 fvdl #endif
425 1.4 fvdl lp = (struct disklabel *)addr;
426 1.4 fvdl
427 1.1 ad if ((flag & FWRITE) == 0)
428 1.1 ad return (EBADF);
429 1.1 ad
430 1.48 ad mutex_enter(&sc->sc_dk.dk_openlock);
431 1.1 ad sc->sc_flags |= LDF_LABELLING;
432 1.1 ad
433 1.1 ad error = setdisklabel(sc->sc_dk.dk_label,
434 1.4 fvdl lp, /*sc->sc_dk.dk_openmask : */0,
435 1.1 ad sc->sc_dk.dk_cpulabel);
436 1.4 fvdl if (error == 0 && (cmd == DIOCWDINFO
437 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
438 1.4 fvdl || cmd == ODIOCWDINFO
439 1.4 fvdl #endif
440 1.4 fvdl ))
441 1.1 ad error = writedisklabel(
442 1.35 perry MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
443 1.35 perry ldstrategy, sc->sc_dk.dk_label,
444 1.1 ad sc->sc_dk.dk_cpulabel);
445 1.1 ad
446 1.1 ad sc->sc_flags &= ~LDF_LABELLING;
447 1.48 ad mutex_exit(&sc->sc_dk.dk_openlock);
448 1.1 ad break;
449 1.1 ad
450 1.22 thorpej case DIOCKLABEL:
451 1.22 thorpej if ((flag & FWRITE) == 0)
452 1.22 thorpej return (EBADF);
453 1.22 thorpej if (*(int *)addr)
454 1.22 thorpej sc->sc_flags |= LDF_KLABEL;
455 1.22 thorpej else
456 1.22 thorpej sc->sc_flags &= ~LDF_KLABEL;
457 1.22 thorpej break;
458 1.22 thorpej
459 1.1 ad case DIOCWLABEL:
460 1.1 ad if ((flag & FWRITE) == 0)
461 1.1 ad return (EBADF);
462 1.1 ad if (*(int *)addr)
463 1.1 ad sc->sc_flags |= LDF_WLABEL;
464 1.1 ad else
465 1.1 ad sc->sc_flags &= ~LDF_WLABEL;
466 1.1 ad break;
467 1.1 ad
468 1.1 ad case DIOCGDEFLABEL:
469 1.1 ad ldgetdefaultlabel(sc, (struct disklabel *)addr);
470 1.1 ad break;
471 1.4 fvdl
472 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
473 1.4 fvdl case ODIOCGDEFLABEL:
474 1.4 fvdl ldgetdefaultlabel(sc, &newlabel);
475 1.4 fvdl if (newlabel.d_npartitions > OLDMAXPARTITIONS)
476 1.5 fvdl return ENOTTY;
477 1.4 fvdl memcpy(addr, &newlabel, sizeof (struct olddisklabel));
478 1.4 fvdl break;
479 1.4 fvdl #endif
480 1.1 ad
481 1.32 thorpej case DIOCCACHESYNC:
482 1.32 thorpej /*
483 1.32 thorpej * XXX Do we really need to care about having a writable
484 1.32 thorpej * file descriptor here?
485 1.32 thorpej */
486 1.32 thorpej if ((flag & FWRITE) == 0)
487 1.32 thorpej error = EBADF;
488 1.32 thorpej else if (sc->sc_flush)
489 1.32 thorpej error = (*sc->sc_flush)(sc);
490 1.32 thorpej else
491 1.32 thorpej error = 0; /* XXX Error out instead? */
492 1.32 thorpej break;
493 1.32 thorpej
494 1.30 thorpej case DIOCAWEDGE:
495 1.30 thorpej {
496 1.30 thorpej struct dkwedge_info *dkw = (void *) addr;
497 1.30 thorpej
498 1.30 thorpej if ((flag & FWRITE) == 0)
499 1.30 thorpej return (EBADF);
500 1.30 thorpej
501 1.30 thorpej /* If the ioctl happens here, the parent is us. */
502 1.57 cegger strlcpy(dkw->dkw_parent, device_xname(&sc->sc_dv),
503 1.57 cegger sizeof(dkw->dkw_parent));
504 1.30 thorpej return (dkwedge_add(dkw));
505 1.30 thorpej }
506 1.35 perry
507 1.30 thorpej case DIOCDWEDGE:
508 1.30 thorpej {
509 1.30 thorpej struct dkwedge_info *dkw = (void *) addr;
510 1.30 thorpej
511 1.30 thorpej if ((flag & FWRITE) == 0)
512 1.30 thorpej return (EBADF);
513 1.30 thorpej
514 1.30 thorpej /* If the ioctl happens here, the parent is us. */
515 1.57 cegger strlcpy(dkw->dkw_parent, device_xname(&sc->sc_dv),
516 1.57 cegger sizeof(dkw->dkw_parent));
517 1.30 thorpej return (dkwedge_del(dkw));
518 1.30 thorpej }
519 1.35 perry
520 1.30 thorpej case DIOCLWEDGES:
521 1.30 thorpej {
522 1.30 thorpej struct dkwedge_list *dkwl = (void *) addr;
523 1.30 thorpej
524 1.39 christos return (dkwedge_list(&sc->sc_dk, dkwl, l));
525 1.30 thorpej }
526 1.51 xtraeme case DIOCGSTRATEGY:
527 1.51 xtraeme {
528 1.51 xtraeme struct disk_strategy *dks = (void *)addr;
529 1.51 xtraeme
530 1.52 xtraeme mutex_enter(&sc->sc_mutex);
531 1.51 xtraeme strlcpy(dks->dks_name, bufq_getstrategyname(sc->sc_bufq),
532 1.51 xtraeme sizeof(dks->dks_name));
533 1.52 xtraeme mutex_exit(&sc->sc_mutex);
534 1.51 xtraeme dks->dks_paramlen = 0;
535 1.51 xtraeme
536 1.51 xtraeme return 0;
537 1.51 xtraeme }
538 1.51 xtraeme case DIOCSSTRATEGY:
539 1.51 xtraeme {
540 1.51 xtraeme struct disk_strategy *dks = (void *)addr;
541 1.51 xtraeme struct bufq_state *new, *old;
542 1.30 thorpej
543 1.51 xtraeme if ((flag & FWRITE) == 0)
544 1.51 xtraeme return EPERM;
545 1.51 xtraeme
546 1.51 xtraeme if (dks->dks_param != NULL)
547 1.51 xtraeme return EINVAL;
548 1.51 xtraeme
549 1.51 xtraeme dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
550 1.51 xtraeme error = bufq_alloc(&new, dks->dks_name,
551 1.51 xtraeme BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
552 1.51 xtraeme if (error)
553 1.51 xtraeme return error;
554 1.51 xtraeme
555 1.52 xtraeme mutex_enter(&sc->sc_mutex);
556 1.51 xtraeme old = sc->sc_bufq;
557 1.51 xtraeme bufq_move(new, old);
558 1.51 xtraeme sc->sc_bufq = new;
559 1.52 xtraeme mutex_exit(&sc->sc_mutex);
560 1.51 xtraeme bufq_free(old);
561 1.51 xtraeme
562 1.51 xtraeme return 0;
563 1.51 xtraeme }
564 1.1 ad default:
565 1.1 ad error = ENOTTY;
566 1.1 ad break;
567 1.1 ad }
568 1.1 ad
569 1.1 ad return (error);
570 1.1 ad }
571 1.1 ad
572 1.29 thorpej static void
573 1.1 ad ldstrategy(struct buf *bp)
574 1.1 ad {
575 1.1 ad struct ld_softc *sc;
576 1.23 thorpej struct disklabel *lp;
577 1.23 thorpej daddr_t blkno;
578 1.23 thorpej int s, part;
579 1.1 ad
580 1.59 tsutsui sc = device_lookup_private(&ld_cd, DISKUNIT(bp->b_dev));
581 1.23 thorpej part = DISKPART(bp->b_dev);
582 1.1 ad
583 1.7 ad if ((sc->sc_flags & LDF_DETACH) != 0) {
584 1.2 ad bp->b_error = EIO;
585 1.49 ad goto done;
586 1.2 ad }
587 1.2 ad
588 1.1 ad lp = sc->sc_dk.dk_label;
589 1.1 ad
590 1.1 ad /*
591 1.1 ad * The transfer must be a whole number of blocks and the offset must
592 1.1 ad * not be negative.
593 1.1 ad */
594 1.1 ad if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
595 1.23 thorpej bp->b_error = EINVAL;
596 1.49 ad goto done;
597 1.1 ad }
598 1.1 ad
599 1.23 thorpej /* If it's a null transfer, return immediately. */
600 1.23 thorpej if (bp->b_bcount == 0)
601 1.23 thorpej goto done;
602 1.1 ad
603 1.1 ad /*
604 1.1 ad * Do bounds checking and adjust the transfer. If error, process.
605 1.1 ad * If past the end of partition, just return.
606 1.1 ad */
607 1.1 ad if (part != RAW_PART &&
608 1.20 thorpej bounds_check_with_label(&sc->sc_dk, bp,
609 1.1 ad (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
610 1.23 thorpej goto done;
611 1.1 ad }
612 1.1 ad
613 1.1 ad /*
614 1.23 thorpej * Convert the block number to absolute and put it in terms
615 1.23 thorpej * of the device's logical block size.
616 1.1 ad */
617 1.23 thorpej if (lp->d_secsize == DEV_BSIZE)
618 1.23 thorpej blkno = bp->b_blkno;
619 1.23 thorpej else if (lp->d_secsize > DEV_BSIZE)
620 1.23 thorpej blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
621 1.1 ad else
622 1.23 thorpej blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
623 1.1 ad
624 1.11 thorpej if (part != RAW_PART)
625 1.23 thorpej blkno += lp->d_partitions[part].p_offset;
626 1.23 thorpej
627 1.23 thorpej bp->b_rawblkno = blkno;
628 1.1 ad
629 1.1 ad s = splbio();
630 1.44 ad ldstart(sc, bp);
631 1.1 ad splx(s);
632 1.23 thorpej return;
633 1.23 thorpej
634 1.23 thorpej done:
635 1.23 thorpej bp->b_resid = bp->b_bcount;
636 1.23 thorpej biodone(bp);
637 1.23 thorpej }
638 1.23 thorpej
639 1.23 thorpej static void
640 1.44 ad ldstart(struct ld_softc *sc, struct buf *bp)
641 1.23 thorpej {
642 1.23 thorpej int error;
643 1.1 ad
644 1.44 ad mutex_enter(&sc->sc_mutex);
645 1.44 ad
646 1.44 ad if (bp != NULL)
647 1.44 ad BUFQ_PUT(sc->sc_bufq, bp);
648 1.44 ad
649 1.23 thorpej while (sc->sc_queuecnt < sc->sc_maxqueuecnt) {
650 1.23 thorpej /* See if there is work to do. */
651 1.38 yamt if ((bp = BUFQ_PEEK(sc->sc_bufq)) == NULL)
652 1.23 thorpej break;
653 1.23 thorpej
654 1.23 thorpej disk_busy(&sc->sc_dk);
655 1.23 thorpej sc->sc_queuecnt++;
656 1.23 thorpej
657 1.23 thorpej if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) {
658 1.23 thorpej /*
659 1.23 thorpej * The back-end is running the job; remove it from
660 1.23 thorpej * the queue.
661 1.23 thorpej */
662 1.38 yamt (void) BUFQ_GET(sc->sc_bufq);
663 1.23 thorpej } else {
664 1.23 thorpej disk_unbusy(&sc->sc_dk, 0, (bp->b_flags & B_READ));
665 1.23 thorpej sc->sc_queuecnt--;
666 1.23 thorpej if (error == EAGAIN) {
667 1.23 thorpej /*
668 1.23 thorpej * Temporary resource shortage in the
669 1.23 thorpej * back-end; just defer the job until
670 1.23 thorpej * later.
671 1.23 thorpej *
672 1.23 thorpej * XXX We might consider a watchdog timer
673 1.23 thorpej * XXX to make sure we are kicked into action.
674 1.23 thorpej */
675 1.23 thorpej break;
676 1.23 thorpej } else {
677 1.38 yamt (void) BUFQ_GET(sc->sc_bufq);
678 1.23 thorpej bp->b_error = error;
679 1.23 thorpej bp->b_resid = bp->b_bcount;
680 1.44 ad mutex_exit(&sc->sc_mutex);
681 1.23 thorpej biodone(bp);
682 1.44 ad mutex_enter(&sc->sc_mutex);
683 1.23 thorpej }
684 1.23 thorpej }
685 1.1 ad }
686 1.44 ad
687 1.44 ad mutex_exit(&sc->sc_mutex);
688 1.1 ad }
689 1.1 ad
690 1.1 ad void
691 1.1 ad lddone(struct ld_softc *sc, struct buf *bp)
692 1.1 ad {
693 1.1 ad
694 1.49 ad if (bp->b_error != 0) {
695 1.1 ad diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
696 1.1 ad printf("\n");
697 1.1 ad }
698 1.1 ad
699 1.18 mrg disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
700 1.18 mrg (bp->b_flags & B_READ));
701 1.1 ad #if NRND > 0
702 1.1 ad rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
703 1.1 ad #endif
704 1.1 ad biodone(bp);
705 1.1 ad
706 1.44 ad mutex_enter(&sc->sc_mutex);
707 1.7 ad if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
708 1.24 thorpej if ((sc->sc_flags & LDF_DRAIN) != 0) {
709 1.24 thorpej sc->sc_flags &= ~LDF_DRAIN;
710 1.7 ad wakeup(&sc->sc_queuecnt);
711 1.24 thorpej }
712 1.44 ad mutex_exit(&sc->sc_mutex);
713 1.44 ad ldstart(sc, NULL);
714 1.44 ad } else
715 1.44 ad mutex_exit(&sc->sc_mutex);
716 1.1 ad }
717 1.1 ad
718 1.29 thorpej static int
719 1.1 ad ldsize(dev_t dev)
720 1.1 ad {
721 1.1 ad struct ld_softc *sc;
722 1.1 ad int part, unit, omask, size;
723 1.1 ad
724 1.1 ad unit = DISKUNIT(dev);
725 1.59 tsutsui if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
726 1.1 ad return (ENODEV);
727 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
728 1.1 ad return (ENODEV);
729 1.1 ad part = DISKPART(dev);
730 1.1 ad
731 1.1 ad omask = sc->sc_dk.dk_openmask & (1 << part);
732 1.1 ad
733 1.1 ad if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
734 1.1 ad return (-1);
735 1.1 ad else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
736 1.1 ad size = -1;
737 1.1 ad else
738 1.1 ad size = sc->sc_dk.dk_label->d_partitions[part].p_size *
739 1.1 ad (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
740 1.1 ad if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
741 1.1 ad return (-1);
742 1.1 ad
743 1.1 ad return (size);
744 1.1 ad }
745 1.1 ad
746 1.1 ad /*
747 1.1 ad * Load the label information from the specified device.
748 1.1 ad */
749 1.1 ad static void
750 1.1 ad ldgetdisklabel(struct ld_softc *sc)
751 1.1 ad {
752 1.1 ad const char *errstring;
753 1.1 ad
754 1.1 ad ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
755 1.1 ad
756 1.1 ad /* Call the generic disklabel extraction routine. */
757 1.40 thorpej errstring = readdisklabel(MAKEDISKDEV(0, device_unit(&sc->sc_dv),
758 1.40 thorpej RAW_PART), ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
759 1.1 ad if (errstring != NULL)
760 1.57 cegger printf("%s: %s\n", device_xname(&sc->sc_dv), errstring);
761 1.22 thorpej
762 1.22 thorpej /* In-core label now valid. */
763 1.22 thorpej sc->sc_flags |= LDF_VLABEL;
764 1.1 ad }
765 1.1 ad
766 1.1 ad /*
767 1.1 ad * Construct a ficticious label.
768 1.1 ad */
769 1.1 ad static void
770 1.1 ad ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
771 1.1 ad {
772 1.1 ad
773 1.1 ad memset(lp, 0, sizeof(struct disklabel));
774 1.1 ad
775 1.1 ad lp->d_secsize = sc->sc_secsize;
776 1.1 ad lp->d_ntracks = sc->sc_nheads;
777 1.1 ad lp->d_nsectors = sc->sc_nsectors;
778 1.1 ad lp->d_ncylinders = sc->sc_ncylinders;
779 1.1 ad lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
780 1.1 ad lp->d_type = DTYPE_LD;
781 1.21 itojun strlcpy(lp->d_typename, "unknown", sizeof(lp->d_typename));
782 1.21 itojun strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
783 1.1 ad lp->d_secperunit = sc->sc_secperunit;
784 1.1 ad lp->d_rpm = 7200;
785 1.1 ad lp->d_interleave = 1;
786 1.1 ad lp->d_flags = 0;
787 1.1 ad
788 1.1 ad lp->d_partitions[RAW_PART].p_offset = 0;
789 1.1 ad lp->d_partitions[RAW_PART].p_size =
790 1.1 ad lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
791 1.1 ad lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
792 1.1 ad lp->d_npartitions = RAW_PART + 1;
793 1.1 ad
794 1.1 ad lp->d_magic = DISKMAGIC;
795 1.1 ad lp->d_magic2 = DISKMAGIC;
796 1.1 ad lp->d_checksum = dkcksum(lp);
797 1.1 ad }
798 1.1 ad
799 1.1 ad /*
800 1.1 ad * Take a dump.
801 1.1 ad */
802 1.29 thorpej static int
803 1.46 christos lddump(dev_t dev, daddr_t blkno, void *vav, size_t size)
804 1.1 ad {
805 1.46 christos char *va = vav;
806 1.1 ad struct ld_softc *sc;
807 1.1 ad struct disklabel *lp;
808 1.1 ad int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
809 1.1 ad static int dumping;
810 1.1 ad
811 1.1 ad unit = DISKUNIT(dev);
812 1.59 tsutsui if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
813 1.1 ad return (ENXIO);
814 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
815 1.1 ad return (ENODEV);
816 1.3 ad if (sc->sc_dump == NULL)
817 1.3 ad return (ENXIO);
818 1.3 ad
819 1.3 ad /* Check if recursive dump; if so, punt. */
820 1.3 ad if (dumping)
821 1.3 ad return (EFAULT);
822 1.3 ad dumping = 1;
823 1.1 ad
824 1.1 ad /* Convert to disk sectors. Request must be a multiple of size. */
825 1.3 ad part = DISKPART(dev);
826 1.1 ad lp = sc->sc_dk.dk_label;
827 1.1 ad if ((size % lp->d_secsize) != 0)
828 1.1 ad return (EFAULT);
829 1.1 ad towrt = size / lp->d_secsize;
830 1.1 ad blkno = dbtob(blkno) / lp->d_secsize; /* blkno in DEV_BSIZE units */
831 1.1 ad
832 1.1 ad nsects = lp->d_partitions[part].p_size;
833 1.1 ad sectoff = lp->d_partitions[part].p_offset;
834 1.1 ad
835 1.1 ad /* Check transfer bounds against partition size. */
836 1.1 ad if ((blkno < 0) || ((blkno + towrt) > nsects))
837 1.1 ad return (EINVAL);
838 1.1 ad
839 1.1 ad /* Offset block number to start of partition. */
840 1.1 ad blkno += sectoff;
841 1.1 ad
842 1.1 ad /* Start dumping and return when done. */
843 1.3 ad maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
844 1.1 ad while (towrt > 0) {
845 1.3 ad nblk = min(maxblkcnt, towrt);
846 1.1 ad
847 1.1 ad if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
848 1.1 ad return (rv);
849 1.1 ad
850 1.1 ad towrt -= nblk;
851 1.1 ad blkno += nblk;
852 1.1 ad va += nblk * sc->sc_secsize;
853 1.1 ad }
854 1.1 ad
855 1.1 ad dumping = 0;
856 1.1 ad return (0);
857 1.1 ad }
858 1.1 ad
859 1.1 ad /*
860 1.1 ad * Adjust the size of a transfer.
861 1.1 ad */
862 1.1 ad static void
863 1.1 ad ldminphys(struct buf *bp)
864 1.1 ad {
865 1.1 ad struct ld_softc *sc;
866 1.1 ad
867 1.59 tsutsui sc = device_lookup_private(&ld_cd, DISKUNIT(bp->b_dev));
868 1.1 ad
869 1.1 ad if (bp->b_bcount > sc->sc_maxxfer)
870 1.1 ad bp->b_bcount = sc->sc_maxxfer;
871 1.1 ad minphys(bp);
872 1.1 ad }
873 1.43 riz
874 1.43 riz static void
875 1.43 riz ld_set_properties(struct ld_softc *ld)
876 1.43 riz {
877 1.43 riz prop_dictionary_t disk_info, odisk_info, geom;
878 1.43 riz
879 1.43 riz disk_info = prop_dictionary_create();
880 1.43 riz
881 1.43 riz geom = prop_dictionary_create();
882 1.43 riz
883 1.43 riz prop_dictionary_set_uint64(geom, "sectors-per-unit",
884 1.43 riz ld->sc_secperunit);
885 1.43 riz
886 1.43 riz prop_dictionary_set_uint32(geom, "sector-size",
887 1.43 riz ld->sc_secsize);
888 1.43 riz
889 1.43 riz prop_dictionary_set_uint16(geom, "sectors-per-track",
890 1.43 riz ld->sc_nsectors);
891 1.43 riz
892 1.43 riz prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
893 1.43 riz ld->sc_nheads);
894 1.43 riz
895 1.43 riz prop_dictionary_set_uint64(geom, "cylinders-per-unit",
896 1.43 riz ld->sc_ncylinders);
897 1.43 riz
898 1.43 riz prop_dictionary_set(disk_info, "geometry", geom);
899 1.43 riz prop_object_release(geom);
900 1.43 riz
901 1.43 riz prop_dictionary_set(device_properties(&ld->sc_dv),
902 1.43 riz "disk-info", disk_info);
903 1.43 riz
904 1.43 riz /*
905 1.43 riz * Don't release disk_info here; we keep a reference to it.
906 1.43 riz * disk_detach() will release it when we go away.
907 1.43 riz */
908 1.43 riz
909 1.43 riz odisk_info = ld->sc_dk.dk_info;
910 1.43 riz ld->sc_dk.dk_info = disk_info;
911 1.43 riz if (odisk_info)
912 1.43 riz prop_object_release(odisk_info);
913 1.43 riz }
914 1.45 riz
915 1.45 riz static void
916 1.45 riz ld_config_interrupts (struct device *d)
917 1.45 riz {
918 1.45 riz struct ld_softc *sc = (struct ld_softc *)d;
919 1.45 riz dkwedge_discover(&sc->sc_dk);
920 1.45 riz }
921