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