Module Name:    src
Committed By:   hannken
Date:           Sun Apr 11 08:23:52 UTC 2010

Modified Files:
        src/doc: CHANGES
        src/sbin/dump: snapshot.c
        src/sbin/fsck_ffs: main.c
        src/sbin/fsck_msdos: Makefile fsck_msdos.8 main.c

Log Message:
Add -x option which allows to run `fsck_msdos -n' on a snapshot of
a live file system.

While here modify snap_open() to accept a character device as its
first arg and remove now unneeded get_snap_device().

Reviewed by: Manuel Bouyer <bou...@netbsd.org>


To generate a diff of this commit:
cvs rdiff -u -r1.1379 -r1.1380 src/doc/CHANGES
cvs rdiff -u -r1.4 -r1.5 src/sbin/dump/snapshot.c
cvs rdiff -u -r1.75 -r1.76 src/sbin/fsck_ffs/main.c
cvs rdiff -u -r1.11 -r1.12 src/sbin/fsck_msdos/Makefile
cvs rdiff -u -r1.15 -r1.16 src/sbin/fsck_msdos/fsck_msdos.8
cvs rdiff -u -r1.21 -r1.22 src/sbin/fsck_msdos/main.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/doc/CHANGES
diff -u src/doc/CHANGES:1.1379 src/doc/CHANGES:1.1380
--- src/doc/CHANGES:1.1379	Fri Apr  9 08:09:18 2010
+++ src/doc/CHANGES	Sun Apr 11 08:23:51 2010
@@ -1,4 +1,4 @@
-# LIST OF CHANGES FROM LAST RELEASE:			<$Revision: 1.1379 $>
+# LIST OF CHANGES FROM LAST RELEASE:			<$Revision: 1.1380 $>
 #
 #
 # [Note: This file does not mention every change made to the NetBSD source tree.
@@ -581,3 +581,5 @@
 	agp(4): Add support for the Intel 82855GM AGP port. [jakllsch 20100404]
 	acpi(4): Updated ACPICA to 20100121. [jruoho 20100408]
 	fss(4): Add snapshot support for MSDOS file systems. [hannken 20100409]
+	fsck_msdos(8): add -x option which allows to run fsck_msdos -n on
+		a snapshot of a live file system. [hannken 20100411]

Index: src/sbin/dump/snapshot.c
diff -u src/sbin/dump/snapshot.c:1.4 src/sbin/dump/snapshot.c:1.5
--- src/sbin/dump/snapshot.c:1.4	Mon Apr 28 20:23:08 2008
+++ src/sbin/dump/snapshot.c	Sun Apr 11 08:23:51 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: snapshot.c,v 1.4 2008/04/28 20:23:08 martin Exp $	*/
+/*	$NetBSD: snapshot.c,v 1.5 2010/04/11 08:23:51 hannken Exp $	*/
 
 /*-
  * Copyright (c) 2005 The NetBSD Foundation, Inc.
@@ -54,25 +54,51 @@
  * snapshot device path.
  */
 int
-snap_open(char *mountpoint, char *backup, time_t *snap_date, char **snap_dev)
+snap_open(char *file, char *backup, time_t *snap_date, char **snap_dev)
 {
-	int i, fd, israw, fsinternal, dounlink, flags;
-	char path[MAXPATHLEN], fss_dev[14];
+	int i, n, fd, israw, fsinternal, dounlink, flags;
+	char path[MAXPATHLEN], fss_dev[14], *cp;
 	dev_t mountdev;
 	struct fss_set fss;
 	struct fss_get fsg;
 	struct stat sb;
-	struct statvfs fsb;
+	struct statvfs *mntbuf, *fs, fsb;
 
 	dounlink = 0;
 	fd = -1;
+	mntbuf = NULL;
 
-	fss.fss_mount = mountpoint;
+	/*
+	 * Lookup the mount point. `file' is either a directory or a raw
+	 * character device.
+	 */
+	if (lstat(file, &sb) < 0)
+		goto fail;
+	fss.fss_mount = NULL;
+	if (S_ISCHR(sb.st_mode)) {
+		if ((cp = strrchr(file, '/')) == NULL || cp[1] != 'r') {
+			errno = EINVAL;
+			goto fail;
+		}
+		snprintf(path, sizeof(path), "%.*s/%s",
+		    (int)(cp - file), file, cp + 2);
+		n = getmntinfo(&mntbuf, MNT_NOWAIT);
+		for (fs = mntbuf, i = 0; i < n; i++, fs++) {
+			if (strcmp(fs->f_mntfromname, path) == 0) {
+				fss.fss_mount = fs->f_mntonname;
+				if (stat(fss.fss_mount, &sb) < 0)
+					goto fail;
+				break;
+			}
+		}
+	} else if (S_ISDIR(sb.st_mode))
+		fss.fss_mount = file;
+	if (fss.fss_mount == NULL) {
+		errno = EINVAL;
+		goto fail;
+	}
 	fss.fss_bstore = backup ? backup : fss.fss_mount;
 	fss.fss_csize = 0;
