Home | History | Annotate | Line # | Download | only in mount_lfs
mount_lfs.c revision 1.32
      1 /*	$NetBSD: mount_lfs.c,v 1.32 2008/07/20 01:20:22 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1993, 1994\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)mount_lfs.c	8.4 (Berkeley) 4/26/95";
     41 #else
     42 __RCSID("$NetBSD: mount_lfs.c,v 1.32 2008/07/20 01:20:22 lukem Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/mount.h>
     48 
     49 #include <ufs/ufs/ufsmount.h>
     50 
     51 #include <err.h>
     52 #include <errno.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 #include <paths.h>
     58 
     59 #include <signal.h>
     60 
     61 #include <mntopts.h>
     62 #include "pathnames.h"
     63 
     64 static const struct mntopt mopts[] = {
     65 	MOPT_STDOPTS,
     66 	MOPT_UPDATE,
     67 	MOPT_GETARGS,
     68 	MOPT_NOATIME,
     69 	MOPT_NULL,
     70 };
     71 
     72 int		mount_lfs(int, char *[]);
     73 static void	invoke_cleaner(char *);
     74 static void	usage(void);
     75 static void	kill_daemon(char *);
     76 static void	kill_cleaner(char *);
     77 
     78 static int short_rds, cleaner_debug, cleaner_bytes, fs_idle;
     79 static const char *nsegs;
     80 
     81 #ifndef MOUNT_NOMAIN
     82 int
     83 main(int argc, char **argv)
     84 {
     85 	return mount_lfs(argc, argv);
     86 }
     87 #endif
     88 
     89 int
     90 mount_lfs(int argc, char *argv[])
     91 {
     92 	struct ufs_args args;
     93 	int ch, mntflags, noclean, mntsize, oldflags, i;
     94 	char fs_name[MAXPATHLEN], canon_dev[MAXPATHLEN];
     95 	char *options;
     96 	mntoptparse_t mp;
     97 
     98 	const char *errcause;
     99 	struct statvfs *mntbuf;
    100 
    101 	options = NULL;
    102 	nsegs = "4";
    103 	mntflags = noclean = 0;
    104 	cleaner_bytes = 1;
    105 	while ((ch = getopt(argc, argv, "bdiN:no:s")) != -1)
    106 		switch (ch) {
    107 		case 'b':
    108 			cleaner_bytes = !cleaner_bytes;
    109 			break;
    110 		case 'd':
    111 			cleaner_debug = 1;
    112 			break;
    113 		case 'i':
    114 			fs_idle = 1;
    115 			break;
    116 		case 'n':
    117 			noclean = 1;
    118 			break;
    119 		case 'N':
    120 			nsegs = optarg;
    121 			break;
    122 		case 'o':
    123 			mp = getmntopts(optarg, mopts, &mntflags, 0);
    124 			if (mp == NULL)
    125 				err(1, "getmntopts");
    126 			freemntopts(mp);
    127 			break;
    128 		case 's':
    129 			short_rds = 1;
    130 			break;
    131 		case '?':
    132 		default:
    133 			usage();
    134 		}
    135 	argc -= optind;
    136 	argv += optind;
    137 
    138 	if (argc != 2)
    139 		usage();
    140 
    141 	if (realpath(argv[0], canon_dev) == NULL)     /* Check device path */
    142 		err(1, "realpath %s", argv[0]);
    143 	if (strncmp(argv[0], canon_dev, MAXPATHLEN)) {
    144 		warnx("\"%s\" is a relative path.", argv[0]);
    145 		warnx("using \"%s\" instead.", canon_dev);
    146 	}
    147 	args.fspec = canon_dev;
    148 
    149 	if (realpath(argv[1], fs_name) == NULL)      /* Check mounton path */
    150 		err(1, "realpath %s", argv[1]);
    151 	if (strncmp(argv[1], fs_name, MAXPATHLEN)) {
    152 		warnx("\"%s\" is a relative path.", argv[1]);
    153 		warnx("using \"%s\" instead.", fs_name);
    154 	}
    155 
    156 	/*
    157 	 * Record the previous status of this filesystem (if any) before
    158 	 * performing the mount, so we can know whether to start or
    159 	 * kill the cleaner process below.
    160 	 */
    161 	oldflags = MNT_RDONLY; /* If not mounted, pretend r/o */
    162 	if (mntflags & MNT_UPDATE) {
    163 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    164 			err(1, "getmntinfo");
    165 		for (i = 0; i < mntsize; i++) {
    166 			if (strcmp(mntbuf[i].f_mntfromname, args.fspec) == 0) {
    167 				oldflags = mntbuf[i].f_flag;
    168 				break;
    169 			}
    170 		}
    171 	}
    172 
    173 	if (mount(MOUNT_LFS, fs_name, mntflags, &args, sizeof args) == -1) {
    174 		switch (errno) {
    175 		case EMFILE:
    176 			errcause = "mount table full";
    177 			break;
    178 		case EINVAL:
    179 			if (mntflags & MNT_UPDATE)
    180 				errcause =
    181 			    "specified device does not match mounted device";
    182 			else
    183 				errcause = "incorrect super block";
    184 			break;
    185 		default:
    186 			errcause = strerror(errno);
    187 			break;
    188 		}
    189 		errx(1, "%s on %s: %s", args.fspec, fs_name, errcause);
    190 	}
    191 
    192 	/* Not mounting fresh or upgrading to r/w; don't start the cleaner */
    193 	if (!(oldflags & MNT_RDONLY) || (mntflags & MNT_RDONLY)
    194 	    || (mntflags & MNT_GETARGS))
    195 		noclean = 1;
    196 	if (!noclean)
    197 		invoke_cleaner(fs_name);
    198 		/* NOTREACHED */
    199 
    200 	/* Downgrade to r/o; kill the cleaner */
    201 	if ((mntflags & MNT_RDONLY) && !(oldflags & MNT_RDONLY))
    202 		kill_cleaner(fs_name);
    203 
    204 	exit(0);
    205 }
    206 
    207 static void
    208 kill_daemon(char *pidname)
    209 {
    210 	FILE *fp;
    211 	char s[80];
    212 	pid_t pid;
    213 
    214 	fp = fopen(pidname, "r");
    215 	if (fp) {
    216 		fgets(s, 80, fp);
    217 		pid = atoi(s);
    218 		if (pid)
    219 			kill(pid, SIGINT);
    220 		fclose(fp);
    221 	}
    222 }
    223 
    224 static void
    225 kill_cleaner(char *name)
    226 {
    227 	char *pidname;
    228 	char *cp;
    229 	int off;
    230 
    231 	/* Parent first */
    232 	asprintf(&pidname, "%slfs_cleanerd:m:%s.pid", _PATH_VARRUN, name);
    233 	if (!pidname)
    234 		err(1, "malloc");
    235 	off = strlen(_PATH_VARRUN);
    236 	while((cp = strchr(pidname + off, '/')) != NULL)
    237 		*cp = '|';
    238 	kill_daemon(pidname);
    239 	free(pidname);
    240 
    241 	/* Then child */
    242 	asprintf(&pidname, "%slfs_cleanerd:s:%s.pid", _PATH_VARRUN, name);
    243 	if (!pidname)
    244 		err(1, "malloc");
    245 	off = strlen(_PATH_VARRUN);
    246 	while((cp = strchr(pidname + off, '/')) != NULL)
    247 		*cp = '|';
    248 	kill_daemon(pidname);
    249 	free(pidname);
    250 }
    251 
    252 static void
    253 invoke_cleaner(char *name)
    254 {
    255 	const char *args[7], **ap = args;
    256 
    257 	/* Build the argument list. */
    258 	*ap++ = _PATH_LFS_CLEANERD;
    259 	if (cleaner_bytes)
    260 		*ap++ = "-b";
    261 	if (nsegs) {
    262 		*ap++ = "-n";
    263 		*ap++ = nsegs;
    264 	}
    265 	if (short_rds)
    266 		*ap++ = "-s";
    267 	if (cleaner_debug)
    268 		*ap++ = "-d";
    269 	if (fs_idle)
    270 		*ap++ = "-f";
    271 	*ap++ = name;
    272 	*ap = NULL;
    273 
    274 	execv(args[0], __UNCONST(args));
    275 	err(1, "exec %s", _PATH_LFS_CLEANERD);
    276 }
    277 
    278 static void
    279 usage(void)
    280 {
    281 	(void)fprintf(stderr,
    282 		"usage: %s [-bdins] [-N nsegs] [-o options] special node\n",
    283 		getprogname());
    284 	exit(1);
    285 }
    286