[Midnightbsd-cvs] src [11121] trunk/sbin/mdconfig/mdconfig.c: sync mdconfig

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Tue Jun 19 15:33:24 EDT 2018


Revision: 11121
          http://svnweb.midnightbsd.org/src/?rev=11121
Author:   laffer1
Date:     2018-06-19 15:32:44 -0400 (Tue, 19 Jun 2018)
Log Message:
-----------
sync mdconfig

Modified Paths:
--------------
    trunk/sbin/mdconfig/Makefile
    trunk/sbin/mdconfig/mdconfig.8
    trunk/sbin/mdconfig/mdconfig.c

Property Changed:
----------------
    trunk/sbin/mdconfig/mdconfig.8

Modified: trunk/sbin/mdconfig/Makefile
===================================================================
--- trunk/sbin/mdconfig/Makefile	2018-06-19 19:32:07 UTC (rev 11120)
+++ trunk/sbin/mdconfig/Makefile	2018-06-19 19:32:44 UTC (rev 11121)
@@ -1,5 +1,8 @@
 # $MidnightBSD$
+# $FreeBSD: stable/10/sbin/mdconfig/Makefile 264996 2014-04-27 01:15:10Z jmmv $
 
+.include <bsd.own.mk>
+
 PROG=		mdconfig
 MAN=		mdconfig.8
 
@@ -6,4 +9,8 @@
 DPADD=	${LIBUTIL} ${LIBGEOM} ${LIBBSDXML} ${LIBSBUF} 
 LDADD=	-lutil -lgeom -lbsdxml -lsbuf
 
+.if ${MK_TESTS} != "no"
+SUBDIR+=    tests
+.endif
+
 .include <bsd.prog.mk>

Modified: trunk/sbin/mdconfig/mdconfig.8
===================================================================
--- trunk/sbin/mdconfig/mdconfig.8	2018-06-19 19:32:07 UTC (rev 11120)
+++ trunk/sbin/mdconfig/mdconfig.8	2018-06-19 19:32:44 UTC (rev 11121)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
 .\" Copyright (c) 1993 University of Utah.
 .\" Copyright (c) 1980, 1989, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -39,14 +40,14 @@
 .\"     @(#)vnconfig.8	8.1 (Berkeley) 6/5/93
 .\" from: src/usr.sbin/vnconfig/vnconfig.8,v 1.19 2000/12/27 15:30:29
 .\"
-.\" $MidnightBSD$
+.\" $FreeBSD: stable/10/sbin/mdconfig/mdconfig.8 307403 2016-10-16 22:02:50Z sevan $
 .\"
-.Dd November 3, 2012
+.Dd October 10, 2015
 .Dt MDCONFIG 8
 .Os
 .Sh NAME
 .Nm mdconfig
-.Nd configure and enable memory disks
+.Nd create and control memory disks
 .Sh SYNOPSIS
 .Nm
 .Fl a
@@ -64,9 +65,15 @@
 .Fl u Ar unit
 .Op Fl o Oo Cm no Oc Ns Ar force
 .Nm
+.Fl r
+.Fl u Ar unit
+.Fl s Ar size
+.Op Fl o Oo Cm no Oc Ns Ar force
+.Nm
 .Fl l
 .Op Fl n
 .Op Fl v
+.Op Fl f Ar file
 .Op Fl u Ar unit
 .Nm
 .Ar file
@@ -73,7 +80,7 @@
 .Sh DESCRIPTION
 The
 .Nm
-utility configures and enables
+utility creates and controls
 .Xr md 4
 devices.
 .Pp
@@ -83,8 +90,13 @@
 Attach a memory disk.
 This will configure and attach a memory disk with the
 parameters specified and attach it to the system.
+If the
+.Fl u Ar unit
+option is not provided, the newly created device name will be printed on stdout.
 .It Fl d
 Detach a memory disk from the system and release all resources.
