1 /* $NetBSD: fstest_lfs.c,v 1.8 2020/06/17 00:16:21 kamil 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_syscallshotgun.h>
47 #include <rump/rump_syscalls.h>
48
49 #include "h_fsmacros.h"
50 #include "mount_lfs.h"
51
52 struct lfstestargs {
53 struct ufs_args ta_uargs;
54 pthread_t ta_cleanerthread;
55 sem_t ta_cleanerloop;
56 char ta_devpath[MAXPATHLEN];
57 char ta_imgpath[MAXPATHLEN];
58 char ta_mntpath[MAXPATHLEN];
59 char ta_hostpath[MAXPATHLEN];
60 };
61
62 int
lfs_fstest_newfs(const atf_tc_t * tc,void ** buf,const char * image,off_t size,void * fspriv)63 lfs_fstest_newfs(const atf_tc_t *tc, void **buf, const char *image, off_t size,
64 void *fspriv)
65 {
66 char cmd[1024];
67 int res;
68 static unsigned int num = 0;
69 struct lfstestargs *args;
70
71 size /= 512;
72 snprintf(cmd, 1024, "newfs_lfs -D -F -s %"PRId64" ./%s >/dev/null",
73 size, image);
74 res = system(cmd);
75 if (res != 0)
76 return res;
77
78 res = rump_init();
79 if (res != 0)
80 return res;
81
82 args = calloc(1, sizeof(*args));
83 if (args == NULL)
84 return -1;
85
86 strcpy(args->ta_hostpath, image);
87 snprintf(args->ta_devpath, MAXPATHLEN, "/dev/device%d.lfs", num);
88 snprintf(args->ta_imgpath, MAXPATHLEN, "%s", image);
89 args->ta_uargs.fspec = args->ta_devpath;
90 sem_init(&args->ta_cleanerloop, 0, 0);
91
92 res = rump_pub_etfs_register(args->ta_devpath, image, RUMP_ETFS_BLK);
93 if (res != 0) {
94 free(args);
95 return res;
96 }
97
98 *buf = args;
99 num++;
100
101 return 0;
102 }
103
104 int
lfs_fstest_delfs(const atf_tc_t * tc,void * buf)105 lfs_fstest_delfs(const atf_tc_t *tc, void *buf)
106 {
107 int res;
108 struct lfstestargs *args = buf;
109
110 res = rump_pub_etfs_remove(args->ta_devpath);
111 if (res != 0)
112 return res;
113
114 res = unlink(args->ta_imgpath);
115 if (res != 0)
116 return res;
117
118 pthread_join(args->ta_cleanerthread, NULL);
119 free(args);
120
121 return 0;
122 }
123
124 static void *
cleaner(void * arg)125 cleaner(void *arg)
126 {
127 char thepath[MAXPATHLEN];
128 struct lfstestargs *args = arg;
129 const char *the_argv[9];
130 char buf[64];
131
132 rump_pub_lwproc_newlwp(rump_sys_getpid());
133
134 /* this inspired by the cleaner code. fixme */
135 sprintf(thepath, "/dev/r%s", args->ta_devpath+5);
136 rump_pub_etfs_register(thepath, args->ta_hostpath, RUMP_ETFS_CHR);
137 sprintf(buf, "%p", &args->ta_cleanerloop);
138
139 the_argv[0] = "megamaid";
140 the_argv[1] = "-D"; /* don't fork() & detach */
141 the_argv[2] = "-S";
142 the_argv[3] = buf;
143 the_argv[4] = "-J";
144 the_argv[5] = thepath;
145 the_argv[6] = args->ta_mntpath;
146 the_argv[7] = NULL;
147
148 /* xxxatf */
149 optind = 1;
150 opterr = 1;
151
152 lfs_cleaner_main(7, __UNCONST(the_argv));
153
154 rump_pub_lwproc_releaselwp();
155
156 return NULL;
157 }
158
159 int
lfs_fstest_mount(const atf_tc_t * tc,void * buf,const char * path,int flags)160 lfs_fstest_mount(const atf_tc_t *tc, void *buf, const char *path, int flags)
161 {
162 struct lfstestargs *args = buf;
163 int res;
164
165 res = rump_sys_mkdir(path, 0777);
166 if (res == -1)
167 return res;
168
169 res = rump_sys_mount(MOUNT_LFS, path, flags, &args->ta_uargs,
170 sizeof(args->ta_uargs));
171 if (res == -1)
172 return res;
173
174 strcpy(args->ta_mntpath, path);
175 res = pthread_create(&args->ta_cleanerthread, NULL, cleaner, args);
176 if (res)
177 return res;
178
179 /* wait for cleaner to initialize */
180 sem_wait(&args->ta_cleanerloop);
181
182 return 0;
183 }
184
185 int
lfs_fstest_unmount(const atf_tc_t * tc,const char * path,int flags)186 lfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
187 {
188 int res;
189
190 res = rump_sys_unmount(path, flags);
191 if (res == -1) {
192 return res;
193 }
194
195 res = rump_sys_rmdir(path);
196 return res;
197 }
198