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