/*
 * 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>

RCSID("$Id: xfs_syscalls.c,v 1.22 1999/08/02 11:43:57 assar Exp $");

/*
 * XFS system calls.
 */

#include <xfs/xfs_syscalls.h>
#include <xfs/xfs_message.h>
#include <xfs/xfs_dev.h>
#include <xfs/xfs_node.h>
#include <xfs/xfs_deb.h>
#include <xfs/xfs_vfsops.h>

/* Misc syscalls */
#include <kafs.h>

/*
 * Def pag:
 *  33536 <= g0 <= 34560
 *  32512 <= g1 <= 48896
 */

#define XFS_PAG1_LLIM 33536
#define XFS_PAG1_ULIM 34560
#define XFS_PAG2_LLIM 32512
#define XFS_PAG2_ULIM 48896

static gid_t pag_part_one = XFS_PAG1_LLIM;
static gid_t pag_part_two = XFS_PAG2_LLIM;

int
xfs_is_pag(struct cred *cred)
{
    /* The first group is the gid of the user ? */

    if (cred->cr_ngroups >= 3 &&
	cred->cr_groups[1] >= XFS_PAG1_LLIM &&
	cred->cr_groups[1] <= XFS_PAG1_ULIM &&
	cred->cr_groups[2] >= XFS_PAG2_LLIM &&
	cred->cr_groups[2] <= XFS_PAG2_ULIM)
	return 1;
    else
	return 0;
}

pag_t
xfs_get_pag(struct cred * cred)
{
    if (xfs_is_pag(cred)) {

	return (((cred->cr_groups[1] << 16) & 0xFFFF0000) |
		((cred->cr_groups[2] & 0x0000FFFF)));

    } else
	return cred->cr_uid;	       /* XXX */
}

/*
 * Set the pag in `ret_cred'.
 */

static int
store_pag (struct cred **ret_cred, gid_t part1, gid_t part2)
{
    struct cred *cred = *ret_cred;

    if (!xfs_is_pag(cred)) {
	int i;

	/* Check if it fits */
	if (cred->cr_ngroups + 2 >= ngroups_max)
	    return set_errno(E2BIG); /* XXX Hmmm, better error ? */

	cred = crcopy(cred);

	/* Copy the groups */
	for (i = cred->cr_ngroups; i >= 0; i--) {
	    cred->cr_groups[i + 2] = cred->cr_groups[i];
	}
	cred->cr_ngroups += 2;
    } else {
	cred = crcopy(cred);
    }

    cred->cr_groups[1] = part1;
    cred->cr_groups[2] = part2;
    *ret_cred = cred;
    return 0;
}

/*
 * Acquire a new pag for `proc'.
 */

static int
xfs_setpag_call(struct proc *proc)
{
    int ret;

    ret = store_pag (&proc->p_cred, pag_part_one, pag_part_two++);
    if (ret)
	return ret;

    if (pag_part_two > XFS_PAG2_ULIM) {
	pag_part_one++;
	pag_part_two = XFS_PAG2_LLIM;
    }
    return 0;
}

static struct sysent old_setgroups;

static int (*old_setgroups_func)(u_int, gid_t *);

static int xfs_setgroups (u_int, gid_t *);

void
xfs_install_setgroups(void)
{
    old_setgroups = sysent[SYS_setgroups];
    old_setgroups_func = old_setgroups.sy_call;
    sysent[SYS_setgroups].sy_call = xfs_setgroups;
}

void
xfs_uninstall_setgroups(void)
{
    sysent[SYS_setgroups] = old_setgroups;
}

/*
 * Remove the pags from the groups
 */

static int
xfs_unpag (struct cred *cred)
{
    while (xfs_is_pag (cred)) {
	int i;

	for (i = 0; i < cred->cr_ngroups - 2; ++i)
	    cred->cr_groups[i] = cred->cr_groups[i+2];
	cred->cr_ngroups -= 2;
    }
    return 0;
}

/*
 * A wrapper around setgroups that preserves the pag.
 */

static int
xfs_setgroups (u_int gidsetsize, gid_t *gidset)
{
    if (xfs_is_pag (curproc->p_cred)) {
	gid_t part1, part2;
	int ret;

	if (gidsetsize + 2 >= ngroups_max)
	    return set_errno(E2BIG);

	part1 = curproc->p_cred->cr_groups[1];
	part2 = curproc->p_cred->cr_groups[2];
	ret   = (*old_setgroups_func) (gidsetsize, gidset);
	if (ret)
	    return set_errno(ret);
	ret = store_pag (&curproc->p_cred, part1, part2);
	if (ret)
	    return set_errno(ret);
	return 0;
    } else {
	int ret;

	ret = (*old_setgroups_func) (gidsetsize, gidset);
	if (xfs_is_pag (curproc->p_cred))
	    xfs_unpag (curproc->p_cred);
	if (ret)
	    return set_errno(ret);
	return 0;
    }
}

