1272343Sngie/*	$NetBSD: fstest_zfs.c,v 1.1 2012/08/20 16:37:35 pooka Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2010, 2011  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 <stdio.h>
37272343Sngie#include <stdlib.h>
38272343Sngie#include <string.h>
39272343Sngie#include <unistd.h>
40272343Sngie
41272343Sngie#include <rump/rump.h>
42272343Sngie#include <rump/rump_syscalls.h>
43272343Sngie
44272343Sngie#include "h_fsmacros.h"
45272343Sngie
46272343Sngie#define SRVPATH "zfssurvo"
47272343Sngie#define SRVURL "unix://" SRVPATH
48272343Sngie#define ZFSDEV "/zfsdev"
49272343Sngie
50272343Sngieint
51272343Sngiezfs_fstest_newfs(const atf_tc_t *tc, void **buf, const char *image,
52272343Sngie	off_t size, void *fspriv)
53272343Sngie{
54272343Sngie	int res;
55272343Sngie	int fd;
56272343Sngie
57272343Sngie	/* XXX: hardcoded zfs minimum size */
58272343Sngie	size = MAX(64*1024*1024, size);
59272343Sngie
60272343Sngie	res = rump_init();
61272343Sngie	if (res != 0) {
62272343Sngie		errno = res;
63272343Sngie		return -1;
64272343Sngie	}
65272343Sngie
66272343Sngie	/* create backing image, sparse file is enough */
67272343Sngie	if ((fd = open(image, O_RDWR | O_CREAT, 0777)) == -1)
68272343Sngie		return -1;
69272343Sngie	if (ftruncate(fd, size) == -1) {
70272343Sngie		close(fd);
71272343Sngie		return -1;
72272343Sngie	}
73272343Sngie	close(fd);
74272343Sngie
75272343Sngie	res = rump_pub_etfs_register(ZFSDEV, image, RUMP_ETFS_BLK);
76272343Sngie	if (res != 0) {
77272343Sngie		errno = res;
78272343Sngie		return -1;
79272343Sngie	}
80272343Sngie
81272343Sngie	res = rump_init_server(SRVURL);
82272343Sngie	if (res != 0) {
83272343Sngie		errno = res;
84272343Sngie		return -1;
85272343Sngie	}
86272343Sngie	*buf = NULL;
87272343Sngie
88272343Sngie	return 0;
89272343Sngie}
90272343Sngie
91272343Sngieint
92272343Sngiezfs_fstest_delfs(const atf_tc_t *tc, void *buf)
93272343Sngie{
94272343Sngie
95272343Sngie	unlink(SRVPATH);
96272343Sngie	return 0;
97272343Sngie}
98272343Sngie
99272343Sngieint
100272343Sngiezfs_fstest_mount(const atf_tc_t *tc, void *buf, const char *path, int flags)
101272343Sngie{
102272343Sngie	char tmpbuf[128];
103272343Sngie	int error;
104272343Sngie
105272343Sngie	/* set up the hijack env for running zpool */
106272343Sngie	setenv("RUMP_SERVER", SRVURL, 1);
107272343Sngie	snprintf(tmpbuf, sizeof(tmpbuf)-1, "blanket=/dev/zfs:%s:%s",
108272343Sngie	    ZFSDEV, path);
109272343Sngie	setenv("RUMPHIJACK", tmpbuf, 1);
110272343Sngie	setenv("LD_PRELOAD", "/usr/lib/librumphijack.so", 1);
111272343Sngie
112272343Sngie	while (*path == '/')
113272343Sngie		path++;
114272343Sngie
115272343Sngie	/* run zpool create */
116272343Sngie	snprintf(tmpbuf, sizeof(tmpbuf)-1, "zpool create %s %s",
117272343Sngie	    path, ZFSDEV);
118272343Sngie	if ((error = system(tmpbuf)) != 0) {
119272343Sngie		errno = error;
120272343Sngie		return -1;
121272343Sngie	}
122272343Sngie
123272343Sngie	return 0;
124272343Sngie}
125272343Sngie
126272343Sngieint
127272343Sngiezfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
128272343Sngie{
129272343Sngie
130272343Sngie	unmount(path, flags);
131272343Sngie	unlink(SRVPATH);
132272343Sngie
133272343Sngie	return 0;
134272343Sngie}
135