xref: /netbsd-src/usr.sbin/mdconfig/mdconfig.c (revision 0c510515ef0daa7f74998d42b10c558674acd3ac)
1*0c510515Schristos /*	$NetBSD: mdconfig.c,v 1.7 2020/08/18 19:26:29 christos Exp $	*/
2ea702e59Sgwr 
3ea702e59Sgwr /*
4ea702e59Sgwr  * Copyright (c) 1995 Gordon W. Ross
5ea702e59Sgwr  * All rights reserved.
6ea702e59Sgwr  *
7ea702e59Sgwr  * Redistribution and use in source and binary forms, with or without
8ea702e59Sgwr  * modification, are permitted provided that the following conditions
9ea702e59Sgwr  * are met:
10ea702e59Sgwr  * 1. Redistributions of source code must retain the above copyright
11ea702e59Sgwr  *    notice, this list of conditions and the following disclaimer.
12ea702e59Sgwr  * 2. Redistributions in binary form must reproduce the above copyright
13ea702e59Sgwr  *    notice, this list of conditions and the following disclaimer in the
14ea702e59Sgwr  *    documentation and/or other materials provided with the distribution.
15ea702e59Sgwr  *
16ea702e59Sgwr  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ea702e59Sgwr  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ea702e59Sgwr  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ea702e59Sgwr  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ea702e59Sgwr  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21ea702e59Sgwr  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22ea702e59Sgwr  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23ea702e59Sgwr  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24ea702e59Sgwr  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25ea702e59Sgwr  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ea702e59Sgwr  */
27ea702e59Sgwr 
28fcfec169Slukem #include <sys/cdefs.h>
29fcfec169Slukem #ifndef lint
30*0c510515Schristos __RCSID("$NetBSD: mdconfig.c,v 1.7 2020/08/18 19:26:29 christos Exp $");
31fcfec169Slukem #endif
32fcfec169Slukem 
33ea702e59Sgwr /*
34ea702e59Sgwr  * This program exists for the sole purpose of providing
35fa84b16bSpk  * user-space memory for the new memory-disk driver (md).
36ea702e59Sgwr  * The job done by this is similar to mount_mfs.
37ea702e59Sgwr  * (But this design allows any filesystem format!)
38ea702e59Sgwr  */
39ea702e59Sgwr 
40*0c510515Schristos #include <sys/errno.h>
41fcfec169Slukem #include <sys/ioctl.h>
42ea702e59Sgwr #include <sys/mman.h>
43ea702e59Sgwr #include <sys/param.h>
44*0c510515Schristos #include <sys/types.h>
45ea702e59Sgwr 
46fa84b16bSpk #include <dev/md.h>
47ea702e59Sgwr 
48*0c510515Schristos #include <assert.h>
49*0c510515Schristos #include <err.h>
50*0c510515Schristos #include <errno.h>
51fcfec169Slukem #include <fcntl.h>
52fcfec169Slukem #include <stdio.h>
53fcfec169Slukem #include <stdlib.h>
54*0c510515Schristos #include <util.h>
55fcfec169Slukem 
56fcfec169Slukem int
main(int argc,char * argv[])57421949a3Ssevan main(int argc, char *argv[])
58ea702e59Sgwr {
59fa84b16bSpk 	struct md_conf md;
60*0c510515Schristos 	char fname[MAXPATHLEN];
61*0c510515Schristos 	int64_t num;
62*0c510515Schristos 	size_t blks, bytes;
6304084538Schs 	int fd;
64ea702e59Sgwr 
65*0c510515Schristos 	setprogname(argv[0]);
66*0c510515Schristos 
67*0c510515Schristos 	if (argc != 3) {
68*0c510515Schristos 		(void)fprintf(stderr, "Usage: %s <device> <%d-byte-blocks>\n",
69*0c510515Schristos 		    getprogname(), DEV_BSIZE);
70*0c510515Schristos 		return EXIT_FAILURE;
71ea702e59Sgwr 	}
72ea702e59Sgwr 
73*0c510515Schristos 	if (dehumanize_number(argv[2], &num) == -1)
74*0c510515Schristos 		goto bad_num;
75ea702e59Sgwr 
76*0c510515Schristos 	blks = (size_t)num;
77*0c510515Schristos 	bytes = blks << DEV_BSHIFT;
78*0c510515Schristos 	if (num <= 0 || bytes >> DEV_BSHIFT != blks) {
79*0c510515Schristos bad_num:	err(EXIT_FAILURE, "blocks: `%s'", argv[2]);
80ea702e59Sgwr 	}
81*0c510515Schristos 	md.md_size = bytes;
82ea702e59Sgwr 
83*0c510515Schristos 	fd = opendisk(argv[1], O_RDWR, fname, sizeof(fname), 0);
84*0c510515Schristos 	if (fd == -1)
85*0c510515Schristos 		err(EXIT_FAILURE, "Can't open `%s'", argv[1]);
86*0c510515Schristos 
87*0c510515Schristos 	md.md_addr = mmap(NULL, md.md_size, PROT_READ | PROT_WRITE,
88*0c510515Schristos 	    MAP_ANON | MAP_PRIVATE, -1, 0);
89*0c510515Schristos 	if (md.md_addr == MAP_FAILED)
90*0c510515Schristos 		err(EXIT_FAILURE, "mmap");
91ea702e59Sgwr 
92ea702e59Sgwr 	/* Become server! */
93fa84b16bSpk 	md.md_type = MD_UMEM_SERVER;
94*0c510515Schristos 	if (ioctl(fd, MD_SETCONF, &md) == -1)
95*0c510515Schristos 		err(EXIT_FAILURE, "ioctl");
96ea702e59Sgwr 
97*0c510515Schristos 	return EXIT_SUCCESS;
98ea702e59Sgwr }
99