xref: /netbsd-src/sys/dev/dksubr.c (revision 6d322f2f4598f0d8a138f10ea648ec4fabe41f8b)
1 /* $NetBSD: dksubr.c,v 1.49 2013/12/28 19:25:07 pgoyette 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.49 2013/12/28 19:25:07 pgoyette 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 	dk_start(di, dksc);
228 	splx(s);
229 	return;
230 }
231 
232 void
233 dk_start(struct dk_intf *di, struct dk_softc *dksc)
234 {
235 	struct	buf *bp;
236 
237 	DPRINTF_FOLLOW(("dk_start(%s, %p)\n", di->di_dkname, dksc));
238 
239 	/* Process the work queue */
240 	while ((bp = bufq_get(dksc->sc_bufq)) != NULL) {
241 		if (di->di_diskstart(dksc, bp) != 0) {
242 			bufq_put(dksc->sc_bufq, bp);
243 			break;
244 		}
245 	}
246 }
247 
248 void
249 dk_iodone(struct dk_intf *di, struct dk_softc *dksc)
250 {
251 
252 	DPRINTF_FOLLOW(("dk_iodone(%s, %p)\n", di->di_dkname, dksc));
253 
254 	/* We kick the queue in case we are able to get more work done */
255 	dk_start(di, dksc);
256 }
257 
258 int
259 dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
260 {
261 	struct	disklabel *lp;
262 	int	is_open;
263 	int	part;
264 	int	size;
265 
266 	if ((dksc->sc_flags & DKF_INITED) == 0)
267 		return -1;
268 
269 	part = DISKPART(dev);
270 	is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
271 
272 	if (!is_open && di->di_open(dev, 0, S_IFBLK, curlwp))
273 		return -1;
274 
275 	lp = dksc->sc_dkdev.dk_label;
276 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
277 		size = -1;
278 	else
279 		size = lp->d_partitions[part].p_size *
280 		    (lp->d_secsize / DEV_BSIZE);
281 
282 	if (!is_open && di->di_close(dev, 0, S_IFBLK, curlwp))
283 		return 1;
284 
285 	return size;
286 }
287 
288 int
289 dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
290 	    u_long cmd, void *data, int flag, struct lwp *l)
291 {
292 	struct	disklabel *lp;
293 	struct	disk *dk;
294 #ifdef __HAVE_OLD_DISKLABEL
295 	struct	disklabel newlabel;
296 #endif
297 	int	error = 0;
298 
299 	DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%"PRIx64", 0x%lx)\n",
300 	    di->di_dkname, dksc, dev, cmd));
301 
302 	/* ensure that the pseudo disk is open for writes for these commands */
303 	switch (cmd) {
304 	case DIOCSDINFO:
305 	case DIOCWDINFO:
306 #ifdef __HAVE_OLD_DISKLABEL
307 	case ODIOCSDINFO:
308 	case ODIOCWDINFO:
309 #endif
310 	case DIOCWLABEL:
311 	case DIOCAWEDGE:
312 	case DIOCDWEDGE:
313 		if ((flag & FWRITE) == 0)
314 			return EBADF;
315 	}
316 
317 	/* ensure that the pseudo-disk is initialized for these */
318 	switch (cmd) {
319 #ifdef DIOCGSECTORSIZE
320 	case DIOCGSECTORSIZE:
321 	case DIOCGMEDIASIZE:
322 #endif
323 	case DIOCGDINFO:
324 	case DIOCSDINFO:
325 	case DIOCWDINFO:
326 	case DIOCGPART:
327 	case DIOCWLABEL:
328 	case DIOCGDEFLABEL:
329 	case DIOCAWEDGE:
330 	case DIOCDWEDGE:
331 	case DIOCLWEDGES:
332 	case DIOCCACHESYNC:
333 #ifdef __HAVE_OLD_DISKLABEL
334 	case ODIOCGDINFO:
335 	case ODIOCSDINFO:
336 	case ODIOCWDINFO:
337 	case ODIOCGDEFLABEL:
338 #endif
339 		if ((dksc->sc_flags & DKF_INITED) == 0)
340 			return ENXIO;
341 	}
342 
343 	switch (cmd) {
344 #ifdef DIOCGSECTORSIZE
345 	case DIOCGSECTORSIZE:
346 		*(u_int *)data = dksc->sc_dkdev.dk_geom.dg_secsize;
347 		return 0;
348 	case DIOCGMEDIASIZE:
349 		*(off_t *)data =
350 		    (off_t)dksc->sc_dkdev.dk_geom.dg_secsize *
351 		    dksc->sc_dkdev.dk_geom.dg_nsectors;
352 		return 0;
353 #endif
354 
355 	case DIOCGDINFO:
356 		*(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
357 		break;
358 
359 #ifdef __HAVE_OLD_DISKLABEL
360 	case ODIOCGDINFO:
361 		newlabel = *(dksc->sc_dkdev.dk_label);
362 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
363 			return ENOTTY;
364 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
365 		break;
366 #endif
367 
368 	case DIOCGPART:
369 		((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
370 		((struct partinfo *)data)->part =
371 		    &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
372 		break;
373 
374 	case DIOCWDINFO:
375 	case DIOCSDINFO:
376 #ifdef __HAVE_OLD_DISKLABEL
377 	case ODIOCWDINFO:
378 	case ODIOCSDINFO:
379 #endif
380 #ifdef __HAVE_OLD_DISKLABEL
381 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
382 			memset(&newlabel, 0, sizeof newlabel);
383 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
384 			lp = &newlabel;
385 		} else
386 #endif
387 		lp = (struct disklabel *)data;
388 
389 		dk = &dksc->sc_dkdev;
390 		mutex_enter(&dk->dk_openlock);
391 		dksc->sc_flags |= DKF_LABELLING;
392 
393 		error = setdisklabel(dksc->sc_dkdev.dk_label,
394 		    lp, 0, dksc->sc_dkdev.dk_cpulabel);
395 		if (error == 0) {
396 			if (cmd == DIOCWDINFO
397 #ifdef __HAVE_OLD_DISKLABEL
398 			    || cmd == ODIOCWDINFO
399 #endif
400 			   )
401 				error = writedisklabel(DKLABELDEV(dev),
402 				    di->di_strategy, dksc->sc_dkdev.dk_label,
403 				    dksc->sc_dkdev.dk_cpulabel);
404 		}
405 
406 		dksc->sc_flags &= ~DKF_LABELLING;
407 		mutex_exit(&dk->dk_openlock);
408 		break;
409 
410 	case DIOCWLABEL:
411 		if (*(int *)data != 0)
412 			dksc->sc_flags |= DKF_WLABEL;
413 		else
414 			dksc->sc_flags &= ~DKF_WLABEL;
415 		break;
416 
417 	case DIOCGDEFLABEL:
418 		dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
419 		break;
420 
421 #ifdef __HAVE_OLD_DISKLABEL
422 	case ODIOCGDEFLABEL:
423 		dk_getdefaultlabel(di, dksc, &newlabel);
424 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
425 			return ENOTTY;
426 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
427 		break;
428 #endif
429 
430 	case DIOCAWEDGE:
431 	    {
432 	    	struct dkwedge_info *dkw = (void *)data;
433 
434 		if ((flag & FWRITE) == 0)
435 			return (EBADF);
436 
437 		/* If the ioctl happens here, the parent is us. */
438 		strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
439 		return (dkwedge_add(dkw));
440 	    }
441 
442 	case DIOCDWEDGE:
443 	    {
444 	    	struct dkwedge_info *dkw = (void *)data;
445 
446 		if ((flag & FWRITE) == 0)
447 			return (EBADF);
448 
449 		/* If the ioctl happens here, the parent is us. */
450 		strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
451 		return (dkwedge_del(dkw));
452 	    }
453 
454 	case DIOCLWEDGES:
455 	    {
456 	    	struct dkwedge_list *dkwl = (void *)data;
457 
458 		return (dkwedge_list(&dksc->sc_dkdev, dkwl, l));
459 	    }
460 
461 	case DIOCGSTRATEGY:
462 	    {
463 		struct disk_strategy *dks = (void *)data;
464 		int s;
465 
466 		s = splbio();
467 		strlcpy(dks->dks_name, bufq_getstrategyname(dksc->sc_bufq),
468 		    sizeof(dks->dks_name));
469 		splx(s);
470 		dks->dks_paramlen = 0;
471 
472 		return 0;
473 	    }
474 
475 	case DIOCSSTRATEGY:
476 	    {
477 		struct disk_strategy *dks = (void *)data;
478 		struct bufq_state *new;
479 		struct bufq_state *old;
480 		int s;
481 
482 		if ((flag & FWRITE) == 0) {
483 			return EBADF;
484 		}
485 		if (dks->dks_param != NULL) {
486 			return EINVAL;
487 		}
488 		dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
489 		error = bufq_alloc(&new, dks->dks_name,
490 		    BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
491 		if (error) {
492 			return error;
493 		}
494 		s = splbio();
495 		old = dksc->sc_bufq;
496 		bufq_move(new, old);
497 		dksc->sc_bufq = new;
498 		splx(s);
499 		bufq_free(old);
500 
501 		return 0;
502 	    }
503 
504 	default:
505 		error = ENOTTY;
506 	}
507 
508 	return error;
509 }
510 
511 /*
512  * dk_dump dumps all of physical memory into the partition specified.
513  * This requires substantially more framework than {s,w}ddump, and hence
514  * is probably much more fragile.
515  *
516  * XXX: we currently do not implement this.
517  */
518 
519 #define DKF_READYFORDUMP	(DKF_INITED|DKF_TAKEDUMP)
520 #define DKFF_READYFORDUMP(x)	(((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
521 static volatile int	dk_dumping = 0;
522 
523 /* ARGSUSED */
524 int
525 dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
526     daddr_t blkno, void *va, size_t size)
527 {
528 
529 	/*
530 	 * ensure that we consider this device to be safe for dumping,
531 	 * and that the device is configured.
532 	 */
533 	if (!DKFF_READYFORDUMP(dksc->sc_flags))
534 		return ENXIO;
535 
536 	/* ensure that we are not already dumping */
537 	if (dk_dumping)
538 		return EFAULT;
539 	dk_dumping = 1;
540 
541 	/* XXX: unimplemented */
542 
543 	dk_dumping = 0;
544 
545 	/* XXX: actually for now, we are going to leave this alone */
546 	return ENXIO;
547 }
548 
549 /* ARGSUSED */
550 void
551 dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
552 		      struct disklabel *lp)
553 {
554 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
555 
556 	memset(lp, 0, sizeof(*lp));
557 
558 	lp->d_secperunit = dg->dg_secperunit;
559 	lp->d_secsize = dg->dg_secsize;
560 	lp->d_nsectors = dg->dg_nsectors;
561 	lp->d_ntracks = dg->dg_ntracks;
562 	lp->d_ncylinders = dg->dg_ncylinders;
563 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
564 
565 	strncpy(lp->d_typename, di->di_dkname, sizeof(lp->d_typename));
566 	lp->d_type = di->di_dtype;
567 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
568 	lp->d_rpm = 3600;
569 	lp->d_interleave = 1;
570 	lp->d_flags = 0;
571 
572 	lp->d_partitions[RAW_PART].p_offset = 0;
573 	lp->d_partitions[RAW_PART].p_size = dg->dg_secperunit;
574 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
575 	lp->d_npartitions = RAW_PART + 1;
576 
577 	lp->d_magic = DISKMAGIC;
578 	lp->d_magic2 = DISKMAGIC;
579 	lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
580 }
581 
582 /* ARGSUSED */
583 void
584 dk_getdisklabel(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
585 {
586 	struct	 disklabel *lp = dksc->sc_dkdev.dk_label;
587 	struct	 cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
588 	struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
589 	struct	 partition *pp;
590 	int	 i;
591 	const char	*errstring;
592 
593 	memset(clp, 0x0, sizeof(*clp));
594 	dk_getdefaultlabel(di, dksc, lp);
595 	errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
596 	    dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
597 	if (errstring) {
598 		dk_makedisklabel(di, dksc);
599 		if (dksc->sc_flags & DKF_WARNLABEL)
600 			printf("%s: %s\n", dksc->sc_xname, errstring);
601 		return;
602 	}
603 
604 	if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
605 		return;
606 
607 	/* Sanity check */
608 	if (lp->d_secperunit != dg->dg_secperunit)
609 		printf("WARNING: %s: total sector size in disklabel (%d) "
610 		    "!= the size of %s (%" PRId64 ")\n", dksc->sc_xname,
611 		    lp->d_secperunit, di->di_dkname, dg->dg_secperunit);
612 
613 	for (i=0; i < lp->d_npartitions; i++) {
614 		pp = &lp->d_partitions[i];
615 		if (pp->p_offset + pp->p_size > dg->dg_secperunit)
616 			printf("WARNING: %s: end of partition `%c' exceeds "
617 			    "the size of %s (%" PRId64 ")\n", dksc->sc_xname,
618 			    'a' + i, di->di_dkname, dg->dg_secperunit);
619 	}
620 }
621 
622 /* ARGSUSED */
623 static void
624 dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
625 {
626 	struct	disklabel *lp = dksc->sc_dkdev.dk_label;
627 
628 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
629 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
630 	lp->d_checksum = dkcksum(lp);
631 }
632 
633 /* This function is taken from ccd.c:1.76  --rcd */
634 
635 /*
636  * XXX this function looks too generic for dksubr.c, shouldn't we
637  *     put it somewhere better?
638  */
639 
640 /*
641  * Lookup the provided name in the filesystem.  If the file exists,
642  * is a valid block device, and isn't being used by anyone else,
643  * set *vpp to the file's vnode.
644  */
645 int
646 dk_lookup(struct pathbuf *pb, struct lwp *l, struct vnode **vpp)
647 {
648 	struct nameidata nd;
649 	struct vnode *vp;
650 	struct vattr va;
651 	int     error;
652 
653 	if (l == NULL)
654 		return ESRCH;	/* Is ESRCH the best choice? */
655 
656 	NDINIT(&nd, LOOKUP, FOLLOW, pb);
657 	if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
658 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
659 		    ("dk_lookup: vn_open error = %d\n", error));
660 		return error;
661 	}
662 
663 	vp = nd.ni_vp;
664 	if ((error = VOP_GETATTR(vp, &va, l->l_cred)) != 0) {
665 		DPRINTF((DKDB_FOLLOW|DKDB_INIT),
666 		    ("dk_lookup: getattr error = %d\n", error));
667 		goto out;
668 	}
669 
670 	/* XXX: eventually we should handle VREG, too. */
671 	if (va.va_type != VBLK) {
672 		error = ENOTBLK;
673 		goto out;
674 	}
675 
676 	IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", vp));
677 
678 	VOP_UNLOCK(vp);
679 	*vpp = vp;
680 	return 0;
681 out:
682 	VOP_UNLOCK(vp);
683 	(void) vn_close(vp, FREAD | FWRITE, l->l_cred);
684 	return error;
685 }
686 
687 MODULE(MODULE_CLASS_MISC, dk_subr, NULL);
688 
689 static int
690 dk_subr_modcmd(modcmd_t cmd, void *arg)
691 {
692 	switch (cmd) {
693 	case MODULE_CMD_INIT:
694 	case MODULE_CMD_FINI:
695 		return 0;
696 	case MODULE_CMD_STAT:
697 	case MODULE_CMD_AUTOUNLOAD:
698 	default:
699 		return ENOTTY;
700 	}
701 }
702