use opendisk()/dehumanize_number

This commit is contained in:
christos 2020-08-18 19:26:29 +00:00
parent 9630bba2c9
commit 0c510515ef
2 changed files with 40 additions and 32 deletions

View File

@ -1,6 +1,10 @@
# $NetBSD: Makefile,v 1.2 1997/01/02 00:22:43 pk Exp $ # $NetBSD: Makefile,v 1.3 2020/08/18 19:26:29 christos Exp $
WARNS?=6
PROG= mdconfig PROG= mdconfig
MAN= mdconfig.8 MAN= mdconfig.8
LDADD+= -lutil
DPADD+= ${LIBUTIL}
.include <bsd.prog.mk> .include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
/* $NetBSD: mdconfig.c,v 1.6 2018/01/23 21:06:25 sevan Exp $ */ /* $NetBSD: mdconfig.c,v 1.7 2020/08/18 19:26:29 christos Exp $ */
/* /*
* Copyright (c) 1995 Gordon W. Ross * Copyright (c) 1995 Gordon W. Ross
@ -27,7 +27,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
__RCSID("$NetBSD: mdconfig.c,v 1.6 2018/01/23 21:06:25 sevan Exp $"); __RCSID("$NetBSD: mdconfig.c,v 1.7 2020/08/18 19:26:29 christos Exp $");
#endif #endif
/* /*
@ -37,58 +37,62 @@ __RCSID("$NetBSD: mdconfig.c,v 1.6 2018/01/23 21:06:25 sevan Exp $");
* (But this design allows any filesystem format!) * (But this design allows any filesystem format!)
*/ */
#include <sys/types.h> #include <sys/errno.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/types.h>
#include <dev/md.h> #include <dev/md.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <util.h>
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct md_conf md; struct md_conf md;
size_t nblks; char fname[MAXPATHLEN];
int64_t num;
size_t blks, bytes;
int fd; int fd;
if (argc <= 2) { setprogname(argv[0]);
fprintf(stderr, "usage: mdconfig <device> <%d-byte-blocks>\n",
DEV_BSIZE); if (argc != 3) {
exit(1); (void)fprintf(stderr, "Usage: %s <device> <%d-byte-blocks>\n",
getprogname(), DEV_BSIZE);
return EXIT_FAILURE;
} }
nblks = (size_t)strtoul(argv[2], NULL, 0); if (dehumanize_number(argv[2], &num) == -1)
if (nblks == 0) { goto bad_num;
fprintf(stderr, "invalid number of blocks\n");
exit(1);
}
md.md_size = nblks << DEV_BSHIFT;
fd = open(argv[1], O_RDWR, 0); blks = (size_t)num;
if (fd < 0) { bytes = blks << DEV_BSHIFT;
perror(argv[1]); if (num <= 0 || bytes >> DEV_BSHIFT != blks) {
exit(1); bad_num: err(EXIT_FAILURE, "blocks: `%s'", argv[2]);
} }
md.md_size = bytes;
md.md_addr = mmap(NULL, md.md_size, fd = opendisk(argv[1], O_RDWR, fname, sizeof(fname), 0);
PROT_READ | PROT_WRITE, if (fd == -1)
MAP_ANON | MAP_PRIVATE, err(EXIT_FAILURE, "Can't open `%s'", argv[1]);
-1, 0);
if (md.md_addr == MAP_FAILED) { md.md_addr = mmap(NULL, md.md_size, PROT_READ | PROT_WRITE,
perror("mmap"); MAP_ANON | MAP_PRIVATE, -1, 0);
exit(1); if (md.md_addr == MAP_FAILED)
} err(EXIT_FAILURE, "mmap");
/* Become server! */ /* Become server! */
md.md_type = MD_UMEM_SERVER; md.md_type = MD_UMEM_SERVER;
if (ioctl(fd, MD_SETCONF, &md)) { if (ioctl(fd, MD_SETCONF, &md) == -1)
perror("ioctl"); err(EXIT_FAILURE, "ioctl");
exit(1);
}
exit(0); return EXIT_SUCCESS;
} }