xref: /netbsd-src/usr.sbin/puffs/rump_lfs/rump_lfs.c (revision f75f5aae154fcd0572e8889e4fea2a51d67bbf08)
1 /*	$NetBSD: rump_lfs.c,v 1.8 2009/10/09 16:38:21 pooka 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 <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 #include <rump/rump.h>
40 #include <rump/p2k.h>
41 #include <rump/ukfs.h>
42 
43 #include "mount_lfs.h"
44 
45 static void *
46 cleaner(void *arg)
47 {
48 	const char *the_argv[7];
49 
50 	the_argv[0] = "megamaid";
51 	the_argv[1] = "-D"; /* don't fork() & detach */
52 	the_argv[2] = arg;
53 
54 	lfs_cleaner_main(3, __UNCONST(the_argv));
55 
56 	return NULL;
57 }
58 
59 int
60 main(int argc, char *argv[])
61 {
62 	struct ufs_args args;
63 	char canon_dev[UKFS_PARTITION_MAXPATHLEN], canon_dir[MAXPATHLEN];
64 	char rawdev[MAXPATHLEN];
65 	struct p2k_mount *p2m;
66 	pthread_t cleanerthread;
67 	int mntflags, part;
68 	int rv;
69 
70 	setprogname(argv[0]);
71 
72 	/*
73 	 * Load FFS and LFS already here.  we need both and link set
74 	 * lossage does not allow them to be linked on the command line.
75 	 */
76 	ukfs_init();
77 	if (ukfs_modload("librumpfs_ffs.so") != 1)
78 		err(1, "modload ffs");
79 	if (ukfs_modload("librumpfs_lfs.so") != 1)
80 		err(1, "modload lfs");
81 
82 	UKFS_PARTITION_ARGVPROBE(part);
83 	if (part != UKFS_PARTITION_NONE) {
84 		errx(1, "lfs does not currently support embedded partitions");
85 	}
86 	mount_lfs_parseargs(argc, argv, &args, &mntflags, canon_dev, canon_dir);
87 
88 	/*
89 	 * XXX: this particular piece inspired by the cleaner code.
90 	 * obviously FIXXXME along with the cleaner.
91 	 */
92 	sprintf(rawdev, "/dev/r%s", canon_dev+5);
93 	rump_etfs_register(rawdev, canon_dev, RUMP_ETFS_CHR);
94 
95 	/*
96 	 * We basically have two choices:
97 	 *  1) run the cleaner in another process and do rump ipc syscalls
98 	 *  2) run it off a thread in this process and do rump syscalls
99 	 *     as function calls.
100 	 *
101 	 * opt for "2" for now
102 	 */
103 #ifdef CLEANER_TESTING
104 	ukfs_mount(MOUNT_LFS, canon_dev, canon_dir, mntflags,
105 	    &args, sizeof(args));
106 	cleaner(canon_dir);
107 #endif
108 
109 	p2m = p2k_setup_diskfs(MOUNT_LFS, canon_dev, part, canon_dir, mntflags,
110 	    &args, sizeof(args), 0);
111 	if (!p2m)
112 		err(1, "mount");
113 
114 #ifndef CLEANER_TESTING
115 	if ((mntflags & MNT_RDONLY) == 0) {
116 		if (pthread_create(&cleanerthread, NULL,
117 		    cleaner, canon_dir) == -1)
118 			err(1, "cannot start cleaner");
119 	}
120 #endif
121 
122 	rv = p2k_mainloop(p2m);
123 	if (rv == -1)
124 		err(1, "fs service");
125 
126 	return 0;
127 }
128