ld.c revision 1.13 1 1.13 drochner /* $NetBSD: ld.c,v 1.13 2002/05/08 15:49:07 drochner 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 ad * must display the following acknowledgement:
20 1.1 ad * This product includes software developed by the NetBSD
21 1.1 ad * Foundation, Inc. and its contributors.
22 1.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 ad * contributors may be used to endorse or promote products derived
24 1.1 ad * from this software without specific prior written permission.
25 1.1 ad *
26 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.1 ad */
38 1.1 ad
39 1.1 ad /*
40 1.1 ad * Disk driver for use by RAID controllers.
41 1.1 ad */
42 1.12 lukem
43 1.12 lukem #include <sys/cdefs.h>
44 1.13 drochner __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.13 2002/05/08 15:49:07 drochner Exp $");
45 1.1 ad
46 1.1 ad #include "rnd.h"
47 1.1 ad
48 1.1 ad #include <sys/param.h>
49 1.1 ad #include <sys/systm.h>
50 1.1 ad #include <sys/kernel.h>
51 1.1 ad #include <sys/device.h>
52 1.1 ad #include <sys/queue.h>
53 1.1 ad #include <sys/proc.h>
54 1.1 ad #include <sys/buf.h>
55 1.1 ad #include <sys/endian.h>
56 1.1 ad #include <sys/disklabel.h>
57 1.1 ad #include <sys/disk.h>
58 1.1 ad #include <sys/dkio.h>
59 1.1 ad #include <sys/stat.h>
60 1.1 ad #include <sys/lock.h>
61 1.1 ad #include <sys/conf.h>
62 1.1 ad #include <sys/fcntl.h>
63 1.2 ad #include <sys/vnode.h>
64 1.1 ad #include <sys/syslog.h>
65 1.1 ad #if NRND > 0
66 1.1 ad #include <sys/rnd.h>
67 1.1 ad #endif
68 1.1 ad
69 1.1 ad #include <dev/ldvar.h>
70 1.1 ad
71 1.1 ad static void ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
72 1.1 ad static void ldgetdisklabel(struct ld_softc *);
73 1.1 ad static int ldlock(struct ld_softc *);
74 1.1 ad static void ldminphys(struct buf *bp);
75 1.1 ad static void ldshutdown(void *);
76 1.1 ad static int ldstart(struct ld_softc *, struct buf *);
77 1.1 ad static void ldunlock(struct ld_softc *);
78 1.1 ad
79 1.1 ad extern struct cfdriver ld_cd;
80 1.1 ad
81 1.1 ad static struct dkdriver lddkdriver = { ldstrategy };
82 1.1 ad static void *ld_sdh;
83 1.1 ad
84 1.1 ad void
85 1.1 ad ldattach(struct ld_softc *sc)
86 1.1 ad {
87 1.1 ad char buf[9];
88 1.1 ad
89 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0) {
90 1.7 ad printf("%s: disabled\n", sc->sc_dv.dv_xname);
91 1.7 ad return;
92 1.7 ad }
93 1.7 ad
94 1.1 ad /* Initialise and attach the disk structure. */
95 1.1 ad sc->sc_dk.dk_driver = &lddkdriver;
96 1.1 ad sc->sc_dk.dk_name = sc->sc_dv.dv_xname;
97 1.1 ad disk_attach(&sc->sc_dk);
98 1.1 ad
99 1.1 ad if (sc->sc_maxxfer > MAXPHYS)
100 1.1 ad sc->sc_maxxfer = MAXPHYS;
101 1.9 ad
102 1.9 ad /* Build synthetic geometry. */
103 1.9 ad if (sc->sc_secperunit <= 528 * 2048) /* 528MB */
104 1.9 ad sc->sc_nheads = 16;
105 1.9 ad else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */
106 1.9 ad sc->sc_nheads = 32;
107 1.9 ad else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */
108 1.9 ad sc->sc_nheads = 64;
109 1.9 ad else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */
110 1.9 ad sc->sc_nheads = 128;
111 1.9 ad else
112 1.9 ad sc->sc_nheads = 255;
113 1.9 ad
114 1.9 ad sc->sc_nsectors = 63;
115 1.9 ad sc->sc_ncylinders = sc->sc_secperunit /
116 1.9 ad (sc->sc_nheads * sc->sc_nsectors);
117 1.1 ad
118 1.1 ad format_bytes(buf, sizeof(buf), (u_int64_t)sc->sc_secperunit *
119 1.1 ad sc->sc_secsize);
120 1.1 ad printf("%s: %s, %d cyl, %d head, %d sec, %d bytes/sect x %d sectors\n",
121 1.1 ad sc->sc_dv.dv_xname, buf, sc->sc_ncylinders, sc->sc_nheads,
122 1.1 ad sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
123 1.1 ad
124 1.1 ad #if NRND > 0
125 1.1 ad /* Attach the device into the rnd source list. */
126 1.1 ad rnd_attach_source(&sc->sc_rnd_source, sc->sc_dv.dv_xname,
127 1.1 ad RND_TYPE_DISK, 0);
128 1.1 ad #endif
129 1.1 ad
130 1.1 ad /* Set the `shutdownhook'. */
131 1.1 ad if (ld_sdh == NULL)
132 1.1 ad ld_sdh = shutdownhook_establish(ldshutdown, NULL);
133 1.1 ad BUFQ_INIT(&sc->sc_bufq);
134 1.1 ad }
135 1.1 ad
136 1.3 ad int
137 1.7 ad ldadjqparam(struct ld_softc *sc, int max)
138 1.3 ad {
139 1.7 ad int s, rv;
140 1.7 ad
141 1.7 ad s = splbio();
142 1.7 ad sc->sc_maxqueuecnt = max;
143 1.7 ad if (sc->sc_queuecnt > max) {
144 1.7 ad sc->sc_flags |= LDF_DRAIN;
145 1.7 ad rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 30 * hz);
146 1.7 ad sc->sc_flags &= ~LDF_DRAIN;
147 1.7 ad } else
148 1.7 ad rv = 0;
149 1.7 ad splx(s);
150 1.7 ad
151 1.7 ad return (rv);
152 1.7 ad }
153 1.7 ad
154 1.7 ad int
155 1.7 ad ldbegindetach(struct ld_softc *sc, int flags)
156 1.7 ad {
157 1.7 ad int s, rv;
158 1.7 ad
159 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
160 1.7 ad return (0);
161 1.3 ad
162 1.3 ad if ((flags & DETACH_FORCE) == 0 && sc->sc_dk.dk_openmask != 0)
163 1.3 ad return (EBUSY);
164 1.3 ad
165 1.3 ad s = splbio();
166 1.7 ad sc->sc_flags |= LDF_DETACH;
167 1.7 ad rv = ldadjqparam(sc, 0);
168 1.3 ad splx(s);
169 1.7 ad
170 1.7 ad return (rv);
171 1.3 ad }
172 1.3 ad
173 1.2 ad void
174 1.7 ad ldenddetach(struct ld_softc *sc)
175 1.2 ad {
176 1.2 ad struct buf *bp;
177 1.13 drochner int s, bmaj, cmaj, i, mn;
178 1.2 ad
179 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
180 1.7 ad return;
181 1.7 ad
182 1.2 ad /* Wait for commands queued with the hardware to complete. */
183 1.2 ad if (sc->sc_queuecnt != 0)
184 1.7 ad if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz))
185 1.7 ad printf("%s: not drained\n", sc->sc_dv.dv_xname);
186 1.2 ad
187 1.2 ad /* Locate the major numbers. */
188 1.2 ad for (bmaj = 0; bmaj <= nblkdev; bmaj++)
189 1.10 ad if (bdevsw[bmaj].d_open == ldopen)
190 1.2 ad break;
191 1.2 ad for (cmaj = 0; cmaj <= nchrdev; cmaj++)
192 1.10 ad if (cdevsw[cmaj].d_open == ldopen)
193 1.2 ad break;
194 1.2 ad
195 1.2 ad /* Kill off any queued buffers. */
196 1.2 ad s = splbio();
197 1.2 ad while ((bp = BUFQ_FIRST(&sc->sc_bufq)) != NULL) {
198 1.2 ad BUFQ_REMOVE(&sc->sc_bufq, bp);
199 1.2 ad bp->b_error = EIO;
200 1.2 ad bp->b_flags |= B_ERROR;
201 1.2 ad bp->b_resid = bp->b_bcount;
202 1.2 ad biodone(bp);
203 1.2 ad }
204 1.2 ad splx(s);
205 1.2 ad
206 1.2 ad /* Nuke the vnodes for any open instances. */
207 1.13 drochner for (i = 0; i < MAXPARTITIONS; i++) {
208 1.13 drochner mn = DISKMINOR(sc->sc_dv.dv_unit, i);
209 1.13 drochner vdevgone(bmaj, mn, mn, VBLK);
210 1.13 drochner vdevgone(cmaj, mn, mn, VCHR);
211 1.13 drochner }
212 1.13 drochner
213 1.2 ad /* Detach from the disk list. */
214 1.2 ad disk_detach(&sc->sc_dk);
215 1.2 ad
216 1.2 ad #if NRND > 0
217 1.2 ad /* Unhook the entropy source. */
218 1.2 ad rnd_detach_source(&sc->sc_rnd_source);
219 1.2 ad #endif
220 1.2 ad
221 1.2 ad /* Flush the device's cache. */
222 1.2 ad if (sc->sc_flush != NULL)
223 1.2 ad if ((*sc->sc_flush)(sc) != 0)
224 1.2 ad printf("%s: unable to flush cache\n",
225 1.2 ad sc->sc_dv.dv_xname);
226 1.2 ad }
227 1.2 ad
228 1.8 lukem /* ARGSUSED */
229 1.1 ad static void
230 1.1 ad ldshutdown(void *cookie)
231 1.1 ad {
232 1.1 ad struct ld_softc *sc;
233 1.1 ad int i;
234 1.1 ad
235 1.1 ad for (i = 0; i < ld_cd.cd_ndevs; i++) {
236 1.1 ad if ((sc = device_lookup(&ld_cd, i)) == NULL)
237 1.1 ad continue;
238 1.1 ad if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
239 1.1 ad printf("%s: unable to flush cache\n",
240 1.1 ad sc->sc_dv.dv_xname);
241 1.1 ad }
242 1.1 ad }
243 1.1 ad
244 1.8 lukem /* ARGSUSED */
245 1.1 ad int
246 1.1 ad ldopen(dev_t dev, int flags, int fmt, struct proc *p)
247 1.1 ad {
248 1.1 ad struct ld_softc *sc;
249 1.1 ad int unit, part;
250 1.1 ad
251 1.1 ad unit = DISKUNIT(dev);
252 1.1 ad if ((sc = device_lookup(&ld_cd, unit))== NULL)
253 1.1 ad return (ENXIO);
254 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
255 1.1 ad return (ENODEV);
256 1.1 ad part = DISKPART(dev);
257 1.1 ad ldlock(sc);
258 1.1 ad
259 1.1 ad if (sc->sc_dk.dk_openmask == 0)
260 1.1 ad ldgetdisklabel(sc);
261 1.1 ad
262 1.1 ad /* Check that the partition exists. */
263 1.1 ad if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
264 1.1 ad sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
265 1.1 ad ldunlock(sc);
266 1.1 ad return (ENXIO);
267 1.1 ad }
268 1.1 ad
269 1.1 ad /* Ensure only one open at a time. */
270 1.1 ad switch (fmt) {
271 1.1 ad case S_IFCHR:
272 1.1 ad sc->sc_dk.dk_copenmask |= (1 << part);
273 1.1 ad break;
274 1.1 ad case S_IFBLK:
275 1.1 ad sc->sc_dk.dk_bopenmask |= (1 << part);
276 1.1 ad break;
277 1.1 ad }
278 1.1 ad sc->sc_dk.dk_openmask =
279 1.1 ad sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
280 1.1 ad
281 1.1 ad ldunlock(sc);
282 1.1 ad return (0);
283 1.1 ad }
284 1.1 ad
285 1.8 lukem /* ARGSUSED */
286 1.1 ad int
287 1.1 ad ldclose(dev_t dev, int flags, int fmt, struct proc *p)
288 1.1 ad {
289 1.1 ad struct ld_softc *sc;
290 1.1 ad int part, unit;
291 1.1 ad
292 1.1 ad unit = DISKUNIT(dev);
293 1.1 ad part = DISKPART(dev);
294 1.1 ad sc = device_lookup(&ld_cd, unit);
295 1.1 ad ldlock(sc);
296 1.1 ad
297 1.1 ad switch (fmt) {
298 1.1 ad case S_IFCHR:
299 1.1 ad sc->sc_dk.dk_copenmask &= ~(1 << part);
300 1.1 ad break;
301 1.1 ad case S_IFBLK:
302 1.1 ad sc->sc_dk.dk_bopenmask &= ~(1 << part);
303 1.1 ad break;
304 1.1 ad }
305 1.1 ad sc->sc_dk.dk_openmask =
306 1.1 ad sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
307 1.1 ad
308 1.1 ad if (sc->sc_dk.dk_openmask == 0 && sc->sc_flush != NULL)
309 1.1 ad if ((*sc->sc_flush)(sc) != 0)
310 1.1 ad printf("%s: unable to flush cache\n",
311 1.1 ad sc->sc_dv.dv_xname);
312 1.1 ad
313 1.1 ad ldunlock(sc);
314 1.1 ad return (0);
315 1.1 ad }
316 1.1 ad
317 1.8 lukem /* ARGSUSED */
318 1.1 ad int
319 1.1 ad ldread(dev_t dev, struct uio *uio, int ioflag)
320 1.1 ad {
321 1.1 ad
322 1.1 ad return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
323 1.1 ad }
324 1.1 ad
325 1.8 lukem /* ARGSUSED */
326 1.1 ad int
327 1.1 ad ldwrite(dev_t dev, struct uio *uio, int ioflag)
328 1.1 ad {
329 1.1 ad
330 1.1 ad return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
331 1.1 ad }
332 1.1 ad
333 1.8 lukem /* ARGSUSED */
334 1.1 ad int
335 1.1 ad ldioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
336 1.1 ad {
337 1.1 ad struct ld_softc *sc;
338 1.1 ad int part, unit, error;
339 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
340 1.6 itojun struct disklabel newlabel;
341 1.4 fvdl #endif
342 1.6 itojun struct disklabel *lp;
343 1.1 ad
344 1.1 ad unit = DISKUNIT(dev);
345 1.1 ad part = DISKPART(dev);
346 1.1 ad sc = device_lookup(&ld_cd, unit);
347 1.1 ad error = 0;
348 1.1 ad
349 1.1 ad switch (cmd) {
350 1.1 ad case DIOCGDINFO:
351 1.1 ad memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
352 1.1 ad return (0);
353 1.1 ad
354 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
355 1.4 fvdl case ODIOCGDINFO:
356 1.4 fvdl newlabel = *(sc->sc_dk.dk_label);
357 1.4 fvdl if (newlabel.d_npartitions > OLDMAXPARTITIONS)
358 1.5 fvdl return ENOTTY;
359 1.4 fvdl memcpy(addr, &newlabel, sizeof(struct olddisklabel));
360 1.4 fvdl return (0);
361 1.4 fvdl #endif
362 1.4 fvdl
363 1.1 ad case DIOCGPART:
364 1.1 ad ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
365 1.1 ad ((struct partinfo *)addr)->part =
366 1.1 ad &sc->sc_dk.dk_label->d_partitions[part];
367 1.1 ad break;
368 1.1 ad
369 1.1 ad case DIOCWDINFO:
370 1.1 ad case DIOCSDINFO:
371 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
372 1.4 fvdl case ODIOCWDINFO:
373 1.4 fvdl case ODIOCSDINFO:
374 1.4 fvdl
375 1.4 fvdl if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
376 1.4 fvdl memset(&newlabel, 0, sizeof newlabel);
377 1.4 fvdl memcpy(&newlabel, addr, sizeof (struct olddisklabel));
378 1.4 fvdl lp = &newlabel;
379 1.4 fvdl } else
380 1.4 fvdl #endif
381 1.4 fvdl lp = (struct disklabel *)addr;
382 1.4 fvdl
383 1.1 ad if ((flag & FWRITE) == 0)
384 1.1 ad return (EBADF);
385 1.1 ad
386 1.1 ad if ((error = ldlock(sc)) != 0)
387 1.1 ad return (error);
388 1.1 ad sc->sc_flags |= LDF_LABELLING;
389 1.1 ad
390 1.1 ad error = setdisklabel(sc->sc_dk.dk_label,
391 1.4 fvdl lp, /*sc->sc_dk.dk_openmask : */0,
392 1.1 ad sc->sc_dk.dk_cpulabel);
393 1.4 fvdl if (error == 0 && (cmd == DIOCWDINFO
394 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
395 1.4 fvdl || cmd == ODIOCWDINFO
396 1.4 fvdl #endif
397 1.4 fvdl ))
398 1.1 ad error = writedisklabel(
399 1.1 ad MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
400 1.1 ad ldstrategy, sc->sc_dk.dk_label,
401 1.1 ad sc->sc_dk.dk_cpulabel);
402 1.1 ad
403 1.1 ad sc->sc_flags &= ~LDF_LABELLING;
404 1.1 ad ldunlock(sc);
405 1.1 ad break;
406 1.1 ad
407 1.1 ad case DIOCWLABEL:
408 1.1 ad if ((flag & FWRITE) == 0)
409 1.1 ad return (EBADF);
410 1.1 ad if (*(int *)addr)
411 1.1 ad sc->sc_flags |= LDF_WLABEL;
412 1.1 ad else
413 1.1 ad sc->sc_flags &= ~LDF_WLABEL;
414 1.1 ad break;
415 1.1 ad
416 1.1 ad case DIOCGDEFLABEL:
417 1.1 ad ldgetdefaultlabel(sc, (struct disklabel *)addr);
418 1.1 ad break;
419 1.4 fvdl
420 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
421 1.4 fvdl case ODIOCGDEFLABEL:
422 1.4 fvdl ldgetdefaultlabel(sc, &newlabel);
423 1.4 fvdl if (newlabel.d_npartitions > OLDMAXPARTITIONS)
424 1.5 fvdl return ENOTTY;
425 1.4 fvdl memcpy(addr, &newlabel, sizeof (struct olddisklabel));
426 1.4 fvdl break;
427 1.4 fvdl #endif
428 1.1 ad
429 1.1 ad default:
430 1.1 ad error = ENOTTY;
431 1.1 ad break;
432 1.1 ad }
433 1.1 ad
434 1.1 ad return (error);
435 1.1 ad }
436 1.1 ad
437 1.1 ad void
438 1.1 ad ldstrategy(struct buf *bp)
439 1.1 ad {
440 1.1 ad struct ld_softc *sc;
441 1.1 ad int s;
442 1.1 ad
443 1.1 ad sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
444 1.1 ad
445 1.1 ad s = splbio();
446 1.7 ad if (sc->sc_queuecnt >= sc->sc_maxqueuecnt) {
447 1.1 ad BUFQ_INSERT_TAIL(&sc->sc_bufq, bp);
448 1.1 ad splx(s);
449 1.1 ad return;
450 1.1 ad }
451 1.1 ad splx(s);
452 1.1 ad ldstart(sc, bp);
453 1.1 ad }
454 1.1 ad
455 1.1 ad static int
456 1.1 ad ldstart(struct ld_softc *sc, struct buf *bp)
457 1.1 ad {
458 1.1 ad struct disklabel *lp;
459 1.1 ad int part, s, rv;
460 1.1 ad
461 1.7 ad if ((sc->sc_flags & LDF_DETACH) != 0) {
462 1.2 ad bp->b_error = EIO;
463 1.2 ad bp->b_flags |= B_ERROR;
464 1.2 ad bp->b_resid = bp->b_bcount;
465 1.2 ad biodone(bp);
466 1.2 ad return (-1);
467 1.2 ad }
468 1.2 ad
469 1.1 ad part = DISKPART(bp->b_dev);
470 1.1 ad lp = sc->sc_dk.dk_label;
471 1.1 ad
472 1.1 ad /*
473 1.1 ad * The transfer must be a whole number of blocks and the offset must
474 1.1 ad * not be negative.
475 1.1 ad */
476 1.1 ad if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
477 1.1 ad bp->b_flags |= B_ERROR;
478 1.1 ad biodone(bp);
479 1.1 ad return (-1);
480 1.1 ad }
481 1.1 ad
482 1.1 ad /*
483 1.1 ad * If it's a null transfer, return.
484 1.1 ad */
485 1.1 ad if (bp->b_bcount == 0) {
486 1.1 ad bp->b_resid = bp->b_bcount;
487 1.1 ad biodone(bp);
488 1.1 ad return (-1);
489 1.1 ad }
490 1.1 ad
491 1.1 ad /*
492 1.1 ad * Do bounds checking and adjust the transfer. If error, process.
493 1.1 ad * If past the end of partition, just return.
494 1.1 ad */
495 1.1 ad if (part != RAW_PART &&
496 1.1 ad bounds_check_with_label(bp, lp,
497 1.1 ad (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
498 1.1 ad bp->b_resid = bp->b_bcount;
499 1.1 ad biodone(bp);
500 1.1 ad return (-1);
501 1.1 ad }
502 1.1 ad
503 1.1 ad /*
504 1.1 ad * Convert the logical block number to a physical one and put it in
505 1.1 ad * terms of the device's logical block size.
506 1.1 ad */
507 1.1 ad if (lp->d_secsize >= DEV_BSIZE)
508 1.1 ad bp->b_rawblkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
509 1.1 ad else
510 1.1 ad bp->b_rawblkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
511 1.1 ad
512 1.11 thorpej if (part != RAW_PART)
513 1.1 ad bp->b_rawblkno += lp->d_partitions[part].p_offset;
514 1.1 ad
515 1.1 ad s = splbio();
516 1.1 ad disk_busy(&sc->sc_dk);
517 1.1 ad sc->sc_queuecnt++;
518 1.1 ad splx(s);
519 1.1 ad
520 1.1 ad if ((rv = (*sc->sc_start)(sc, bp)) != 0) {
521 1.1 ad bp->b_error = rv;
522 1.1 ad bp->b_flags |= B_ERROR;
523 1.1 ad bp->b_resid = bp->b_bcount;
524 1.1 ad s = splbio();
525 1.1 ad lddone(sc, bp);
526 1.1 ad splx(s);
527 1.1 ad }
528 1.1 ad
529 1.1 ad return (0);
530 1.1 ad }
531 1.1 ad
532 1.1 ad void
533 1.1 ad lddone(struct ld_softc *sc, struct buf *bp)
534 1.1 ad {
535 1.1 ad
536 1.1 ad if ((bp->b_flags & B_ERROR) != 0) {
537 1.1 ad diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
538 1.1 ad printf("\n");
539 1.1 ad }
540 1.1 ad
541 1.1 ad disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid);
542 1.1 ad #if NRND > 0
543 1.1 ad rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
544 1.1 ad #endif
545 1.1 ad biodone(bp);
546 1.1 ad
547 1.7 ad if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
548 1.7 ad if ((sc->sc_flags & LDF_DRAIN) != 0)
549 1.7 ad wakeup(&sc->sc_queuecnt);
550 1.7 ad while ((bp = BUFQ_FIRST(&sc->sc_bufq)) != NULL) {
551 1.7 ad BUFQ_REMOVE(&sc->sc_bufq, bp);
552 1.7 ad if (!ldstart(sc, bp))
553 1.7 ad break;
554 1.7 ad }
555 1.1 ad }
556 1.1 ad }
557 1.1 ad
558 1.1 ad int
559 1.1 ad ldsize(dev_t dev)
560 1.1 ad {
561 1.1 ad struct ld_softc *sc;
562 1.1 ad int part, unit, omask, size;
563 1.1 ad
564 1.1 ad unit = DISKUNIT(dev);
565 1.1 ad if ((sc = device_lookup(&ld_cd, unit)) == NULL)
566 1.1 ad return (ENODEV);
567 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
568 1.1 ad return (ENODEV);
569 1.1 ad part = DISKPART(dev);
570 1.1 ad
571 1.1 ad omask = sc->sc_dk.dk_openmask & (1 << part);
572 1.1 ad
573 1.1 ad if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
574 1.1 ad return (-1);
575 1.1 ad else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
576 1.1 ad size = -1;
577 1.1 ad else
578 1.1 ad size = sc->sc_dk.dk_label->d_partitions[part].p_size *
579 1.1 ad (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
580 1.1 ad if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
581 1.1 ad return (-1);
582 1.1 ad
583 1.1 ad return (size);
584 1.1 ad }
585 1.1 ad
586 1.1 ad /*
587 1.1 ad * Load the label information from the specified device.
588 1.1 ad */
589 1.1 ad static void
590 1.1 ad ldgetdisklabel(struct ld_softc *sc)
591 1.1 ad {
592 1.1 ad const char *errstring;
593 1.1 ad
594 1.1 ad ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
595 1.1 ad
596 1.1 ad /* Call the generic disklabel extraction routine. */
597 1.1 ad errstring = readdisklabel(MAKEDISKDEV(0, sc->sc_dv.dv_unit, RAW_PART),
598 1.1 ad ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
599 1.1 ad if (errstring != NULL)
600 1.1 ad printf("%s: %s\n", sc->sc_dv.dv_xname, errstring);
601 1.1 ad }
602 1.1 ad
603 1.1 ad /*
604 1.1 ad * Construct a ficticious label.
605 1.1 ad */
606 1.1 ad static void
607 1.1 ad ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
608 1.1 ad {
609 1.1 ad
610 1.1 ad memset(lp, 0, sizeof(struct disklabel));
611 1.1 ad
612 1.1 ad lp->d_secsize = sc->sc_secsize;
613 1.1 ad lp->d_ntracks = sc->sc_nheads;
614 1.1 ad lp->d_nsectors = sc->sc_nsectors;
615 1.1 ad lp->d_ncylinders = sc->sc_ncylinders;
616 1.1 ad lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
617 1.1 ad lp->d_type = DTYPE_LD;
618 1.1 ad strcpy(lp->d_typename, "unknown");
619 1.1 ad strcpy(lp->d_packname, "fictitious");
620 1.1 ad lp->d_secperunit = sc->sc_secperunit;
621 1.1 ad lp->d_rpm = 7200;
622 1.1 ad lp->d_interleave = 1;
623 1.1 ad lp->d_flags = 0;
624 1.1 ad
625 1.1 ad lp->d_partitions[RAW_PART].p_offset = 0;
626 1.1 ad lp->d_partitions[RAW_PART].p_size =
627 1.1 ad lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
628 1.1 ad lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
629 1.1 ad lp->d_npartitions = RAW_PART + 1;
630 1.1 ad
631 1.1 ad lp->d_magic = DISKMAGIC;
632 1.1 ad lp->d_magic2 = DISKMAGIC;
633 1.1 ad lp->d_checksum = dkcksum(lp);
634 1.1 ad }
635 1.1 ad
636 1.1 ad /*
637 1.1 ad * Wait interruptibly for an exclusive lock.
638 1.1 ad *
639 1.1 ad * XXX Several drivers do this; it should be abstracted and made MP-safe.
640 1.1 ad */
641 1.1 ad static int
642 1.1 ad ldlock(struct ld_softc *sc)
643 1.1 ad {
644 1.1 ad int error;
645 1.1 ad
646 1.1 ad while ((sc->sc_flags & LDF_LKHELD) != 0) {
647 1.1 ad sc->sc_flags |= LDF_LKWANTED;
648 1.1 ad if ((error = tsleep(sc, PRIBIO | PCATCH, "ldlck", 0)) != 0)
649 1.1 ad return (error);
650 1.1 ad }
651 1.1 ad sc->sc_flags |= LDF_LKHELD;
652 1.1 ad return (0);
653 1.1 ad }
654 1.1 ad
655 1.1 ad /*
656 1.1 ad * Unlock and wake up any waiters.
657 1.1 ad */
658 1.1 ad static void
659 1.1 ad ldunlock(struct ld_softc *sc)
660 1.1 ad {
661 1.1 ad
662 1.1 ad sc->sc_flags &= ~LDF_LKHELD;
663 1.1 ad if ((sc->sc_flags & LDF_LKWANTED) != 0) {
664 1.1 ad sc->sc_flags &= ~LDF_LKWANTED;
665 1.1 ad wakeup(sc);
666 1.1 ad }
667 1.1 ad }
668 1.1 ad
669 1.1 ad /*
670 1.1 ad * Take a dump.
671 1.1 ad */
672 1.1 ad int
673 1.1 ad lddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
674 1.1 ad {
675 1.1 ad struct ld_softc *sc;
676 1.1 ad struct disklabel *lp;
677 1.1 ad int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
678 1.1 ad static int dumping;
679 1.1 ad
680 1.1 ad unit = DISKUNIT(dev);
681 1.1 ad if ((sc = device_lookup(&ld_cd, unit)) == NULL)
682 1.1 ad return (ENXIO);
683 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
684 1.1 ad return (ENODEV);
685 1.3 ad if (sc->sc_dump == NULL)
686 1.3 ad return (ENXIO);
687 1.3 ad
688 1.3 ad /* Check if recursive dump; if so, punt. */
689 1.3 ad if (dumping)
690 1.3 ad return (EFAULT);
691 1.3 ad dumping = 1;
692 1.1 ad
693 1.1 ad /* Convert to disk sectors. Request must be a multiple of size. */
694 1.3 ad part = DISKPART(dev);
695 1.1 ad lp = sc->sc_dk.dk_label;
696 1.1 ad if ((size % lp->d_secsize) != 0)
697 1.1 ad return (EFAULT);
698 1.1 ad towrt = size / lp->d_secsize;
699 1.1 ad blkno = dbtob(blkno) / lp->d_secsize; /* blkno in DEV_BSIZE units */
700 1.1 ad
701 1.1 ad nsects = lp->d_partitions[part].p_size;
702 1.1 ad sectoff = lp->d_partitions[part].p_offset;
703 1.1 ad
704 1.1 ad /* Check transfer bounds against partition size. */
705 1.1 ad if ((blkno < 0) || ((blkno + towrt) > nsects))
706 1.1 ad return (EINVAL);
707 1.1 ad
708 1.1 ad /* Offset block number to start of partition. */
709 1.1 ad blkno += sectoff;
710 1.1 ad
711 1.1 ad /* Start dumping and return when done. */
712 1.3 ad maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
713 1.1 ad while (towrt > 0) {
714 1.3 ad nblk = min(maxblkcnt, towrt);
715 1.1 ad
716 1.1 ad if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
717 1.1 ad return (rv);
718 1.1 ad
719 1.1 ad towrt -= nblk;
720 1.1 ad blkno += nblk;
721 1.1 ad va += nblk * sc->sc_secsize;
722 1.1 ad }
723 1.1 ad
724 1.1 ad dumping = 0;
725 1.1 ad return (0);
726 1.1 ad }
727 1.1 ad
728 1.1 ad /*
729 1.1 ad * Adjust the size of a transfer.
730 1.1 ad */
731 1.1 ad static void
732 1.1 ad ldminphys(struct buf *bp)
733 1.1 ad {
734 1.1 ad struct ld_softc *sc;
735 1.1 ad
736 1.1 ad sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
737 1.1 ad
738 1.1 ad if (bp->b_bcount > sc->sc_maxxfer)
739 1.1 ad bp->b_bcount = sc->sc_maxxfer;
740 1.1 ad minphys(bp);
741 1.1 ad }
742