1*6f990d2aSbrad /* $NetBSD: rump_lfs.c,v 1.19 2019/08/30 23:41:48 brad Exp $ */
2bdf6e0b0Spooka
3bdf6e0b0Spooka /*
4d559e459Spooka * Copyright (c) 2008 Antti Kantee. All Rights Reserved.
5bdf6e0b0Spooka *
6bdf6e0b0Spooka * Redistribution and use in source and binary forms, with or without
7bdf6e0b0Spooka * modification, are permitted provided that the following conditions
8bdf6e0b0Spooka * are met:
9bdf6e0b0Spooka * 1. Redistributions of source code must retain the above copyright
10bdf6e0b0Spooka * notice, this list of conditions and the following disclaimer.
11bdf6e0b0Spooka * 2. Redistributions in binary form must reproduce the above copyright
12bdf6e0b0Spooka * notice, this list of conditions and the following disclaimer in the
13bdf6e0b0Spooka * documentation and/or other materials provided with the distribution.
14bdf6e0b0Spooka *
15bdf6e0b0Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16bdf6e0b0Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17bdf6e0b0Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18bdf6e0b0Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19bdf6e0b0Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20bdf6e0b0Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21bdf6e0b0Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22bdf6e0b0Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23bdf6e0b0Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24bdf6e0b0Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25bdf6e0b0Spooka * SUCH DAMAGE.
26bdf6e0b0Spooka */
27bdf6e0b0Spooka
28bdf6e0b0Spooka #include <sys/types.h>
29bdf6e0b0Spooka #include <sys/mount.h>
30bdf6e0b0Spooka
31bdf6e0b0Spooka #include <ufs/ufs/ufsmount.h>
32bdf6e0b0Spooka
33bdf6e0b0Spooka #include <err.h>
34ceedea85Spooka #include <pthread.h>
35ef38ca99Spooka #include <puffs.h>
36ceedea85Spooka #include <stdio.h>
37bdf6e0b0Spooka #include <stdlib.h>
38bdf6e0b0Spooka #include <unistd.h>
39bdf6e0b0Spooka
40ceedea85Spooka #include <rump/rump.h>
41bdf6e0b0Spooka #include <rump/p2k.h>
42d6d0aeb4Spooka #include <rump/ukfs.h>
43bdf6e0b0Spooka
4499fed726Spooka #include "mount_lfs.h"
45bdf6e0b0Spooka
46*6f990d2aSbrad #define RUMPRAWDEVICE "/dev/rrumpy0"
47*6f990d2aSbrad
48ceedea85Spooka static void *
cleaner(void * arg)49ceedea85Spooka cleaner(void *arg)
50ceedea85Spooka {
51*6f990d2aSbrad const char *the_argv[9];
52ceedea85Spooka
53ceedea85Spooka the_argv[0] = "megamaid";
54ceedea85Spooka the_argv[1] = "-D"; /* don't fork() & detach */
55*6f990d2aSbrad the_argv[2] = "-J"; /* treat arg as a device */
56*6f990d2aSbrad the_argv[3] = RUMPRAWDEVICE;
57*6f990d2aSbrad the_argv[4] = arg;
58ceedea85Spooka
59*6f990d2aSbrad lfs_cleaner_main(5, __UNCONST(the_argv));
60ceedea85Spooka
61ceedea85Spooka return NULL;
62ceedea85Spooka }
63ceedea85Spooka
64bdf6e0b0Spooka int
main(int argc,char * argv[])65bdf6e0b0Spooka main(int argc, char *argv[])
66bdf6e0b0Spooka {
679263edc7Sdholland struct ulfs_args args;
68a1c46739Spooka char canon_dev[UKFS_DEVICE_MAXPATHLEN], canon_dir[MAXPATHLEN];
69d07d1f30Spooka char rawdev[MAXPATHLEN];
7068c55751Spooka struct p2k_mount *p2m;
71ceedea85Spooka pthread_t cleanerthread;
72a1c46739Spooka struct ukfs_part *part;
73a1c46739Spooka int mntflags;
7499fed726Spooka int rv;
75bdf6e0b0Spooka
76bdf6e0b0Spooka setprogname(argv[0]);
77ef38ca99Spooka puffs_unmountonsignal(SIGINT, true);
78ef38ca99Spooka puffs_unmountonsignal(SIGTERM, true);
79d6d0aeb4Spooka
80ed49ad5fSpooka if (argc >= 3) {
81a1c46739Spooka UKFS_DEVICE_ARGVPROBE(&part);
82a1c46739Spooka if (part != ukfs_part_none) {
83ed49ad5fSpooka errx(1, "lfs does not currently support "
84ed49ad5fSpooka "embedded partitions");
85ed49ad5fSpooka }
86d07d1f30Spooka }
8799fed726Spooka mount_lfs_parseargs(argc, argv, &args, &mntflags, canon_dev, canon_dir);
88ceedea85Spooka
89014f25f7Sriastradh /* Reset getopt for lfs_cleaner_main. */
90014f25f7Sriastradh optreset = 1;
91014f25f7Sriastradh optind = 1;
92014f25f7Sriastradh
93597f5b55Spooka p2m = p2k_init(0);
94597f5b55Spooka if (!p2m)
95597f5b55Spooka err(1, "init p2k");
96ceedea85Spooka /*
97ceedea85Spooka * XXX: this particular piece inspired by the cleaner code.
98ceedea85Spooka * obviously FIXXXME along with the cleaner.
99ceedea85Spooka */
100*6f990d2aSbrad strlcpy(rawdev, RUMPRAWDEVICE, MAXPATHLEN);
101bf3992afSpooka rump_pub_etfs_register(rawdev, canon_dev, RUMP_ETFS_CHR);
102ceedea85Spooka
103ceedea85Spooka /*
104ceedea85Spooka * We basically have two choices:
105ceedea85Spooka * 1) run the cleaner in another process and do rump ipc syscalls
106ceedea85Spooka * 2) run it off a thread in this process and do rump syscalls
107ceedea85Spooka * as function calls.
108ceedea85Spooka *
109ceedea85Spooka * opt for "2" for now
110ceedea85Spooka */
11168c55751Spooka #ifdef CLEANER_TESTING
11268c55751Spooka ukfs_mount(MOUNT_LFS, canon_dev, canon_dir, mntflags,
11368c55751Spooka &args, sizeof(args));
11468c55751Spooka cleaner(canon_dir);
11568c55751Spooka #endif
1168209f3a1Spooka if (p2k_setup_diskfs(p2m, MOUNT_LFS, canon_dev, part, canon_dir,
1178209f3a1Spooka mntflags, &args, sizeof(args)) == -1)
11868c55751Spooka err(1, "mount");
1190446aae7Spooka ukfs_part_release(part);
12068c55751Spooka
121ceedea85Spooka #ifndef CLEANER_TESTING
122ceedea85Spooka if ((mntflags & MNT_RDONLY) == 0) {
123ceedea85Spooka if (pthread_create(&cleanerthread, NULL,
124ceedea85Spooka cleaner, canon_dir) == -1)
125ceedea85Spooka err(1, "cannot start cleaner");
126ceedea85Spooka }
127ceedea85Spooka #endif
128ceedea85Spooka
12968c55751Spooka rv = p2k_mainloop(p2m);
13068c55751Spooka if (rv == -1)
13168c55751Spooka err(1, "fs service");
132bdf6e0b0Spooka
133bdf6e0b0Spooka return 0;
134bdf6e0b0Spooka }
135