2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sbin/mountctl/mountctl.c,v 1.4 2005/03/22 21:42:39 dillon Exp $
37 * This utility implements the userland mountctl command which is used to
38 * manage high level journaling on mount points.
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/ucred.h>
44 #include <sys/mount.h>
46 #include <sys/mountctl.h>
53 static volatile void usage(void);
54 static void parse_option_keyword(const char *opt,
55 const char **wopt, const char **xopt);
56 static int64_t getsize(const char *str);
57 static const char *numtostr(int64_t num);
59 static int mountctl_scan(void (*func)(const char *, const char *, int, void *),
60 const char *keyword, const char *mountpt, int fd);
61 static void mountctl_list(const char *keyword, const char *mountpt,
62 int __unused fd, void *info);
63 static void mountctl_add(const char *keyword, const char *mountpt, int fd);
64 static void mountctl_delete(const char *keyword, const char *mountpt,
65 int __unused fd, void __unused *);
66 static void mountctl_modify(const char *keyword, const char *mountpt, int fd, void __unused *);
69 * For all options 0 means unspecified, -1 means noOPT or nonOPT, and a
70 * positive number indicates enabling or execution of the option.
73 static int freeze_opt;
78 static int reversable_opt;
79 static int twoway_opt;
80 static int64_t memfifo_opt;
81 static int64_t swapfifo_opt;
84 main(int ac, char **av)
94 const char *wopt = NULL;
95 const char *xopt = NULL;
96 const char *keyword = NULL;
97 const char *mountpt = NULL;
100 while ((ch = getopt(ac, av, "adflo:mw:x:ACFSZ")) != -1) {
104 if (aopt + dopt + lopt + mopt != 1) {
105 fprintf(stderr, "too many action options specified\n");
111 if (aopt + dopt + lopt + mopt != 1) {
112 fprintf(stderr, "too many action options specified\n");
121 if (aopt + dopt + lopt + mopt != 1) {
122 fprintf(stderr, "too many action options specified\n");
127 parse_option_keyword(optarg, &wopt, &xopt);
131 if (aopt + dopt + lopt + mopt != 1) {
132 fprintf(stderr, "too many action options specified\n");
165 fprintf(stderr, "unknown option: -%c\n", optopt);
173 * Parse the keyword and/or mount point.
178 fprintf(stderr, "action requires a tag and/or mount "
179 "point to be specified\n");
184 if (av[0][0] == '/') {
186 if ((keyword = strchr(mountpt, ':')) != NULL) {
188 tmp = strdup(mountpt);
189 *strchr(tmp, ':') = 0;
197 fprintf(stderr, "unexpected extra arguments to command\n");
202 * Additional sanity checks
204 if (aopt + dopt + lopt + mopt + mimplied == 0) {
205 fprintf(stderr, "no action or implied action options were specified\n");
208 if (mimplied && aopt + dopt + lopt == 0)
210 if ((wopt || xopt) && !(aopt || mopt)) {
211 fprintf(stderr, "-w/-x/path/fd options may only be used with -m/-a\n");
214 if (aopt && (keyword == NULL || mountpt == NULL)) {
215 fprintf(stderr, "a keyword AND a mountpt must be specified "
216 "when adding a journal\n");
219 if (fopt == 0 && mopt + dopt && keyword == NULL && mountpt == NULL) {
220 fprintf(stderr, "a keyword, a mountpt, or both must be specified "
221 "when modifying or deleting a journal, unless "
222 "-f is also specified for safety\n");
227 * Open the journaling file descriptor if required.
230 fprintf(stderr, "you must specify only one of -w/-x/path/fd\n");
233 if ((fd = open(wopt, O_RDWR|O_CREAT|O_APPEND, 0666)) < 0) {
234 fprintf(stderr, "unable to create %s: %s\n", wopt, strerror(errno));
238 fd = strtol(xopt, NULL, 0);
240 fd = 1; /* stdout default for -a */
246 * And finally execute the core command.
249 mountctl_scan(mountctl_list, keyword, mountpt, fd);
251 mountctl_add(keyword, mountpt, fd);
253 ch = mountctl_scan(mountctl_delete, keyword, mountpt, -1);
255 printf("%d journals deleted\n", ch);
257 printf("Unable to locate any matching journals\n");
260 ch = mountctl_scan(mountctl_modify, keyword, mountpt, fd);
262 printf("%d journals modified\n", ch);
264 printf("Unable to locate any matching journals\n");
271 parse_option_keyword(const char *opt, const char **wopt, const char **xopt)
273 char *str = strdup(opt);
281 * multiple comma delimited options may be specified.
283 while ((name = strsep(&str, ",")) != NULL) {
285 * some options have associated data.
287 if ((val = strchr(name, '=')) != NULL)
291 * options beginning with 'no' or 'non' are negated. A positive
292 * number means not negated, a negative number means negated.
297 if (strncmp(name, "non", 3) == 0) {
300 } else if (strncmp(name, "no", 2) == 0) {
306 * Parse supported options
308 if (strcmp(name, "reversable") == 0) {
309 reversable_opt = negate;
310 } else if (strcmp(name, "twoway") == 0) {
312 } else if (strcmp(name, "memfifo") == 0) {
316 if ((memfifo_opt = getsize(val)) == 0)
319 } else if (strcmp(name, "swapfifo") == 0) {
322 if ((swapfifo_opt = getsize(val)) == 0)
324 } else if (negate < 0) {
327 hasval = 1; /* force error */
329 } else if (strcmp(name, "fd") == 0) {
334 } else if (strcmp(name, "path") == 0) {
339 } else if (strcmp(name, "freeze") == 0 || strcmp(name, "stop") == 0) {
344 } else if (strcmp(name, "start") == 0) {
346 freeze_opt = -negate;
349 } else if (strcmp(name, "close") == 0) {
351 } else if (strcmp(name, "abort") == 0) {
353 } else if (strcmp(name, "flush") == 0) {
356 fprintf(stderr, "unknown option keyword: %s\n", name);
363 if (cannotnegate && negate < 0) {
364 fprintf(stderr, "option %s may not be negated\n", name);
367 if (hasval && val == NULL) {
368 fprintf(stderr, "option %s requires assigned data\n", name);
371 if (hasval == 0 && val) {
372 fprintf(stderr, "option %s does not take an assignment\n", name);
380 mountctl_scan(void (*func)(const char *, const char *, int, void *),
381 const char *keyword, const char *mountpt, int fd)
387 struct mountctl_status_journal statreq;
388 struct mountctl_journal_ret_status rstat[4]; /* BIG */
392 bzero(&statreq, sizeof(statreq));
394 statreq.index = MC_JOURNAL_INDEX_ID;
395 count = strlen(keyword);
398 bcopy(keyword, statreq.id, count);
400 statreq.index = MC_JOURNAL_INDEX_ALL;
402 count = mountctl(mountpt, MOUNTCTL_STATUS_VFS_JOURNAL, -1,
403 &statreq, sizeof(statreq), &rstat, sizeof(rstat));
404 if (count > 0 && rstat[0].recsize != sizeof(rstat[0])) {
405 fprintf(stderr, "Unable to access status, "
406 "structure size mismatch\n");
410 count /= sizeof(rstat[0]);
411 for (i = 0; i < count; ++i) {
412 func(rstat[i].id, mountpt, fd, &rstat[i]);
417 if ((count = getmntinfo(&sfs, MNT_WAIT)) > 0) {
418 for (i = 0; i < count; ++i) {
419 calls += mountctl_scan(func, keyword, sfs[i].f_mntonname, fd);
421 } else if (count < 0) {
429 mountctl_list(const char *keyword, const char *mountpt, int __unused fd, void *info)
431 struct mountctl_journal_ret_status *rstat = info;
433 printf("%s:%s\n", mountpt, rstat->id[0] ? rstat->id : "<NOID>");
434 printf(" membufsize=%s\n", numtostr(rstat->membufsize));
435 printf(" membufused=%s\n", numtostr(rstat->membufused));
436 printf(" membufiopend=%s\n", numtostr(rstat->membufiopend));
437 printf(" total_bytes=%s\n", numtostr(rstat->bytessent));
441 mountctl_add(const char *keyword, const char *mountpt, int fd)
443 struct mountctl_install_journal joinfo;
449 * Make sure the file descriptor is not on the same filesystem as the
450 * mount point. This isn't a perfect test, but it should catch most
453 if (fstat(fd, &st1) == 0 && S_ISREG(st1.st_mode) &&
454 stat(mountpt, &st2) == 0 && st1.st_dev == st2.st_dev
456 fprintf(stderr, "%s:%s failed to add, the journal cannot be on the "
457 "same filesystem being journaled!\n",
464 * Setup joinfo and issue the add
466 bzero(&joinfo, sizeof(joinfo));
467 snprintf(joinfo.id, sizeof(joinfo.id), "%s", keyword);
469 joinfo.membufsize = memfifo_opt;
471 error = mountctl(mountpt, MOUNTCTL_INSTALL_VFS_JOURNAL, fd,
472 &joinfo, sizeof(joinfo), NULL, 0);
474 fprintf(stderr, "%s:%s added\n", mountpt, joinfo.id);
476 fprintf(stderr, "%s:%s failed to add, error %s\n", mountpt, joinfo.id, strerror(errno));
482 mountctl_delete(const char *keyword, const char *mountpt, int __unused fd, void __unused *info)
484 struct mountctl_remove_journal joinfo;
487 bzero(&joinfo, sizeof(joinfo));
488 snprintf(joinfo.id, sizeof(joinfo.id), "%s", keyword);
489 error = mountctl(mountpt, MOUNTCTL_REMOVE_VFS_JOURNAL, -1,
490 &joinfo, sizeof(joinfo), NULL, 0);
492 fprintf(stderr, "%s:%s deleted\n", mountpt, joinfo.id);
494 fprintf(stderr, "%s:%s deletion failed, error %s\n", mountpt, joinfo.id, strerror(errno));
499 mountctl_modify(const char *keyword, const char *mountpt, int fd, void __unused *info)
501 fprintf(stderr, "modify not yet implemented\n");
510 " mountctl -l [tag/mountpt | mountpt:tag]\n"
511 " mountctl -a [-w output_path] [-x filedesc]\n"
512 " [-o option] [-o option ...] mountpt:tag\n"
513 " mountctl -d [tag/mountpt | mountpt:tag]\n"
514 " mountctl -m [-o option] [-o option ...] [tag/mountpt | mountpt:tag]\n"
515 " mountctl -FZSCA [tag/mountpt | mountpt:tag]\n"
522 getsize(const char *str)
527 val = strtoll(str, &suffix, 0);
546 fprintf(stderr, "data value '%s' has unknown suffix\n", str);
555 numtostr(int64_t num)
562 snprintf(buf, sizeof(buf), "%lld", num);
563 else if (num < 10 * 1024)
564 snprintf(buf, sizeof(buf), "%3.2fK", num / 1024.0);
565 else if (num < 1024 * 1024)
566 snprintf(buf, sizeof(buf), "%3.0fK", num / 1024.0);
567 else if (num < 10 * 1024 * 1024)
568 snprintf(buf, sizeof(buf), "%3.2fM", num / (1024.0 * 1024.0));
569 else if (num < 1024 * 1024 * 1024)
570 snprintf(buf, sizeof(buf), "%3.0fM", num / (1024.0 * 1024.0));
571 else if (num < 10LL * 1024 * 1024 * 1024)
572 snprintf(buf, sizeof(buf), "%3.2fG", num / (1024.0 * 1024.0 * 1024.0));
574 snprintf(buf, sizeof(buf), "%3.0fG", num / (1024.0 * 1024.0 * 1024.0));