+.It Fl r
+Resize a memory disk.
 .It Fl t Ar type
 Select the type of the memory disk.
 .Bl -tag -width "malloc"
@@ -96,7 +108,7 @@
 .Fl o Cm reserve
 option is not set, creating and filling a large
 malloc-backed memory disk is a very easy way to
-panic a system.
+panic the system.
 .It Cm vnode
 A file specified with
 .Fl f Ar file
@@ -111,6 +123,8 @@
 backing is generally preferred instead of using
 .Cm malloc
 backing.
+.It Cm null
+Bitsink; all writes do nothing, all reads return zeroes.
 .El
 .It Fl f Ar file
 Filename to use for the vnode type memory disk.
@@ -124,11 +138,24 @@
 If given with
 .Fl u ,
 display details about that particular device.
+If given with
+.Fl f Ar file ,
+display
+.Xr md 4
+device names of which
+.Ar file
+is used as the backing store.
+If both of
+.Fl u
+and
+.Fl f
+options are specified,
+display devices which match the two conditions.
 If the
 .Fl v
 option is specified, show all details.
 .It Fl n
-When printing 
+When printing
 .Xr md 4
 device names, print only the unit number without the
 .Xr md 4
@@ -137,12 +164,14 @@
 Size of the memory disk.
 .Ar Size
 is the number of 512 byte sectors unless suffixed with a
-.Cm b , k , m , g ,
+.Cm b , k , m , g , t ,
 or
-.Cm t
+.Cm p
 which
-denotes byte, kilobyte, megabyte, gigabyte and terabyte respectively.
-The
+denotes byte, kilobyte, megabyte, gigabyte, terabyte and petabyte respectively.
+When used without the
+.Fl r
+option, the
 .Fl a
 and
 .Fl t Ar swap
@@ -184,13 +213,21 @@
 .It Oo Cm no Oc Ns Cm force
 Disable/enable extra sanity checks to prevent the user from doing something
 that might adversely affect the system.
+This can be used with the
+.Fl d
+flag to forcibly destroy an
+.Xr md 4
+disk that is still in use.
 .It Oo Cm no Oc Ns Cm readonly
 Enable/disable readonly mode.
 .El
 .It Fl u Ar unit
-Request a specific unit number for the
+Request a specific unit number or device name for the
 .Xr md 4
 device instead of automatic allocation.
+If a device name is specified, it must be start with
+.Dq md
+followed by the unit number.
 .El
 .Pp
 The last form,
@@ -202,64 +239,58 @@
 .Fl t Ar vnode
 .Fl f Ar file .
 .Sh EXAMPLES
-Create a 4 megabyte
-.Xr malloc 9
-backed memory disk.
+Create a disk with
+.Pa /tmp/boot.flp
+as backing storage.
 The name of the allocated unit will be printed on stdout, such as
+.Dq Li md0 :
+.Bd -literal -offset indent
+mdconfig /tmp/boot.flp
+.Ed
+.Pp
+Create a 1 gigabyte swap backed memory disk named
 .Dq Li md3 :
+.Bd -literal -offset indent
+mdconfig -s 1g -u md3
+.Ed
 .Pp
-.Dl mdconfig -a -t malloc -s 4m
+Detach and free all resources used by
+.Pa /dev/md3 :
+.Bd -literal -offset indent
+mdconfig -du md3
+.Ed
 .Pp
-Create a disk named
-.Pa /dev/md4
-with
-.Pa /tmp/boot.flp
-as backing storage:
+Show detailed information on current memory disks:
+.Bd -literal -offset indent
+mdconfig -lv
+.Ed
 .Pp
-.Dl mdconfig -a -t vnode -f /tmp/boot.flp -u 4
+Resize the
+.Dq Li md3
+memory disk to 2 gigabytes:
+.Bd -literal -offset indent
+mdconfig -rs 2g -u md3
+.Ed
 .Pp
