dksubr.c revision 1.10 1 /* $NetBSD: dksubr.c,v 1.10 2003/07/14 15:47:03 lukem 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.10 2003/07/14 15:47:03 lukem 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/vnode.h>
51 #include <sys/fcntl.h>
52 #include <sys/namei.h>
53
54 #include <dev/dkvar.h>
55
56 int dkdebug = 0;
57
58 #ifdef DEBUG
59 #define DKDB_FOLLOW 0x1
60 #define DKDB_INIT 0x2
61 #define DKDB_VNODE 0x4
62
63 #define IFDEBUG(x,y) if (dkdebug & (x)) y
64 #define DPRINTF(x,y) IFDEBUG(x, printf y)
65 #define DPRINTF_FOLLOW(y) DPRINTF(DKDB_FOLLOW, y)
66 #else
67 #define IFDEBUG(x,y)
68 #define DPRINTF(x,y)
69 #define DPRINTF_FOLLOW(y)
70 #endif
71
72 #define DKLABELDEV(dev) \
73 (MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
74
75 void dk_makedisklabel(struct dk_intf *, struct dk_softc *);
76
77 void
78 dk_sc_init(struct dk_softc *dksc, void *osc, char *xname)
79 {
80
81 memset(dksc, 0x0, sizeof(*dksc));
82 dksc->sc_osc = osc;
83 strncpy(dksc->sc_xname, xname, DK_XNAME_SIZE);
84 dksc->sc_dkdev.dk_name = dksc->sc_xname;
85 lockinit(&dksc->sc_lock, PRIBIO, "dklk", 0, 0);
86 }
87
88 /* ARGSUSED */
89 int
90 dk_open(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
91 int flags, int fmt, struct proc *p)
92 {
93 struct disklabel *lp = dksc->sc_dkdev.dk_label;
94 int part = DISKPART(dev);
95 int pmask = 1 << part;
96 int ret = 0;
97
98 DPRINTF_FOLLOW(("dk_open(%s, %p, 0x%x, 0x%x)\n",
99 di->di_dkname, dksc, dev, flags));
100
101 if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
102 return ret;
103
104 part = DISKPART(dev);
105 pmask = 1 << part;
106
107 /*
108 * If we're init'ed and there are no other open partitions then
109 * update the in-core disklabel.
110 */
111 if ((dksc->sc_flags & DKF_INITED) && dksc->sc_dkdev.dk_openmask == 0)
112 dk_getdisklabel(di, dksc, dev);
113
114 /* Fail if we can't find the partition. */
115 if ((part != RAW_PART) &&
116 (((dksc->sc_flags & DKF_INITED) == 0) ||
117 ((part >= lp->d_npartitions) ||
118 (lp->d_partitions[part].p_fstype == FS_UNUSED)))) {
119 ret = ENXIO;
120 goto done;
121 }
122
123 /* Mark our unit as open. */
124 switch (fmt) {
125 case S_IFCHR:
126 dksc->sc_dkdev.dk_copenmask |= pmask;
127 break;
128 case S_IFBLK:
129 dksc->sc_dkdev.dk_bopenmask |= pmask;
130 break;
131 }
132
133 dksc->sc_dkdev.dk_openmask =
134 dksc->sc_dkdev.dk_copenmask | dksc->sc_dkdev.dk_bopenmask;
135
136 done:
137 lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
138 return ret;
139 }
140
141 /* ARGSUSED */
142 int
143 dk_close(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
144 int flags, int fmt, struct proc *p)
145 {
146 int part = DISKPART(dev);
147 int pmask = 1 << part;
148 int ret;
149
150 DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%x, 0x%x)\n",
151 di->di_dkname, dksc, dev, flags));
152
153 if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
154 return ret;
155
156 switch (fmt) {
157 case S_IFCHR:
158 dksc->sc_dkdev.dk_copenmask &= ~pmask;
159 break;
160 case S_IFBLK:
161 dksc->sc_dkdev.dk_bopenmask &= ~pmask;
162 break;
163 }
164 dksc->sc_dkdev.dk_openmask =
165 dksc->sc_dkdev.dk_copenmask | dksc->sc_dkdev.dk_bopenmask;
166
167 lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
168 return 0;
169 }
170
171 void
172 dk_strategy(struct dk_intf *di, struct dk_softc *dksc, struct buf *bp)
173 {
174 int s;
175 int wlabel;
176
177 DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
178 di->di_dkname, dksc, bp));
179
180 if (!(dksc->sc_flags & DKF_INITED)) {
181 DPRINTF_FOLLOW(("dk_stragy: not inited\n"));
182 bp->b_error = ENXIO;
183 bp->b_flags |= B_ERROR;
184 biodone(bp);
185 return;
186 }
187
188 /* XXX look for some more errors, c.f. ld.c */
189
190 bp->b_resid = bp->b_bcount;
191
192 /* If there is nothing to do, then we are done */
193 if (bp->b_bcount == 0) {
194 biodone(bp);
195 return;
196 }
197
198 wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
199 if (DISKPART(bp->b_dev) != RAW_PART &&
200 bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
201 biodone(bp);
202 return;
203 }
204
205 /*
206 * Start the unit by calling the start routine
207 * provided by the individual driver.
208 */
209 s = splbio();
210 di->di_diskstart(dksc, bp);
211 splx(s);
212 return;
213 }
214
215 int
216 dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
217 {
218 struct disklabel *lp;
219 int is_open;
220 int part;
221 int size;
222
223 if ((dksc->sc_flags & DKF_INITED) == 0)
224 return -1;
225
226 part = DISKPART(dev);
227 is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
228
229 if (!is_open && di->di_open(dev, 0, S_IFBLK, curproc))
230 return -1;
231
232 lp = dksc->sc_dkdev.dk_label;
233 if (lp->d_partitions[part].p_fstype != FS_SWAP)
234 size = -1;
235 else
236 size = lp->d_partitions[part].p_size *
237 (lp->d_secsize / DEV_BSIZE);
238
239 if (!is_open && di->di_close(dev, 0, S_IFBLK, curproc))
240 return 1;
241
242 return size;
243 }
244
245 int
246 dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
247 u_long cmd, caddr_t data, int flag, struct proc *p)
248 {
249 struct disklabel *lp;
250 #ifdef __HAVE_OLD_DISKLABEL
251 struct disklabel newlabel;
252 #endif
253 int error = 0;
254
255 DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%x, 0x%lx)\n",
256 di->di_dkname, dksc, dev, cmd));
257
258 /* ensure that the pseudo disk is open for writes for these commands */
259 switch (cmd) {
260 case DIOCSDINFO:
261 case DIOCWDINFO:
262 #ifdef __HAVE_OLD_DISKLABEL
263 case ODIOCSDINFO:
264 case ODIOCWDINFO:
265 #endif
266 case DIOCWLABEL:
267 if ((flag & FWRITE) == 0)
268 return EBADF;
269 }
270
271 /* ensure that the pseudo-disk is initialized for these */
272 switch (cmd) {
273 case DIOCGDINFO:
274 case DIOCSDINFO:
275 case DIOCWDINFO:
276 case DIOCGPART:
277 case DIOCWLABEL:
278 case DIOCGDEFLABEL:
279 #ifdef __HAVE_OLD_DISKLABEL
280 case ODIOCGDINFO:
281 case ODIOCSDINFO:
282 case ODIOCWDINFO:
283 case ODIOCGDEFLABEL:
284 #endif
285 if ((dksc->sc_flags & DKF_INITED) == 0)
286 return ENXIO;
287 }
288
289 switch (cmd) {
290 case DIOCGDINFO:
291 *(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
292 break;
293
294 #ifdef __HAVE_OLD_DISKLABEL
295 case ODIOCGDINFO:
296 newlabel = *(dksc->sc_dkdev.dk_label);
297 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
298 return ENOTTY;
299 memcpy(data, &newlabel, sizeof (struct olddisklabel));
300 break;
301 #endif
302
303 case DIOCGPART:
304 ((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
305 ((struct partinfo *)data)->part =
306 &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
307 break;
308
309 case DIOCWDINFO:
310 case DIOCSDINFO:
311 #ifdef __HAVE_OLD_DISKLABEL
312 case ODIOCWDINFO:
313 case ODIOCSDINFO:
314 #endif
315 #ifdef __HAVE_OLD_DISKLABEL
316 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
317 memset(&newlabel, 0, sizeof newlabel);
318 memcpy(&newlabel, data, sizeof (struct olddisklabel));
319 lp = &newlabel;
320 } else
321 #endif
322 lp = (struct disklabel *)data;
323
324 dksc->sc_flags |= DKF_LABELLING;
325
326 error = setdisklabel(dksc->sc_dkdev.dk_label,
327 lp, 0, dksc->sc_dkdev.dk_cpulabel);
328 if (error == 0) {
329 if (cmd == DIOCWDINFO
330 #ifdef __HAVE_OLD_DISKLABEL
331 || cmd == ODIOCWDINFO
332 #endif
333 )
334 error = writedisklabel(DKLABELDEV(dev),
335 di->di_strategy, dksc->sc_dkdev.dk_label,
336 dksc->sc_dkdev.dk_cpulabel);
337 }
338
339 dksc->sc_flags &= ~DKF_LABELLING;
340 break;
341
342 case DIOCWLABEL:
343 if (*(int *)data != 0)
344 dksc->sc_flags |= DKF_WLABEL;
345 else
346 dksc->sc_flags &= ~DKF_WLABEL;
347 break;
348
349 case DIOCGDEFLABEL:
350 dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
351 break;
352
353 #ifdef __HAVE_OLD_DISKLABEL
354 case ODIOCGDEFLABEL:
355 dk_getdefaultlabel(di, dksc, &newlabel);
356 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
357 return ENOTTY;
358 memcpy(data, &newlabel, sizeof (struct olddisklabel));
359 break;
360 #endif
361
362 default:
363 error = ENOTTY;
364 }
365
366 return error;
367 }
368
369 /*
370 * dk_dump dumps all of physical memory into the partition specified.
371 * This requires substantially more framework than {s,w}ddump, and hence
372 * is probably much more fragile.
373 *
374 * XXX: we currently do not implement this.
375 */
376
377 #define DKF_READYFORDUMP (DKF_INITED|DKF_TAKEDUMP)
378 #define DKFF_READYFORDUMP(x) (((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
379 static volatile int dk_dumping = 0;
380
381 /* ARGSUSED */
382 int
383 dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
384 daddr_t blkno, caddr_t va, size_t size)
385 {
386
387 /*
388 * ensure that we consider this device to be safe for dumping,
389 * and that the device is configured.
390 */
391 if (!DKFF_READYFORDUMP(dksc->sc_flags))
392 return ENXIO;
393
394 /* ensure that we are not already dumping */
395 if (dk_dumping)
396 return EFAULT;
397 dk_dumping = 1;
398
399 /* XXX: unimplemented */
400
401 dk_dumping = 0;
402
403 /* XXX: actually for now, we are going to leave this alone */
404 return ENXIO;
405 }
406
407 /* ARGSUSED */
408 void
409 dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
410 struct disklabel *lp)
411 {
412 struct dk_geom *pdg = &dksc->sc_geom;
413
414 memset(lp, 0, sizeof(*lp));
415
416 lp->d_secperunit = dksc->sc_size;
417 lp->d_secsize = pdg->pdg_secsize;
418 lp->d_nsectors = pdg->pdg_nsectors;
419 lp->d_ntracks = pdg->pdg_ntracks;
420 lp->d_ncylinders = pdg->pdg_ncylinders;
421 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
422
423 strncpy(lp->d_typename, di->di_dkname, sizeof(lp->d_typename));
424 lp->d_type = di->di_dtype;
425 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
426 lp->d_rpm = 3600;
427 lp->d_interleave = 1;
428 lp->d_flags = 0;
429
430 lp->d_partitions[RAW_PART].p_offset = 0;
431 lp->d_partitions[RAW_PART].p_size = dksc->sc_size;
432 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
433 lp->d_npartitions = RAW_PART + 1;
434
435 lp->d_magic = DISKMAGIC;
436 lp->d_magic2 = DISKMAGIC;
437 lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
438 }
439
440 /* ARGSUSED */
441 void
442 dk_getdisklabel(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
443 {
444 struct disklabel *lp = dksc->sc_dkdev.dk_label;
445 struct cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
446 struct partition *pp;
447 int i;
448 const char *errstring;
449
450 memset(clp, 0x0, sizeof(*clp));
451 dk_getdefaultlabel(di, dksc, lp);
452 errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
453 dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
454 if (errstring) {
455 dk_makedisklabel(di, dksc);
456 if (dksc->sc_flags & DKF_WARNLABEL)
457 printf("%s: %s\n", dksc->sc_xname, errstring);
458 return;
459 }
460
461 if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
462 return;
463
464 /* Sanity check */
465 if (lp->d_secperunit != dksc->sc_size)
466 printf("WARNING: %s: total sector size in disklabel (%d) "
467 "!= the size of %s (%lu)\n", dksc->sc_xname,
468 lp->d_secperunit, di->di_dkname, (u_long)dksc->sc_size);
469
470 for (i=0; i < lp->d_npartitions; i++) {
471 pp = &lp->d_partitions[i];
472 if (pp->p_offset + pp->p_size > dksc->sc_size)
473 printf("WARNING: %s: end of partition `%c' exceeds "
474 "the size of %s (%lu)\n", dksc->sc_xname,
475 'a' + i, di->di_dkname, (u_long)dksc->sc_size);
476 }
477 }
478
479 /* ARGSUSED */
480 void
481 dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
482 {
483 struct disklabel *lp = dksc->sc_dkdev.dk_label;
484
485 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
486 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
487 lp->d_checksum = dkcksum(lp);
488 }
489
490 /* This function is taken from ccd.c:1.76 --rcd */
491
492 /*
493 * XXX this function looks too generic for dksubr.c, shouldn't we
494 * put it somewhere better?
495 */
496
497 /*
498 * Lookup the provided name in the filesystem. If the file exists,
499 * is a valid block device, and isn't being used by anyone else,
500 * set *vpp to the file's vnode.
501 */
502 int
503 dk_lookup(path, p, vpp)
504 char *path;
505 struct proc *p;
506 struct vnode **vpp; /* result */
507 {
508 struct nameidata nd;
509 struct vnode *vp;
510 struct vattr va;
511 int error;
512
513 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
514 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
515 DPRINTF((DKDB_FOLLOW|DKDB_INIT),
516 ("dk_lookup: vn_open error = %d\n", error));
517 return (error);
518 }
519 vp = nd.ni_vp;
520
521 if (vp->v_usecount > 1) {
522 VOP_UNLOCK(vp, 0);
523 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
524 return (EBUSY);
525 }
526
527 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
528 DPRINTF((DKDB_FOLLOW|DKDB_INIT),
529 ("dk_lookup: getattr error = %d\n", error));
530 VOP_UNLOCK(vp, 0);
531 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
532 return (error);
533 }
534
535 /* XXX: eventually we should handle VREG, too. */
536 if (va.va_type != VBLK) {
537 VOP_UNLOCK(vp, 0);
538 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
539 return (ENOTBLK);
540 }
541
542 IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", vp));
543
544 VOP_UNLOCK(vp, 0);
545 *vpp = vp;
546 return (0);
547 }
548