/*
 *
 */

static int
fhget_call (struct vnode *vp,
	    struct ViceIoctl *vice_ioctl)
{
    int error;
    struct xfs_fh_args fh_args;

    XFSDEB(XDEBSYS, ("xfs_fhget: vp = %x, vice_ioctl = %x\n",
		     (int)vp, (int)vice_ioctl));

    if (vp == NULL)
	return set_errno(EBADF);

    fh_args.fsid = vp->v_vfsp->vfs_fsid;

    fh_args.fid.fid_len = MAXFIDSZ;

    error = VOP_FID(vp, &fh_args.fid);
    VN_RELE(vp);
    if (error) {
	XFSDEB(XDEBSYS, ("fhget: vop_fid failed: %d\n", error));
	return set_errno(error);
    }

    if (vice_ioctl->out_size < sizeof(fh_args)) {
	XFSDEB(XDEBSYS, ("fhget: too small argument\n"));
	return set_errno(EINVAL);
    }

    error = copyout ((caddr_t)&fh_args,
		     (caddr_t)vice_ioctl->out,
		     sizeof(fh_args));

    return set_errno(error);
}

/*
 *
 */

static int
fhopen_call (struct vnode *vp,
	     struct ViceIoctl *vice_ioctl,
	     int flags)
{
    int error;
    struct xfs_fh_args fh_args;

    XFSDEB(XDEBSYS, ("xfs_fhopen: vp = %x\n", (int)vp));

    if (vp != NULL) {
	VN_RELE(vp);
	return set_errno(EINVAL);
    }

    if (vice_ioctl->in_size < sizeof(fh_args))
	return set_errno(EINVAL);

    error = copyin ((caddr_t)vice_ioctl->in,
		    (caddr_t)&fh_args, sizeof(fh_args));
    if (error)
	return set_errno(error);
    return xfs_fhopen (fh_args.fsid, fh_args.fid, flags);
}

/*
 * Send the pioctl to arlad
 */

static int
remote_pioctl (int a_opcode,
	       struct vnode *vp,
	       struct ViceIoctl *vice_ioctl)
{
    struct xfs_message_pioctl msg;
    struct xfs_message_wakeup_data *msg2;
    int error;

    if (vice_ioctl->in_size > 2048) {
	printf("xfs_pioctl_call: got a humongous in packet: opcode: %d",
	       a_opcode);
	return set_errno(EINVAL);
    }
    if (vice_ioctl->in_size != 0) {
	error = copyin((caddr_t)vice_ioctl->in,
		       (caddr_t)&msg.msg,
		       vice_ioctl->in_size);

	if (error)
	    return set_errno(error);
    }

    if (vp != NULL) {
	struct xfs_node *xn;

	xn = VNODE_TO_XNODE(vp);
	msg.handle = xn->handle;
	VN_RELE(vp);
    }

    msg.header.opcode = XFS_MSG_PIOCTL;
    msg.opcode = a_opcode;

    msg.insize = vice_ioctl->in_size;
    msg.cred.uid = curproc->p_cred->cr_uid;
    msg.cred.pag = xfs_get_pag(curproc->p_cred);

    error = xfs_message_rpc(0, &msg.header, sizeof(msg)); /* XXX */
    msg2 = (struct xfs_message_wakeup_data *) & msg;

    if (error == 0)
	error = msg2->error;
    else
	error = EINVAL; /* return EINVAL to not confuse applications */
    
    if (error == 0 && vice_ioctl->out_size)
	error = copyout((caddr_t)msg2->msg,
			(caddr_t)vice_ioctl->out, 
			min(msg2->len, vice_ioctl->out_size));
    return set_errno(error);
}

/*
 * Read/set the xfs debug level according to `vice_ioctl'
 */

static int
xfs_debug(struct ViceIoctl *vice_ioctl)
{
    int32_t flags;
    int error;

    if (!suser(CRED()))
	return EPERM;

    if (vice_ioctl->in_size != 0) {
	if (vice_ioctl->in_size < sizeof(int32_t))
	    return EINVAL;
	
	error = copyin (vice_ioctl->in,
			&flags,
			sizeof(flags));
	if (error)
	    return error;
	
	xfsdeb = flags;
    }
    
    if (vice_ioctl->out_size != 0) {
	if (vice_ioctl->out_size < sizeof(int32_t))
	    return EINVAL;
	
	error = copyout (&xfsdeb,
			 vice_ioctl->out,
			 sizeof(int32_t));
	if (error)
	    return error;
    }
    return 0;
}

/*
 * Handle `pioctl'
 */

