Home | History | Annotate | Line # | Download | only in mdconfig
mdconfig.c revision 1.4
      1 /*	$NetBSD: mdconfig.c,v 1.4 2005/11/01 01:42:29 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Gordon W. Ross
      5  * All rights reserved.
      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  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  * 4. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by Gordon W. Ross
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __RCSID("$NetBSD: mdconfig.c,v 1.4 2005/11/01 01:42:29 chs Exp $");
     36 #endif
     37 
     38 /*
     39  * This program exists for the sole purpose of providing
     40  * user-space memory for the new memory-disk driver (md).
     41  * The job done by this is similar to mount_mfs.
     42  * (But this design allows any filesystem format!)
     43  */
     44 
     45 #include <sys/types.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/mman.h>
     48 #include <sys/param.h>
     49 
     50 #include <dev/md.h>
     51 
     52 #include <fcntl.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 
     56 int main __P((int, char **));
     57 
     58 int
     59 main(argc, argv)
     60 	int argc;
     61 	char **argv;
     62 {
     63 	struct md_conf md;
     64 	size_t nblks;
     65 	int fd;
     66 
     67 	if (argc <= 2) {
     68 		fprintf(stderr, "usage: mdconfig <device> <%d-byte-blocks>\n",
     69 				DEV_BSIZE);
     70 		exit(1);
     71 	}
     72 
     73 	nblks = (size_t)strtoul(argv[2], NULL, 0);
     74 	if (nblks == 0) {
     75 		fprintf(stderr, "invalid number of blocks\n");
     76 		exit(1);
     77 	}
     78 	md.md_size = nblks << DEV_BSHIFT;
     79 
     80 	fd = open(argv[1], O_RDWR, 0);
     81 	if (fd < 0) {
     82 		perror(argv[1]);
     83 		exit(1);
     84 	}
     85 
     86 	md.md_addr = mmap(NULL, md.md_size,
     87 				PROT_READ | PROT_WRITE,
     88 				MAP_ANON | MAP_PRIVATE,
     89 				-1, 0);
     90 	if (md.md_addr == MAP_FAILED) {
     91 		perror("mmap");
     92 		exit(1);
     93 	}
     94 
     95 	/* Become server! */
     96 	md.md_type = MD_UMEM_SERVER;
     97 	if (ioctl(fd, MD_SETCONF, &md)) {
     98 		perror("ioctl");
     99 		exit(1);
    100 	}
    101 
    102 	exit(0);
    103 }
    104