ca8629a637a59b2b9ed35504618d43fccf1121f1
[dragonfly.git] / sys / kern / subr_disk.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * ----------------------------------------------------------------------------
35  * "THE BEER-WARE LICENSE" (Revision 42):
36  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
37  * can do whatever you want with this stuff. If we meet some day, and you think
38  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
39  * ----------------------------------------------------------------------------
40  *
41  * Copyright (c) 1982, 1986, 1988, 1993
42  *      The Regents of the University of California.  All rights reserved.
43  * (c) UNIX System Laboratories, Inc.
44  * All or some portions of this file are derived from material licensed
45  * to the University of California by American Telephone and Telegraph
46  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47  * the permission of UNIX System Laboratories, Inc.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  *    notice, this list of conditions and the following disclaimer.
54  * 2. Redistributions in binary form must reproduce the above copyright
55  *    notice, this list of conditions and the following disclaimer in the
56  *    documentation and/or other materials provided with the distribution.
57  * 3. All advertising materials mentioning features or use of this software
58  *    must display the following acknowledgement:
59  *      This product includes software developed by the University of
60  *      California, Berkeley and its contributors.
61  * 4. Neither the name of the University nor the names of its contributors
62  *    may be used to endorse or promote products derived from this software
63  *    without specific prior written permission.
64  *
65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75  * SUCH DAMAGE.
76  *
77  *      @(#)ufs_disksubr.c      8.5 (Berkeley) 1/21/94
78  * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
79  * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
80  * $DragonFly: src/sys/kern/subr_disk.c,v 1.40 2008/06/05 18:06:32 swildner Exp $
81  */
82
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/proc.h>
87 #include <sys/sysctl.h>
88 #include <sys/buf.h>
89 #include <sys/conf.h>
90 #include <sys/diskslice.h>
91 #include <sys/disk.h>
92 #include <sys/malloc.h>
93 #include <sys/sysctl.h>
94 #include <machine/md_var.h>
95 #include <sys/ctype.h>
96 #include <sys/syslog.h>
97 #include <sys/device.h>
98 #include <sys/msgport.h>
99 #include <sys/msgport2.h>
100 #include <sys/buf2.h>
101
102 static MALLOC_DEFINE(M_DISK, "disk", "disk data");
103
104 static d_open_t diskopen;
105 static d_close_t diskclose; 
106 static d_ioctl_t diskioctl;
107 static d_strategy_t diskstrategy;
108 static d_psize_t diskpsize;
109 static d_clone_t diskclone;
110 static d_dump_t diskdump;
111
112 static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist);
113
114 static struct dev_ops disk_ops = {
115         { "disk", 0, D_DISK },
116         .d_open = diskopen,
117         .d_close = diskclose,
118         .d_read = physread,
119         .d_write = physwrite,
120         .d_ioctl = diskioctl,
121         .d_strategy = diskstrategy,
122         .d_dump = diskdump,
123         .d_psize = diskpsize,
124         .d_clone = diskclone
125 };
126
127 /*
128  * Create a raw device for the dev_ops template (which is returned).  Also
129  * create a slice and unit managed disk and overload the user visible
130  * device space with it.
131  *
132  * NOTE: The returned raw device is NOT a slice and unit managed device.
133  * It is an actual raw device representing the raw disk as specified by
134  * the passed dev_ops.  The disk layer not only returns such a raw device,
135  * it also uses it internally when passing (modified) commands through.
136  */
137 cdev_t
138 disk_create(int unit, struct disk *dp, struct dev_ops *raw_ops)
139 {
140         cdev_t rawdev;
141         struct dev_ops *dev_ops;
142
143         /*
144          * Create the raw backing device
145          */
146         compile_dev_ops(raw_ops);
147         rawdev = make_dev(raw_ops, dkmakewholedisk(unit),
148                             UID_ROOT, GID_OPERATOR, 0640,
149                             "%s%d", raw_ops->head.name, unit);
150
151         bzero(dp, sizeof(*dp));
152
153         /*
154          * We install a custom cdevsw rather then the passed cdevsw,
155          * and save our disk structure in d_data so we can get at it easily
156          * without any complex cloning code.
157          */
158         dev_ops = dev_ops_add_override(rawdev, &disk_ops,
159                                        dkunitmask(), dkmakeunit(unit));
160         dev_ops->head.data = dp;
161
162         dp->d_rawdev = rawdev;
163         dp->d_raw_ops = raw_ops;
164         dp->d_dev_ops = dev_ops;
165         dp->d_cdev = make_dev(dev_ops, 
166                             dkmakewholedisk(unit),
167                             UID_ROOT, GID_OPERATOR, 0640,
168                             "%s%d", dev_ops->head.name, unit);
169
170         LIST_INSERT_HEAD(&disklist, dp, d_list);
171         return (dp->d_rawdev);
172 }
173
174 /*
175  * Disk drivers must call this routine when media parameters are available
176  * or have changed.
177  */
178 void
179 disk_setdiskinfo(struct disk *disk, struct disk_info *info)
180 {
181         bcopy(info, &disk->d_info, sizeof(disk->d_info));
182         info = &disk->d_info;
183
184         KKASSERT(info->d_media_size == 0 || info->d_media_blksize == 0);
185         if (info->d_media_size == 0 && info->d_media_blocks) {
186                 info->d_media_size = (u_int64_t)info->d_media_blocks * 
187                                      info->d_media_blksize;
188         } else if (info->d_media_size && info->d_media_blocks == 0 && 
189                    info->d_media_blksize) {
190                 info->d_media_blocks = info->d_media_size / 
191                                        info->d_media_blksize;
192         }
193 }
194
195 /*
196  * This routine is called when an adapter detaches.  The higher level
197  * managed disk device is destroyed while the lower level raw device is
198  * released.
199  */
200 void
201 disk_destroy(struct disk *disk)
202 {
203         u_int match;
204
205         if (disk->d_dev_ops) {
206             match = dkmakeunit(dkunit(disk->d_cdev));
207             dev_ops_remove_override(disk->d_dev_ops, dkunitmask(), match);
208             LIST_REMOVE(disk, d_list);
209         }
210         if (disk->d_raw_ops) {
211             match = dkmakeunit(dkunit(disk->d_rawdev));
212             destroy_all_devs(disk->d_raw_ops, dkunitmask(), match);
213         }
214         bzero(disk, sizeof(*disk));
215 }
216
217 int
218 disk_dumpcheck(cdev_t dev, u_int64_t *count, u_int64_t *blkno, u_int *secsize)
219 {
220         struct partinfo pinfo;
221         int error;
222
223         bzero(&pinfo, sizeof(pinfo));
224         error = dev_dioctl(dev, DIOCGPART, (void *)&pinfo, 0, proc0.p_ucred);
225         if (error)
226                 return (error);
227         if (pinfo.media_blksize == 0)
228                 return (ENXIO);
229         *count = (u_int64_t)Maxmem * PAGE_SIZE / pinfo.media_blksize;
230         if (dumplo64 < pinfo.reserved_blocks ||
231             dumplo64 + *count > pinfo.media_blocks) {
232                 return (ENOSPC);
233         }
234         *blkno = dumplo64 + pinfo.media_offset / pinfo.media_blksize;
235         *secsize = pinfo.media_blksize;
236         return (0);
237 }
238
239 void 
240 disk_invalidate (struct disk *disk)
241 {
242         if (disk->d_slice)
243                 dsgone(&disk->d_slice);
244 }
245
246 struct disk *
247 disk_enumerate(struct disk *disk)
248 {
249         if (!disk)
250                 return (LIST_FIRST(&disklist));
251         else
252                 return (LIST_NEXT(disk, d_list));
253 }
254
255 static 
256 int
257 sysctl_disks(SYSCTL_HANDLER_ARGS)
258 {
259         struct disk *disk;
260         int error, first;
261
262         disk = NULL;
263         first = 1;
264
265         while ((disk = disk_enumerate(disk))) {
266                 if (!first) {
267                         error = SYSCTL_OUT(req, " ", 1);
268                         if (error)
269                                 return error;
270                 } else {
271                         first = 0;
272                 }
273                 error = SYSCTL_OUT(req, disk->d_rawdev->si_name,
274                                    strlen(disk->d_rawdev->si_name));
275                 if (error)
276                         return error;
277         }
278         error = SYSCTL_OUT(req, "", 1);
279         return error;
280 }
281  
282 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
283     sysctl_disks, "A", "names of available disks");
284
285 /*
286  * Open a disk device or partition.
287  */
288 static
289 int
290 diskopen(struct dev_open_args *ap)
291 {
292         cdev_t dev = ap->a_head.a_dev;
293         struct disk *dp;
294         int error;
295
296         /*
297          * dp can't be NULL here XXX.
298          */
299         dp = dev->si_disk;
300         if (dp == NULL)
301                 return (ENXIO);
302         error = 0;
303
304         /*
305          * Deal with open races
306          */
307         while (dp->d_flags & DISKFLAG_LOCK) {
308                 dp->d_flags |= DISKFLAG_WANTED;
309                 error = tsleep(dp, PCATCH, "diskopen", hz);
310                 if (error)
311                         return (error);
312         }
313         dp->d_flags |= DISKFLAG_LOCK;
314
315         /*
316          * Open the underlying raw device.
317          */
318         if (!dsisopen(dp->d_slice)) {
319 #if 0
320                 if (!pdev->si_iosize_max)
321                         pdev->si_iosize_max = dev->si_iosize_max;
322 #endif
323                 error = dev_dopen(dp->d_rawdev, ap->a_oflags,
324                                   ap->a_devtype, ap->a_cred);
325         }
326
327         /*
328          * Inherit properties from the underlying device now that it is
329          * open.
330          */
331         dev_dclone(dev);
332
333         if (error)
334                 goto out;
335         
336         error = dsopen(dev, ap->a_devtype, dp->d_info.d_dsflags,
337                        &dp->d_slice, &dp->d_info);
338
339         if (!dsisopen(dp->d_slice)) 
340                 dev_dclose(dp->d_rawdev, ap->a_oflags, ap->a_devtype);
341 out:    
342         dp->d_flags &= ~DISKFLAG_LOCK;
343         if (dp->d_flags & DISKFLAG_WANTED) {
344                 dp->d_flags &= ~DISKFLAG_WANTED;
345                 wakeup(dp);
346         }
347         
348         return(error);
349 }
350
351 /*
352  * Close a disk device or partition
353  */
354 static
355 int
356 diskclose(struct dev_close_args *ap)
357 {
358         cdev_t dev = ap->a_head.a_dev;
359         struct disk *dp;
360         int error;
361
362         error = 0;
363         dp = dev->si_disk;
364
365         dsclose(dev, ap->a_devtype, dp->d_slice);
366         if (!dsisopen(dp->d_slice))
367                 error = dev_dclose(dp->d_rawdev, ap->a_fflag, ap->a_devtype);
368         return (error);
369 }
370
371 /*
372  * First execute the ioctl on the disk device, and if it isn't supported 
373  * try running it on the backing device.
374  */
375 static
376 int
377 diskioctl(struct dev_ioctl_args *ap)
378 {
379         cdev_t dev = ap->a_head.a_dev;
380         struct disk *dp;
381         int error;
382
383         dp = dev->si_disk;
384         if (dp == NULL)
385                 return (ENXIO);
386         error = dsioctl(dev, ap->a_cmd, ap->a_data, ap->a_fflag,
387                         &dp->d_slice, &dp->d_info);
388         if (error == ENOIOCTL) {
389                 error = dev_dioctl(dp->d_rawdev, ap->a_cmd, ap->a_data,
390                                    ap->a_fflag, ap->a_cred);
391         }
392         return (error);
393 }
394
395 /*
396  * Execute strategy routine
397  */
398 static
399 int
400 diskstrategy(struct dev_strategy_args *ap)
401 {
402         cdev_t dev = ap->a_head.a_dev;
403         struct bio *bio = ap->a_bio;
404         struct bio *nbio;
405         struct disk *dp;
406
407         dp = dev->si_disk;
408
409         if (dp == NULL) {
410                 bio->bio_buf->b_error = ENXIO;
411                 bio->bio_buf->b_flags |= B_ERROR;
412                 biodone(bio);
413                 return(0);
414         }
415         KKASSERT(dev->si_disk == dp);
416
417         /*
418          * The dscheck() function will also transform the slice relative
419          * block number i.e. bio->bio_offset into a block number that can be
420          * passed directly to the underlying raw device.  If dscheck()
421          * returns NULL it will have handled the bio for us (e.g. EOF
422          * or error due to being beyond the device size).
423          */
424         if ((nbio = dscheck(dev, bio, dp->d_slice)) != NULL)
425                 dev_dstrategy(dp->d_rawdev, nbio);
426         else
427                 biodone(bio);
428         return(0);
429 }
430
431 /*
432  * Return the partition size in ?blocks?
433  */
434 static
435 int
436 diskpsize(struct dev_psize_args *ap)
437 {
438         cdev_t dev = ap->a_head.a_dev;
439         struct disk *dp;
440
441         dp = dev->si_disk;
442         if (dp == NULL)
443                 return(ENODEV);
444         ap->a_result = dssize(dev, &dp->d_slice);
445         return(0);
446 }
447
448 /*
449  * When new device entries are instantiated, make sure they inherit our
450  * si_disk structure and block and iosize limits from the raw device.
451  *
452  * This routine is always called synchronously in the context of the 
453  * client.
454  *
455  * XXX The various io and block size constraints are not always initialized
456  * properly by devices.
457  */
458 static
459 int
460 diskclone(struct dev_clone_args *ap)
461 {
462         cdev_t dev = ap->a_head.a_dev;
463         struct disk *dp;
464
465         dp = dev->si_ops->head.data;
466         KKASSERT(dp != NULL);
467         dev->si_disk = dp;
468         dev->si_iosize_max = dp->d_rawdev->si_iosize_max;
469         dev->si_bsize_phys = dp->d_rawdev->si_bsize_phys;
470         dev->si_bsize_best = dp->d_rawdev->si_bsize_best;
471         return(0);
472 }
473
474 int
475 diskdump(struct dev_dump_args *ap)
476 {
477         cdev_t dev = ap->a_head.a_dev;
478         struct disk *dp = dev->si_ops->head.data;
479         int error;
480
481         error = disk_dumpcheck(dev, &ap->a_count, &ap->a_blkno, &ap->a_secsize);
482         if (error == 0) {
483                 ap->a_head.a_dev = dp->d_rawdev;
484                 error = dev_doperate(&ap->a_head);
485         }
486
487         return(error);
488 }
489
490
491 SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD, 
492     0, sizeof(struct diskslices), "sizeof(struct diskslices)");
493
494 SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD, 
495     0, sizeof(struct disk), "sizeof(struct disk)");
496
497
498 /*
499  * Seek sort for disks.
500  *
501  * The bio_queue keep two queues, sorted in ascending block order.  The first
502  * queue holds those requests which are positioned after the current block
503  * (in the first request); the second, which starts at queue->switch_point,
504  * holds requests which came in after their block number was passed.  Thus
505  * we implement a one way scan, retracting after reaching the end of the drive
506  * to the first request on the second queue, at which time it becomes the
507  * first queue.
508  *
509  * A one-way scan is natural because of the way UNIX read-ahead blocks are
510  * allocated.
511  */
512 void
513 bioqdisksort(struct bio_queue_head *bioq, struct bio *bio)
514 {
515         struct bio *bq;
516         struct bio *bn;
517         struct bio *be;
518         
519         be = TAILQ_LAST(&bioq->queue, bio_queue);
520         /*
521          * If the queue is empty or we are an
522          * ordered transaction, then it's easy.
523          */
524         if ((bq = bioq_first(bioq)) == NULL || 
525             (bio->bio_buf->b_flags & B_ORDERED) != 0) {
526                 bioq_insert_tail(bioq, bio);
527                 return;
528         } else if (bioq->insert_point != NULL) {
529
530                 /*
531                  * A certain portion of the list is
532                  * "locked" to preserve ordering, so
533                  * we can only insert after the insert
534                  * point.
535                  */
536                 bq = bioq->insert_point;
537         } else {
538
539                 /*
540                  * If we lie before the last removed (currently active)
541                  * request, and are not inserting ourselves into the
542                  * "locked" portion of the list, then we must add ourselves
543                  * to the second request list.
544                  */
545                 if (bio->bio_offset < bioq->last_offset) {
546                         bq = bioq->switch_point;
547                         /*
548                          * If we are starting a new secondary list,
549                          * then it's easy.
550                          */
551                         if (bq == NULL) {
552                                 bioq->switch_point = bio;
553                                 bioq_insert_tail(bioq, bio);
554                                 return;
555                         }
556                         /*
557                          * If we lie ahead of the current switch point,
558                          * insert us before the switch point and move
559                          * the switch point.
560                          */
561                         if (bio->bio_offset < bq->bio_offset) {
562                                 bioq->switch_point = bio;
563                                 TAILQ_INSERT_BEFORE(bq, bio, bio_act);
564                                 return;
565                         }
566                 } else {
567                         if (bioq->switch_point != NULL)
568                                 be = TAILQ_PREV(bioq->switch_point,
569                                                 bio_queue, bio_act);
570                         /*
571                          * If we lie between last_offset and bq,
572                          * insert before bq.
573                          */
574                         if (bio->bio_offset < bq->bio_offset) {
575                                 TAILQ_INSERT_BEFORE(bq, bio, bio_act);
576                                 return;
577                         }
578                 }
579         }
580
581         /*
582          * Request is at/after our current position in the list.
583          * Optimize for sequential I/O by seeing if we go at the tail.
584          */
585         if (bio->bio_offset > be->bio_offset) {
586                 TAILQ_INSERT_AFTER(&bioq->queue, be, bio, bio_act);
587                 return;
588         }
589
590         /* Otherwise, insertion sort */
591         while ((bn = TAILQ_NEXT(bq, bio_act)) != NULL) {
592                 
593                 /*
594                  * We want to go after the current request if it is the end
595                  * of the first request list, or if the next request is a
596                  * larger cylinder than our request.
597                  */
598                 if (bn == bioq->switch_point
599                  || bio->bio_offset < bn->bio_offset)
600                         break;
601                 bq = bn;
602         }
603         TAILQ_INSERT_AFTER(&bioq->queue, bq, bio, bio_act);
604 }
605
606 /*
607  * Disk error is the preface to plaintive error messages
608  * about failing disk transfers.  It prints messages of the form
609
610 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
611
612  * if the offset of the error in the transfer and a disk label
613  * are both available.  blkdone should be -1 if the position of the error
614  * is unknown; the disklabel pointer may be null from drivers that have not
615  * been converted to use them.  The message is printed with kprintf
616  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
617  * The message should be completed (with at least a newline) with kprintf
618  * or log(-1, ...), respectively.  There is no trailing space.
619  */
620 void
621 diskerr(struct bio *bio, cdev_t dev, const char *what, int pri, int donecnt)
622 {
623         struct buf *bp = bio->bio_buf;
624         int unit = dkunit(dev);
625         int slice = dkslice(dev);
626         int part = dkpart(dev);
627         char partname[2];
628         char *sname;
629         const char *term;
630
631         switch(bp->b_cmd) {
632         case BUF_CMD_READ:
633                 term = "read";
634                 break;
635         case BUF_CMD_WRITE:
636                 term = "write";
637                 break;
638         default:
639                 term = "access";
640                 break;
641         }
642         sname = dsname(dev, unit, slice, part, partname);
643         kprintf("%s%s: %s %sing ", sname, partname, what, term);
644         kprintf("offset %012llx for %d", bio->bio_offset, bp->b_bcount);
645         if (donecnt)
646                 kprintf(" (%d bytes completed)", donecnt);
647 }
648
649 /*
650  * Locate a disk device
651  */
652 cdev_t
653 disk_locate(const char *devname)
654 {
655         struct disk *dp;
656         cdev_t dev;
657         char *ptr;
658         int i;
659         int prefix;
660         int slice;
661         int part;
662
663         /*
664          * Device and unit
665          */
666         for (i = 0; devname[i]; ++i) {
667                 if (devname[i] >= '0' && devname[i] <= '9')
668                         break;
669         }
670         while (devname[i] >= '0' && devname[i] <= '9')
671                 ++i;
672         prefix = i;
673
674         /*
675          * Slice and partition.  s1 starts at slice #2.  s0 is slice #0.
676          * slice #1 is the WHOLE_DISK_SLICE.
677          */
678         if (devname[i] == 's') {
679                 slice = strtol(devname + i + 1, &ptr, 10);
680                 i = (const char *)ptr - devname;
681                 if (slice > 0)
682                         ++slice;
683         } else {
684                 slice = WHOLE_DISK_SLICE;
685         }
686         if (devname[i] >= 'a' && devname[i] <= 'z') {
687                 part = devname[i] - 'a';
688         } else {
689                 part = WHOLE_SLICE_PART;
690         }
691
692         /*
693          * Find the device
694          */
695         LIST_FOREACH(dp, &disklist, d_list) {
696                 dev = dp->d_cdev;
697                 if (strlen(dev->si_name) == prefix &&
698                     strncmp(devname, dev->si_name, prefix) == 0
699                 ) {
700                         return(dkmodpart(dkmodslice(dev, slice), part));
701                 }
702         }
703         return(NULL);
704 }
705