-Detach and free all resources used by
-.Pa /dev/md4 :
-.Pp
-.Dl mdconfig -d -u 4
-.Pp
-Create a 128MByte swap backed disk, initialize an
+Create a 1 gigabyte swap backed disk, initialize an
 .Xr ffs 7
 file system on it, and mount it on
 .Pa /tmp :
 .Bd -literal -offset indent
-mdconfig -a -t swap -s 128M -u 10
+mdconfig -s 1g -u md10
 newfs -U /dev/md10
 mount /dev/md10 /tmp
 chmod 1777 /tmp
 .Ed
 .Pp
-Create a 5MB file-backed disk
-.Ns ( Fl a
-and
-.Fl t Ar vnode
-are implied):
-.Bd -literal -offset indent
-dd if=/dev/zero of=somebackingfile bs=1k count=5k
-mdconfig -f somebackingfile -u 0
-bsdlabel -w md0 auto
-newfs md0c
-mount /dev/md0c /mnt
-.Ed
-.Pp
-Create an
+Create a memory disk out of an ISO 9660 CD image file,
+using the first available
 .Xr md 4
-device out of an ISO 9660 CD image file
-.Ns ( Fl a
-and
-.Fl t Ar vnode
-are implied), using the first available
-.Xr md 4
-device, and then mount the new memory disk:
+device, and then mount it:
 .Bd -literal -offset indent
 mount -t cd9660 /dev/`mdconfig -f cdimage.iso` /mnt
+.Ed
 .Pp
-.Ed
 Create a file-backed device from a hard disk image that begins
 with 512K of raw header information.
 .Xr gnop 8
@@ -267,7 +298,7 @@
 .Pa md1.nop
 to the start of the filesystem in the image.
 .Bd -literal -offset indent
-mdconfig -f diskimage.img -u 1
+mdconfig -u md1 -f diskimage.img
 gnop create -o 512K md1
 mount /dev/md1.nop /mnt
 .Ed
@@ -274,8 +305,7 @@
 .Sh SEE ALSO
 .Xr md 4 ,
 .Xr ffs 7 ,
-.Xr bsdlabel 8 ,
-.Xr fdisk 8 ,
+.Xr gpart 8 ,
 .Xr mdmfs 8 ,
 .Xr malloc 9
 .Sh HISTORY
@@ -292,5 +322,4 @@
 The
 .Nm
 utility was written by
-.An Poul-Henning Kamp
-.Aq phk at FreeBSD.org .
+.An Poul-Henning Kamp Aq Mt phk at FreeBSD.org .


Property changes on: trunk/sbin/mdconfig/mdconfig.8
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/sbin/mdconfig/mdconfig.c
===================================================================
--- trunk/sbin/mdconfig/mdconfig.c	2018-06-19 19:32:07 UTC (rev 11120)
+++ trunk/sbin/mdconfig/mdconfig.c	2018-06-19 19:32:44 UTC (rev 11121)
@@ -1,14 +1,36 @@
-/*
- * ----------------------------------------------------------------------------
- * "THE BEER-WARE LICENSE" (Revision 42):
- * <phk at FreeBSD.ORG> wrote this file.  As long as you retain this notice you
- * can do whatever you want with this stuff. If we meet some day, and you think
- * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
- * ----------------------------------------------------------------------------
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2000-2004 Poul-Henning Kamp <phk at FreeBSD.org>
+ * Copyright (c) 2012 The FreeBSD Foundation
+ * All rights reserved.
  *
- * $MidnightBSD$
+ * Portions of this software were developed by Edward Tomasz Napierala
+ * under sponsorship from the FreeBSD Foundation.
  *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sbin/mdconfig/mdconfig.c 293690 2016-01-11 20:01:50Z trasz $
  */
+
 #include <sys/param.h>
 #include <sys/devicestat.h>
 #include <sys/ioctl.h>
@@ -26,6 +48,7 @@
 #include <inttypes.h>
 #include <libgeom.h>
 #include <libutil.h>
