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