git: fe7121ec5b8b - stable/13 - Update fsck(8) to ignore failures from a check program for a filesystem when the fstab(5) entry for the filesystem has the "failok" attribute.

From: Kirk McKusick <mckusick_at_FreeBSD.org>
Date: Sun, 02 Jan 2022 22:19:39 UTC
The branch stable/13 has been updated by mckusick:

URL: https://cgit.FreeBSD.org/src/commit/?id=fe7121ec5b8b1c98c2933902be7aef1850030154

commit fe7121ec5b8b1c98c2933902be7aef1850030154
Author:     Kirk McKusick <mckusick@FreeBSD.org>
AuthorDate: 2021-12-16 00:51:55 +0000
Commit:     Kirk McKusick <mckusick@FreeBSD.org>
CommitDate: 2022-01-02 22:12:14 +0000

    Update fsck(8) to ignore failures from a check program for a filesystem
    when the fstab(5) entry for the filesystem has the "failok" attribute.
    
    PR:           246201
    Sponsored by: Netflix
    Differential Revision: https://reviews.freebsd.org/D33424
    
    (cherry picked from commit c72372c693b36058b58a525981e833515ce8e441)
---
 sbin/fsck/fsck.c   |  3 +++
 sbin/fsck/fsutil.c | 39 +++++++++++++++++++++++++++++++++++++++
 sbin/fsck/fsutil.h |  7 +++----
 sbin/fsck/preen.c  | 24 +++++++++++++++---------
 4 files changed, 60 insertions(+), 13 deletions(-)

diff --git a/sbin/fsck/fsck.c b/sbin/fsck/fsck.c
index 84d2e2fe422f..bb053fe56253 100644
--- a/sbin/fsck/fsck.c
+++ b/sbin/fsck/fsck.c
@@ -254,6 +254,9 @@ isok(struct fstab *fs)
 		return (0);
 	if (!selected(fs->fs_vfstype))
 		return (0);
