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