dksubr.c revision 1.16 1 /* $NetBSD: dksubr.c,v 1.16 2005/08/20 12:03:52 yamt 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.16 2005/08/20 12:03:52 yamt 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 proc *p)
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 if ((ret = lockmgr(&dk->dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
104 return ret;
105
106 part = DISKPART(dev);
107
108 /*
109 * If there are wedges, and this is not RAW_PART, then we
110 * need to fail.
111 */
112 if (dk->dk_nwedges != 0 && part != RAW_PART) {
113 ret = EBUSY;
114 goto done;
115 }
116
117 pmask = 1 << part;
118
119 /*
120 * If we're init'ed and there are no other open partitions then
121 * update the in-core disklabel.
122 */
123 if ((dksc->sc_flags & DKF_INITED)) {
124 if (dk->dk_openmask == 0) {
125 dk_getdisklabel(di, dksc, dev);
126 }
127 /* XXX re-discover wedges? */
128 }
129
130 /* Fail if we can't find the partition. */
131 if ((part != RAW_PART) &&
132 (((dksc->sc_flags & DKF_INITED) == 0) ||
133 ((part >= lp->d_npartitions) ||
134 (lp->d_partitions[part].p_fstype == FS_UNUSED)))) {
135 ret = ENXIO;
136 goto done;
137 }
138
139 /* Mark our unit as open. */
140 switch (fmt) {
141 case S_IFCHR:
142 dk->dk_copenmask |= pmask;
143 break;
144 case S_IFBLK:
145 dk->dk_bopenmask |= pmask;
146 break;
147 }
148
149 dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
150
151 done:
152 lockmgr(&dk->dk_openlock, LK_RELEASE, NULL);
153 return ret;
154 }
155
156 /* ARGSUSED */
157 int
158 dk_close(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
159 int flags, int fmt, struct proc *p)
160 {
161 int part = DISKPART(dev);
162 int pmask = 1 << part;
163 int ret;
164 struct disk *dk = &dksc->sc_dkdev;
165
166 DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%x, 0x%x)\n",
167 di->di_dkname, dksc, dev, flags));
168
169 if ((ret = lockmgr(&dk->dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
170 return ret;
171
172 switch (fmt) {
173 case S_IFCHR:
174 dk->dk_copenmask &= ~pmask;
175 break;
176 case S_IFBLK:
177 dk->dk_bopenmask &= ~pmask;
178 break;
179 }
180 dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
181
182 lockmgr(&dk->dk_openlock, LK_RELEASE, NULL);
183 return 0;
184 }
185
186 void
187 dk_strategy(struct dk_intf *di, struct dk_softc *dksc, struct buf *bp)
188 {
189 int s;
190 int wlabel;
191
192 DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
193 di->di_dkname, dksc, bp));
194
195 if (!(dksc->sc_flags & DKF_INITED)) {
196 DPRINTF_FOLLOW(("dk_stragy: not inited\n"));
197 bp->b_error = ENXIO;
198 bp->b_flags |= B_ERROR;
199 biodone(bp);
200 return;
201 }
202
203 /* XXX look for some more errors, c.f. ld.c */
204
205 bp->b_resid = bp->b_bcount;
206
207 /* If there is nothing to do, then we are done */
208 if (bp->b_bcount == 0) {
209 biodone(bp);
210 return;
211 }
212
213 wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
214 if (DISKPART(bp->b_dev) != RAW_PART &&
215 bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
216 biodone(bp);
217 return;
218 }
219
220 /*
221 * Start the unit by calling the start routine
222 * provided by the individual driver.
223 */
224 s = splbio();
225 BUFQ_PUT(&dksc->sc_bufq, bp);
226 dk_start(di, dksc);
227 splx(s);
228 return;
229 }
230
231 void
232 dk_start(struct dk_intf *di, struct dk_softc *dksc)
233 {
234 struct buf *bp;
235
236 DPRINTF_FOLLOW(("dk_start(%s, %p)\n", di->di_dkname, dksc));
237
238 /* Process the work queue */
239 while ((bp = BUFQ_GET(&dksc->sc_bufq)) != NULL) {
240 if (di->di_diskstart(dksc, bp) != 0) {
241 BUFQ_PUT(&dksc->sc_bufq, bp);
242 break;
243 }
244 }
245 }
246
247 void
248 dk_iodone(struct dk_intf *di, struct dk_softc *dksc)
249 {
250
251 DPRINTF_FOLLOW(("dk_iodone(%s, %p)\n", di->di_dkname, dksc));
252
253 /* We kick the queue in case we are able to get more work done */
254 dk_start(di, dksc);
255 }
256
257 int
258 dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
259 {
260 struct disklabel *lp;
261 int is_open;
262 int part;
263 int size;
264
265 if ((dksc->sc_flags & DKF_INITED) == 0)
266 return -1;
267
268 part = DISKPART(dev);
269 is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
270
271 if (!is_open && di->di_open(dev, 0, S_IFBLK, curproc))
272 return -1;
273
274 lp = dksc->sc_dkdev.dk_label;
275 if (lp->d_partitions[part].p_fstype != FS_SWAP)
276 size = -1;
277 else
278 size = lp->d_partitions[part].p_size *
279 (lp->d_secsize / DEV_BSIZE);
280
281 if (!is_open && di->di_close(dev, 0, S_IFBLK, curproc))
282 return 1;
283
284 return size;
285 }
286
287 int
288 dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
289 u_long cmd, caddr_t data, int flag, struct proc *p)
290 {
291 struct disklabel *lp;
292 struct disk *dk;
293 #ifdef __HAVE_OLD_DISKLABEL
294 struct disklabel newlabel;
295 #endif
296 int error = 0;
297
298 DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%x, 0x%lx)\n",
299 di->di_dkname, dksc, dev, cmd));
300
301 /* ensure that the pseudo disk is open for writes for these commands */
302 switch (cmd) {
303 case DIOCSDINFO:
304 case DIOCWDINFO:
305 #ifdef __HAVE_OLD_DISKLABEL
306 case ODIOCSDINFO:
307 case ODIOCWDINFO:
308 #endif
309 case DIOCWLABEL:
310 if ((flag & FWRITE) == 0)
311 return EBADF;
312 }
313
314 /* ensure that the pseudo-disk is initialized for these */
315 switch (cmd) {
316 case DIOCGDINFO:
317 case DIOCSDINFO:
318 case DIOCWDINFO:
319 case DIOCGPART:
320 case DIOCWLABEL:
321 case DIOCGDEFLABEL:
322 #ifdef __HAVE_OLD_DISKLABEL
323 case ODIOCGDINFO:
324 case ODIOCSDINFO:
325 case ODIOCWDINFO:
326 case ODIOCGDEFLABEL:
327 #endif
328 if ((dksc->sc_flags & DKF_INITED) == 0)
329 return ENXIO;
330 }
331
332 switch (cmd) {
333 case DIOCGDINFO:
334 *(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
335 break;
336
337 #ifdef __HAVE_OLD_DISKLABEL
338 case ODIOCGDINFO:
339 newlabel = *(dksc->sc_dkdev.dk_label);
340 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
341 return ENOTTY;
342 memcpy(data, &newlabel, sizeof (struct olddisklabel));
343 break;
344 #endif
345
346 case DIOCGPART:
347 ((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
348 ((struct partinfo *)data)->part =
349 &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
350 break;
351
352 case DIOCWDINFO:
353 case DIOCSDINFO:
354 #ifdef __HAVE_OLD_DISKLABEL
355 case ODIOCWDINFO:
356 case ODIOCSDINFO:
357 #endif
358 #ifdef __HAVE_OLD_DISKLABEL
359 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
360 memset(&newlabel, 0, sizeof newlabel);
361 memcpy(&newlabel, data, sizeof (struct olddisklabel));
362 lp = &newlabel;
363 } else
364 #endif
365 lp = (struct disklabel *)data;
366
367 dk = &dksc->sc_dkdev;
368 error = lockmgr(&dk->dk_openlock, LK_EXCLUSIVE, NULL);
369 if (error) {
370 break;
371 }
372
373 dksc->sc_flags |= DKF_LABELLING;
374
375 error = setdisklabel(dksc->sc_dkdev.dk_label,
376 lp, 0, dksc->sc_dkdev.dk_cpulabel);
377 if (error == 0) {
378 if (cmd == DIOCWDINFO
379 #ifdef __HAVE_OLD_DISKLABEL
380 || cmd == ODIOCWDINFO
381 #endif
382 )
383 error = writedisklabel(DKLABELDEV(dev),
384 di->di_strategy, dksc->sc_dkdev.dk_label,
385 dksc->sc_dkdev.dk_cpulabel);
386 }
387
388 dksc->sc_flags &= ~DKF_LABELLING;
389 error = lockmgr(&dk->dk_openlock, LK_RELEASE, NULL);
390 break;
391
392 case DIOCWLABEL:
393 if (*(int *)data != 0)
394 dksc->sc_flags |= DKF_WLABEL;
395 else
396 dksc->sc_flags &= ~DKF_WLABEL;
397 break;
398
399 case DIOCGDEFLABEL:
400 dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
401 break;
402
403 #ifdef __HAVE_OLD_DISKLABEL
404 case ODIOCGDEFLABEL:
405 dk_getdefaultlabel(di, dksc, &newlabel);
406 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
407 return ENOTTY;
408 memcpy(data, &newlabel, sizeof (struct olddisklabel));
409 break;
410 #endif
411
412 case DIOCAWEDGE:
413 {
414 struct dkwedge_info *dkw = (void *)data;
415
416 if ((flag & FWRITE) == 0)
417 return (EBADF);
418
419 /* If the ioctl happens here, the parent is us. */
420 strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
421 return (dkwedge_add(dkw));
422 }
423
424 case DIOCDWEDGE:
425 {
426 struct dkwedge_info *dkw = (void *)data;
427
428 if ((flag & FWRITE) == 0)
429 return (EBADF);
430
431 /* If the ioctl happens here, the parent is us. */
432 strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
433 return (dkwedge_del(dkw));
434 }
435
436 case DIOCLWEDGES:
437 {
438 struct dkwedge_list *dkwl = (void *)data;
439
440 return (dkwedge_list(&dksc->sc_dkdev, dkwl, p));
441 }
442
443 default:
444 error = ENOTTY;
445 }
446
447 return error;
448 }
449
450 /*
451 * dk_dump dumps all of physical memory into the partition specified.
452 * This requires substantially more framework than {s,w}ddump, and hence
453 * is probably much more fragile.
454 *
455 * XXX: we currently do not implement this.
456 */
457
458 #define DKF_READYFORDUMP (DKF_INITED|DKF_TAKEDUMP)
459 #define DKFF_READYFORDUMP(x) (((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
460 static volatile int dk_dumping = 0;
461
462 /* ARGSUSED */
463 int
464 dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
465 daddr_t blkno, caddr_t va, size_t size)
466 {
467
468 /*
469 * ensure that we consider this device to be safe for dumping,
470 * and that the device is configured.
471 */
472 if (!DKFF_READYFORDUMP(dksc->sc_flags))
473 return ENXIO;
474
475 /* ensure that we are not already dumping */
476 if (dk_dumping)
477 return EFAULT;
478 dk_dumping = 1;
479
480 /* XXX: unimplemented */
481
482 dk_dumping = 0;
483
484 /* XXX: actually for now, we are going to leave this alone */
485 return ENXIO;
486 }
487
488 /* ARGSUSED */
489 void
490 dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
491 struct disklabel *lp)
492 {
493 struct dk_geom *pdg = &dksc->sc_geom;
494
495 memset(lp, 0, sizeof(*lp));
496
497 lp->d_secperunit = dksc->sc_size;
498 lp->d_secsize = pdg->pdg_secsize;
499 lp->d_nsectors = pdg->pdg_nsectors;
500 lp->d_ntracks = pdg->pdg_ntracks;
501 lp->d_ncylinders = pdg->pdg_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 = dksc->sc_size;
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 partition *pp;
528 int i;
529 const char *errstring;
530
531 memset(clp, 0x0, sizeof(*clp));
532 dk_getdefaultlabel(di, dksc, lp);
533 errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
534 dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
535 if (errstring) {
536 dk_makedisklabel(di, dksc);
537 if (dksc->sc_flags & DKF_WARNLABEL)
538 printf("%s: %s\n", dksc->sc_xname, errstring);
539 return;
540 }
541
542 if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
543 return;
544
545 /* Sanity check */
546 if (lp->d_secperunit != dksc->sc_size)
547 printf("WARNING: %s: total sector size in disklabel (%d) "
548 "!= the size of %s (%lu)\n", dksc->sc_xname,
549 lp->d_secperunit, di->di_dkname, (u_long)dksc->sc_size);
550
551 for (i=0; i < lp->d_npartitions; i++) {
552 pp = &lp->d_partitions[i];
553 if (pp->p_offset + pp->p_size > dksc->sc_size)
554 printf("WARNING: %s: end of partition `%c' exceeds "
555 "the size of %s (%lu)\n", dksc->sc_xname,
556 'a' + i, di->di_dkname, (u_long)dksc->sc_size);
557 }
558 }
559
560 /* ARGSUSED */
561 static void
562 dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
563 {
564 struct disklabel *lp = dksc->sc_dkdev.dk_label;
565
566 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
567 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
568 lp->d_checksum = dkcksum(lp);
569 }
570
571 /* This function is taken from ccd.c:1.76 --rcd */
572
573 /*
574 * XXX this function looks too generic for dksubr.c, shouldn't we
575 * put it somewhere better?
576 */
577
578 /*
579 * Lookup the provided name in the filesystem. If the file exists,
580 * is a valid block device, and isn't being used by anyone else,
581 * set *vpp to the file's vnode.
582 */
583 int
584 dk_lookup(path, p, vpp)
585 const char *path;
586 struct proc *p;
587 struct vnode **vpp; /* result */
588 {
589 struct nameidata nd;
590 struct vnode *vp;
591 struct vattr va;
592 int error;
593
594 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
595 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
596 DPRINTF((DKDB_FOLLOW|DKDB_INIT),
597 ("dk_lookup: vn_open error = %d\n", error));
598 return (error);
599 }
600 vp = nd.ni_vp;
601
602 if (vp->v_usecount > 1) {
603 VOP_UNLOCK(vp, 0);
604 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
605 return (EBUSY);
606 }
607
608 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
609 DPRINTF((DKDB_FOLLOW|DKDB_INIT),
610 ("dk_lookup: getattr error = %d\n", error));
611 VOP_UNLOCK(vp, 0);
612 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
613 return (error);
614 }
615
616 /* XXX: eventually we should handle VREG, too. */
617 if (va.va_type != VBLK) {
618 VOP_UNLOCK(vp, 0);
619 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
620 return (ENOTBLK);
621 }
622
623 IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", vp));
624
625 VOP_UNLOCK(vp, 0);
626 *vpp = vp;
627 return (0);
628 }
629