static int
xfs_pioctl_call(char *a_pathP,
		int a_opcode,
		struct ViceIoctl *a_paramsP,
		int a_followSymlinks)
{
    int error;
    struct vnode *vp = NULL;
	
    XFSDEB(XDEBSYS, ("xfs_pioctl (opcode = %d)\n", a_opcode));
    XFSDEB(XDEBSYS, ("xfs_pioctl: params.size = (%d, %d)\n",
		     a_paramsP->in_size, a_paramsP->out_size));

    if (a_pathP != NULL) {
	char path[MAXPATHLEN];

	error = copyinstr (a_pathP, path, sizeof(path), NULL);
	if (error)
	    return set_errno(error);

	XFSDEB(XDEBSYS, ("xfs_syscall: looking up: %s\n", path));

	error = lookupname(path,
			   UIO_SYSSPACE,
			   a_followSymlinks ? FOLLOW : NO_FOLLOW,
			   NULL,
			   &vp);
	if (error)
	    return set_errno(EINVAL);
	XFSDEB(XDEBSYS, ("xfs_syscall: lookup -> %d, vp = %x\n",
			 error, (int)vp));
    }

    switch (a_opcode) {
    case VIOC_FHGET :
    case VIOC_FHGET_32 :
	XFSDEB(XDEBSYS, ("calling fhget(%x, %x)\n",
			 (int)vp, (int)a_paramsP));
	return fhget_call (vp, a_paramsP);
    case VIOC_FHOPEN :
    case VIOC_FHOPEN_32 :
	return fhopen_call (vp, a_paramsP, a_followSymlinks);
    case VIOC_XFSDEBUG :
    case VIOC_XFSDEBUG_32 :
	return xfs_debug (a_paramsP);
    default :
	return remote_pioctl (a_opcode, vp, a_paramsP);
    }
}

static int
xfs_syscall_int(int operation,
		char *a_pathP,
		int a_opcode,
		struct ViceIoctl *a_paramsP,
		int a_followSymlinks)
{
    int ret;

    switch (operation) {
    case AFSCALL_PIOCTL:
	ret = xfs_pioctl_call(a_pathP, a_opcode, a_paramsP,
			      a_followSymlinks);
	break;
    case AFSCALL_SETPAG:
	ret = xfs_setpag_call(curproc);
	break;
    default:
	uprintf("Unimplemeted call: %d\n", operation);
	ret = set_errno(EINVAL);
    }
    return ret;
}

static int
xfs_syscall(int operation,
	    char *a_pathP,
	    int a_opcode,
	    struct ViceIoctl *a_paramsP,
	    int a_followSymlinks)
{
    int error;
    struct ViceIoctl vice_ioctl;

    XFSDEB(XDEBSYS, ("xfs_syscall\n"));

    error = copyin ((caddr_t)a_paramsP,
		    (caddr_t)&vice_ioctl,
		    sizeof(vice_ioctl));
    if (error)
	return set_errno (error);

    return xfs_syscall_int (operation, a_pathP, a_opcode, &vice_ioctl,
			    a_followSymlinks);
}

static struct sysent xfs_sysent = {
    5,
    SE_ARGC | SE_LOADABLE,
    xfs_syscall
};

struct modlsys xfs_modlsys = {
    &mod_syscallops,
    "xfs syscall",
    &xfs_sysent
};

#ifdef _SYSCALL32_IMPL

static int
xfs_syscall32(int operation,
	      char *a_pathP,
	      int a_opcode,
	      struct ViceIoctl32 *a_paramsP32,
	      int a_followSymlinks)
{
    struct ViceIoctl vice_ioctl;
    struct ViceIoctl32 vice_ioctl32;
    int error;

    XFSDEB(XDEBSYS, ("xfs_syscall32\n"));

    error = copyin ((caddr_t)a_paramsP32,
		    &vice_ioctl32,
		    sizeof(vice_ioctl32));
    if (error)
	return set_errno (error);

    XFSDEB(XDEBSYS, ("xfs_syscall32: params = (%x, %x, %d, %d)\n",
		     vice_ioctl32.in, vice_ioctl32.out,
		     vice_ioctl32.in_size, vice_ioctl32.out_size));

    vice_ioctl.in       = (caddr_t)vice_ioctl32.in;
    vice_ioctl.out      = (caddr_t)vice_ioctl32.out;
    vice_ioctl.in_size  = vice_ioctl32.in_size;
    vice_ioctl.out_size = vice_ioctl32.out_size;

    return xfs_syscall_int (operation, a_pathP, a_opcode, &vice_ioctl,
			    a_followSymlinks);
}

static struct sysent xfs_sysent32 = {
    5,
    SE_ARGC | SE_LOADABLE,
    xfs_syscall32
};

struct modlsys xfs_modlsys32 = {
    &mod_syscallops32,
    "32-bit xfs syscall",
    &xfs_sysent32
};

#endif /* _SYSCALL32_IMPL */
