1272343Sngie/*	$NetBSD: fstest_lfs.c,v 1.4 2010/07/30 16:15:05 pooka Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Nicolas Joly.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie#include <sys/mount.h>
33272343Sngie#include <sys/stat.h>
34272343Sngie
35272343Sngie#include <atf-c.h>
36272343Sngie#include <pthread.h>
37272343Sngie#include <semaphore.h>
38272343Sngie#include <stdio.h>
39272343Sngie#include <stdlib.h>
40272343Sngie#include <string.h>
41272343Sngie#include <unistd.h>
42272343Sngie
43272343Sngie#include <ufs/ufs/ufsmount.h>
44272343Sngie
45272343Sngie#include <rump/rump.h>
46272343Sngie#include <rump/rump_syscalls.h>
47272343Sngie
48272343Sngie#include "h_fsmacros.h"
49272343Sngie#include "mount_lfs.h"
50272343Sngie
51272343Sngiesem_t lfs_clearnerloop;
52272343Sngie
53272343Sngiestruct lfstestargs {
54272343Sngie	struct ufs_args ta_uargs;
55272343Sngie	pthread_t ta_cleanerthread;
56272343Sngie	sem_t ta_cleanerloop;
57272343Sngie	char ta_devpath[MAXPATHLEN];
58272343Sngie	char ta_imgpath[MAXPATHLEN];
59272343Sngie	char ta_mntpath[MAXPATHLEN];
60272343Sngie	char ta_hostpath[MAXPATHLEN];
61272343Sngie};
62272343Sngie
63272343Sngieint
64272343Sngielfs_fstest_newfs(const atf_tc_t *tc, void **buf, const char *image, off_t size,
65272343Sngie	void *fspriv)
66272343Sngie{
67272343Sngie	char cmd[1024];
68272343Sngie	int res;
69272343Sngie	static unsigned int num = 0;
70272343Sngie	struct lfstestargs *args;
71272343Sngie
72272343Sngie	size /= 512;
73272343Sngie	snprintf(cmd, 1024, "newfs_lfs -D -F -s %"PRId64" ./%s >/dev/null",
74272343Sngie	    size, image);
75272343Sngie	res = system(cmd);
76272343Sngie	if (res != 0)
77272343Sngie		return res;
78272343Sngie
79272343Sngie	res = rump_init();
80272343Sngie	if (res != 0)
81272343Sngie		return res;
82272343Sngie
83272343Sngie	args = calloc(1, sizeof(*args));
84272343Sngie	if (args == NULL)
85272343Sngie		return -1;
86272343Sngie
87272343Sngie	strcpy(args->ta_hostpath, image);
88272343Sngie	snprintf(args->ta_devpath, MAXPATHLEN, "/dev/device%d.lfs", num);
89272343Sngie	snprintf(args->ta_imgpath, MAXPATHLEN, "%s", image);
90272343Sngie	args->ta_uargs.fspec = args->ta_devpath;
91272343Sngie	sem_init(&args->ta_cleanerloop, 0, 0);
92272343Sngie
93272343Sngie	res = rump_pub_etfs_register(args->ta_devpath, image, RUMP_ETFS_BLK);
94272343Sngie	if (res != 0) {
95272343Sngie		free(args);
96272343Sngie		return res;
97272343Sngie	}
98272343Sngie
99272343Sngie	*buf = args;
100272343Sngie	num++;
101272343Sngie
102272343Sngie	return 0;
103272343Sngie}
104272343Sngie
105272343Sngieint
106272343Sngielfs_fstest_delfs(const atf_tc_t *tc, void *buf)
107272343Sngie{
108272343Sngie	int res;
109272343Sngie	struct lfstestargs *args = buf;
110272343Sngie
111272343Sngie	res = rump_pub_etfs_remove(args->ta_devpath);
112272343Sngie	if (res != 0)
113272343Sngie		return res;
114272343Sngie
115272343Sngie	res = unlink(args->ta_imgpath);
116272343Sngie	if (res != 0)
117272343Sngie		return res;
118272343Sngie
119272343Sngie	pthread_join(args->ta_cleanerthread, NULL);
120272343Sngie	free(args);
121272343Sngie
122272343Sngie	return 0;
123272343Sngie}
124272343Sngie
125272343Sngiestatic void *
126272343Sngiecleaner(void *arg)
127272343Sngie{
128272343Sngie	char thepath[MAXPATHLEN];
129272343Sngie	struct lfstestargs *args = arg;
130272343Sngie	const char *the_argv[7];
131272343Sngie	char buf[64];
132272343Sngie
133272343Sngie	/* this inspired by the cleaner code.  fixme */
134272343Sngie	sprintf(thepath, "/dev/r%s", args->ta_devpath+5);
135272343Sngie	rump_pub_etfs_register(thepath, args->ta_hostpath, RUMP_ETFS_CHR);
136272343Sngie	sprintf(buf, "%p", &args->ta_cleanerloop);
137272343Sngie
138272343Sngie	the_argv[0] = "megamaid";
139272343Sngie	the_argv[1] = "-D"; /* don't fork() & detach */
140272343Sngie	the_argv[2] = "-S";
141272343Sngie	the_argv[3] = buf;
142272343Sngie	the_argv[4] = args->ta_mntpath;
143272343Sngie	the_argv[5] = NULL;
144272343Sngie
145272343Sngie	/* xxxatf */
146272343Sngie	optind = 1;
147272343Sngie	opterr = 1;
148272343Sngie
149272343Sngie	lfs_cleaner_main(5, __UNCONST(the_argv));
150272343Sngie
151272343Sngie	return NULL;
152272343Sngie}
153272343Sngie
154272343Sngieint
155272343Sngielfs_fstest_mount(const atf_tc_t *tc, void *buf, const char *path, int flags)
156272343Sngie{
157272343Sngie	struct lfstestargs *args = buf;
158272343Sngie	int res;
159272343Sngie
160272343Sngie	res = rump_sys_mkdir(path, 0777);
161272343Sngie	if (res == -1)
162272343Sngie		return res;
163272343Sngie
164272343Sngie	res = rump_sys_mount(MOUNT_LFS, path, flags, &args->ta_uargs,
165272343Sngie	    sizeof(args->ta_uargs));
166272343Sngie	if (res == -1)
167272343Sngie		return res;
168272343Sngie
169272343Sngie	strcpy(args->ta_mntpath, path);
170272343Sngie	res = pthread_create(&args->ta_cleanerthread, NULL, cleaner, args);
171272343Sngie	if (res)
172272343Sngie		return res;
173272343Sngie
174272343Sngie	/* wait for cleaner to initialize */
175272343Sngie	sem_wait(&args->ta_cleanerloop);
176272343Sngie
177272343Sngie	return 0;
178272343Sngie}
179272343Sngie
180272343Sngieint
181272343Sngielfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
182272343Sngie{
183272343Sngie	int res;
184272343Sngie
185272343Sngie	res = rump_sys_unmount(path, flags);
186272343Sngie	if (res == -1) {
187272343Sngie		return res;
188272343Sngie	}
189272343Sngie
190272343Sngie	res = rump_sys_rmdir(path);
191272343Sngie	return res;
192272343Sngie}
193