mount_lfs.c revision 1.21 1 /* $NetBSD: mount_lfs.c,v 1.21 2005/01/31 05:19:19 erh 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\n\
35 The Regents of the University of California. All rights reserved.\n");
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.21 2005/01/31 05:19:19 erh 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 { NULL }
70 };
71
72 int main __P((int, char *[]));
73 int mount_lfs __P((int, char *[]));
74 static void invoke_cleaner __P((char *));
75 static void usage __P((void));
76 static void kill_daemon __P((char *));
77 static void kill_cleaner __P((char *));
78
79 static int short_rds, cleaner_debug, cleaner_bytes;
80 static char *nsegs;
81
82 #ifndef MOUNT_NOMAIN
83 int
84 main(argc, argv)
85 int argc;
86 char **argv;
87 {
88 return mount_lfs(argc, argv);
89 }
90 #endif
91
92 int
93 mount_lfs(argc, argv)
94 int argc;
95 char *argv[];
96 {
97 struct ufs_args args;
98 int ch, mntflags, noclean, mntsize, oldflags, i;
99 char fs_name[MAXPATHLEN], canon_dev[MAXPATHLEN];
100 char *options;
101
102 const char *errcause;
103 struct statvfs *mntbuf;
104
105 options = NULL;
106 nsegs = "4";
107 mntflags = noclean = 0;
108 cleaner_bytes = 1;
109 while ((ch = getopt(argc, argv, "bdN: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 'n':
118 noclean = 1;
119 break;
120 case 'N':
121 nsegs = optarg;
122 break;
123 case 'o':
124 getmntopts(optarg, mopts, &mntflags, 0);
125 break;
126 case 's':
127 short_rds = 1;
128 break;
129 case '?':
130 default:
131 usage();
132 }
133 argc -= optind;
134 argv += optind;
135
136 if (argc != 2)
137 usage();
138
139 if (realpath(argv[0], canon_dev) == NULL) /* Check device path */
140 err(1, "realpath %s", argv[0]);
141 if (strncmp(argv[0], canon_dev, MAXPATHLEN)) {
142 warnx("\"%s\" is a relative path.", argv[0]);
143 warnx("using \"%s\" instead.", canon_dev);
144 }
145 args.fspec = canon_dev;
146
147 if (realpath(argv[1], fs_name) == NULL) /* Check mounton path */
148 err(1, "realpath %s", argv[1]);
149 if (strncmp(argv[1], fs_name, MAXPATHLEN)) {
150 warnx("\"%s\" is a relative path.", argv[1]);
151 warnx("using \"%s\" instead.", fs_name);
152 }
153
154 #define DEFAULT_ROOTUID -2
155 args.export.ex_root = DEFAULT_ROOTUID;
156 if (mntflags & MNT_RDONLY) {
157 args.export.ex_flags = MNT_EXRDONLY;
158 noclean = 1;
159 } else
160 args.export.ex_flags = 0;
161
162 /*
163 * Record the previous status of this filesystem (if any) before
164 * performing the mount, so we can know whether to start or
165 * kill the cleaner process below.
166 */
167 oldflags = MNT_RDONLY; /* If not mounted, pretend r/o */
168 if (mntflags & MNT_UPDATE) {
169 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
170 err(1, "getmntinfo");
171 for (i = 0; i < mntsize; i++) {
172 if (strcmp(mntbuf[i].f_mntfromname, args.fspec) == 0) {
173 oldflags = mntbuf[i].f_flag;
174 break;
175 }
176 }
177 }
178
179 if (mount(MOUNT_LFS, fs_name, mntflags, &args)) {
180 switch (errno) {
181 case EMFILE:
182 errcause = "mount table full";
183 break;
184 case EINVAL:
185 if (mntflags & MNT_UPDATE)
186 errcause =
187 "specified device does not match mounted device";
188 else
189 errcause = "incorrect super block";
190 break;
191 default:
192 errcause = strerror(errno);
193 break;
194 }
195 errx(1, "%s on %s: %s", args.fspec, fs_name, errcause);
196 }
197
198 /* Not mounting fresh or upgrading to r/w; don't start the cleaner */
199 if (!(oldflags & MNT_RDONLY) || (mntflags & MNT_RDONLY))
200 noclean = 1;
201 if (!noclean)
202 invoke_cleaner(fs_name);
203 /* NOTREACHED */
204
205 /* Downgrade to r/o; kill the cleaner */
206 if ((mntflags & MNT_RDONLY) && !(oldflags & MNT_RDONLY))
207 kill_cleaner(fs_name);
208
209 exit(0);
210 }
211
212 static void
213 kill_daemon(pidname)
214 char *pidname;
215 {
216 FILE *fp;
217 char s[80];
218 pid_t pid;
219
220 fp = fopen(pidname, "r");
221 if (fp) {
222 fgets(s, 80, fp);
223 pid = atoi(s);
224 if (pid)
225 kill(pid, SIGINT);
226 fclose(fp);
227 }
228 }
229
230 static void
231 kill_cleaner(name)
232 char *name;
233 {
234 char *pidname;
235 char *cp;
236 int off;
237
238 /* Parent first */
239 asprintf(&pidname, "%slfs_cleanerd:m:%s.pid", _PATH_VARRUN, name);
240 if (!pidname)
241 err(1, "malloc");
242 off = strlen(_PATH_VARRUN);
243 while((cp = strchr(pidname + off, '/')) != NULL)
244 *cp = '|';
245 kill_daemon(pidname);
246 free(pidname);
247
248 /* Then child */
249 asprintf(&pidname, "%slfs_cleanerd:s:%s.pid", _PATH_VARRUN, name);
250 if (!pidname)
251 err(1, "malloc");
252 off = strlen(_PATH_VARRUN);
253 while((cp = strchr(pidname + off, '/')) != NULL)
254 *cp = '|';
255 kill_daemon(pidname);
256 free(pidname);
257 }
258
259 static void
260 invoke_cleaner(name)
261 char *name;
262 {
263 char *args[6], **ap = args;
264
265 /* Build the argument list. */
266 *ap++ = _PATH_LFS_CLEANERD;
267 if (cleaner_bytes)
268 *ap++ = "-b";
269 if (nsegs) {
270 *ap++ = "-n";
271 *ap++ = nsegs;
272 }
273 if (short_rds)
274 *ap++ = "-s";
275 if (cleaner_debug)
276 *ap++ = "-d";
277 *ap++ = name;
278 *ap = NULL;
279
280 execv(args[0], args);
281 err(1, "exec %s", _PATH_LFS_CLEANERD);
282 }
283
284 static void
285 usage()
286 {
287 (void)fprintf(stderr,
288 "usage: %s [-bdns] [-N nsegs] [-o options] special node\n",
289 getprogname());
290 exit(1);
291 }
292