/*
 * Copyright (c) 1995, 1996, 1997, 1998 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.
 */

/* 	$Id: xfs_syscalls.c,v 1.5 1998/01/12 22:16:42 map Exp $	 */
/*      $Source: /usr/local/cvsroot/arla/xfs/linux/xfs_syscalls.c,v $      */

#ifndef lint
static char vcid[] = "$Id: xfs_syscalls.c,v 1.5 1998/01/12 22:16:42 map Exp $";
#endif /* lint */

#define __NO_VERSION__

#include "xfs_locl.h"
#include <asm/unistd.h>
#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 ;

static int xfs_get_ngroups(void)
{
    int i;

    for (i = 0 ; current->groups[i] != NOGROUP && i < NGROUPS; i++);
    return i;
}

static int xfs_is_pag(void)
{
    
    if (current->groups[0] != NOGROUP &&
	current->groups[1] != NOGROUP &&
	current->groups[0] >= XFS_PAG1_LLIM &&
	current->groups[0] <= XFS_PAG1_ULIM &&
	current->groups[1] >= XFS_PAG2_LLIM &&
	current->groups[1] <= XFS_PAG2_ULIM)
	
	return 1;
    else
	return 0 ;
}


pag_t xfs_get_pag(void)
{
    if (xfs_is_pag()) {
	
	return (((current->groups[0] << 16) & 0xFFFF0000) |
		((current->groups[1] & 0x0000FFFF))) ;

    } else
	return current->uid;
}

static int
xfs_setpag_call(void)
{
    int i ;

#ifdef DEBUG
    XFSDEB(XDEBSYS, ("xfs_setpag_call groups: "));
    for (i = 0 ; current->groups[i] != NOGROUP && i < NGROUPS; i++)
	XFSDEB(XDEBSYS, ("%d ",current->groups[i]));
    XFSDEB(XDEBSYS, ("\n"));
#endif

    if (!xfs_is_pag()) {
	int ngroups = xfs_get_ngroups();

	/* Check if it fits */
	if (ngroups + 2 >= NGROUPS) 
	    return -E2BIG;             /* XXX Hmmm, better error ? */

	/* Copy the groups */
	for (i = ngroups - 1; i >= 0 ; i--) {
	    current->groups[i+2] = current->groups[i] ; 
	}
	ngroups += 2 ;
	if (ngroups < NGROUPS)
	    current->groups[ngroups] = NOGROUP;

    }

    current->groups[0] = pag_part_one ;
    current->groups[1] = pag_part_two++ ;

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

    /* Opps, we wraped around... do some smarter stuff ? */
    if (pag_part_one > XFS_PAG1_ULIM) 
	pag_part_one = XFS_PAG1_LLIM ;
	
    return 0;
}


asmlinkage int
sys_afs (int operation,
	 char *a_pathP,
	 int a_opcode,
	 struct ViceIoctl *a_paramsP,
	 int a_followSymlinks)
{
    int error;
    int errno = 0 ;
    struct ViceIoctl vice_ioctl; 
    struct xfs_message_pioctl msg;
    struct xfs_message_wakeup_data *msg2;
    
    printk("sys_afs operation: %d a_pathP: %p a_opcode: %d a_paramsP: %p a_followSymlinks: %d\n",operation,a_pathP,a_opcode,a_paramsP,a_followSymlinks);
    
    switch (operation) {
    case AFSCALL_PIOCTL:
	printk("xfs_pioctl\n");
	error = verify_area(VERIFY_WRITE, a_paramsP, sizeof(*a_paramsP));
	if (error)
	    return error;
	memcpy_fromfs(&vice_ioctl,a_paramsP,sizeof(*a_paramsP));
	if (vice_ioctl.in_size > 2048) {
	    printk("xfs_pioctl_call: got a humongous in packet: opcode: %d",
		   a_opcode);
	    return EINVAL;
	}
	if (vice_ioctl.in_size != 0) {
	    memcpy_fromfs(&msg.msg,vice_ioctl.in,vice_ioctl.in_size);
	}
	msg.header.opcode = XFS_MSG_PIOCTL;
	msg.opcode = a_opcode;
	
	msg.insize = vice_ioctl.in_size;
	msg.cred.uid = current->uid;
	msg.cred.pag = xfs_get_pag();
	
	switch(a_opcode) {
	case VIOCSETTOK:
	case VIOCUNLOG:
	    errno = xfs_message_rpc(0, &msg.header, sizeof(msg)); /* XXX */
	    if (errno == 0)
		errno = ((struct xfs_message_wakeup *) &msg)->errno;

	    printk("xfs_sys: VIOCSETTOK/UNLOG: errno: %d\n",errno);
	    break ;
	case VIOC_AFS_SYSNAME:
	    errno = xfs_message_rpc(0, &msg.header, sizeof(msg)); /* XXX */
	    printk("xfs_sys: xfs_message_rpc errno: %d\n",errno);
	    if (errno == 0) {
		msg2 = (struct xfs_message_wakeup_data *) &msg;
		errno = msg2->errno;
		printk("xfs_sys: message errno: %d\n",errno);
	    } else {
		return -errno;
	    }
	    if (errno != 0)
		return -errno;

	    printk("xfs_sys: copying %d bytes\n",msg2->len);
	    
	    memcpy_tofs(vice_ioctl.out, msg2->msg, msg2->len);
	    
	    break ;
	default:
	    printk("xfs_syscalls: unimplemeted call\n");
	    errno = EINVAL ;
	    break ;
	}
	
	printk("xfs_sys returns %d\n",errno);
	return -errno ;
    case AFSCALL_SETPAG:
	return xfs_setpag_call();
	break;
    default:
	printk("xfs_syscalls: unimplemented call\n");
	return -EINVAL;
    }
    
}

typedef int (*sys_call_routine)(int operation,
				char *a_pathP,
				int a_opcode,
				struct ViceIoctl *a_paramsP,
				int a_followSymlinks);

extern sys_call_routine sys_call_table[];

static sys_call_routine old_afs_syscall=NULL;

void
install_afs_syscall (void)
{
  old_afs_syscall = sys_call_table[__NR_afs_syscall];
  sys_call_table[__NR_afs_syscall] = &sys_afs;
}

void
restore_afs_syscall (void)
{
  if (old_afs_syscall) {
    sys_call_table[__NR_afs_syscall] = old_afs_syscall;
    old_afs_syscall = NULL;
  }
}