-
-	if (stat(fss.fss_mount, &sb) < 0)
-		goto fail;
 	mountdev = sb.st_dev;
 
 	/*
@@ -153,12 +179,16 @@
 			goto fail;
 		}
 
+		if (mntbuf)
+			free(mntbuf);
 		if (snap_date != NULL)
 			*snap_date = fsg.fsg_time.tv_sec;
 		return fd;
 	}
 
 fail:
+	if (mntbuf)
+		free(mntbuf);
 	if (dounlink)
 		unlink(fss.fss_bstore);
 	if (fd >= 0)

Index: src/sbin/fsck_ffs/main.c
diff -u src/sbin/fsck_ffs/main.c:1.75 src/sbin/fsck_ffs/main.c:1.76
--- src/sbin/fsck_ffs/main.c:1.75	Thu Jan  7 01:39:56 2010
+++ src/sbin/fsck_ffs/main.c	Sun Apr 11 08:23:51 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.75 2010/01/07 01:39:56 christos Exp $	*/
+/*	$NetBSD: main.c,v 1.76 2010/04/11 08:23:51 hannken Exp $	*/
 
 /*
  * Copyright (c) 1980, 1986, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
 #else
-__RCSID("$NetBSD: main.c,v 1.75 2010/01/07 01:39:56 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.76 2010/04/11 08:23:51 hannken Exp $");
 #endif
 #endif /* not lint */
 
@@ -76,7 +76,6 @@
 static int	argtoi(int, const char *, const char *, int);
 static int	checkfilesys(const char *, const char *, int);
 static void	usage(void);
-static char 	*get_snap_device(char *);
 
 int
 main(int argc, char *argv[])
@@ -223,23 +222,17 @@
 			pfatal("Can't check %s\n", *argv);
 		
 		if (snap_backup || snap_internal) {
-			char *mpt;
 			char *snap_dev;
 			int snapfd;
 
-			mpt = get_snap_device(*argv);
-			if (mpt == NULL)
-				goto next;
-			snapfd = snap_open(mpt, snap_backup, NULL, &snap_dev);
+			snapfd = snap_open(*argv, snap_backup, NULL, &snap_dev);
 			if (snapfd < 0) {
-				warn("can't take snapshot of %s", mpt);
-				free(mpt);
+				warn("can't take snapshot of %s", *argv);
 				goto next;
 			}
 			nret = checkfilesys(blockcheck(snap_dev), path, 0);
 			if (ret < nret)
 				ret = nret;
-			free(mpt);
 			close(snapfd);
 		} else {
 			nret = checkfilesys(path, path, 0);
@@ -512,57 +505,3 @@
 	    getprogname());
 	exit(FSCK_EXIT_USAGE);
 }