+#include <paths.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -32,16 +55,15 @@
 #include <string.h>
 #include <unistd.h>
 
-
 static struct md_ioctl mdio;
-static enum {UNSET, ATTACH, DETACH, LIST} action = UNSET;
+static enum {UNSET, ATTACH, DETACH, RESIZE, LIST} action = UNSET;
 static int nflag;
 
 static void usage(void);
 static void md_set_file(const char *);
-static int md_find(char *, const char *);
-static int md_query(char *name);
-static int md_list(char *units, int opt);
+static int md_find(const char *, const char *);
+static int md_query(const char *, const int, const char *);
+static int md_list(const char *, int, const char *);
 static char *geom_config_get(struct gconf *g, const char *name);
 static void md_prthumanval(char *length);
 
@@ -55,18 +77,20 @@
 static void
 usage(void)
 {
+
 	fprintf(stderr,
 "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n"
 "                [-s size] [-S sectorsize] [-u unit]\n"
 "                [-x sectors/track] [-y heads/cylinder]\n"
 "       mdconfig -d -u unit [-o [no]force]\n"
-"       mdconfig -l [-v] [-n] [-u unit]\n"
+"       mdconfig -r -u unit -s size [-o [no]force]\n"
+"       mdconfig -l [-v] [-n] [-f file] [-u unit]\n"
 "       mdconfig file\n");
 	fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n");
 	fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n");
 	fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n");
-	fprintf(stderr, "\t\t       %%dk (kB), %%dm (MB), %%dg (GB) or\n");
-	fprintf(stderr, "\t\t       %%dt (TB)\n");
+	fprintf(stderr, "\t\t       %%dk (kB), %%dm (MB), %%dg (GB), \n");
+	fprintf(stderr, "\t\t       %%dt (TB), or %%dp (PB)\n");
 	exit(1);
 }
 
