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