xref: /minix3/tests/fs/common/fstest_lfs.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: fstest_lfs.c,v 1.5 2015/08/30 18:27:26 dholland Exp $	*/
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Nicolas Joly.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc 
3211be35a1SLionel Sambuc #include <sys/mount.h>
3311be35a1SLionel Sambuc #include <sys/stat.h>
3411be35a1SLionel Sambuc 
3511be35a1SLionel Sambuc #include <atf-c.h>
3611be35a1SLionel Sambuc #include <pthread.h>
3711be35a1SLionel Sambuc #include <semaphore.h>
3811be35a1SLionel Sambuc #include <stdio.h>
3911be35a1SLionel Sambuc #include <stdlib.h>
4011be35a1SLionel Sambuc #include <string.h>
4111be35a1SLionel Sambuc #include <unistd.h>
4211be35a1SLionel Sambuc 
4311be35a1SLionel Sambuc #include <ufs/ufs/ufsmount.h>
4411be35a1SLionel Sambuc 
4511be35a1SLionel Sambuc #include <rump/rump.h>
4611be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
4711be35a1SLionel Sambuc 
4811be35a1SLionel Sambuc #include "h_fsmacros.h"
4911be35a1SLionel Sambuc #include "mount_lfs.h"
5011be35a1SLionel Sambuc 
5111be35a1SLionel Sambuc struct lfstestargs {
5211be35a1SLionel Sambuc 	struct ufs_args ta_uargs;
5311be35a1SLionel Sambuc 	pthread_t ta_cleanerthread;
5411be35a1SLionel Sambuc 	sem_t ta_cleanerloop;
5511be35a1SLionel Sambuc 	char ta_devpath[MAXPATHLEN];
5611be35a1SLionel Sambuc 	char ta_imgpath[MAXPATHLEN];
5711be35a1SLionel Sambuc 	char ta_mntpath[MAXPATHLEN];
5811be35a1SLionel Sambuc 	char ta_hostpath[MAXPATHLEN];
5911be35a1SLionel Sambuc };
6011be35a1SLionel Sambuc 
6111be35a1SLionel Sambuc int
lfs_fstest_newfs(const atf_tc_t * tc,void ** buf,const char * image,off_t size,void * fspriv)6211be35a1SLionel Sambuc lfs_fstest_newfs(const atf_tc_t *tc, void **buf, const char *image, off_t size,
6311be35a1SLionel Sambuc 	void *fspriv)
6411be35a1SLionel Sambuc {
6511be35a1SLionel Sambuc 	char cmd[1024];
6611be35a1SLionel Sambuc 	int res;
6711be35a1SLionel Sambuc 	static unsigned int num = 0;
6811be35a1SLionel Sambuc 	struct lfstestargs *args;
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc 	size /= 512;
7111be35a1SLionel Sambuc 	snprintf(cmd, 1024, "newfs_lfs -D -F -s %"PRId64" ./%s >/dev/null",
7211be35a1SLionel Sambuc 	    size, image);
7311be35a1SLionel Sambuc 	res = system(cmd);
7411be35a1SLionel Sambuc 	if (res != 0)
7511be35a1SLionel Sambuc 		return res;
7611be35a1SLionel Sambuc 
7711be35a1SLionel Sambuc 	res = rump_init();
7811be35a1SLionel Sambuc 	if (res != 0)
7911be35a1SLionel Sambuc 		return res;
8011be35a1SLionel Sambuc 
8111be35a1SLionel Sambuc 	args = calloc(1, sizeof(*args));
8211be35a1SLionel Sambuc 	if (args == NULL)
8311be35a1SLionel Sambuc 		return -1;
8411be35a1SLionel Sambuc 
8511be35a1SLionel Sambuc 	strcpy(args->ta_hostpath, image);
8611be35a1SLionel Sambuc 	snprintf(args->ta_devpath, MAXPATHLEN, "/dev/device%d.lfs", num);
8711be35a1SLionel Sambuc 	snprintf(args->ta_imgpath, MAXPATHLEN, "%s", image);
8811be35a1SLionel Sambuc 	args->ta_uargs.fspec = args->ta_devpath;
8911be35a1SLionel Sambuc 	sem_init(&args->ta_cleanerloop, 0, 0);
9011be35a1SLionel Sambuc 
9111be35a1SLionel Sambuc 	res = rump_pub_etfs_register(args->ta_devpath, image, RUMP_ETFS_BLK);
9211be35a1SLionel Sambuc 	if (res != 0) {
9311be35a1SLionel Sambuc 		free(args);
9411be35a1SLionel Sambuc 		return res;
9511be35a1SLionel Sambuc 	}
9611be35a1SLionel Sambuc 
9711be35a1SLionel Sambuc 	*buf = args;
9811be35a1SLionel Sambuc 	num++;
9911be35a1SLionel Sambuc 
10011be35a1SLionel Sambuc 	return 0;
10111be35a1SLionel Sambuc }
10211be35a1SLionel Sambuc 
10311be35a1SLionel Sambuc int
lfs_fstest_delfs(const atf_tc_t * tc,void * buf)10411be35a1SLionel Sambuc lfs_fstest_delfs(const atf_tc_t *tc, void *buf)
10511be35a1SLionel Sambuc {
10611be35a1SLionel Sambuc 	int res;
10711be35a1SLionel Sambuc 	struct lfstestargs *args = buf;
10811be35a1SLionel Sambuc 
10911be35a1SLionel Sambuc 	res = rump_pub_etfs_remove(args->ta_devpath);
11011be35a1SLionel Sambuc 	if (res != 0)
11111be35a1SLionel Sambuc 		return res;
11211be35a1SLionel Sambuc 
11311be35a1SLionel Sambuc 	res = unlink(args->ta_imgpath);
11411be35a1SLionel Sambuc 	if (res != 0)
11511be35a1SLionel Sambuc 		return res;
11611be35a1SLionel Sambuc 
11711be35a1SLionel Sambuc 	pthread_join(args->ta_cleanerthread, NULL);
11811be35a1SLionel Sambuc 	free(args);
11911be35a1SLionel Sambuc 
12011be35a1SLionel Sambuc 	return 0;
12111be35a1SLionel Sambuc }
12211be35a1SLionel Sambuc 
12311be35a1SLionel Sambuc static void *
cleaner(void * arg)12411be35a1SLionel Sambuc cleaner(void *arg)
12511be35a1SLionel Sambuc {
12611be35a1SLionel Sambuc 	char thepath[MAXPATHLEN];
12711be35a1SLionel Sambuc 	struct lfstestargs *args = arg;
12811be35a1SLionel Sambuc 	const char *the_argv[7];
12911be35a1SLionel Sambuc 	char buf[64];
13011be35a1SLionel Sambuc 
13111be35a1SLionel Sambuc 	/* this inspired by the cleaner code.  fixme */
13211be35a1SLionel Sambuc 	sprintf(thepath, "/dev/r%s", args->ta_devpath+5);
13311be35a1SLionel Sambuc 	rump_pub_etfs_register(thepath, args->ta_hostpath, RUMP_ETFS_CHR);
13411be35a1SLionel Sambuc 	sprintf(buf, "%p", &args->ta_cleanerloop);
13511be35a1SLionel Sambuc 
13611be35a1SLionel Sambuc 	the_argv[0] = "megamaid";
13711be35a1SLionel Sambuc 	the_argv[1] = "-D"; /* don't fork() & detach */
13811be35a1SLionel Sambuc 	the_argv[2] = "-S";
13911be35a1SLionel Sambuc 	the_argv[3] = buf;
14011be35a1SLionel Sambuc 	the_argv[4] = args->ta_mntpath;
14111be35a1SLionel Sambuc 	the_argv[5] = NULL;
14211be35a1SLionel Sambuc 
14311be35a1SLionel Sambuc 	/* xxxatf */
14411be35a1SLionel Sambuc 	optind = 1;
14511be35a1SLionel Sambuc 	opterr = 1;
14611be35a1SLionel Sambuc 
14711be35a1SLionel Sambuc 	lfs_cleaner_main(5, __UNCONST(the_argv));
14811be35a1SLionel Sambuc 
14911be35a1SLionel Sambuc 	return NULL;
15011be35a1SLionel Sambuc }
15111be35a1SLionel Sambuc 
15211be35a1SLionel Sambuc int
lfs_fstest_mount(const atf_tc_t * tc,void * buf,const char * path,int flags)15311be35a1SLionel Sambuc lfs_fstest_mount(const atf_tc_t *tc, void *buf, const char *path, int flags)
15411be35a1SLionel Sambuc {
15511be35a1SLionel Sambuc 	struct lfstestargs *args = buf;
15611be35a1SLionel Sambuc 	int res;
15711be35a1SLionel Sambuc 
15811be35a1SLionel Sambuc 	res = rump_sys_mkdir(path, 0777);
15911be35a1SLionel Sambuc 	if (res == -1)
16011be35a1SLionel Sambuc 		return res;
16111be35a1SLionel Sambuc 
16211be35a1SLionel Sambuc 	res = rump_sys_mount(MOUNT_LFS, path, flags, &args->ta_uargs,
16311be35a1SLionel Sambuc 	    sizeof(args->ta_uargs));
16411be35a1SLionel Sambuc 	if (res == -1)
16511be35a1SLionel Sambuc 		return res;
16611be35a1SLionel Sambuc 
16711be35a1SLionel Sambuc 	strcpy(args->ta_mntpath, path);
16811be35a1SLionel Sambuc 	res = pthread_create(&args->ta_cleanerthread, NULL, cleaner, args);
16911be35a1SLionel Sambuc 	if (res)
17011be35a1SLionel Sambuc 		return res;
17111be35a1SLionel Sambuc 
17211be35a1SLionel Sambuc 	/* wait for cleaner to initialize */
17311be35a1SLionel Sambuc 	sem_wait(&args->ta_cleanerloop);
17411be35a1SLionel Sambuc 
17511be35a1SLionel Sambuc 	return 0;
17611be35a1SLionel Sambuc }
17711be35a1SLionel Sambuc 
17811be35a1SLionel Sambuc int
lfs_fstest_unmount(const atf_tc_t * tc,const char * path,int flags)17911be35a1SLionel Sambuc lfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
18011be35a1SLionel Sambuc {
18111be35a1SLionel Sambuc 	int res;
18211be35a1SLionel Sambuc 
18311be35a1SLionel Sambuc 	res = rump_sys_unmount(path, flags);
18411be35a1SLionel Sambuc 	if (res == -1) {
18511be35a1SLionel Sambuc 		return res;
18611be35a1SLionel Sambuc 	}
18711be35a1SLionel Sambuc 
18811be35a1SLionel Sambuc 	res = rump_sys_rmdir(path);
18911be35a1SLionel Sambuc 	return res;
19011be35a1SLionel Sambuc }
191