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