xref: /netbsd-src/usr.sbin/mdconfig/mdconfig.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: mdconfig.c,v 1.6 2018/01/23 21:06:25 sevan 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: mdconfig.c,v 1.6 2018/01/23 21:06:25 sevan Exp $");
31 #endif
32 
33 /*
34  * This program exists for the sole purpose of providing
35  * user-space memory for the new memory-disk driver (md).
36  * The job done by this is similar to mount_mfs.
37  * (But this design allows any filesystem format!)
38  */
39 
40 #include <sys/types.h>
41 #include <sys/ioctl.h>
42 #include <sys/mman.h>
43 #include <sys/param.h>
44 
45 #include <dev/md.h>
46 
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 
51 int
52 main(int argc, char *argv[])
53 {
54 	struct md_conf md;
55 	size_t nblks;
56 	int fd;
57 
58 	if (argc <= 2) {
59 		fprintf(stderr, "usage: mdconfig <device> <%d-byte-blocks>\n",
60 				DEV_BSIZE);
61 		exit(1);
62 	}
63 
64 	nblks = (size_t)strtoul(argv[2], NULL, 0);
65 	if (nblks == 0) {
66 		fprintf(stderr, "invalid number of blocks\n");
67 		exit(1);
68 	}
69 	md.md_size = nblks << DEV_BSHIFT;
70 
71 	fd = open(argv[1], O_RDWR, 0);
72 	if (fd < 0) {
73 		perror(argv[1]);
74 		exit(1);
75 	}
76 
77 	md.md_addr = mmap(NULL, md.md_size,
78 				PROT_READ | PROT_WRITE,
79 				MAP_ANON | MAP_PRIVATE,
80 				-1, 0);
81 	if (md.md_addr == MAP_FAILED) {
82 		perror("mmap");
83 		exit(1);
84 	}
85 
86 	/* Become server! */
87 	md.md_type = MD_UMEM_SERVER;
88 	if (ioctl(fd, MD_SETCONF, &md)) {
89 		perror("ioctl");
90 		exit(1);
91 	}
92 
93 	exit(0);
94 }
95