dksubr.c revision 1.30 1 /* $NetBSD: dksubr.c,v 1.30 2007/07/21 19:51:47 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Roland C. Dowdeswell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: dksubr.c,v 1.30 2007/07/21 19:51:47 ad Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/stat.h>
45 #include <sys/proc.h>
46 #include <sys/ioctl.h>
47 #include <sys/device.h>
48 #include <sys/disk.h>
49 #include <sys/disklabel.h>
50 #include <sys/buf.h>
51 #include <sys/bufq.h>
52 #include <sys/vnode.h>
53 #include <sys/fcntl.h>
54 #include <sys/namei.h>
55
56 #include <dev/dkvar.h>
57
58 int dkdebug = 0;
59
60 #ifdef DEBUG
61 #define DKDB_FOLLOW 0x1
62 #define DKDB_INIT 0x2
63 #define DKDB_VNODE 0x4
64
65 #define IFDEBUG(x,y) if (dkdebug & (x)) y
66 #define DPRINTF(x,y) IFDEBUG(x, printf y)
67 #define DPRINTF_FOLLOW(y) DPRINTF(DKDB_FOLLOW, y)
68 #else
69 #define IFDEBUG(x,y)
70 #define DPRINTF(x,y)
71 #define DPRINTF_FOLLOW(y)
72 #endif
73
74 #define DKLABELDEV(dev) \
75 (MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
76
77 static void dk_makedisklabel(struct dk_intf *, struct dk_softc *);
78
79 void
80 dk_sc_init(struct dk_softc *dksc, void *osc, char *xname)
81 {
82
83 memset(dksc, 0x0, sizeof(*dksc));
84 dksc->sc_osc = osc;
85 strncpy(dksc->sc_xname, xname, DK_XNAME_SIZE);
86 dksc->sc_dkdev.dk_name = dksc->sc_xname;
87 }
88
89 /* ARGSUSED */
90 int
91 dk_open(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
92 int flags, int fmt, struct lwp *l)
93 {
94 struct disklabel *lp = dksc->sc_dkdev.dk_label;
95 int part = DISKPART(dev);
96 int pmask = 1 << part;
97 int ret = 0;
98 struct disk *dk = &dksc->sc_dkdev;
99
100 DPRINTF_FOLLOW(("dk_open(%s, %p, 0x%x, 0x%x)\n",
101 di->di_dkname, dksc, dev, flags));
102
103 mutex_enter(&dk->dk_openlock);
104 part = DISKPART(dev);
105
106 /*
107 * If there are wedges, and this is not RAW_PART, then we
108 * need to fail.
109 */
110 if (dk->dk_nwedges != 0 && part != RAW_PART) {
111 ret = EBUSY;
112 goto done;
113 }
114
115 pmask = 1 << part;
116
117 /*
118 * If we're init'ed and there are no other open partitions then
119 * update the in-core disklabel.
120 */
121 if ((dksc->sc_flags & DKF_INITED)) {
122 if (dk->dk_openmask == 0) {
123 dk_getdisklabel(di, dksc, dev);
124 }
125 /* XXX re-discover wedges? */
126 }
127
128 /* Fail if we can't find the partition. */
129 if ((part != RAW_PART) &&
130 (((dksc->sc_flags & DKF_INITED) == 0) ||
131 ((part >= lp->d_npartitions) ||
132 (lp->d_partitions[part].p_fstype == FS_UNUSED)))) {
133 ret = ENXIO;
134 goto done;
135 }
136
137 /* Mark our unit as open. */
138 switch (fmt) {
139 case S_IFCHR:
140 dk->dk_copenmask |= pmask;
141 break;
142 case S_IFBLK:
143 dk->dk_bopenmask |= pmask;
144 break;
145 }
146
147 dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
148
149 done:
150 mutex_exit(&dk->dk_openlock);
151 return ret;
152 }
153
154 /* ARGSUSED */
155 int
156 dk_close(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
157 int flags, int fmt, struct lwp *l)
158 {
159 int part = DISKPART(dev);
160 int pmask = 1 << part;
161 struct disk *dk = &dksc->sc_dkdev;
162
163 DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%x, 0x%x)\n",
164 di->di_dkname, dksc, dev, flags));
165
166 mutex_enter(&dk->dk_openlock);
167
168 switch (fmt) {
169 case S_IFCHR:
170 dk->dk_copenmask &= ~pmask;
171 break;
172 case S_IFBLK:
173 dk->dk_bopenmask &= ~pmask;
174 break;
175 }
176 dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
177
178 mutex_exit(&dk->dk_openlock);
179 return 0;
180 }
181
182 void
183 dk_strategy(struct dk_intf *di, struct dk_softc *dksc, struct buf *bp)
184 {
185 int s;
186 int wlabel;
187 daddr_t blkno;
188
189 DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
190 di->di_dkname, dksc, bp));
191
192 if (!(dksc->sc_flags & DKF_INITED)) {
193 DPRINTF_FOLLOW(("dk_strategy: not inited\n"));
194 bp->b_error = ENXIO;
195 bp->b_flags |= B_ERROR;
196 biodone(bp);
197 return;
198 }
199
200 /* XXX look for some more errors, c.f. ld.c */
201
202 bp->b_resid = bp->b_bcount;
203
204 /* If there is nothing to do, then we are done */
205 if (bp->b_bcount == 0) {
206 biodone(bp);
207 return;
208 }
209
210 wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
211 if (DISKPART(bp->b_dev) != RAW_PART &&
212 bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
213 biodone(bp);
214 return;
215 }
216
217 blkno = bp->b_blkno;
218 if (DISKPART(bp->b_dev) != RAW_PART) {
219 struct partition *pp;
220
221 pp =
222 &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
223 blkno += pp->p_offset;
224 }
225 bp->b_rawblkno = blkno;
226
227 /*
228 * Start the unit by calling the start routine
229 * provided by the individual driver.
230 */
231 s = splbio();
232 BUFQ_PUT(dksc->sc_bufq, bp);
233 dk_start(di, dksc);
234 splx(s);
235 return;
236 }
237
238 void
239 dk_start(struct dk_intf *di, struct dk_softc *dksc)
240 {
241 struct buf *bp;
242
243 DPRINTF_FOLLOW(("dk_start(%s, %p)\n", di->di_dkname, dksc));
244
245 /* Process the work queue */
246 while ((bp = BUFQ_GET(dksc->sc_bufq)) != NULL) {
247 if (di->di_diskstart(dksc, bp) != 0) {
248 BUFQ_PUT(dksc->sc_bufq, bp);
249 break;
250 }
251 }
252 }
253
254 void
255 dk_iodone(struct dk_intf *di, struct dk_softc *dksc)
256 {
257
258 DPRINTF_FOLLOW(("dk_iodone(%s, %p)\n", di->di_dkname, dksc));
259
260 /* We kick the queue in case we are able to get more work done */
261 dk_start(di, dksc);
262 }
263
264 int
265 dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
266 {
267 struct disklabel *lp;
268 int is_open;
269 int part;
270 int size;
271
272 if ((dksc->sc_flags & DKF_INITED) == 0)
273 return -1;
274
275 part = DISKPART(dev);
276 is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
277
278 if (!is_open && di->di_open(dev, 0, S_IFBLK, curlwp))
279 return -1;
280
281 lp = dksc->sc_dkdev.dk_label;
282 if (lp->d_partitions[part].p_fstype != FS_SWAP)
283 size = -1;
284 else
285 size = lp->d_partitions[part].p_size *
286 (lp->d_secsize / DEV_BSIZE);
287
288 if (!is_open && di->di_close(dev, 0, S_IFBLK, curlwp))
289 return 1;
290
291 return size;
292 }
293
294 int
295 dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
296 u_long cmd, void *data, int flag, struct lwp *l)
297 {
298 struct disklabel *lp;
299 struct disk *dk;
300 #ifdef __HAVE_OLD_DISKLABEL
301 struct disklabel newlabel;
302 #endif
303 int error = 0;
304
305 DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%x, 0x%lx)\n",
306 di->di_dkname, dksc, dev, cmd));
307
308 /* ensure that the pseudo disk is open for writes for these commands */
309 switch (cmd) {
310 case DIOCSDINFO:
311 case DIOCWDINFO:
312 #ifdef __HAVE_OLD_DISKLABEL
313 case ODIOCSDINFO:
314 case ODIOCWDINFO:
315 #endif
316 case DIOCWLABEL:
317 if ((flag & FWRITE) == 0)
318 return EBADF;
319 }
320
321 /* ensure that the pseudo-disk is initialized for these */
322 switch (cmd) {
323 case DIOCGDINFO:
324 case DIOCSDINFO:
325 case DIOCWDINFO:
326 case DIOCGPART:
327 case DIOCWLABEL:
328 case DIOCGDEFLABEL:
329 #ifdef __HAVE_OLD_DISKLABEL
330 case ODIOCGDINFO:
331 case ODIOCSDINFO:
332 case ODIOCWDINFO:
333 case ODIOCGDEFLABEL:
334 #endif
335 if ((dksc->sc_flags & DKF_INITED) == 0)
336 return ENXIO;
337 }
338
339 switch (cmd) {
340 case DIOCGDINFO:
341 *(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
342 break;
343
344 #ifdef __HAVE_OLD_DISKLABEL
345 case ODIOCGDINFO:
346 newlabel = *(dksc->sc_dkdev.dk_label);
347 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
348 return ENOTTY;
349 memcpy(data, &newlabel, sizeof (struct olddisklabel));
350 break;
351 #endif
352
353 case DIOCGPART:
354 ((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
355 ((struct partinfo *)data)->part =
356 &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
357 break;
358
359 case DIOCWDINFO:
360 case DIOCSDINFO:
361 #ifdef __HAVE_OLD_DISKLABEL
362 case ODIOCWDINFO:
363 case ODIOCSDINFO:
364 #endif
365 #ifdef __HAVE_OLD_DISKLABEL
366 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
367 memset(&newlabel, 0, sizeof newlabel);
368 memcpy(&newlabel, data, sizeof (struct olddisklabel));
369 lp = &newlabel;
370 } else
371 #endif
372 lp = (struct disklabel *)data;
373
374 dk = &dksc->sc_dkdev;
375 mutex_enter(&dk->dk_openlock);
376 dksc->sc_flags |= DKF_LABELLING;
377
378 error = setdisklabel(dksc->sc_dkdev.dk_label,
379 lp, 0, dksc->sc_dkdev.dk_cpulabel);
380 if (error == 0) {
381 if (cmd == DIOCWDINFO
382 #ifdef __HAVE_OLD_DISKLABEL
383 || cmd == ODIOCWDINFO
384 #endif
385 )
386 error = writedisklabel(DKLABELDEV(dev),
387 di->di_strategy, dksc->sc_dkdev.dk_label,
388 dksc->sc_dkdev.dk_cpulabel);
389 }
390
391 dksc->sc_flags &= ~DKF_LABELLING;
392 mutex_exit(&dk->dk_openlock);
393 break;
394
395 case DIOCWLABEL:
396 if (*(int *)data != 0)
397 dksc->sc_flags |= DKF_WLABEL;
398 else
399 dksc->sc_flags &= ~DKF_WLABEL;
400 break;
401
402 case DIOCGDEFLABEL:
403 dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
404 break;
405
406 #ifdef __HAVE_OLD_DISKLABEL
407 case ODIOCGDEFLABEL:
408 dk_getdefaultlabel(di, dksc, &newlabel);
409 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
410 return ENOTTY;
411 memcpy(data, &newlabel, sizeof (struct olddisklabel));
412 break;
413 #endif
414
415 case DIOCAWEDGE:
416 {
417 struct dkwedge_info *dkw = (void *)data;
418
419 if ((flag & FWRITE) == 0)
420 return (EBADF);
421
422 /* If the ioctl happens here, the parent is us. */
423 strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
424 return (dkwedge_add(dkw));
425 }
426
427 case DIOCDWEDGE:
428 {
429 struct dkwedge_info *dkw = (void *)data;
430
431 if ((flag & FWRITE) == 0)
432 return (EBADF);
433
434 /* If the ioctl happens here, the parent is us. */
435 strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
436 return (dkwedge_del(dkw));
437 }
438
439 case DIOCLWEDGES:
440 {
441 struct dkwedge_list *dkwl = (void *)data;
442
443 return (dkwedge_list(&dksc->sc_dkdev, dkwl, l));
444 }
445
446 case DIOCGSTRATEGY:
447 {
448 struct disk_strategy *dks = (void *)data;
449 int s;
450
451 s = splbio();
452 strlcpy(dks->dks_name, bufq_getstrategyname(dksc->sc_bufq),
453 sizeof(dks->dks_name));
454 splx(s);
455 dks->dks_paramlen = 0;
456
457 return 0;
458 }
459
460 case DIOCSSTRATEGY:
461 {
462 struct disk_strategy *dks = (void *)data;
463 struct bufq_state *new;
464 struct bufq_state *old;
465 int s;
466
467 if ((flag & FWRITE) == 0) {
468 return EBADF;
469 }
470 if (dks->dks_param != NULL) {
471 return EINVAL;
472 }
473 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
474 error = bufq_alloc(&new, dks->dks_name,
475 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
476 if (error) {
477 return error;
478 }
479 s = splbio();
480 old = dksc->sc_bufq;
481 bufq_move(new, old);
482 dksc->sc_bufq = new;
483 splx(s);
484 bufq_free(old);
485
486 return 0;
487 }
488
489 default:
490 error = ENOTTY;
491 }
492
493 return error;
494 }
495
496 /*
497 * dk_dump dumps all of physical memory into the partition specified.
498 * This requires substantially more framework than {s,w}ddump, and hence
499 * is probably much more fragile.
500 *
501 * XXX: we currently do not implement this.
502 */
503
504 #define DKF_READYFORDUMP (DKF_INITED|DKF_TAKEDUMP)
505 #define DKFF_READYFORDUMP(x) (((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
506 static volatile int dk_dumping = 0;
507
508 /* ARGSUSED */
509 int
510 dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
511 daddr_t blkno, void *va, size_t size)
512 {
513
514 /*
515 * ensure that we consider this device to be safe for dumping,
516 * and that the device is configured.
517 */
518 if (!DKFF_READYFORDUMP(dksc->sc_flags))
519 return ENXIO;
520
521 /* ensure that we are not already dumping */
522 if (dk_dumping)
523 return EFAULT;
524 dk_dumping = 1;
525
526 /* XXX: unimplemented */
527
528 dk_dumping = 0;
529
530 /* XXX: actually for now, we are going to leave this alone */
531 return ENXIO;
532 }
533
534 /* ARGSUSED */
535 void
536 dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
537 struct disklabel *lp)
538 {
539 struct dk_geom *pdg = &dksc->sc_geom;
540
541 memset(lp, 0, sizeof(*lp));
542
543 lp->d_secperunit = dksc->sc_size;
544 lp->d_secsize = pdg->pdg_secsize;
545 lp->d_nsectors = pdg->pdg_nsectors;
546 lp->d_ntracks = pdg->pdg_ntracks;
547 lp->d_ncylinders = pdg->pdg_ncylinders;
548 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
549
550 strncpy(lp->d_typename, di->di_dkname, sizeof(lp->d_typename));
551 lp->d_type = di->di_dtype;
552 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
553 lp->d_rpm = 3600;
554 lp->d_interleave = 1;
555 lp->d_flags = 0;
556
557 lp->d_partitions[RAW_PART].p_offset = 0;
558 lp->d_partitions[RAW_PART].p_size = dksc->sc_size;
559 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
560 lp->d_npartitions = RAW_PART + 1;
561
562 lp->d_magic = DISKMAGIC;
563 lp->d_magic2 = DISKMAGIC;
564 lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
565 }
566
567 /* ARGSUSED */
568 void
569 dk_getdisklabel(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
570 {
571 struct disklabel *lp = dksc->sc_dkdev.dk_label;
572 struct cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
573 struct partition *pp;
574 int i;
575 const char *errstring;
576
577 memset(clp, 0x0, sizeof(*clp));
578 dk_getdefaultlabel(di, dksc, lp);
579 errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
580 dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
581 if (errstring) {
582 dk_makedisklabel(di, dksc);
583 if (dksc->sc_flags & DKF_WARNLABEL)
584 printf("%s: %s\n", dksc->sc_xname, errstring);
585 return;
586 }
587
588 if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
589 return;
590
591 /* Sanity check */
592 if (lp->d_secperunit != dksc->sc_size)
593 printf("WARNING: %s: total sector size in disklabel (%d) "
594 "!= the size of %s (%lu)\n", dksc->sc_xname,
595 lp->d_secperunit, di->di_dkname, (u_long)dksc->sc_size);
596
597 for (i=0; i < lp->d_npartitions; i++) {
598 pp = &lp->d_partitions[i];
599 if (pp->p_offset + pp->p_size > dksc->sc_size)
600 printf("WARNING: %s: end of partition `%c' exceeds "
601 "the size of %s (%lu)\n", dksc->sc_xname,
602 'a' + i, di->di_dkname, (u_long)dksc->sc_size);
603 }
604 }
605
606 /* ARGSUSED */
607 static void
608 dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
609 {
610 struct disklabel *lp = dksc->sc_dkdev.dk_label;
611
612 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
613 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
614 lp->d_checksum = dkcksum(lp);
615 }
616
617 /* This function is taken from ccd.c:1.76 --rcd */
618
619 /*
620 * XXX this function looks too generic for dksubr.c, shouldn't we
621 * put it somewhere better?
622 */
623
624 /*
625 * Lookup the provided name in the filesystem. If the file exists,
626 * is a valid block device, and isn't being used by anyone else,
627 * set *vpp to the file's vnode.
628 */
629 int
630 dk_lookup(const char *path, struct lwp *l, struct vnode **vpp,
631 enum uio_seg segflg)
632 {
633 struct nameidata nd;
634 struct vnode *vp;
635 struct vattr va;
636 int error;
637
638 if (l == NULL)
639 return ESRCH; /* Is ESRCH the best choice? */
640
641 NDINIT(&nd, LOOKUP, FOLLOW, segflg, path, l);
642 if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
643 DPRINTF((DKDB_FOLLOW|DKDB_INIT),
644 ("dk_lookup: vn_open error = %d\n", error));
645 return error;
646 }
647
648 vp = nd.ni_vp;
649 if ((error = VOP_GETATTR(vp, &va, l->l_cred, l)) != 0) {
650 DPRINTF((DKDB_FOLLOW|DKDB_INIT),
651 ("dk_lookup: getattr error = %d\n", error));
652 goto out;
653 }
654
655 /* XXX: eventually we should handle VREG, too. */
656 if (va.va_type != VBLK) {
657 error = ENOTBLK;
658 goto out;
659 }
660
661 /* XXX: wedges have a writecount of 1; this is disgusting */
662 if (vp->v_usecount > 1 + (major(va.va_rdev) == 168)) {
663 error = EBUSY;
664 goto out;
665 }
666
667 IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", vp));
668
669 VOP_UNLOCK(vp, 0);
670 *vpp = vp;
671 return 0;
672 out:
673 VOP_UNLOCK(vp, 0);
674 (void) vn_close(vp, FREAD | FWRITE, l->l_cred, l);
675 return error;
676 }
677