1/*	$NetBSD: mdconfig.c,v 1.7 2020/08/18 19:26:29 christos 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.7 2020/08/18 19:26:29 christos 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/errno.h>
41#include <sys/ioctl.h>
42#include <sys/mman.h>
43#include <sys/param.h>
44#include <sys/types.h>
45
46#include <dev/md.h>
47
48#include <assert.h>
49#include <err.h>
50#include <errno.h>
51#include <fcntl.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <util.h>
55
56int
57main(int argc, char *argv[])
58{
59	struct md_conf md;
60	char fname[MAXPATHLEN];
61	int64_t num;
62	size_t blks, bytes;
63	int fd;
64
65	setprogname(argv[0]);
66
67	if (argc != 3) {
68		(void)fprintf(stderr, "Usage: %s <device> <%d-byte-blocks>\n",
69		    getprogname(), DEV_BSIZE);
70		return EXIT_FAILURE;
71	}
72
73	if (dehumanize_number(argv[2], &num) == -1)
74		goto bad_num;
75
76	blks = (size_t)num;
77	bytes = blks << DEV_BSHIFT;
78	if (num <= 0 || bytes >> DEV_BSHIFT != blks) {
79bad_num:	err(EXIT_FAILURE, "blocks: `%s'", argv[2]);
80	}
81	md.md_size = bytes;
82
83	fd = opendisk(argv[1], O_RDWR, fname, sizeof(fname), 0);
84	if (fd == -1)
85		err(EXIT_FAILURE, "Can't open `%s'", argv[1]);
86
87	md.md_addr = mmap(NULL, md.md_size, PROT_READ | PROT_WRITE,
88	    MAP_ANON | MAP_PRIVATE, -1, 0);
89	if (md.md_addr == MAP_FAILED)
90		err(EXIT_FAILURE, "mmap");
91
92	/* Become server! */
93	md.md_type = MD_UMEM_SERVER;
94	if (ioctl(fd, MD_SETCONF, &md) == -1)
95		err(EXIT_FAILURE, "ioctl");
96
97	return EXIT_SUCCESS;
98}
99