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