kernel: Remove the old unionfs that was unhooked from the build since 2004.
[dragonfly.git] / sbin / mount_ufs / mount.c
CommitLineData
9ecd3762
MD
1/*-
2 * Copyright (c) 1980, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
9ecd3762
MD
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#) Copyright (c) 1980, 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)mount.c 8.25 (Berkeley) 5/8/95
31 * $FreeBSD: src/sbin/mount/mount.c,v 1.39.2.3 2001/08/01 08:26:23 obrien Exp $
9ecd3762
MD
32 */
33
34#include <sys/param.h>
35#include <sys/mount.h>
36#include <sys/mountctl.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39
40#include <err.h>
41#include <errno.h>
42#include <fstab.h>
43#include <pwd.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50#include "extern.h"
51#include "mntopts.h"
52#include "pathnames.h"
53
54/* `meta' options */
55#define MOUNT_META_OPTION_FSTAB "fstab"
56#define MOUNT_META_OPTION_CURRENT "current"
57
58int debug, fstab_style, verbose;
59
60static char *catopt(char *, const char *);
61static struct statfs
62 *getmntpt(const char *);
63static int hasopt(const char *, const char *);
64static int ismounted(struct fstab *, struct statfs *, int);
65static int isremountable(const char *);
66static void mangle(char *, int *, const char **);
67static char *update_options(char *, char *, int);
68static int mountfs(const char *, const char *, const char *,
69 int, const char *, const char *);
70static void remopt(char *, const char *);
71static void printdefvals(const struct statfs *);
72static void prmount(struct statfs *);
73static void putfsent(const struct statfs *);
74static void usage(void);
75static char *flags2opts(int);
76static char *xstrdup(const char *str);
77
78/* mount_ufs.c */
79int mount_ufs(int, const char **);
80
81/*
82 * List of VFS types that can be remounted without becoming mounted on top
83 * of each other.
84 * XXX Is this list correct?
85 */
86static const char *
87remountable_fs_names[] = {
88 "ufs", "ffs", "ext2fs",
89 0
90};
91
92int
93main(int argc, char **argv)
94{
95 const char *mntfromname, **vfslist, *vfstype;
96 struct fstab *fs;
97 struct statfs *mntbuf;
98 FILE *mountdfp;
99 pid_t pid;
100 int all, ch, i, init_flags, mntsize, rval, have_fstab;
101 char *options;
102
103 all = init_flags = 0;
104 options = NULL;
105 vfslist = NULL;
106 vfstype = "ufs";
107 while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
108 switch (ch) {
109 case 'a':
110 all = 1;
111 break;
112 case 'd':
113 debug = 1;
114 break;
115 case 'F':
116 setfstab(optarg);
117 break;
118 case 'f':
119 init_flags |= MNT_FORCE;
120 break;
121 case 'o':
122 if (*optarg)
123 options = catopt(options, optarg);
124 break;
125 case 'p':
126 fstab_style = 1;
127 verbose = 1;
128 break;
129 case 'r':
130 options = catopt(options, "ro");
131 break;
132 case 't':
133 if (vfslist != NULL)
134 errx(1, "only one -t option may be specified");
135 vfslist = makevfslist(optarg);
136 vfstype = optarg;
137 break;
138 case 'u':
139 init_flags |= MNT_UPDATE;
140 break;
141 case 'v':
142 verbose = 1;
143 break;
144 case 'w':
145 options = catopt(options, "noro");
146 break;
147 case '?':
148 default:
149 usage();
150 /* NOTREACHED */
151 }
152 argc -= optind;
153 argv += optind;
154
155#define BADTYPE(type) \
156 (strcmp(type, FSTAB_RO) && \
157 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
158
159 rval = 0;
160 switch (argc) {
161 case 0:
162 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
163 err(1, "getmntinfo");
164 if (all) {
165 while ((fs = getfsent()) != NULL) {
166 if (BADTYPE(fs->fs_type))
167 continue;
168 if (checkvfsname(fs->fs_vfstype, vfslist))
169 continue;
170 if (hasopt(fs->fs_mntops, "noauto"))
171 continue;
172 if (!(init_flags & MNT_UPDATE) &&
173 ismounted(fs, mntbuf, mntsize))
174 continue;
175 options = update_options(options,
176 fs->fs_mntops, mntbuf->f_flags);
177 if (mountfs(fs->fs_vfstype, fs->fs_spec,
178 fs->fs_file, init_flags, options,
179 fs->fs_mntops))
180 rval = 1;
181 }
182 } else if (fstab_style) {
183 for (i = 0; i < mntsize; i++) {
184 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
185 continue;
186 putfsent(&mntbuf[i]);
187 }
188 } else {
189 for (i = 0; i < mntsize; i++) {
190 if (checkvfsname(mntbuf[i].f_fstypename,
191 vfslist))
192 continue;
193 prmount(&mntbuf[i]);
194 }
195 }
196 exit(rval);
197 case 1:
198 if (vfslist != NULL)
199 usage();
200
201 rmslashes(*argv, *argv);
202
203 if (init_flags & MNT_UPDATE) {
204 mntfromname = NULL;
205 have_fstab = 0;
206 if ((mntbuf = getmntpt(*argv)) == NULL)
207 errx(1, "not currently mounted %s", *argv);
208 /*
209 * Only get the mntflags from fstab if both mntpoint
210 * and mntspec are identical. Also handle the special
211 * case where just '/' is mounted and 'spec' is not
212 * identical with the one from fstab ('/dev' is missing
213 * in the spec-string at boot-time).
214 */
215 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
216 if (strcmp(fs->fs_spec,
217 mntbuf->f_mntfromname) == 0 &&
218 strcmp(fs->fs_file,
219 mntbuf->f_mntonname) == 0) {
220 have_fstab = 1;
221 mntfromname = mntbuf->f_mntfromname;
222 } else if (argv[0][0] == '/' &&
223 argv[0][1] == '\0') {
224 fs = getfsfile("/");
225 have_fstab = 1;
226 mntfromname = fs->fs_spec;
227 }
228 }
229 if (have_fstab) {
230 options = update_options(options, fs->fs_mntops,
231 mntbuf->f_flags);
232 } else {
233 mntfromname = mntbuf->f_mntfromname;
234 options = update_options(options, NULL,
235 mntbuf->f_flags);
236 }
237 rval = mountfs(mntbuf->f_fstypename, mntfromname,
238 mntbuf->f_mntonname, init_flags, options, 0);
239 break;
240 }
241 if ((fs = getfsfile(*argv)) == NULL &&
242 (fs = getfsspec(*argv)) == NULL)
243 errx(1, "%s: unknown special file or file system",
244 *argv);
245 if (BADTYPE(fs->fs_type))
246 errx(1, "%s has unknown file system type",
247 *argv);
248 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
249 init_flags, options, fs->fs_mntops);
250 break;
251 case 2:
252 /*
253 * If -t flag has not been specified, the path cannot be
254 * found, spec contains either a ':' or a '@', and the
255 * spec is not a file with those characters, then assume
256 * that an NFS filesystem is being specified ala Sun.
257 */
258 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
259 access(argv[0], 0) == -1)
260 vfstype = "nfs";
261 rval = mountfs(vfstype, getdevpath(argv[0], 0), argv[1],
262 init_flags, options, NULL);
263 break;
264 default:
265 usage();
266 /* NOTREACHED */
267 }
268
269 /*
270 * If the mount was successfully, and done by root, tell mountd the
271 * good news. Pid checks are probably unnecessary, but don't hurt.
272 */
273 if (rval == 0 && getuid() == 0 &&
274 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
275 if (fscanf(mountdfp, "%d", &pid) == 1 &&
276 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
277 err(1, "signal mountd");
278 fclose(mountdfp);
279 }
280
281 exit(rval);
282}
283
284static int
285ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
286{
287 int i;
288
289 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
290 /* the root file system can always be remounted */
291 return (0);
292
293 for (i = mntsize - 1; i >= 0; --i)
294 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
295 (!isremountable(fs->fs_vfstype) ||
296 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
297 return (1);
298 return (0);
299}
300
301static int
302isremountable(const char *vfsname)
303{
304 const char **cp;
305
306 for (cp = remountable_fs_names; *cp; cp++)
307 if (strcmp(*cp, vfsname) == 0)
308 return (1);
309 return (0);
310}
311
312static int
313hasopt(const char *mntopts, const char *option)
314{
315 int negative, found;
316 char *opt, *optbuf;
317
318 if (option[0] == 'n' && option[1] == 'o') {
319 negative = 1;
320 option += 2;
321 } else
322 negative = 0;
323 optbuf = xstrdup(mntopts);
324 found = 0;
325 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
326 if (opt[0] == 'n' && opt[1] == 'o') {
327 if (!strcasecmp(opt + 2, option))
328 found = negative;
329 } else if (!strcasecmp(opt, option))
330 found = !negative;
331 }
332 free(optbuf);
333 return (found);
334}
335
336static int
337mountfs(const char *vfstype, const char *spec, const char *name, int flags,
338 const char *options, const char *mntopts)
339{
340 /* List of directories containing mount_xxx subcommands. */
341 static const char *edirs[] = {
342 _PATH_SBIN,
343 _PATH_USRSBIN,
344 NULL
345 };
346 const char *argv[100], **edir;
347 struct statfs sf;
348 pid_t pid;
349 int argc, i, status;
350 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
351
352#if __GNUC__
353 (void)&optbuf;
354 (void)&name;
355#endif
356
357 /* resolve the mountpoint with realpath(3) */
358 checkpath(name, mntpath);
359 name = mntpath;
360
361 if (mntopts == NULL)
362 mntopts = "";
363 if (options == NULL) {
364 if (*mntopts == '\0') {
365 options = "rw";
366 } else {
367 options = mntopts;
368 mntopts = "";
369 }
370 }
371 optbuf = catopt(xstrdup(mntopts), options);
372
373 if (strcmp(name, "/") == 0)
374 flags |= MNT_UPDATE;
375 if (flags & MNT_FORCE)
376 optbuf = catopt(optbuf, "force");
377 if (flags & MNT_RDONLY)
378 optbuf = catopt(optbuf, "ro");
379 /*
380 * XXX
381 * The mount_mfs (newfs) command uses -o to select the
382 * optimization mode. We don't pass the default "-o rw"
383 * for that reason.
384 */
385 if (flags & MNT_UPDATE)
386 optbuf = catopt(optbuf, "update");
387
388 argc = 0;
389 argv[argc++] = vfstype;
390 mangle(optbuf, &argc, argv);
391 argv[argc++] = spec;
392 argv[argc++] = name;
393 argv[argc] = NULL;
394
395 if (debug) {
396 printf("exec: mount_%s", vfstype);
397 for (i = 1; i < argc; i++)
398 printf(" %s", argv[i]);
399 printf("\n");
400 return (0);
401 }
402
403 switch (pid = fork()) {
404 case -1: /* Error. */
405 warn("fork");
406 free(optbuf);
407 return (1);
408 case 0: /* Child. */
409 if (strcmp(vfstype, "ufs") == 0)
410 exit(mount_ufs(argc, argv));
411
412 /* Go find an executable. */
413 for (edir = edirs; *edir; edir++) {
414 snprintf(execname,
415 sizeof(execname), "%s/mount_%s", *edir, vfstype);
416 execv(execname, __DECONST(char * const *, argv));
417 }
418 if (errno == ENOENT) {
419 int len = 0;
420 char *cp;
421 for (edir = edirs; *edir; edir++)
422 len += strlen(*edir) + 2; /* ", " */
423 if ((cp = malloc(len)) == NULL)
424 errx(1, "malloc failed");
425 cp[0] = '\0';
426 for (edir = edirs; *edir; edir++) {
427 strcat(cp, *edir);
428 if (edir[1] != NULL)
429 strcat(cp, ", ");
430 }
431 warn("exec mount_%s not found in %s", vfstype, cp);
432 }
433 exit(1);
434 /* NOTREACHED */
435 default: /* Parent. */
436 free(optbuf);
437
438 if (waitpid(pid, &status, 0) < 0) {
439 warn("waitpid");
440 return (1);
441 }
442
443 if (WIFEXITED(status)) {
444 if (WEXITSTATUS(status) != 0)
445 return (WEXITSTATUS(status));
446 } else if (WIFSIGNALED(status)) {
447 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
448 return (1);
449 }
450
451 if (verbose) {
452 if (statfs(name, &sf) < 0) {
453 warn("statfs %s", name);
454 return (1);
455 }
456 if (fstab_style)
457 putfsent(&sf);
458 else
459 prmount(&sf);
460 }
461 break;
462 }
463
464 return (0);
465}
466
467static void
468prmount(struct statfs *sfp)
469{
470 struct passwd *pw;
471 char *buf;
199559e7 472 int error;
9ecd3762
MD
473 int len;
474
475 error = 0;
476 len = 256;
477
478 if ((buf = malloc(len)) == NULL)
479 errx(1, "malloc failed");
480
481 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
482 sfp->f_fstypename);
483
484 /* Get a string buffer with all the used flags names */
485 error = mountctl(sfp->f_mntonname, MOUNTCTL_MOUNTFLAGS,
486 -1, NULL, 0, buf, len);
487
488 if (sfp->f_owner) {
489 printf(", mounted by ");
490 if ((pw = getpwuid(sfp->f_owner)) != NULL)
491 printf("%s", pw->pw_name);
492 else
493 printf("%d", sfp->f_owner);
494 }
495
44869c11 496 if (error != -1 && strlen(buf))
9ecd3762
MD
497 printf(", %s", buf);
498
499 if (verbose) {
500 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
501 printf(", writes: sync %ld async %ld",
502 sfp->f_syncwrites, sfp->f_asyncwrites);
503 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
504 printf(", reads: sync %ld async %ld",
505 sfp->f_syncreads, sfp->f_asyncreads);
506 }
507 printf(")\n");
508}
509
510static struct statfs *
511getmntpt(const char *name)
512{
513 struct statfs *mntbuf;
514 int i, mntsize;
515
516 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
517 for (i = mntsize - 1; i >= 0; i--) {
518 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
519 strcmp(mntbuf[i].f_mntonname, name) == 0)
520 return (&mntbuf[i]);
521 }
522 return (NULL);
523}
524
525static char *
526catopt(char *s0, const char *s1)
527{
528 size_t i;
529 char *cp;
530
531 if (s1 == NULL || *s1 == '\0')
532 return s0;
533
534 if (s0 && *s0) {
535 i = strlen(s0) + strlen(s1) + 1 + 1;
536 if ((cp = malloc(i)) == NULL)
537 errx(1, "malloc failed");
538 snprintf(cp, i, "%s,%s", s0, s1);
539 } else
540 cp = xstrdup(s1);
541
542 if (s0)
543 free(s0);
544 return (cp);
545}
546
547static void
548mangle(char *options, int *argcp, const char **argv)
549{
550 char *p, *s;
551 int argc;
552
553 argc = *argcp;
554 for (s = options; (p = strsep(&s, ",")) != NULL;)
555 if (*p != '\0') {
556 if (*p == '-') {
557 argv[argc++] = p;
558 p = strchr(p, '=');
559 if (p) {
560 *p = '\0';
561 argv[argc++] = p+1;
562 }
563 } else if (strcmp(p, "rw") != 0) {
564 argv[argc++] = "-o";
565 argv[argc++] = p;
566 }
567 }
568
569 *argcp = argc;
570}
571
572
573static char *
574update_options(char *opts, char *fstab, int curflags)
575{
576 char *o, *p;
577 char *cur;
578 char *expopt, *newopt, *tmpopt;
579
580 if (opts == NULL)
581 return xstrdup("");
582
583 /* remove meta options from list */
584 remopt(fstab, MOUNT_META_OPTION_FSTAB);
585 remopt(fstab, MOUNT_META_OPTION_CURRENT);
586 cur = flags2opts(curflags);
587
588 /*
589 * Expand all meta-options passed to us first.
590 */
591 expopt = NULL;
592 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
593 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
594 expopt = catopt(expopt, fstab);
595 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
596 expopt = catopt(expopt, cur);
597 else
598 expopt = catopt(expopt, o);
599 }
600 free(cur);
601 free(opts);
602
603 /*
604 * Remove previous contradictory arguments. Given option "foo" we
605 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
606 * and "foo" - so we can deal with possible options like "notice".
607 */
608 newopt = NULL;
609 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
610 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
611 errx(1, "malloc failed");
612
613 strcpy(tmpopt, "no");
614 strcat(tmpopt, o);
615 remopt(newopt, tmpopt);
616 free(tmpopt);
617
618 if (strncmp("no", o, 2) == 0)
619 remopt(newopt, o+2);
620
621 newopt = catopt(newopt, o);
622 }
623 free(expopt);
624
625 return newopt;
626}
627
628static void
629remopt(char *string, const char *opt)
630{
631 char *o, *p, *r;
632
633 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
634 return;
635
636 r = string;
637
638 for (p = string; (o = strsep(&p, ",")) != NULL;) {
639 if (strcmp(opt, o) != 0) {
640 if (*r == ',' && *o != '\0')
641 r++;
642 while ((*r++ = *o++) != '\0')
643 ;
644 *--r = ',';
645 }
646 }
647 *r = '\0';
648}
649
650static void
651usage(void)
652{
653
654 fprintf(stderr, "%s\n%s\n%s\n",
655"usage: mount [-adfpruvw] [-F fstab] [-o options] [-t type]",
656" mount [-dfpruvw] {special | node}",
657" mount [-dfpruvw] [-o options] [-t type] special node");
658 exit(1);
659}
660
661/*
662 * Prints the default values for dump frequency and pass number of fsck.
663 * This happens when mount(8) is called with -p and there is no fstab file
664 * or there is but the values aren't specified.
665 */
666static void
667printdefvals(const struct statfs *ent)
668{
669 if (strcmp(ent->f_fstypename, "ufs") == 0) {
670 if (strcmp(ent->f_mntonname, "/") == 0)
671 printf("\t1 1\n");
672 else
673 printf("\t2 2\n");
674 } else {
675 printf("\t0 0\n");
676 }
677}
678
679static void
680putfsent(const struct statfs *ent)
681{
682 struct stat sb;
683 struct fstab *fst;
684 char *opts;
685
686 opts = flags2opts(ent->f_flags);
687 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
688 ent->f_fstypename, opts);
689 free(opts);
690
691 /*
692 * If fstab file is missing don't call getfsspec() or getfsfile()
693 * at all, because the same warning will be printed twice for every
694 * mounted filesystem.
695 */
696 if (stat(_PATH_FSTAB, &sb) != -1) {
697 if ((fst = getfsspec(ent->f_mntfromname)))
698 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
699 else if ((fst = getfsfile(ent->f_mntonname)))
700 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
701 else
702 printdefvals(ent);
703 } else {
704 printdefvals(ent);
705 }
706}
707
708
709static char *
710flags2opts(int flags)
711{
712 char *res;
713
714 res = NULL;
715
716 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
717
718 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
719 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
720 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
721 if (flags & MNT_NODEV) res = catopt(res, "nodev");
9ecd3762
MD
722 if (flags & MNT_ASYNC) res = catopt(res, "async");
723 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
724 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
725 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
726 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
727 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
728 if (flags & MNT_IGNORE) res = catopt(res, "ignore");
729
730 return res;
731}
732
733static char*
734xstrdup(const char *str)
735{
736 char* ret = strdup(str);
737 if(ret == NULL) {
738 errx(1, "strdup failed (could not allocate memory)");
739 }
740 return ret;
741}