Home | History | Annotate | Line # | Download | only in mount_lfs
mount_lfs.c revision 1.34
      1 /*	$NetBSD: mount_lfs.c,v 1.34 2011/08/29 14:35:01 joerg 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.34 2011/08/29 14:35:01 joerg 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 #include "mountprog.h"
     64 #include "mount_lfs.h"
     65 
     66 static const struct mntopt mopts[] = {
     67 	MOPT_STDOPTS,
     68 	MOPT_UPDATE,
     69 	MOPT_GETARGS,
     70 	MOPT_NOATIME,
     71 	MOPT_NULL,
     72 };
     73 
     74 __dead static void	usage(void);
     75 
     76 #ifdef WANT_CLEANER
     77 __dead static void	invoke_cleaner(char *);
     78 static void	kill_daemon(char *);
     79 static void	kill_cleaner(char *);
     80 #endif /* WANT_CLEANER */
     81 
     82 static int short_rds, cleaner_debug, cleaner_bytes, fs_idle, noclean;
     83 static const char *nsegs;
     84 
     85 #ifndef MOUNT_NOMAIN
     86 int
     87 main(int argc, char **argv)
     88 {
     89 
     90 	setprogname(argv[0]);
     91 	return mount_lfs(argc, argv);
     92 }
     93 #endif
     94 
     95 void
     96 mount_lfs_parseargs(int argc, char *argv[],
     97 	struct ufs_args *args, int *mntflags,
     98 	char *canon_dev, char *canon_dir)
     99 {
    100 	int ch;
    101 	char *options;
    102 	mntoptparse_t mp;
    103 
    104 	memset(args, 0, sizeof(*args));
    105 	options = NULL;
    106 	nsegs = "4";
    107 	*mntflags = noclean = 0;
    108 	cleaner_bytes = 1;
    109 	while ((ch = getopt(argc, argv, "bdiN:no:s")) != -1)
    110 		switch (ch) {
    111 		case 'b':
    112 			cleaner_bytes = !cleaner_bytes;
    113 			break;
    114 		case 'd':
    115 			cleaner_debug = 1;
    116 			break;
    117 		case 'i':
    118 			fs_idle = 1;
    119 			break;
    120 		case 'n':
    121 			noclean = 1;
    122 			break;
    123 		case 'N':
    124 			nsegs = optarg;
    125 			break;
    126 		case 'o':
    127 			mp = getmntopts(optarg, mopts, mntflags, 0);
    128 			if (mp == NULL)
    129 				err(1, "getmntopts");
    130 			freemntopts(mp);
    131 			break;
    132 		case 's':
    133 			short_rds = 1;
    134 			break;
    135 		case '?':
    136 		default:
    137 			usage();
    138 		}
    139 	argc -= optind;
    140 	argv += optind;
    141 
    142 	if (argc != 2)
    143 		usage();
    144 
    145 	pathadj(argv[0], canon_dev);
    146 	args->fspec = canon_dev;
    147 
    148 	pathadj(argv[1], canon_dir);
    149 }
    150 
    151 int
    152 mount_lfs(int argc, char *argv[])
    153 {
    154 	struct ufs_args args;
    155 	int mntflags;
    156 	int mntsize, oldflags, i;
    157 	char fs_name[MAXPATHLEN], canon_dev[MAXPATHLEN];
    158 	struct statvfs *mntbuf;
    159 	const char *errcause;
    160 
    161 	mount_lfs_parseargs(argc, argv, &args, &mntflags, canon_dev, fs_name);
    162 
    163 	/*
    164 	 * Record the previous status of this filesystem (if any) before
    165 	 * performing the mount, so we can know whether to start or
    166 	 * kill the cleaner process below.
    167 	 */
    168 	oldflags = MNT_RDONLY; /* If not mounted, pretend r/o */
    169 	if (mntflags & MNT_UPDATE) {
    170 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    171 			err(1, "getmntinfo");
    172 		for (i = 0; i < mntsize; i++) {
    173 			if (strcmp(mntbuf[i].f_mntfromname, args.fspec) == 0) {
    174 				oldflags = mntbuf[i].f_flag;
    175 				break;
    176 			}
    177 		}
    178 	}
    179 
    180 	if (mount(MOUNT_LFS, fs_name, mntflags, &args, sizeof args) == -1) {
    181 		switch (errno) {
    182 		case EMFILE:
    183 			errcause = "mount table full";
    184 			break;
    185 		case EINVAL:
    186 			if (mntflags & MNT_UPDATE)
    187 				errcause =
    188 			    "specified device does not match mounted device";
    189 			else
    190 				errcause = "incorrect super block";
    191 			break;
    192 		default:
    193 			errcause = strerror(errno);
    194 			break;
    195 		}
    196 		errx(1, "%s on %s: %s", args.fspec, fs_name, errcause);
    197 	}
    198 
    199 #ifdef WANT_CLEANER
    200 	/* Not mounting fresh or upgrading to r/w; don't start the cleaner */
    201 	if (!(oldflags & MNT_RDONLY) || (mntflags & MNT_RDONLY)
    202 	    || (mntflags & MNT_GETARGS))
    203 		noclean = 1;
    204 	if (!noclean)
    205 		invoke_cleaner(fs_name);
    206 		/* NOTREACHED */
    207 
    208 	/* Downgrade to r/o; kill the cleaner */
    209 	if ((mntflags & MNT_RDONLY) && !(oldflags & MNT_RDONLY))
    210 		kill_cleaner(fs_name);
    211 #endif /* WANT_CLEANER */
    212 
    213 	exit(0);
    214 }
    215 
    216 #ifdef WANT_CLEANER
    217 static void
    218 kill_daemon(char *pidname)
    219 {
    220 	FILE *fp;
    221 	char s[80];
    222 	pid_t pid;
    223 
    224 	fp = fopen(pidname, "r");
    225 	if (fp) {
    226 		fgets(s, 80, fp);
    227 		pid = atoi(s);
    228 		if (pid)
    229 			kill(pid, SIGINT);
    230 		fclose(fp);
    231 	}
    232 }
    233 
    234 static void
    235 kill_cleaner(char *name)
    236 {
    237 	char *pidname;
    238 	char *cp;
    239 	int off;
    240 
    241 	/* Parent first */
    242 	asprintf(&pidname, "%slfs_cleanerd:m:%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 	/* Then child */
    252 	asprintf(&pidname, "%slfs_cleanerd:s:%s.pid", _PATH_VARRUN, name);
    253 	if (!pidname)
    254 		err(1, "malloc");
    255 	off = strlen(_PATH_VARRUN);
    256 	while((cp = strchr(pidname + off, '/')) != NULL)
    257 		*cp = '|';
    258 	kill_daemon(pidname);
    259 	free(pidname);
    260 }
    261 
    262 static void
    263 invoke_cleaner(char *name)
    264 {
    265 	const char *args[7], **ap = args;
    266 
    267 	/* Build the argument list. */
    268 	*ap++ = _PATH_LFS_CLEANERD;
    269 	if (cleaner_bytes)
    270 		*ap++ = "-b";
    271 	if (nsegs) {
    272 		*ap++ = "-n";
    273 		*ap++ = nsegs;
    274 	}
    275 	if (short_rds)
    276 		*ap++ = "-s";
    277 	if (cleaner_debug)
    278 		*ap++ = "-d";
    279 	if (fs_idle)
    280 		*ap++ = "-f";
    281 	*ap++ = name;
    282 	*ap = NULL;
    283 
    284 	execv(args[0], __UNCONST(args));
    285 	err(1, "exec %s", _PATH_LFS_CLEANERD);
    286 }
    287 #endif /* WANT_CLEANER */
    288 
    289 static void
    290 usage(void)
    291 {
    292 	(void)fprintf(stderr,
    293 		"usage: %s [-bdins] [-N nsegs] [-o options] special node\n",
    294 		getprogname());
    295 	exit(1);
    296 }
    297