-
-static 
-char *get_snap_device(char *file)
-{
-	char *mountpoint = NULL;
-	struct statvfs *mntbuf, *fs, fsbuf;
-	struct stat sb;
-
-	/* find the mount point */
-	if (lstat(file, &sb) == -1) {
-		warn("can't stat %s", file);
-		return NULL;
-	}
-	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
-		int mntbufc, i;
-		if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
-			pfatal("can't get mount list: %s\n", strerror(errno));
-		for (fs = mntbuf, i = 0;
-		     i < mntbufc; i++, fs++) {
-			if (strcmp(fs->f_fstypename, "ufs") != 0 &&
-			    strcmp(fs->f_fstypename, "ffs") != 0)
-				continue;
-			if (fs->f_flag & ST_RDONLY) {
-				warnx("Cannot use -x or -X "
-				     "on read-only filesystem");
-				free(mntbuf);
-				return NULL;
-			}
-			if (strcmp(fs->f_mntfromname, unrawname(file)) == 0) {
-				mountpoint = strdup(fs->f_mntonname);
-				free(mntbuf);
-				return mountpoint;
-			}
-		}
-		warnx("Cannot use -x or -X on unmounted device");
-		free(mntbuf);
-		return NULL;
-	}
-	if (S_ISDIR(sb.st_mode)) {
-		if (statvfs(file, &fsbuf) == -1)
-			pfatal("can't statvfs %s: %s\n", file, strerror(errno));
-		if (strcmp(fsbuf.f_mntonname, file))
-			pfatal("%s is not a mount point\n", file);
-		if (fsbuf.f_flag & ST_RDONLY) {
-			warnx("Cannot use -x or -X "
-			     "on read-only filesystem");
-			return NULL;
-		}
-		mountpoint = strdup(file);
-		return mountpoint;
-	}
-	pfatal("%s is not a mount point\n", file);
-	return NULL;
-}

Index: src/sbin/fsck_msdos/Makefile
diff -u src/sbin/fsck_msdos/Makefile:1.11 src/sbin/fsck_msdos/Makefile:1.12
--- src/sbin/fsck_msdos/Makefile:1.11	Sat Aug 26 18:14:28 2006
+++ src/sbin/fsck_msdos/Makefile	Sun Apr 11 08:23:52 2010
@@ -1,14 +1,15 @@
-#	$NetBSD: Makefile,v 1.11 2006/08/26 18:14:28 christos Exp $
+#	$NetBSD: Makefile,v 1.12 2010/04/11 08:23:52 hannken Exp $
 
 .include <bsd.own.mk>
 
 PROG=	fsck_msdos
 MAN=	fsck_msdos.8
-SRCS=	main.c check.c boot.c fat.c dir.c fsutil.c
+SRCS=	main.c check.c boot.c fat.c dir.c fsutil.c snapshot.c
 
 FSCK=	${NETBSDSRCDIR}/sbin/fsck
-CPPFLAGS+= -I${FSCK}
-.PATH:	${FSCK}
+DUMP=	${NETBSDSRCDIR}/sbin/dump
+CPPFLAGS+= -I${FSCK} -I${DUMP}
+.PATH:	${FSCK} ${DUMP}
 
 LDADD+=-lutil
 DPADD+=${LIBUTIL}

Index: src/sbin/fsck_msdos/fsck_msdos.8
diff -u src/sbin/fsck_msdos/fsck_msdos.8:1.15 src/sbin/fsck_msdos/fsck_msdos.8:1.16
--- src/sbin/fsck_msdos/fsck_msdos.8:1.15	Fri Jun 13 20:46:09 2008
+++ src/sbin/fsck_msdos/fsck_msdos.8	Sun Apr 11 08:23:52 2010
@@ -1,4 +1,4 @@
-.\"	$NetBSD: fsck_msdos.8,v 1.15 2008/06/13 20:46:09 martin Exp $
+.\"	$NetBSD: fsck_msdos.8,v 1.16 2010/04/11 08:23:52 hannken Exp $
 .\"
 .\" Copyright (C) 1995 Wolfgang Solfrank
 .\" Copyright (c) 1995 Martin Husemann
@@ -24,7 +24,7 @@
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
 .\"
-.Dd August 13, 1995
+.Dd April 11, 2010
 .Dt FSCK_MSDOS 8
 .Os
 .Sh NAME