@@ -75,8 +99,7 @@
 {
 	int ch, fd, i, vflag;
 	char *p;
-	int cmdline = 0;
-	char *mdunit = NULL;
+	char *fflag = NULL, *sflag = NULL, *tflag = NULL, *uflag = NULL;
 
 	bzero(&mdio, sizeof(mdio));
 	mdio.md_file = malloc(PATH_MAX);
@@ -84,76 +107,69 @@
 		err(1, "could not allocate memory");
 	vflag = 0;
 	bzero(mdio.md_file, PATH_MAX);
-	while ((ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:vx:y:")) != -1) {
+
+	if (argc == 1)
+		usage();
+
+	while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:")) != -1) {
 		switch (ch) {
 		case 'a':
-			if (cmdline != 0)
-				usage();
+			if (action != UNSET && action != ATTACH)
+				errx(1, "-a is mutually exclusive "
+				    "with -d, -r, and -l");
 			action = ATTACH;
-			cmdline = 1;
 			break;
 		case 'd':
-			if (cmdline != 0)
-				usage();
+			if (action != UNSET && action != DETACH)
+				errx(1, "-d is mutually exclusive "
+				    "with -a, -r, and -l");
 			action = DETACH;
-			mdio.md_options = MD_AUTOUNIT;
-			cmdline = 3;
+			mdio.md_options |= MD_AUTOUNIT;
 			break;
+		case 'r':
+			if (action != UNSET && action != RESIZE)
+				errx(1, "-r is mutually exclusive "
+				    "with -a, -d, and -l");
+			action = RESIZE;
+			mdio.md_options |= MD_AUTOUNIT;
+			break;
 		case 'l':
-			if (cmdline != 0)
-				usage();
+			if (action != UNSET && action != LIST)
+				errx(1, "-l is mutually exclusive "
+				    "with -a, -r, and -d");
 			action = LIST;
-			mdio.md_options = MD_AUTOUNIT;
-			cmdline = 3;
+			mdio.md_options |= MD_AUTOUNIT;
 			break;
 		case 'n':
 			nflag = 1;
 			break;
 		case 't':
-			if (cmdline != 1)
-				usage();
+			if (tflag != NULL)
+				errx(1, "-t can be passed only once");
+			tflag = optarg;
 			if (!strcmp(optarg, "malloc")) {
 				mdio.md_type = MD_MALLOC;
-				mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
+				mdio.md_options |= MD_AUTOUNIT | MD_COMPRESS;
 			} else if (!strcmp(optarg, "vnode")) {
 				mdio.md_type = MD_VNODE;
-				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
+				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
 			} else if (!strcmp(optarg, "swap")) {
 				mdio.md_type = MD_SWAP;
-				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
-			} else {
-				usage();
-			}
-			cmdline=2;
+				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
+			} else if (!strcmp(optarg, "null")) {
+				mdio.md_type = MD_NULL;
+				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
+			} else
+				errx(1, "unknown type: %s", optarg);
 			break;
 		case 'f':
-			if (cmdline == 0) {
-				action = ATTACH;
-				cmdline = 1;
-			}
-			if (cmdline == 1) {
-				/* Imply ``-t vnode'' */
-				mdio.md_type = MD_VNODE;
-				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
-				cmdline = 2;
-			}
-			if (cmdline != 2)
-				usage();
-			md_set_file(optarg);
+			if (fflag != NULL)
+				errx(1, "-f can be passed only once");
+			fflag = realpath(optarg, NULL);
+			if (fflag == NULL)
+				err(1, "realpath");
 			break;
 		case 'o':
-			if (action == DETACH) {
-				if (!strcmp(optarg, "force"))
-					mdio.md_options |= MD_FORCE;
-				else if (!strcmp(optarg, "noforce"))
-					mdio.md_options &= ~MD_FORCE;
-				else
-					errx(1, "Unknown option: %s.", optarg);
-				break;
-			}
-
-			if (cmdline != 2)
-				usage();
 			if (!strcmp(optarg, "async"))
 				mdio.md_options |= MD_ASYNC;
 			else if (!strcmp(optarg, "noasync"))
@@ -179,27 +195,15 @@
 			else if (!strcmp(optarg, "noreserve"))
 				mdio.md_options &= ~MD_RESERVE;
 			else
-				errx(1, "Unknown option: %s.", optarg);
+				errx(1, "unknown option: %s", optarg);
 			break;
 		case 'S':
-			if (cmdline != 2)
-				usage();
 			mdio.md_sectorsize = strtoul(optarg, &p, 0);
 			break;
 		case 's':
-			if (cmdline == 0) {
-				/* Imply ``-a'' */
-				action = ATTACH;
-				cmdline = 1;
-			}
-			if (cmdline == 1) {
-				/* Imply ``-t swap'' */
-				mdio.md_type = MD_SWAP;
-				mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
-				cmdline = 2;
-			}
-			if (cmdline != 2)
-				usage();
+			if (sflag != NULL)
+				errx(1, "-s can be passed only once");
+			sflag = optarg;
 			mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0);
 			if (p == NULL || *p == '\0')
 				mdio.md_mediasize *= DEV_BSIZE;
@@ -214,35 +218,26 @@
 			else if (*p == 't' || *p == 'T') {
 				mdio.md_mediasize <<= 30;
 				mdio.md_mediasize <<= 10;
+			} else if (*p == 'p' || *p == 'P') {
+				mdio.md_mediasize <<= 30;
+				mdio.md_mediasize <<= 20;
 			} else
-				errx(1, "Unknown suffix on -s argument");
+				errx(1, "unknown suffix on -s argument");
 			break;
 		case 'u':
-			if (cmdline != 2 && cmdline != 3)
-				usage();
-			if (!strncmp(optarg, "/dev/", 5))
-				optarg += 5;
+			if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
+				optarg += sizeof(_PATH_DEV) - 1;
 			if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1))
 				optarg += sizeof(MD_NAME) - 1;
