xref: /freebsd/contrib/netbsd-tests/fs/common/fstest_lfs.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*	$NetBSD: fstest_lfs.c,v 1.5 2015/08/30 18:27:26 dholland Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nicolas Joly.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/mount.h>
33 #include <sys/stat.h>
34 
35 #include <atf-c.h>
36 #include <pthread.h>
37 #include <semaphore.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include <ufs/ufs/ufsmount.h>
44 
45 #include <rump/rump.h>
46 #include <rump/rump_syscalls.h>
47 
48 #include "h_fsmacros.h"
49 #include "mount_lfs.h"
50 
51 struct lfstestargs {
52 	struct ufs_args ta_uargs;
53 	pthread_t ta_cleanerthread;
54 	sem_t ta_cleanerloop;
55 	char ta_devpath[MAXPATHLEN];
56 	char ta_imgpath[MAXPATHLEN];
57 	char ta_mntpath[MAXPATHLEN];
58 	char ta_hostpath[MAXPATHLEN];
59 };
60 
61 int
62 lfs_fstest_newfs(const atf_tc_t *tc, void **buf, const char *image, off_t size,
63 	void *fspriv)
64 {
65 	char cmd[1024];
66 	int res;
67 	static unsigned int num = 0;
68 	struct lfstestargs *args;
69 
70 	size /= 512;
71 	snprintf(cmd, 1024, "newfs_lfs -D -F -s %"PRId64" ./%s >/dev/null",
72 	    size, image);
73 	res = system(cmd);
74 	if (res != 0)
75 		return res;
76 
77 	res = rump_init();
78 	if (res != 0)
79 		return res;
80 
81 	args = calloc(1, sizeof(*args));
82 	if (args == NULL)
83 		return -1;
84 
85 	strcpy(args->ta_hostpath, image);
86 	snprintf(args->ta_devpath, MAXPATHLEN, "/dev/device%d.lfs", num);
87 	snprintf(args->ta_imgpath, MAXPATHLEN, "%s", image);
88 	args->ta_uargs.fspec = args->ta_devpath;
89 	sem_init(&args->ta_cleanerloop, 0, 0);
90 
91 	res = rump_pub_etfs_register(args->ta_devpath, image, RUMP_ETFS_BLK);
92 	if (res != 0) {
93 		free(args);
94 		return res;
95 	}
96 
97 	*buf = args;
98 	num++;
99 
100 	return 0;
101 }
102 
103 int
104 lfs_fstest_delfs(const atf_tc_t *tc, void *buf)
105 {
106 	int res;
107 	struct lfstestargs *args = buf;
108 
109 	res = rump_pub_etfs_remove(args->ta_devpath);
110 	if (res != 0)
111 		return res;
112 
113 	res = unlink(args->ta_imgpath);
114 	if (res != 0)
115 		return res;
116 
117 	pthread_join(args->ta_cleanerthread, NULL);
118 	free(args);
119 
120 	return 0;
121 }
122 
123 static void *
124 cleaner(void *arg)
125 {
126 	char thepath[MAXPATHLEN];
127 	struct lfstestargs *args = arg;
128 	const char *the_argv[7];
129 	char buf[64];
130 
131 	/* this inspired by the cleaner code.  fixme */
132 	sprintf(thepath, "/dev/r%s", args->ta_devpath+5);
133 	rump_pub_etfs_register(thepath, args->ta_hostpath, RUMP_ETFS_CHR);
134 	sprintf(buf, "%p", &args->ta_cleanerloop);
135 
136 	the_argv[0] = "megamaid";
137 	the_argv[1] = "-D"; /* don't fork() & detach */
138 	the_argv[2] = "-S";
139 	the_argv[3] = buf;
140 	the_argv[4] = args->ta_mntpath;
141 	the_argv[5] = NULL;
142 
143 	/* xxxatf */
144 	optind = 1;
145 	opterr = 1;
146 
147 	lfs_cleaner_main(5, __UNCONST(the_argv));
148 
149 	return NULL;
150 }
151 
152 int
153 lfs_fstest_mount(const atf_tc_t *tc, void *buf, const char *path, int flags)
154 {
155 	struct lfstestargs *args = buf;
156 	int res;
157 
158 	res = rump_sys_mkdir(path, 0777);
159 	if (res == -1)
160 		return res;
161 
162 	res = rump_sys_mount(MOUNT_LFS, path, flags, &args->ta_uargs,
163 	    sizeof(args->ta_uargs));
164 	if (res == -1)
165 		return res;
166 
167 	strcpy(args->ta_mntpath, path);
168 	res = pthread_create(&args->ta_cleanerthread, NULL, cleaner, args);
169 	if (res)
170 		return res;
171 
172 	/* wait for cleaner to initialize */
173 	sem_wait(&args->ta_cleanerloop);
174 
175 	return 0;
176 }
177 
178 int
179 lfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
180 {
181 	int res;
182 
183 	res = rump_sys_unmount(path, flags);
184 	if (res == -1) {
185 		return res;
186 	}
187 
188 	res = rump_sys_rmdir(path);
189 	return res;
190 }
191