1 /* $NetBSD: mount_lfs.c,v 1.32 2008/07/20 01:20:22 lukem Exp $ */
4 * Copyright (c) 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)mount_lfs.c 8.4 (Berkeley) 4/26/95";
42 __RCSID("$NetBSD: mount_lfs.c,v 1.32 2008/07/20 01:20:22 lukem Exp $");
46 #include <sys/param.h>
47 #include <sys/mount.h>
49 #include <ufs/ufs/ufsmount.h>
62 #include "pathnames.h"
63 #include "mountprog.h"
64 #include "mount_lfs.h"
66 static const struct mntopt mopts
[] = {
74 static void usage(void);
77 static void invoke_cleaner(char *);
78 static void kill_daemon(char *);
79 static void kill_cleaner(char *);
80 #endif /* WANT_CLEANER */
82 static int short_rds
, cleaner_debug
, cleaner_bytes
, fs_idle
, noclean
;
83 static const char *nsegs
;
87 main(int argc
, char **argv
)
91 return mount_lfs(argc
, argv
);
96 mount_lfs_parseargs(int argc
, char *argv
[],
97 struct ufs_args
*args
, int *mntflags
,
98 char *canon_dev
, char *canon_dir
)
104 memset(args
, 0, sizeof(*args
));
107 *mntflags
= noclean
= 0;
109 while ((ch
= getopt(argc
, argv
, "bdiN:no:s")) != -1)
112 cleaner_bytes
= !cleaner_bytes
;
127 mp
= getmntopts(optarg
, mopts
, mntflags
, 0);
129 err(1, "getmntopts");
145 pathadj(argv
[0], canon_dev
);
146 args
->fspec
= canon_dev
;
148 pathadj(argv
[1], canon_dir
);
152 mount_lfs(int argc
, char *argv
[])
154 struct ufs_args args
;
156 int mntsize
, oldflags
, i
;
157 char fs_name
[MAXPATHLEN
], canon_dev
[MAXPATHLEN
];
158 struct statvfs
*mntbuf
;
159 const char *errcause
;
161 mount_lfs_parseargs(argc
, argv
, &args
, &mntflags
, canon_dev
, fs_name
);
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.
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
;
180 if (mount(MOUNT_LFS
, fs_name
, mntflags
, &args
, sizeof args
) == -1) {
183 errcause
= "mount table full";
186 if (mntflags
& MNT_UPDATE
)
188 "specified device does not match mounted device";
190 errcause
= "incorrect super block";
193 errcause
= strerror(errno
);
196 errx(1, "%s on %s: %s", args
.fspec
, fs_name
, errcause
);
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
))
205 invoke_cleaner(fs_name
);
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 */
218 kill_daemon(char *pidname
)
224 fp
= fopen(pidname
, "r");
235 kill_cleaner(char *name
)
242 asprintf(&pidname
, "%slfs_cleanerd:m:%s.pid", _PATH_VARRUN
, name
);
245 off
= strlen(_PATH_VARRUN
);
246 while((cp
= strchr(pidname
+ off
, '/')) != NULL
)
248 kill_daemon(pidname
);
252 asprintf(&pidname
, "%slfs_cleanerd:s:%s.pid", _PATH_VARRUN
, name
);
255 off
= strlen(_PATH_VARRUN
);
256 while((cp
= strchr(pidname
+ off
, '/')) != NULL
)
258 kill_daemon(pidname
);
263 invoke_cleaner(char *name
)
265 const char *args
[7], **ap
= args
;
267 /* Build the argument list. */
268 *ap
++ = _PATH_LFS_CLEANERD
;
284 execv(args
[0], __UNCONST(args
));
285 err(1, "exec %s", _PATH_LFS_CLEANERD
);
287 #endif /* WANT_CLEANER */
292 (void)fprintf(stderr
,
293 "usage: %s [-bdins] [-N nsegs] [-o options] special node\n",