-			mdio.md_unit = strtoul(optarg, &p, 0);
-			if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
-				errx(1, "bad unit: %s", optarg);
-			mdunit = optarg;
-			mdio.md_options &= ~MD_AUTOUNIT;
+			uflag = optarg;
 			break;
 		case 'v':
-			if (cmdline != 3)
-				usage();
 			vflag = OPT_VERBOSE;
 			break;
 		case 'x':
-			if (cmdline != 2)
-				usage();
 			mdio.md_fwsectors = strtoul(optarg, &p, 0);
 			break;
 		case 'y':
-			if (cmdline != 2)
-				usage();
 			mdio.md_fwheads = strtoul(optarg, &p, 0);
 			break;
 		default:
@@ -252,68 +247,137 @@
 
 	argc -= optind;
 	argv += optind;
-	if (action == UNSET) {
-		if (argc != 1)
-			usage();
+
+	if (action == UNSET)
 		action = ATTACH;
-		mdio.md_type = MD_VNODE;
-		mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS;
-		cmdline = 2;
-		md_set_file(*argv);
+
+	if (action == ATTACH) {
+		if (tflag == NULL) {
+			/*
+			 * Try to infer the type based on other arguments.
+			 */
+			if (fflag != NULL || argc > 0) {
+				/* Imply ``-t vnode'' */
+				mdio.md_type = MD_VNODE;
+				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
+				    MD_COMPRESS;
+			} else if (sflag != NULL) {
+				/* Imply ``-t swap'' */
+				mdio.md_type = MD_SWAP;
+				mdio.md_options |= MD_CLUSTER | MD_AUTOUNIT |
+				    MD_COMPRESS;
+			} else
+				errx(1, "unable to determine type");
+		}
+
+		if ((fflag != NULL || argc > 0) && mdio.md_type != MD_VNODE)
+			errx(1, "only -t vnode can be used with file name");
+
+		if (mdio.md_type == MD_VNODE) {
+			if (fflag != NULL) {
+				if (argc != 0)
+					usage();
+				md_set_file(fflag);
+			} else {
+				if (argc != 1)
+					usage();
+				md_set_file(*argv);
+			}
+
+			if ((mdio.md_options & MD_READONLY) == 0 &&
+			    access(mdio.md_file, W_OK) < 0 &&
+			    (errno == EACCES || errno == EPERM ||
+			     errno == EROFS)) {
+				warnx("WARNING: opening backing store: %s "
+				    "readonly", mdio.md_file);
+				mdio.md_options |= MD_READONLY;
+			}
+		}
+
+		if ((mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP ||
+		    mdio.md_type == MD_NULL) && sflag == NULL)
+			errx(1, "must specify -s for -t malloc, -t swap, "
+			    "or -t null");
+		if (mdio.md_type == MD_VNODE && mdio.md_file[0] == '\0')
+			errx(1, "must specify -f for -t vnode");
+	} else {
+		if (mdio.md_sectorsize != 0)
+			errx(1, "-S can only be used with -a");
+		if (action != RESIZE && sflag != NULL)
+			errx(1, "-s can only be used with -a and -r");
+		if (mdio.md_fwsectors != 0)
+			errx(1, "-x can only be used with -a");
+		if (mdio.md_fwheads != 0)
+			errx(1, "-y can only be used with -a");
+		if (fflag != NULL && action != LIST)
+			errx(1, "-f can only be used with -a and -l");
+		if (tflag != NULL)
+			errx(1, "-t can only be used with -a");
+		if (argc > 0)
+			errx(1, "file can only be used with -a");
+		if ((action != DETACH && action != RESIZE) &&
+		    (mdio.md_options & ~MD_AUTOUNIT) != 0)
+			errx(1, "-o can only be used with -a, -d, and -r");
+		if (action == DETACH &&
+		    (mdio.md_options & ~(MD_FORCE | MD_AUTOUNIT)) != 0)
+			errx(1, "only -o [no]force can be used with -d");
+		if (action == RESIZE &&
+		    (mdio.md_options & ~(MD_FORCE | MD_RESERVE | MD_AUTOUNIT)) != 0)
+			errx(1, "only -o [no]force and -o [no]reserve can be used with -r");
 	}
 
+	if (action == RESIZE && sflag == NULL)
+		errx(1, "must specify -s for -r");
+
+	if (action != LIST && vflag == OPT_VERBOSE)
+		errx(1, "-v can only be used with -l");
+
+	if (uflag != NULL) {
+		mdio.md_unit = strtoul(uflag, &p, 0);
+		if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0')
+			errx(1, "bad unit: %s", uflag);
+		mdio.md_options &= ~MD_AUTOUNIT;
+	}
+
 	mdio.md_version = MDIOVERSION;
 
 	if (!kld_isloaded("g_md") && kld_load("geom_md") == -1)
 		err(1, "failed to load geom_md module");
 
-	fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
+	fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0);
 	if (fd < 0)
