2005-04-17 20:28:26 +04:00
|
|
|
/* $NetBSD: fssconfig.c,v 1.5 2005/04/17 16:28:26 hannken Exp $ */
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Juergen Hannken-Illjes.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
|
|
|
|
|
|
|
#include <dev/fssvar.h>
|
|
|
|
|
|
|
|
int vflag = 0;
|
|
|
|
int xflag = 0;
|
|
|
|
|
|
|
|
void config(int, char **);
|
|
|
|
void unconfig(int, char **);
|
|
|
|
void list(int, char **);
|
|
|
|
void usage(void);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
void (*action)(int, char **);
|
|
|
|
|
|
|
|
action = config;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "cluvx")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'c':
|
|
|
|
action = config;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
action = list;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
action = unconfig;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
vflag++;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
xflag++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
(*action)(argc, argv);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
config(int argc, char **argv)
|
|
|
|
{
|
2004-05-25 18:54:55 +04:00
|
|
|
int fd, isreg, istmp, ispersistent;
|
2003-12-10 14:40:11 +03:00
|
|
|
char full[64], path[MAXPATHLEN];
|
|
|
|
off_t bssize;
|
2004-05-25 18:54:55 +04:00
|
|
|
dev_t mountdev;
|
2003-12-10 14:40:11 +03:00
|
|
|
struct stat sbuf;
|
2004-04-21 05:05:31 +04:00
|
|
|
struct statvfs fsbuf;
|
2003-12-10 14:40:11 +03:00
|
|
|
struct fss_set fss;
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
usage();
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
istmp = ispersistent = 0;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
fss.fss_mount = argv[1];
|
|
|
|
fss.fss_bstore = argv[2];
|
2004-05-25 18:54:55 +04:00
|
|
|
|
|
|
|
if (statvfs(argv[1], &fsbuf) != 0 || stat(argv[1], &sbuf) != 0)
|
|
|
|
err(1, "stat %s", argv[1]);
|
|
|
|
mountdev = sbuf.st_dev;
|
|
|
|
if (stat(argv[2], &sbuf) == 0 &&
|
|
|
|
S_ISREG(sbuf.st_mode) &&
|
|
|
|
sbuf.st_dev == mountdev) {
|
|
|
|
if ((sbuf.st_flags & SF_SNAPSHOT) == 0)
|
|
|
|
errx(1, "%s: exists and is not a snapshot", argv[2]);
|
|
|
|
if (argc != 3)
|
|
|
|
usage();
|
|
|
|
isreg = ispersistent = 1;
|
|
|
|
|
|
|
|
goto configure;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 5)
|
|
|
|
usage();
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
if (argc > 3)
|
|
|
|
fss.fss_csize = strsuftoll("cluster size", argv[3], 0, INT_MAX);
|
|
|
|
else
|
|
|
|
fss.fss_csize = 0;
|
|
|
|
if (argc > 4)
|
|
|
|
bssize = strsuftoll("bs size", argv[4], 0, LLONG_MAX);
|
|
|
|
else
|
2004-04-21 05:05:31 +04:00
|
|
|
bssize = (off_t)fsbuf.f_blocks*fsbuf.f_frsize;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the backing store. If it is a directory, create a temporary
|
|
|
|
* file and set the unlink flag.
|
|
|
|
*/
|
|
|
|
if ((fd = open(fss.fss_bstore, O_CREAT|O_TRUNC|O_WRONLY, 0600)) < 0) {
|
|
|
|
if (errno != EISDIR)
|
|
|
|
err(1, "create: %s", fss.fss_bstore);
|
|
|
|
snprintf(path, sizeof(path), "%s/XXXXXXXXXX", fss.fss_bstore);
|
|
|
|
if ((fd = mkstemp(path)) < 0)
|
|
|
|
err(1, "mkstemp: %s", path);
|
|
|
|
fss.fss_bstore = path;
|
|
|
|
istmp = 1;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &sbuf) < 0)
|
|
|
|
err(1, "stat: %s", fss.fss_bstore);
|
2004-05-25 18:54:55 +04:00
|
|
|
if (!ispersistent && sbuf.st_dev == mountdev)
|
|
|
|
ispersistent = 1;
|
2003-12-10 14:40:11 +03:00
|
|
|
isreg = S_ISREG(sbuf.st_mode);
|
2004-05-25 18:54:55 +04:00
|
|
|
if (!ispersistent && isreg && ftruncate(fd, bssize) < 0)
|
2004-01-11 22:05:26 +03:00
|
|
|
err(1, "truncate %s", fss.fss_bstore);
|
2003-12-10 14:40:11 +03:00
|
|
|
close(fd);
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
configure:
|
2003-12-10 14:40:11 +03:00
|
|
|
if ((fd = opendisk(argv[0], O_RDWR, full, sizeof(full), 0)) < 0) {
|
|
|
|
if (istmp)
|
|
|
|
unlink(fss.fss_bstore);
|
|
|
|
err(1, "open: %s", argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, FSSIOCSET, &fss) < 0) {
|
|
|
|
if (istmp)
|
|
|
|
unlink(fss.fss_bstore);
|
|
|
|
err(1, "%s: FSSIOCSET", full);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((xflag || istmp) && isreg && unlink(fss.fss_bstore) < 0)
|
|
|
|
err(1, "unlink: %s", fss.fss_bstore);
|
2004-01-11 22:05:26 +03:00
|
|
|
|
|
|
|
if (vflag)
|
|
|
|
list(1, argv);
|
2003-12-10 14:40:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
unconfig(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char full[64];
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
if (vflag)
|
|
|
|
list(1, argv);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
if ((fd = opendisk(argv[0], O_RDWR, full, sizeof(full), 0)) < 0)
|
|
|
|
err(1, "open: %s", argv[0]);
|
|
|
|
|
|
|
|
if (ioctl(fd, FSSIOCCLR) < 0)
|
|
|
|
err(1, "%s: FSSIOCCLR", full);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
list(int argc, char **argv)
|
|
|
|
{
|
2005-04-17 20:28:26 +04:00
|
|
|
int n, fd, flags;
|
2003-12-10 14:40:11 +03:00
|
|
|
char *dev, path[64], full[64];
|
|
|
|
char clbuf[5], bsbuf[5], tmbuf[64];
|
|
|
|
time_t t;
|
|
|
|
struct fss_get fsg;
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (argc > 0)
|
|
|
|
dev = argv[0];
|
|
|
|
else
|
|
|
|
dev = path;
|
|
|
|
|
|
|
|
for (n = 0; ; n++) {
|
|
|
|
if (argc == 0)
|
|
|
|
snprintf(path, sizeof(path), "fss%d", n);
|
|
|
|
if ((fd = opendisk(dev, O_RDONLY, full, sizeof(full), 0)) < 0) {
|
|
|
|
if (argc == 0 && (errno == ENOENT || errno == ENXIO))
|
|
|
|
break;
|
|
|
|
err(1, "open: %s", dev);
|
|
|
|
}
|
|
|
|
|
2005-04-17 20:28:26 +04:00
|
|
|
if (ioctl(fd, FSSIOFGET, &flags) < 0)
|
|
|
|
flags = 0;
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
if (ioctl(fd, FSSIOCGET, &fsg) < 0) {
|
|
|
|
if (errno == ENXIO)
|
|
|
|
printf("%s: not in use\n", dev);
|
|
|
|
else
|
|
|
|
err(1, "%s: FSSIOCGET", full);
|
|
|
|
} else if (vflag) {
|
|
|
|
humanize_number(clbuf, sizeof(clbuf),
|
|
|
|
(int64_t)fsg.fsg_csize,
|
|
|
|
"", HN_AUTOSCALE, HN_B|HN_NOSPACE);
|
|
|
|
|
|
|
|
humanize_number(bsbuf, sizeof(bsbuf),
|
|
|
|
(int64_t)fsg.fsg_bs_size*fsg.fsg_csize,
|
|
|
|
"", HN_AUTOSCALE, HN_B|HN_NOSPACE);
|
|
|
|
|
|
|
|
t = fsg.fsg_time.tv_sec;
|
|
|
|
strftime(tmbuf, sizeof(tmbuf), "%F %T", localtime(&t));
|
|
|
|
|
2005-04-17 20:28:26 +04:00
|
|
|
printf("%s: %s, taken %s", dev, fsg.fsg_mount, tmbuf);
|
|
|
|
if ((flags & FSS_UNCONFIG_ON_CLOSE) != 0)
|
|
|
|
printf(", unconfig on close");
|
|
|
|
if (fsg.fsg_csize == 0)
|
|
|
|
printf(", file system internal\n");
|
2004-05-25 18:54:55 +04:00
|
|
|
else
|
2005-04-17 20:28:26 +04:00
|
|
|
printf(", %"PRId64" cluster of %s, %s backup\n",
|
|
|
|
fsg.fsg_mount_size, clbuf, bsbuf);
|
2003-12-10 14:40:11 +03:00
|
|
|
} else
|
|
|
|
printf("%s: %s\n", dev, fsg.fsg_mount);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (argc > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s",
|
2004-05-25 18:54:55 +04:00
|
|
|
"usage: fssconfig [-cxv] device path backup [cluster [size]]\n"
|
|
|
|
" fssconfig -u [-v] device\n"
|
2003-12-10 14:40:11 +03:00
|
|
|
" fssconfig -l [-v] [device]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|