mount_lfs.c revision 1.36 1 /* $NetBSD: mount_lfs.c,v 1.36 2013/10/19 17:04:41 christos 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.36 2013/10/19 17:04:41 christos 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 mntoptparse_t mp;
102
103 memset(args, 0, sizeof(*args));
104 nsegs = "4";
105 *mntflags = noclean = 0;
106 cleaner_bytes = 1;
107 while ((ch = getopt(argc, argv, "bdiN:no:s")) != -1)
108 switch (ch) {
109 case 'b':
110 cleaner_bytes = !cleaner_bytes;
111 break;
112 case 'd':
113 cleaner_debug = 1;
114 break;
115 case 'i':
116 fs_idle = 1;
117 break;
118 case 'n':
119 noclean = 1;
120 break;
121 case 'N':
122 nsegs = optarg;
123 break;
124 case 'o':
125 mp = getmntopts(optarg, mopts, mntflags, 0);
126 if (mp == NULL)
127 err(1, "getmntopts");
128 freemntopts(mp);
129 break;
130 case 's':
131 short_rds = 1;
132 break;
133 case '?':
134 default:
135 usage();
136 }
137 argc -= optind;
138 argv += optind;
139
140 if (argc != 2)
141 usage();
142
143 pathadj(argv[0], canon_dev);
144 args->fspec = canon_dev;
145
146 pathadj(argv[1], canon_dir);
147 }
148
149 int
150 mount_lfs(int argc, char *argv[])
151 {
152 struct ufs_args args;
153 int mntflags;
154 int mntsize, i;
155 char fs_name[MAXPATHLEN], canon_dev[MAXPATHLEN];
156 struct statvfs *mntbuf;
157 const char *errcause;
158
159 mount_lfs_parseargs(argc, argv, &args, &mntflags, canon_dev, fs_name);
160
161 /*
162 * Record the previous status of this filesystem (if any) before
163 * performing the mount, so we can know whether to start or
164 * kill the cleaner process below.
165 */
166 if (mntflags & MNT_UPDATE) {
167 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
168 err(1, "getmntinfo");
169 for (i = 0; i < mntsize; i++) {
170 if (strcmp(mntbuf[i].f_mntfromname, args.fspec) == 0) {
171 break;
172 }
173 }
174 }
175
176 if (mount(MOUNT_LFS, fs_name, mntflags, &args, sizeof args) == -1) {
177 switch (errno) {
178 case EMFILE:
179 errcause = "mount table full";
180 break;
181 case EINVAL:
182 if (mntflags & MNT_UPDATE)
183 errcause =
184 "specified device does not match mounted device";
185 else
186 errcause = "incorrect super block";
187 break;
188 default:
189 errcause = strerror(errno);
190 break;
191 }
192 errx(1, "%s on %s: %s", args.fspec, fs_name, errcause);
193 }
194
195 #ifdef WANT_CLEANER
196 /* Not mounting fresh or upgrading to r/w; don't start the cleaner */
197 if (!(oldflags & MNT_RDONLY) || (mntflags & MNT_RDONLY)
198 || (mntflags & MNT_GETARGS))
199 noclean = 1;
200 if (!noclean)
201 invoke_cleaner(fs_name);
202 /* NOTREACHED */
203
204 /* Downgrade to r/o; kill the cleaner */
205 if ((mntflags & MNT_RDONLY) && !(oldflags & MNT_RDONLY))
206 kill_cleaner(fs_name);
207 #endif /* WANT_CLEANER */
208
209 exit(0);
210 }
211
212 #ifdef WANT_CLEANER
213 static void
214 kill_daemon(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(char *name)
232 {
233 char *pidname;
234 char *cp;
235 int off;
236
237 /* Parent first */
238 asprintf(&pidname, "%slfs_cleanerd:m:%s.pid", _PATH_VARRUN, name);
239 if (!pidname)
240 err(1, "malloc");
241 off = strlen(_PATH_VARRUN);
242 while((cp = strchr(pidname + off, '/')) != NULL)
243 *cp = '|';
244 kill_daemon(pidname);
245 free(pidname);
246
247 /* Then child */
248 asprintf(&pidname, "%slfs_cleanerd:s:%s.pid", _PATH_VARRUN, name);
249 if (!pidname)
250 err(1, "malloc");
251 off = strlen(_PATH_VARRUN);
252 while((cp = strchr(pidname + off, '/')) != NULL)
253 *cp = '|';
254 kill_daemon(pidname);
255 free(pidname);
256 }
257
258 static void
259 invoke_cleaner(char *name)
260 {
261 const char *args[7], **ap = args;
262
263 /* Build the argument list. */
264 *ap++ = _PATH_LFS_CLEANERD;
265 if (cleaner_bytes)
266 *ap++ = "-b";
267 if (nsegs) {
268 *ap++ = "-n";
269 *ap++ = nsegs;
270 }
271 if (short_rds)
272 *ap++ = "-s";
273 if (cleaner_debug)
274 *ap++ = "-d";
275 if (fs_idle)
276 *ap++ = "-f";
277 *ap++ = name;
278 *ap = NULL;
279
280 execv(args[0], __UNCONST(args));
281 err(1, "exec %s", _PATH_LFS_CLEANERD);
282 }
283 #endif /* WANT_CLEANER */
284
285 static void
286 usage(void)
287 {
288 (void)fprintf(stderr,
289 "usage: %s [-bdins] [-N nsegs] [-o options] special node\n",
290 getprogname());
291 exit(1);
292 }
293