-		err(1, "open(/dev/%s)", MDCTL_NAME);
-	if (cmdline == 2
-	    && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP))
-		if (mdio.md_mediasize == 0)
-			errx(1, "must specify -s for -t malloc or -t swap");
-	if (cmdline == 2 && mdio.md_type == MD_VNODE)
-		if (mdio.md_file[0] == '\0')
-			errx(1, "must specify -f for -t vnode");
-	if (mdio.md_type == MD_VNODE &&
-	    (mdio.md_options & MD_READONLY) == 0) {
-		if (access(mdio.md_file, W_OK) < 0 &&
-		    (errno == EACCES || errno == EPERM || errno == EROFS)) {
-			fprintf(stderr,
-			    "WARNING: opening backing store: %s readonly\n",
-			    mdio.md_file);
-			mdio.md_options |= MD_READONLY;
-		}
-	}
-	if (action == LIST) {
-		if (mdio.md_options & MD_AUTOUNIT) {
-			/*
-			 * Listing all devices. This is why we pass NULL
-			 * together with OPT_LIST.
-			 */
-			md_list(NULL, OPT_LIST | vflag);
-		} else {
-			return (md_query(mdunit));
-		}
-	} else if (action == ATTACH) {
-		if (cmdline < 2)
-			usage();
+		err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME);
+
+	if (action == ATTACH) {
 		i = ioctl(fd, MDIOCATTACH, &mdio);
 		if (i < 0)
-			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
+			err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
 		if (mdio.md_options & MD_AUTOUNIT)
 			printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
 	} else if (action == DETACH) {
 		if (mdio.md_options & MD_AUTOUNIT)
-			usage();
+			errx(1, "-d requires -u");
 		i = ioctl(fd, MDIOCDETACH, &mdio);
 		if (i < 0)
-			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
+			err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
+	} else if (action == RESIZE) {
+		if (mdio.md_options & MD_AUTOUNIT)
+			errx(1, "-r requires -u");
+		i = ioctl(fd, MDIOCRESIZE, &mdio);
+		if (i < 0)
+			err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME);
+	} else if (action == LIST) {
+		if (mdio.md_options & MD_AUTOUNIT) {
+			/*
+			 * Listing all devices. This is why we pass NULL
+			 * together with OPT_LIST.
+			 */
+			return (md_list(NULL, OPT_LIST | vflag, fflag));
+		} else
+			return (md_query(uflag, vflag, fflag));
 	} else
 		usage();
-	close (fd);
+	close(fd);
 	return (0);
 }
 
@@ -344,7 +408,7 @@
  * between list and query mode.
  */
 static int