+	/* If failok, always check now */
+	if (getfsopt(fs, "failok"))
+		return (1);
 	/*
 	 * If the -B flag has been given, then process the needed
 	 * background checks. Background checks cannot be run on
diff --git a/sbin/fsck/fsutil.c b/sbin/fsck/fsutil.c
index 012bd434dce6..9644697e2530 100644
--- a/sbin/fsck/fsutil.c
+++ b/sbin/fsck/fsutil.c
@@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/mount.h>
 
 #include <err.h>
+#include <fstab.h>
 #include <paths.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -55,6 +56,44 @@ static int preen = 0;
 
 static void vmsg(int, const char *, va_list) __printflike(2, 0);
 
+/*
+ * The getfsopt() function checks whether an option is present in
+ * an fstab(5) fs_mntops entry. There are six possible cases:
+ *
+ * fs_mntops  getfsopt  result
+ *  rw,foo       foo    true
+ *  rw,nofoo    nofoo   true
+ *  rw,nofoo     foo    false
+ *  rw,foo      nofoo   false
+ *  rw           foo    false
+ *  rw          nofoo   false
+ *
+ * This function should be part of and documented in getfsent(3).
+ */
+int
+getfsopt(struct fstab *fs, const char *option)
+{
+	int negative, found;
+	char *opt, *optbuf;
+
+	if (option[0] == 'n' && option[1] == 'o') {
+		negative = 1;
+		option += 2;
+	} else
+		negative = 0;
+	optbuf = strdup(fs->fs_mntops);
+	found = 0;
+	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
+		if (opt[0] == 'n' && opt[1] == 'o') {
+			if (!strcasecmp(opt + 2, option))
+				found = negative;
+		} else if (!strcasecmp(opt, option))
+			found = !negative;
+	}
+	free(optbuf);
+	return (found);
+}
+
 void
 setcdevname(const char *cd, int pr)
 {
diff --git a/sbin/fsck/fsutil.h b/sbin/fsck/fsutil.h
index c6300a0cc0c4..8bb9ddb90d6e 100644
--- a/sbin/fsck/fsutil.h
+++ b/sbin/fsck/fsutil.h
@@ -28,6 +28,9 @@
  * $FreeBSD$
  */
 
+int checkfstab(int, int (*)(struct fstab *), 
+    int (*) (const char *, const char *, const char *, const char *, pid_t *));
+int getfsopt(struct fstab *, const char *);
 void pfatal(const char *, ...) __printflike(1, 2);
 void pwarn(const char *, ...) __printflike(1, 2);
 void perr(const char *, ...) __printflike(1, 2);
@@ -46,7 +49,3 @@ char *estrdup(const char *);
 #define	CHECK_BACKGRD	0x0008
 #define	DO_BACKGRD	0x0010
 #define	CHECK_CLEAN	0x0020
-
-struct fstab;
-int checkfstab(int, int (*)(struct fstab *), 
-    int (*) (const char *, const char *, const char *, const char *, pid_t *));
diff --git a/sbin/fsck/preen.c b/sbin/fsck/preen.c
index e40ec5d4fb91..c8a36a608695 100644
--- a/sbin/fsck/preen.c
+++ b/sbin/fsck/preen.c
@@ -62,6 +62,7 @@ struct partentry {
 	char		  	*p_devname;	/* device name */
 	char			*p_mntpt;	/* mount point */
 	char		  	*p_type;	/* file system type */
+	int			 p_failok;	/* failok option set */
 };
 
 static TAILQ_HEAD(part, partentry) badh;
@@ -78,7 +79,7 @@ static TAILQ_HEAD(disk, diskentry) diskh;
 static int nrun = 0, ndisks = 0;
 
 static struct diskentry *finddisk(const char *);
-static void addpart(const char *, const char *, const char *);
+static void addpart(const char *, const char *, const char *, const int);
 static int startdisk(struct diskentry *, 
     int (*)(const char *, const char *, const char *, const char *, pid_t *));
 static void printpart(void);
@@ -132,7 +133,6 @@ checkfstab(int flags, int (*docheck)(struct fstab *),
 				}
 				sumstatus = (*checkit)(fs->fs_vfstype,
 				    name, fs->fs_file, NULL, NULL);
-
 				if (sumstatus)
 					return (sumstatus);
 				continue;
@@ -143,7 +143,8 @@ checkfstab(int flags, int (*docheck)(struct fstab *),
 				sumstatus |= 8;
 				continue;
 			}
-			addpart(fs->fs_vfstype, name, fs->fs_file);
+			addpart(fs->fs_vfstype, name, fs->fs_file,
+			    getfsopt(fs, "failok"));
 		}
 
 		if ((flags & CHECK_PREEN) == 0 || passno == 1 ||
@@ -172,12 +173,17 @@ checkfstab(int flags, int (*docheck)(struct fstab *),
 				continue;
 			}
 
-			if (WIFEXITED(status))
+			p = TAILQ_FIRST(&d->d_part);
+
+			if (WIFEXITED(status) == 0) {
+				retcode = 0;
+			} else if (p->p_failok == 0) {
 				retcode = WEXITSTATUS(status);
-			else
+			} else {
 				retcode = 0;
-
-			p = TAILQ_FIRST(&d->d_part);
+				fprintf(stderr, "%s: failok SPECIFIED, FSCK "
+				    "ERROR(S) IGNORED\n", p->p_devname);
+			}
 
 			if (flags & (CHECK_DEBUG|CHECK_VERBOSE))
 				(void) printf("done %s: %s (%s) = 0x%x\n",
@@ -243,7 +249,6 @@ checkfstab(int flags, int (*docheck)(struct fstab *),
 	return (0);
 }
 
-
 static struct diskentry *
 finddisk(const char *name)
 {
@@ -297,7 +302,7 @@ printpart(void)
 
 
 static void
-addpart(const char *type, const char *dev, const char *mntpt)
+addpart(const char *type, const char *dev, const char *mntpt, const int failok)
 {
 	struct diskentry *d = finddisk(dev);
 	struct partentry *p;
@@ -312,6 +317,7 @@ addpart(const char *type, const char *dev, const char *mntpt)
 	p->p_devname = estrdup(dev);
 	p->p_mntpt = estrdup(mntpt);
 	p->p_type = estrdup(type);
+	p->p_failok = failok;
 
 	TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
 }