@@ -37,6 +37,7 @@
 .Ar filesystem ...
 .Nm
 .Op Fl fny
+.Op Fl x Ar snap-backup
 .Ar filesystem ...
 .Sh DESCRIPTION
 The
@@ -86,6 +87,17 @@
 .Dq CONTINUE? .
 .It Fl p
 Preen the specified filesystems.
+.It Fl x Ar snap-backup
+Use a snapshot with
+.Ar snap-backup
+as backup to check a read-write mounted filesystem. Must be used with
+.Fl n .
+See
+.Xr fss 4
+for more details.
+The point is to check an internally-consistent version of the
+filesystem to find out if it is damaged; on failure one should unmount
+the filesystem and repair it.
 .It Fl y
 Causes
 .Nm
@@ -94,6 +106,7 @@
 .Sh SEE ALSO
 .Xr fsck 8 ,
 .Xr fsck_ffs 8 ,
+.Xr fss 4 ,
 .Xr mount_msdos 8
 .Sh BUGS
 .Nm

Index: src/sbin/fsck_msdos/main.c
diff -u src/sbin/fsck_msdos/main.c:1.21 src/sbin/fsck_msdos/main.c:1.22
--- src/sbin/fsck_msdos/main.c:1.21	Fri Jun 13 20:46:09 2008
+++ src/sbin/fsck_msdos/main.c	Sun Apr 11 08:23:52 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.21 2008/06/13 20:46:09 martin Exp $	*/
+/*	$NetBSD: main.c,v 1.22 2010/04/11 08:23:52 hannken Exp $	*/
 
 /*
  * Copyright (C) 1995 Wolfgang Solfrank
@@ -28,18 +28,20 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: main.c,v 1.21 2008/06/13 20:46:09 martin Exp $");
+__RCSID("$NetBSD: main.c,v 1.22 2010/04/11 08:23:52 hannken Exp $");
 #endif /* not lint */
 
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <err.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <signal.h>
 
 #include "fsutil.h"
+#include "snapshot.h"
 #include "ext.h"
 #include "exitvalues.h"
 
@@ -53,7 +55,8 @@
 static void
 usage(void)
 {
-    	(void)fprintf(stderr, "Usage: %s [-fnpy] filesystem ... \n",
+    	(void)fprintf(stderr,
+	    "Usage: %s [-fnpy] [-x snap_backup] filesystem ... \n",
 	    getprogname());
 	exit(FSCK_EXIT_USAGE);
 }
@@ -68,9 +71,10 @@
 main(int argc, char **argv)
 {
 	int ret = FSCK_EXIT_OK, erg;
-	int ch;
+	int ch, snapfd;
+	char *snap_backup = NULL, *snap_dev;
 
-	while ((ch = getopt(argc, argv, "pPqynf")) != -1) {
+	while ((ch = getopt(argc, argv, "pPqynfx:")) != -1) {
 		switch (ch) {
 		case 'f':
 			/*
@@ -98,11 +102,19 @@
 		case 'q':		/* Quiet not implemented. */
 			break;
 
+		case 'x':
+			snap_backup = optarg;
+			break;
+
 		default:
 			usage();
 			break;
 		}
 	}
+	if (snap_backup != NULL && (!alwaysno || alwaysyes)) {
+		warnx("Cannot use -x without -n\n");
+		snap_backup = NULL;
+	}
 	argc -= optind;
 	argv += optind;
 
@@ -116,7 +128,18 @@
 
 	while (--argc >= 0) {
 		setcdevname(*argv, preen);
-		erg = checkfilesys(*argv++);
+		if (snap_backup != NULL) {
+			snapfd = snap_open(*argv, snap_backup, NULL, &snap_dev);
+			if (snapfd < 0) {
+				warn("can't take snapshot of %s", *argv);
+				erg = checkfilesys(*argv);
+			} else {
+				erg = checkfilesys(snap_dev);
+				close(snapfd);
+			}
+			argv++;
+		} else
+			erg = checkfilesys(*argv++);
 		if (erg > ret)
 			ret = erg;
 	}

Reply via email to