-md_list(char *units, int opt)
+md_list(const char *units, int opt, const char *fflag)
 {
 	struct gmesh gm;
 	struct gprovider *pp;
@@ -354,7 +418,7 @@
 	struct ggeom *gg;
 	struct gclass *gcl;
 	void *sq;
-	int retcode, found;
+	int retcode, ffound, ufound;
 	char *type, *file, *length;
 
 	type = file = length = NULL;
@@ -369,7 +433,7 @@
 	if (sq == NULL)
 		return (-1);
 
-	found = 0;
+	ffound = ufound = 0;
 	while ((gsp = geom_stats_snapshot_next(sq)) != NULL) {
 		gid = geom_lookupid(&gm, gsp->id);
 		if (gid == NULL)
@@ -385,18 +449,26 @@
 				if (retcode != 1)
 					continue;
 				else
-					found = 1;
+					ufound = 1;
 			}
 			gc = &pp->lg_config;
-			if (nflag && strncmp(pp->lg_name, "md", 2) == 0)
+			type = geom_config_get(gc, "type");
+			if (strcmp(type, "vnode") == 0) {
+				file = geom_config_get(gc, "file");
+				if (fflag != NULL &&
+				    strcmp(fflag, file) != 0)
+					continue;
+				else
+					ffound = 1;
+			} else if (fflag != NULL)
+					continue;
+			if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0)
 				printf("%s", pp->lg_name + 2);
 			else
 				printf("%s", pp->lg_name);
 
-			if (opt & OPT_VERBOSE || opt & OPT_UNIT) {
-				type = geom_config_get(gc, "type");
-				if (strcmp(type, "vnode") == 0)
-					file = geom_config_get(gc, "file");
+			if (opt & OPT_VERBOSE ||
+			    ((opt & OPT_UNIT) && fflag == NULL)) {
 				length = geom_config_get(gc, "length");
 				printf("\t%s\t", type);
 				if (length != NULL)
@@ -417,10 +489,18 @@
 		printf("\n");
 	/* XXX: Check if it's enough to clean everything. */
 	geom_stats_snapshot_free(sq);
-	if ((opt & OPT_UNIT) && found)
-		return (0);
-	else
-		return (-1);
+	if (opt & OPT_UNIT) {
+		if (((fflag == NULL) && ufound) ||
+		    ((fflag == NULL) && (units != NULL) && ufound) ||
+		    ((fflag != NULL) && ffound) ||
+		    ((fflag != NULL) && (units != NULL) && ufound && ffound))
+			return (0);
+	} else if (opt & OPT_LIST) {
+		if ((fflag == NULL) ||
+		    ((fflag != NULL) && ffound))
+			return (0);
+	}
+	return (-1);
 }
 
 /*
@@ -444,10 +524,10 @@
  * otherwise.
  */
 static int
-md_find(char *list, const char *name)
+md_find(const char *list, const char *name)
 {
 	int ret;
-	char num[16];
+	char num[PATH_MAX];
 	char *ptr, *p, *u;
 
 	ret = 0;
@@ -455,10 +535,10 @@
 	if (ptr == NULL)
 		return (-1);
 	for (p = ptr; (u = strsep(&p, ",")) != NULL;) {
-		if (strncmp(u, "/dev/", 5) == 0)
-			u += 5;
+		if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
+			u += sizeof(_PATH_DEV) - 1;
 		/* Just in case user specified number instead of full name */
-		snprintf(num, sizeof(num), "md%s", u);
+		snprintf(num, sizeof(num), "%s%s", MD_NAME, u);
 		if (strcmp(u, name) == 0 || strcmp(num, name) == 0) {
 			ret = 1;
 			break;
@@ -484,8 +564,9 @@
 	(void)printf("%6s", buf);
 }
 
-int
-md_query(char *name)
+static int
+md_query(const char *name, const int opt, const char *fflag)
 {
-	return (md_list(name, OPT_UNIT));
+
+	return (md_list(name, opt | OPT_UNIT, fflag));
 }



More information about the Midnightbsd-cvs mailing list