ld.c revision 1.40.8.2 1 1.40.8.2 ad /* $NetBSD: ld.c,v 1.40.8.2 2007/01/15 17:58:57 ad 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.40.8.2 ad __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.40.8.2 2007/01/15 17:58:57 ad 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.33 yamt #include <sys/bufq.h>
56 1.1 ad #include <sys/endian.h>
57 1.1 ad #include <sys/disklabel.h>
58 1.1 ad #include <sys/disk.h>
59 1.1 ad #include <sys/dkio.h>
60 1.1 ad #include <sys/stat.h>
61 1.1 ad #include <sys/lock.h>
62 1.1 ad #include <sys/conf.h>
63 1.1 ad #include <sys/fcntl.h>
64 1.2 ad #include <sys/vnode.h>
65 1.1 ad #include <sys/syslog.h>
66 1.40.8.2 ad #include <sys/mutex.h>
67 1.1 ad #if NRND > 0
68 1.1 ad #include <sys/rnd.h>
69 1.1 ad #endif
70 1.1 ad
71 1.1 ad #include <dev/ldvar.h>
72 1.1 ad
73 1.1 ad static void ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
74 1.1 ad static void ldgetdisklabel(struct ld_softc *);
75 1.1 ad static void ldminphys(struct buf *bp);
76 1.1 ad static void ldshutdown(void *);
77 1.40.8.2 ad static void ldstart(struct ld_softc *, struct buf *);
78 1.1 ad
79 1.1 ad extern struct cfdriver ld_cd;
80 1.1 ad
81 1.29 thorpej static dev_type_open(ldopen);
82 1.29 thorpej static dev_type_close(ldclose);
83 1.29 thorpej static dev_type_read(ldread);
84 1.29 thorpej static dev_type_write(ldwrite);
85 1.29 thorpej static dev_type_ioctl(ldioctl);
86 1.29 thorpej static dev_type_strategy(ldstrategy);
87 1.29 thorpej static dev_type_dump(lddump);
88 1.29 thorpej static 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.30 thorpej static struct dkdriver lddkdriver = { ldstrategy, ldminphys };
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.37 christos char tbuf[9];
106 1.1 ad
107 1.40.8.2 ad mutex_init(&sc->sc_mutex, MUTEX_DRIVER, IPL_BIO);
108 1.40.8.2 ad
109 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0) {
110 1.34 briggs aprint_normal("%s: disabled\n", sc->sc_dv.dv_xname);
111 1.7 ad return;
112 1.7 ad }
113 1.7 ad
114 1.1 ad /* Initialise and attach the disk structure. */
115 1.1 ad sc->sc_dk.dk_driver = &lddkdriver;
116 1.1 ad sc->sc_dk.dk_name = sc->sc_dv.dv_xname;
117 1.1 ad disk_attach(&sc->sc_dk);
118 1.1 ad
119 1.1 ad if (sc->sc_maxxfer > MAXPHYS)
120 1.1 ad sc->sc_maxxfer = MAXPHYS;
121 1.9 ad
122 1.19 thorpej /* Build synthetic geometry if necessary. */
123 1.19 thorpej if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
124 1.19 thorpej sc->sc_ncylinders == 0) {
125 1.28 dbj uint64_t ncyl;
126 1.28 dbj
127 1.19 thorpej if (sc->sc_secperunit <= 528 * 2048) /* 528MB */
128 1.19 thorpej sc->sc_nheads = 16;
129 1.19 thorpej else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */
130 1.19 thorpej sc->sc_nheads = 32;
131 1.19 thorpej else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */
132 1.19 thorpej sc->sc_nheads = 64;
133 1.19 thorpej else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */
134 1.19 thorpej sc->sc_nheads = 128;
135 1.19 thorpej else
136 1.19 thorpej sc->sc_nheads = 255;
137 1.19 thorpej
138 1.19 thorpej sc->sc_nsectors = 63;
139 1.28 dbj sc->sc_ncylinders = INT_MAX;
140 1.35 perry ncyl = sc->sc_secperunit /
141 1.19 thorpej (sc->sc_nheads * sc->sc_nsectors);
142 1.28 dbj if (ncyl < INT_MAX)
143 1.28 dbj sc->sc_ncylinders = (int)ncyl;
144 1.19 thorpej }
145 1.1 ad
146 1.37 christos format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
147 1.1 ad sc->sc_secsize);
148 1.34 briggs aprint_normal("%s: %s, %d cyl, %d head, %d sec, %d bytes/sect x %"PRIu64" sectors\n",
149 1.37 christos sc->sc_dv.dv_xname, tbuf, sc->sc_ncylinders, sc->sc_nheads,
150 1.1 ad sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
151 1.1 ad
152 1.1 ad #if NRND > 0
153 1.1 ad /* Attach the device into the rnd source list. */
154 1.1 ad rnd_attach_source(&sc->sc_rnd_source, sc->sc_dv.dv_xname,
155 1.1 ad RND_TYPE_DISK, 0);
156 1.1 ad #endif
157 1.1 ad
158 1.1 ad /* Set the `shutdownhook'. */
159 1.1 ad if (ld_sdh == NULL)
160 1.1 ad ld_sdh = shutdownhook_establish(ldshutdown, NULL);
161 1.38 yamt bufq_alloc(&sc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
162 1.30 thorpej
163 1.30 thorpej /* Discover wedges on this disk. */
164 1.30 thorpej dkwedge_discover(&sc->sc_dk);
165 1.1 ad }
166 1.1 ad
167 1.3 ad int
168 1.37 christos ldadjqparam(struct ld_softc *sc, int xmax)
169 1.3 ad {
170 1.24 thorpej int s;
171 1.7 ad
172 1.7 ad s = splbio();
173 1.37 christos sc->sc_maxqueuecnt = xmax;
174 1.7 ad splx(s);
175 1.7 ad
176 1.24 thorpej return (0);
177 1.7 ad }
178 1.7 ad
179 1.7 ad int
180 1.7 ad ldbegindetach(struct ld_softc *sc, int flags)
181 1.7 ad {
182 1.24 thorpej int s, rv = 0;
183 1.7 ad
184 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
185 1.7 ad return (0);
186 1.3 ad
187 1.3 ad if ((flags & DETACH_FORCE) == 0 && sc->sc_dk.dk_openmask != 0)
188 1.3 ad return (EBUSY);
189 1.3 ad
190 1.3 ad s = splbio();
191 1.24 thorpej sc->sc_maxqueuecnt = 0;
192 1.7 ad sc->sc_flags |= LDF_DETACH;
193 1.24 thorpej while (sc->sc_queuecnt > 0) {
194 1.24 thorpej sc->sc_flags |= LDF_DRAIN;
195 1.24 thorpej rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 0);
196 1.24 thorpej if (rv)
197 1.24 thorpej break;
198 1.24 thorpej }
199 1.3 ad splx(s);
200 1.7 ad
201 1.7 ad return (rv);
202 1.3 ad }
203 1.3 ad
204 1.2 ad void
205 1.7 ad ldenddetach(struct ld_softc *sc)
206 1.2 ad {
207 1.13 drochner int s, bmaj, cmaj, i, mn;
208 1.2 ad
209 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
210 1.7 ad return;
211 1.7 ad
212 1.2 ad /* Wait for commands queued with the hardware to complete. */
213 1.2 ad if (sc->sc_queuecnt != 0)
214 1.7 ad if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz))
215 1.7 ad printf("%s: not drained\n", sc->sc_dv.dv_xname);
216 1.2 ad
217 1.2 ad /* Locate the major numbers. */
218 1.16 gehenna bmaj = bdevsw_lookup_major(&ld_bdevsw);
219 1.16 gehenna cmaj = cdevsw_lookup_major(&ld_cdevsw);
220 1.2 ad
221 1.2 ad /* Kill off any queued buffers. */
222 1.2 ad s = splbio();
223 1.38 yamt bufq_drain(sc->sc_bufq);
224 1.36 yamt splx(s);
225 1.36 yamt
226 1.38 yamt bufq_free(sc->sc_bufq);
227 1.2 ad
228 1.2 ad /* Nuke the vnodes for any open instances. */
229 1.13 drochner for (i = 0; i < MAXPARTITIONS; i++) {
230 1.40 thorpej mn = DISKMINOR(device_unit(&sc->sc_dv), i);
231 1.13 drochner vdevgone(bmaj, mn, mn, VBLK);
232 1.13 drochner vdevgone(cmaj, mn, mn, VCHR);
233 1.13 drochner }
234 1.13 drochner
235 1.30 thorpej /* Delete all of our wedges. */
236 1.30 thorpej dkwedge_delall(&sc->sc_dk);
237 1.30 thorpej
238 1.2 ad /* Detach from the disk list. */
239 1.2 ad disk_detach(&sc->sc_dk);
240 1.2 ad
241 1.2 ad #if NRND > 0
242 1.2 ad /* Unhook the entropy source. */
243 1.2 ad rnd_detach_source(&sc->sc_rnd_source);
244 1.2 ad #endif
245 1.2 ad
246 1.24 thorpej /*
247 1.24 thorpej * XXX We can't really flush the cache here, beceause the
248 1.24 thorpej * XXX device may already be non-existent from the controller's
249 1.24 thorpej * XXX perspective.
250 1.24 thorpej */
251 1.24 thorpej #if 0
252 1.2 ad /* Flush the device's cache. */
253 1.2 ad if (sc->sc_flush != NULL)
254 1.2 ad if ((*sc->sc_flush)(sc) != 0)
255 1.2 ad printf("%s: unable to flush cache\n",
256 1.2 ad sc->sc_dv.dv_xname);
257 1.24 thorpej #endif
258 1.2 ad }
259 1.2 ad
260 1.8 lukem /* ARGSUSED */
261 1.1 ad static void
262 1.1 ad ldshutdown(void *cookie)
263 1.1 ad {
264 1.1 ad struct ld_softc *sc;
265 1.1 ad int i;
266 1.1 ad
267 1.1 ad for (i = 0; i < ld_cd.cd_ndevs; i++) {
268 1.1 ad if ((sc = device_lookup(&ld_cd, i)) == NULL)
269 1.1 ad continue;
270 1.1 ad if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
271 1.1 ad printf("%s: unable to flush cache\n",
272 1.1 ad sc->sc_dv.dv_xname);
273 1.1 ad }
274 1.1 ad }
275 1.1 ad
276 1.8 lukem /* ARGSUSED */
277 1.29 thorpej static int
278 1.39 christos ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
279 1.1 ad {
280 1.1 ad struct ld_softc *sc;
281 1.30 thorpej int error, unit, part;
282 1.1 ad
283 1.1 ad unit = DISKUNIT(dev);
284 1.30 thorpej if ((sc = device_lookup(&ld_cd, unit)) == NULL)
285 1.1 ad return (ENXIO);
286 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
287 1.1 ad return (ENODEV);
288 1.1 ad part = DISKPART(dev);
289 1.30 thorpej
290 1.30 thorpej if ((error = lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
291 1.30 thorpej return (error);
292 1.1 ad
293 1.22 thorpej if (sc->sc_dk.dk_openmask == 0) {
294 1.22 thorpej /* Load the partition info if not already loaded. */
295 1.22 thorpej if ((sc->sc_flags & LDF_VLABEL) == 0)
296 1.22 thorpej ldgetdisklabel(sc);
297 1.22 thorpej }
298 1.1 ad
299 1.1 ad /* Check that the partition exists. */
300 1.1 ad if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
301 1.1 ad sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
302 1.30 thorpej error = ENXIO;
303 1.30 thorpej goto bad1;
304 1.1 ad }
305 1.1 ad
306 1.1 ad /* Ensure only one open at a time. */
307 1.1 ad switch (fmt) {
308 1.1 ad case S_IFCHR:
309 1.1 ad sc->sc_dk.dk_copenmask |= (1 << part);
310 1.1 ad break;
311 1.1 ad case S_IFBLK:
312 1.1 ad sc->sc_dk.dk_bopenmask |= (1 << part);
313 1.1 ad break;
314 1.1 ad }
315 1.1 ad sc->sc_dk.dk_openmask =
316 1.1 ad sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
317 1.1 ad
318 1.30 thorpej (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
319 1.1 ad return (0);
320 1.30 thorpej
321 1.30 thorpej bad1:
322 1.30 thorpej (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
323 1.30 thorpej return (error);
324 1.1 ad }
325 1.1 ad
326 1.8 lukem /* ARGSUSED */
327 1.29 thorpej static int
328 1.39 christos ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
329 1.1 ad {
330 1.1 ad struct ld_softc *sc;
331 1.30 thorpej int error, part, unit;
332 1.1 ad
333 1.1 ad unit = DISKUNIT(dev);
334 1.1 ad part = DISKPART(dev);
335 1.1 ad sc = device_lookup(&ld_cd, unit);
336 1.30 thorpej
337 1.30 thorpej if ((error = lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
338 1.30 thorpej return (error);
339 1.1 ad
340 1.1 ad switch (fmt) {
341 1.1 ad case S_IFCHR:
342 1.1 ad sc->sc_dk.dk_copenmask &= ~(1 << part);
343 1.1 ad break;
344 1.1 ad case S_IFBLK:
345 1.1 ad sc->sc_dk.dk_bopenmask &= ~(1 << part);
346 1.1 ad break;
347 1.1 ad }
348 1.1 ad sc->sc_dk.dk_openmask =
349 1.1 ad sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
350 1.1 ad
351 1.22 thorpej if (sc->sc_dk.dk_openmask == 0) {
352 1.22 thorpej if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
353 1.1 ad printf("%s: unable to flush cache\n",
354 1.1 ad sc->sc_dv.dv_xname);
355 1.22 thorpej if ((sc->sc_flags & LDF_KLABEL) == 0)
356 1.22 thorpej sc->sc_flags &= ~LDF_VLABEL;
357 1.22 thorpej }
358 1.1 ad
359 1.30 thorpej (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
360 1.1 ad return (0);
361 1.1 ad }
362 1.1 ad
363 1.8 lukem /* ARGSUSED */
364 1.29 thorpej static int
365 1.1 ad ldread(dev_t dev, struct uio *uio, int ioflag)
366 1.1 ad {
367 1.1 ad
368 1.1 ad return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
369 1.1 ad }
370 1.1 ad
371 1.8 lukem /* ARGSUSED */
372 1.29 thorpej static int
373 1.1 ad ldwrite(dev_t dev, struct uio *uio, int ioflag)
374 1.1 ad {
375 1.1 ad
376 1.1 ad return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
377 1.1 ad }
378 1.1 ad
379 1.8 lukem /* ARGSUSED */
380 1.29 thorpej static int
381 1.39 christos ldioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct lwp *l)
382 1.1 ad {
383 1.1 ad struct ld_softc *sc;
384 1.1 ad int part, unit, error;
385 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
386 1.6 itojun struct disklabel newlabel;
387 1.4 fvdl #endif
388 1.6 itojun struct disklabel *lp;
389 1.1 ad
390 1.1 ad unit = DISKUNIT(dev);
391 1.1 ad part = DISKPART(dev);
392 1.1 ad sc = device_lookup(&ld_cd, unit);
393 1.1 ad error = 0;
394 1.1 ad
395 1.1 ad switch (cmd) {
396 1.1 ad case DIOCGDINFO:
397 1.1 ad memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
398 1.1 ad return (0);
399 1.1 ad
400 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
401 1.4 fvdl case ODIOCGDINFO:
402 1.4 fvdl newlabel = *(sc->sc_dk.dk_label);
403 1.4 fvdl if (newlabel.d_npartitions > OLDMAXPARTITIONS)
404 1.5 fvdl return ENOTTY;
405 1.4 fvdl memcpy(addr, &newlabel, sizeof(struct olddisklabel));
406 1.4 fvdl return (0);
407 1.4 fvdl #endif
408 1.4 fvdl
409 1.1 ad case DIOCGPART:
410 1.1 ad ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
411 1.1 ad ((struct partinfo *)addr)->part =
412 1.1 ad &sc->sc_dk.dk_label->d_partitions[part];
413 1.1 ad break;
414 1.1 ad
415 1.1 ad case DIOCWDINFO:
416 1.1 ad case DIOCSDINFO:
417 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
418 1.4 fvdl case ODIOCWDINFO:
419 1.4 fvdl case ODIOCSDINFO:
420 1.4 fvdl
421 1.4 fvdl if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
422 1.4 fvdl memset(&newlabel, 0, sizeof newlabel);
423 1.4 fvdl memcpy(&newlabel, addr, sizeof (struct olddisklabel));
424 1.4 fvdl lp = &newlabel;
425 1.4 fvdl } else
426 1.4 fvdl #endif
427 1.4 fvdl lp = (struct disklabel *)addr;
428 1.4 fvdl
429 1.1 ad if ((flag & FWRITE) == 0)
430 1.1 ad return (EBADF);
431 1.1 ad
432 1.30 thorpej if ((error = lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE,
433 1.31 jdolecek NULL)) != 0)
434 1.1 ad return (error);
435 1.1 ad sc->sc_flags |= LDF_LABELLING;
436 1.1 ad
437 1.1 ad error = setdisklabel(sc->sc_dk.dk_label,
438 1.4 fvdl lp, /*sc->sc_dk.dk_openmask : */0,
439 1.1 ad sc->sc_dk.dk_cpulabel);
440 1.4 fvdl if (error == 0 && (cmd == DIOCWDINFO
441 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
442 1.4 fvdl || cmd == ODIOCWDINFO
443 1.4 fvdl #endif
444 1.4 fvdl ))
445 1.1 ad error = writedisklabel(
446 1.35 perry MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
447 1.35 perry ldstrategy, sc->sc_dk.dk_label,
448 1.1 ad sc->sc_dk.dk_cpulabel);
449 1.1 ad
450 1.1 ad sc->sc_flags &= ~LDF_LABELLING;
451 1.30 thorpej (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
452 1.1 ad break;
453 1.1 ad
454 1.22 thorpej case DIOCKLABEL:
455 1.22 thorpej if ((flag & FWRITE) == 0)
456 1.22 thorpej return (EBADF);
457 1.22 thorpej if (*(int *)addr)
458 1.22 thorpej sc->sc_flags |= LDF_KLABEL;
459 1.22 thorpej else
460 1.22 thorpej sc->sc_flags &= ~LDF_KLABEL;
461 1.22 thorpej break;
462 1.22 thorpej
463 1.1 ad case DIOCWLABEL:
464 1.1 ad if ((flag & FWRITE) == 0)
465 1.1 ad return (EBADF);
466 1.1 ad if (*(int *)addr)
467 1.1 ad sc->sc_flags |= LDF_WLABEL;
468 1.1 ad else
469 1.1 ad sc->sc_flags &= ~LDF_WLABEL;
470 1.1 ad break;
471 1.1 ad
472 1.1 ad case DIOCGDEFLABEL:
473 1.1 ad ldgetdefaultlabel(sc, (struct disklabel *)addr);
474 1.1 ad break;
475 1.4 fvdl
476 1.4 fvdl #ifdef __HAVE_OLD_DISKLABEL
477 1.4 fvdl case ODIOCGDEFLABEL:
478 1.4 fvdl ldgetdefaultlabel(sc, &newlabel);
479 1.4 fvdl if (newlabel.d_npartitions > OLDMAXPARTITIONS)
480 1.5 fvdl return ENOTTY;
481 1.4 fvdl memcpy(addr, &newlabel, sizeof (struct olddisklabel));
482 1.4 fvdl break;
483 1.4 fvdl #endif
484 1.1 ad
485 1.32 thorpej case DIOCCACHESYNC:
486 1.32 thorpej /*
487 1.32 thorpej * XXX Do we really need to care about having a writable
488 1.32 thorpej * file descriptor here?
489 1.32 thorpej */
490 1.32 thorpej if ((flag & FWRITE) == 0)
491 1.32 thorpej error = EBADF;
492 1.32 thorpej else if (sc->sc_flush)
493 1.32 thorpej error = (*sc->sc_flush)(sc);
494 1.32 thorpej else
495 1.32 thorpej error = 0; /* XXX Error out instead? */
496 1.32 thorpej break;
497 1.32 thorpej
498 1.30 thorpej case DIOCAWEDGE:
499 1.30 thorpej {
500 1.30 thorpej struct dkwedge_info *dkw = (void *) addr;
501 1.30 thorpej
502 1.30 thorpej if ((flag & FWRITE) == 0)
503 1.30 thorpej return (EBADF);
504 1.30 thorpej
505 1.30 thorpej /* If the ioctl happens here, the parent is us. */
506 1.30 thorpej strcpy(dkw->dkw_parent, sc->sc_dv.dv_xname);
507 1.30 thorpej return (dkwedge_add(dkw));
508 1.30 thorpej }
509 1.35 perry
510 1.30 thorpej case DIOCDWEDGE:
511 1.30 thorpej {
512 1.30 thorpej struct dkwedge_info *dkw = (void *) addr;
513 1.30 thorpej
514 1.30 thorpej if ((flag & FWRITE) == 0)
515 1.30 thorpej return (EBADF);
516 1.30 thorpej
517 1.30 thorpej /* If the ioctl happens here, the parent is us. */
518 1.30 thorpej strcpy(dkw->dkw_parent, sc->sc_dv.dv_xname);
519 1.30 thorpej return (dkwedge_del(dkw));
520 1.30 thorpej }
521 1.35 perry
522 1.30 thorpej case DIOCLWEDGES:
523 1.30 thorpej {
524 1.30 thorpej struct dkwedge_list *dkwl = (void *) addr;
525 1.30 thorpej
526 1.39 christos return (dkwedge_list(&sc->sc_dk, dkwl, l));
527 1.30 thorpej }
528 1.30 thorpej
529 1.1 ad default:
530 1.1 ad error = ENOTTY;
531 1.1 ad break;
532 1.1 ad }
533 1.1 ad
534 1.1 ad return (error);
535 1.1 ad }
536 1.1 ad
537 1.29 thorpej static void
538 1.1 ad ldstrategy(struct buf *bp)
539 1.1 ad {
540 1.1 ad struct ld_softc *sc;
541 1.23 thorpej struct disklabel *lp;
542 1.23 thorpej daddr_t blkno;
543 1.23 thorpej int s, part;
544 1.1 ad
545 1.1 ad sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
546 1.23 thorpej part = DISKPART(bp->b_dev);
547 1.1 ad
548 1.7 ad if ((sc->sc_flags & LDF_DETACH) != 0) {
549 1.2 ad bp->b_error = EIO;
550 1.23 thorpej goto bad;
551 1.2 ad }
552 1.2 ad
553 1.1 ad lp = sc->sc_dk.dk_label;
554 1.1 ad
555 1.1 ad /*
556 1.1 ad * The transfer must be a whole number of blocks and the offset must
557 1.1 ad * not be negative.
558 1.1 ad */
559 1.1 ad if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
560 1.23 thorpej bp->b_error = EINVAL;
561 1.23 thorpej goto bad;
562 1.1 ad }
563 1.1 ad
564 1.23 thorpej /* If it's a null transfer, return immediately. */
565 1.23 thorpej if (bp->b_bcount == 0)
566 1.23 thorpej goto done;
567 1.1 ad
568 1.1 ad /*
569 1.1 ad * Do bounds checking and adjust the transfer. If error, process.
570 1.1 ad * If past the end of partition, just return.
571 1.1 ad */
572 1.1 ad if (part != RAW_PART &&
573 1.20 thorpej bounds_check_with_label(&sc->sc_dk, bp,
574 1.1 ad (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
575 1.23 thorpej goto done;
576 1.1 ad }
577 1.1 ad
578 1.1 ad /*
579 1.23 thorpej * Convert the block number to absolute and put it in terms
580 1.23 thorpej * of the device's logical block size.
581 1.1 ad */
582 1.23 thorpej if (lp->d_secsize == DEV_BSIZE)
583 1.23 thorpej blkno = bp->b_blkno;
584 1.23 thorpej else if (lp->d_secsize > DEV_BSIZE)
585 1.23 thorpej blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
586 1.1 ad else
587 1.23 thorpej blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
588 1.1 ad
589 1.11 thorpej if (part != RAW_PART)
590 1.23 thorpej blkno += lp->d_partitions[part].p_offset;
591 1.23 thorpej
592 1.23 thorpej bp->b_rawblkno = blkno;
593 1.1 ad
594 1.1 ad s = splbio();
595 1.40.8.2 ad ldstart(sc, bp);
596 1.1 ad splx(s);
597 1.23 thorpej return;
598 1.23 thorpej
599 1.23 thorpej bad:
600 1.23 thorpej bp->b_flags |= B_ERROR;
601 1.23 thorpej done:
602 1.23 thorpej bp->b_resid = bp->b_bcount;
603 1.23 thorpej biodone(bp);
604 1.23 thorpej }
605 1.23 thorpej
606 1.23 thorpej static void
607 1.40.8.2 ad ldstart(struct ld_softc *sc, struct buf *bp)
608 1.23 thorpej {
609 1.23 thorpej int error;
610 1.1 ad
611 1.40.8.2 ad mutex_enter(&sc->sc_mutex);
612 1.40.8.2 ad
613 1.40.8.2 ad if (bp != NULL)
614 1.40.8.2 ad BUFQ_PUT(sc->sc_bufq, bp);
615 1.40.8.2 ad
616 1.23 thorpej while (sc->sc_queuecnt < sc->sc_maxqueuecnt) {
617 1.23 thorpej /* See if there is work to do. */
618 1.38 yamt if ((bp = BUFQ_PEEK(sc->sc_bufq)) == NULL)
619 1.23 thorpej break;
620 1.23 thorpej
621 1.23 thorpej disk_busy(&sc->sc_dk);
622 1.23 thorpej sc->sc_queuecnt++;
623 1.23 thorpej
624 1.23 thorpej if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) {
625 1.23 thorpej /*
626 1.23 thorpej * The back-end is running the job; remove it from
627 1.23 thorpej * the queue.
628 1.23 thorpej */
629 1.38 yamt (void) BUFQ_GET(sc->sc_bufq);
630 1.23 thorpej } else {
631 1.23 thorpej disk_unbusy(&sc->sc_dk, 0, (bp->b_flags & B_READ));
632 1.23 thorpej sc->sc_queuecnt--;
633 1.23 thorpej if (error == EAGAIN) {
634 1.23 thorpej /*
635 1.23 thorpej * Temporary resource shortage in the
636 1.23 thorpej * back-end; just defer the job until
637 1.23 thorpej * later.
638 1.23 thorpej *
639 1.23 thorpej * XXX We might consider a watchdog timer
640 1.23 thorpej * XXX to make sure we are kicked into action.
641 1.23 thorpej */
642 1.23 thorpej break;
643 1.23 thorpej } else {
644 1.38 yamt (void) BUFQ_GET(sc->sc_bufq);
645 1.23 thorpej bp->b_error = error;
646 1.23 thorpej bp->b_flags |= B_ERROR;
647 1.23 thorpej bp->b_resid = bp->b_bcount;
648 1.40.8.2 ad mutex_exit(&sc->sc_mutex);
649 1.23 thorpej biodone(bp);
650 1.40.8.2 ad mutex_enter(&sc->sc_mutex);
651 1.23 thorpej }
652 1.23 thorpej }
653 1.1 ad }
654 1.40.8.2 ad
655 1.40.8.2 ad mutex_exit(&sc->sc_mutex);
656 1.1 ad }
657 1.1 ad
658 1.1 ad void
659 1.1 ad lddone(struct ld_softc *sc, struct buf *bp)
660 1.1 ad {
661 1.1 ad
662 1.1 ad if ((bp->b_flags & B_ERROR) != 0) {
663 1.1 ad diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
664 1.1 ad printf("\n");
665 1.1 ad }
666 1.1 ad
667 1.18 mrg disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
668 1.18 mrg (bp->b_flags & B_READ));
669 1.1 ad #if NRND > 0
670 1.1 ad rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
671 1.1 ad #endif
672 1.1 ad biodone(bp);
673 1.1 ad
674 1.40.8.2 ad mutex_enter(&sc->sc_mutex);
675 1.7 ad if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
676 1.24 thorpej if ((sc->sc_flags & LDF_DRAIN) != 0) {
677 1.24 thorpej sc->sc_flags &= ~LDF_DRAIN;
678 1.7 ad wakeup(&sc->sc_queuecnt);
679 1.24 thorpej }
680 1.40.8.2 ad mutex_exit(&sc->sc_mutex);
681 1.40.8.2 ad ldstart(sc, NULL);
682 1.40.8.2 ad } else
683 1.40.8.2 ad mutex_exit(&sc->sc_mutex);
684 1.1 ad }
685 1.1 ad
686 1.29 thorpej static int
687 1.1 ad ldsize(dev_t dev)
688 1.1 ad {
689 1.1 ad struct ld_softc *sc;
690 1.1 ad int part, unit, omask, size;
691 1.1 ad
692 1.1 ad unit = DISKUNIT(dev);
693 1.1 ad if ((sc = device_lookup(&ld_cd, unit)) == NULL)
694 1.1 ad return (ENODEV);
695 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
696 1.1 ad return (ENODEV);
697 1.1 ad part = DISKPART(dev);
698 1.1 ad
699 1.1 ad omask = sc->sc_dk.dk_openmask & (1 << part);
700 1.1 ad
701 1.1 ad if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
702 1.1 ad return (-1);
703 1.1 ad else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
704 1.1 ad size = -1;
705 1.1 ad else
706 1.1 ad size = sc->sc_dk.dk_label->d_partitions[part].p_size *
707 1.1 ad (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
708 1.1 ad if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
709 1.1 ad return (-1);
710 1.1 ad
711 1.1 ad return (size);
712 1.1 ad }
713 1.1 ad
714 1.1 ad /*
715 1.1 ad * Load the label information from the specified device.
716 1.1 ad */
717 1.1 ad static void
718 1.1 ad ldgetdisklabel(struct ld_softc *sc)
719 1.1 ad {
720 1.1 ad const char *errstring;
721 1.1 ad
722 1.1 ad ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
723 1.1 ad
724 1.1 ad /* Call the generic disklabel extraction routine. */
725 1.40 thorpej errstring = readdisklabel(MAKEDISKDEV(0, device_unit(&sc->sc_dv),
726 1.40 thorpej RAW_PART), ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
727 1.1 ad if (errstring != NULL)
728 1.1 ad printf("%s: %s\n", sc->sc_dv.dv_xname, errstring);
729 1.22 thorpej
730 1.22 thorpej /* In-core label now valid. */
731 1.22 thorpej sc->sc_flags |= LDF_VLABEL;
732 1.1 ad }
733 1.1 ad
734 1.1 ad /*
735 1.1 ad * Construct a ficticious label.
736 1.1 ad */
737 1.1 ad static void
738 1.1 ad ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
739 1.1 ad {
740 1.1 ad
741 1.1 ad memset(lp, 0, sizeof(struct disklabel));
742 1.1 ad
743 1.1 ad lp->d_secsize = sc->sc_secsize;
744 1.1 ad lp->d_ntracks = sc->sc_nheads;
745 1.1 ad lp->d_nsectors = sc->sc_nsectors;
746 1.1 ad lp->d_ncylinders = sc->sc_ncylinders;
747 1.1 ad lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
748 1.1 ad lp->d_type = DTYPE_LD;
749 1.21 itojun strlcpy(lp->d_typename, "unknown", sizeof(lp->d_typename));
750 1.21 itojun strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
751 1.1 ad lp->d_secperunit = sc->sc_secperunit;
752 1.1 ad lp->d_rpm = 7200;
753 1.1 ad lp->d_interleave = 1;
754 1.1 ad lp->d_flags = 0;
755 1.1 ad
756 1.1 ad lp->d_partitions[RAW_PART].p_offset = 0;
757 1.1 ad lp->d_partitions[RAW_PART].p_size =
758 1.1 ad lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
759 1.1 ad lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
760 1.1 ad lp->d_npartitions = RAW_PART + 1;
761 1.1 ad
762 1.1 ad lp->d_magic = DISKMAGIC;
763 1.1 ad lp->d_magic2 = DISKMAGIC;
764 1.1 ad lp->d_checksum = dkcksum(lp);
765 1.1 ad }
766 1.1 ad
767 1.1 ad /*
768 1.1 ad * Take a dump.
769 1.1 ad */
770 1.29 thorpej static int
771 1.1 ad lddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
772 1.1 ad {
773 1.1 ad struct ld_softc *sc;
774 1.1 ad struct disklabel *lp;
775 1.1 ad int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
776 1.1 ad static int dumping;
777 1.1 ad
778 1.1 ad unit = DISKUNIT(dev);
779 1.1 ad if ((sc = device_lookup(&ld_cd, unit)) == NULL)
780 1.1 ad return (ENXIO);
781 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
782 1.1 ad return (ENODEV);
783 1.3 ad if (sc->sc_dump == NULL)
784 1.3 ad return (ENXIO);
785 1.3 ad
786 1.3 ad /* Check if recursive dump; if so, punt. */
787 1.3 ad if (dumping)
788 1.3 ad return (EFAULT);
789 1.3 ad dumping = 1;
790 1.1 ad
791 1.1 ad /* Convert to disk sectors. Request must be a multiple of size. */
792 1.3 ad part = DISKPART(dev);
793 1.1 ad lp = sc->sc_dk.dk_label;
794 1.1 ad if ((size % lp->d_secsize) != 0)
795 1.1 ad return (EFAULT);
796 1.1 ad towrt = size / lp->d_secsize;
797 1.1 ad blkno = dbtob(blkno) / lp->d_secsize; /* blkno in DEV_BSIZE units */
798 1.1 ad
799 1.1 ad nsects = lp->d_partitions[part].p_size;
800 1.1 ad sectoff = lp->d_partitions[part].p_offset;
801 1.1 ad
802 1.1 ad /* Check transfer bounds against partition size. */
803 1.1 ad if ((blkno < 0) || ((blkno + towrt) > nsects))
804 1.1 ad return (EINVAL);
805 1.1 ad
806 1.1 ad /* Offset block number to start of partition. */
807 1.1 ad blkno += sectoff;
808 1.1 ad
809 1.1 ad /* Start dumping and return when done. */
810 1.3 ad maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
811 1.1 ad while (towrt > 0) {
812 1.3 ad nblk = min(maxblkcnt, towrt);
813 1.1 ad
814 1.1 ad if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
815 1.1 ad return (rv);
816 1.1 ad
817 1.1 ad towrt -= nblk;
818 1.1 ad blkno += nblk;
819 1.1 ad va += nblk * sc->sc_secsize;
820 1.1 ad }
821 1.1 ad
822 1.1 ad dumping = 0;
823 1.1 ad return (0);
824 1.1 ad }
825 1.1 ad
826 1.1 ad /*
827 1.1 ad * Adjust the size of a transfer.
828 1.1 ad */
829 1.1 ad static void
830 1.1 ad ldminphys(struct buf *bp)
831 1.1 ad {
832 1.1 ad struct ld_softc *sc;
833 1.1 ad
834 1.1 ad sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
835 1.1 ad
836 1.1 ad if (bp->b_bcount > sc->sc_maxxfer)
837 1.1 ad bp->b_bcount = sc->sc_maxxfer;
838 1.1 ad minphys(bp);
839 1.1 ad }
840