/*
 * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the Kungliga Tekniska
 *      Högskolan and its contributors.
 *
 * 4. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "xfs_locl.h"
#include <xfs_dev.h>
#include <xfs_syscalls.h>

RCSID("$Id: xfs_wrap.c,v 1.1 1998/01/15 09:08:23 assar Exp $");

#if defined(__NetBSD__) || defined(__OpenBSD__)

MOD_MISC("xfs");

extern struct vnodeopv_desc xfs_vnodeop_opv_desc;

#elif defined(__FreeBSD__)

MOD_MISC(xfs);

#endif

static int
xfs_uninstall(void)
{
    int err, ret = 0;

    if ((err = xfs_uninstall_device()) != 0)
	ret = err;
    if ((err = xfs_uninstall_syscalls()) != 0)
	ret = err;
    if ((err = xfs_uninstall_filesys()) != 0)
	ret = err;

    return ret;
}

static int
xfs_install(void)
{
    int err = 0;

    if ((err = xfs_install_device()) ||
	(err = xfs_install_syscalls()) ||
	(err = xfs_install_filesys()))
	xfs_uninstall();
    return err;
}

static int
xfs_stat(void)
{
    int err = 0;

    if ((err = xfs_stat_device()) != 0)
	return err;
    else if ((err = xfs_stat_syscalls()) != 0)
	return err;
    else if ((err = xfs_stat_filesys()) != 0)
	return err;
    return err;
}

static int
xfs_load(struct lkm_table * lkmtp, int cmd)
{
    int err = 0;

    switch (cmd) {
    case LKM_E_LOAD:
#if 0				       /* isn't this handled by the kernel? */
	if (lkmexists(lkmtp))
	    return EEXIST;
#endif
	printf("Loading mod...\n");
	err = xfs_install();
	xfs_stat();
	break;
    case LKM_E_UNLOAD:
	printf("xfs: trying to unload\n");
	err = xfs_uninstall();
	if (!err)
	    printf("xfs: unload successful\n");
	else
	    printf("xfs: unload unsuccessful, system unstable\n");
	break;
    case LKM_E_STAT:
	printf("stating\n");
	err = xfs_stat();
	break;
    default:
	err = EINVAL;
	break;
    }
    return err;
}

int
xfs_mod(struct lkm_table * lkmtp,
	int cmd,
	int ver)
{
#if defined(__NetBSD__) || defined(__OpenBSD__)
    vfs_opv_init_explicit(&xfs_vnodeop_opv_desc);
    vfs_opv_init_default(&xfs_vnodeop_opv_desc);
#endif
    DISPATCH(lkmtp, cmd, ver, xfs_load, xfs_load, xfs_load);
}
