/*
 * Copyright (c) 1995, 1996, 1997, 1998, 1999 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/xfs_locl.h>
#include <xfs/xfs_message.h>
#include <xfs/xfs_fs.h>
#include <xfs/xfs_dev.h>
#include <xfs/xfs_syscalls.h>
#include <xfs/xfs_deb.h>

RCSID("$Id: xfs_wrap-bsd.c,v 1.22 1999/06/08 08:21:10 lha Exp $");

static int xfs_dev_major;

/*
 * Iff `dev' represents a valid xfs device.
 */

int
xfs_is_xfs_dev (dev_t dev)
{
    return major (dev) == xfs_dev_major
	&& minor(dev) >= 0 && minor(dev) < NXFS;
}

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

    if ((err = xfs_uninstall_filesys()) != 0)
	ret = err;
    if ((err = xfs_uninstall_device()) != 0)
	ret = err;
    if ((err = xfs_uninstall_syscalls()) != 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;
}

extern struct cdevsw xfs_dev;

/*
 * This is to build a kld module (FreeBSD3.0)
 */

#if KLD_MODULE

/*
 *
 */

static int
xfs_load(struct module *mod, int cmd, void *arg)
{
    int ret;

    XFSDEB(XDEBLKM, ("xfs_load\n"));

    switch (cmd) {
    case MOD_LOAD :
	ret = xfs_install ();
	if (ret == 0) {
	    xfs_dev_major = xfs_dev.d_maj;
	    printf ("xfs cdev loaded at %d\n", xfs_dev_major);
	}
	break;
    case MOD_UNLOAD :
	ret = xfs_uninstall ();
	break;
    default :
	ret = EINVAL;
	break;
    }
    XFSDEB(XDEBLKM, ("xfs_load = %d\n", ret));
    return ret;
}

extern struct vfsops xfs_vfsops;

extern struct sysent xfs_syscallent;

/* XXX */

#define AFS_SYSCALL 210

VFS_SET(xfs_vfsops, xfs, 0);

/*
 * DEV_MODULE is something that apperad around fbsd4.0+some month
 * at the same time CDEV_MODULE seams to have vanished.
 */

#ifdef DEV_MODULE

#define XFS_DEV_NO NOMAJ

DEV_MODULE(xfsdev, XFS_DEV_NO, NOMAJ, xfs_dev, xfs_load, NULL);

#else /* DEV_MODULE */

CDEV_MODULE(xfs_dev, NODEV, xfs_dev, xfs_load, NULL);

#endif /* DECLARE_MODULE */


#ifdef SYSCALL_MODULE
static int offset = NO_SYSCALL;
SYSCALL_MODULE(xfs_syscall, &offset, &xfs_syscallent, NULL, NULL);
#endif /* SYSCALL_MODULE */



#else /* KLD_MODULE */

/*
 * An ordinary lkm-module
 */

#if defined(__FreeBSD__) && __FreeBSD_version < 300000
static struct lkm_dev _module = {
    LM_DEV,
    LKM_VERSION,
    "xfs_mod",
    -1,
    LM_DT_CHAR,
    {(void *)&xfs_dev}
};
#elif defined(__FreeBSD__)
MOD_DEV(xfs,LM_DT_CHAR,-1,&xfs_dev);
#else
MOD_DEV("xfs_mod",LM_DT_CHAR,-1,&xfs_dev)
#endif

/*
 *
 */

static int
xfs_modload(struct lkm_table *lkmtp, int cmd)
{
    int error = 0;

    XFSDEB(XDEBLKM, ("xfs_modload\n"));

    error = xfs_install();
    if (error == 0)
	xfs_stat();
    return error;
}


/*
 *
 */

static int
xfs_modunload(struct lkm_table * lkmtp, int cmd)
{
    int error = 0;

    XFSDEB(XDEBLKM, ("xfs_modunload\n"));

    error = xfs_uninstall();
    if (!error)
	XFSDEB(XDEBLKM, ("xfs_modunload: successful\n"));
    else
	XFSDEB(XDEBLKM, ("xfs_modunload: unsuccessful, system unstable\n"));
    return error;
}

/*
 *
 */

static int
xfs_modstat(struct lkm_table * lkmtp, int cmd)
{
    int error = 0;

    XFSDEB(XDEBLKM, ("xfs_modstat\n"));

    error = xfs_stat();
    return error;
}

int
xfs_mod(struct lkm_table * lkmtp,
	int cmd,
	int ver)
{
    int ret;
    if (ver != LKM_VERSION)						
	return EINVAL;	/* version mismatch */			
    switch (cmd) {							
	int	error;							
    case LKM_E_LOAD:
	lkmtp->private.lkm_any = (struct lkm_any *)&_module;
	if ((error = xfs_modload(lkmtp, cmd)) != 0)
	    return error;
	break;
    case LKM_E_UNLOAD:
	if ((error = xfs_modunload(lkmtp, cmd)) != 0)
	    return error;
	break;
    case LKM_E_STAT:
	if ((error = xfs_modstat(lkmtp, cmd)) != 0)
	    return error;
	break;
    }
    ret = lkmdispatch(lkmtp, cmd);
    if(cmd == LKM_E_LOAD)
	xfs_dev_major = _module.lkm_offset;
    return ret;
}
    
#endif /* KLD_MODULE */

