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

/* 	$Id: xfs_vfsops.c,v 1.11 1998/01/19 12:28:09 map Exp $	 */
/*      $Source: /usr/local/cvsroot/arla/xfs/linux/xfs_vfsops.c,v $      */

#ifndef lint
static char vcid[] = "$Id: xfs_vfsops.c,v 1.11 1998/01/19 12:28:09 map Exp $";
#endif /* lint */

#define __NO_VERSION__

#include "xfs_locl.h"
#include "xfs_cache.h"
#include <nxfs.h>

struct xfs xfs[NXFS];

static void xfs_read_inode(struct inode *inode);
static void xfs_put_inode(struct inode *inode);
static void xfs_put_super(struct super_block *sb);
static struct inode *
make_dead_vnode(struct super_block *sb);

extern struct inode_operations xfs_dead_inode_operations;

static struct super_operations xfs_sops = { 
  	xfs_read_inode,      /* read_inode */
	NULL,                /* notify_change */
	NULL,                /* write_inode */
        xfs_put_inode,       /* put_inode */
	xfs_put_super,       /* put_super */
	NULL,                /* write_super */
	NULL,                /* xfs_statfs */
	NULL                 /* remount */
};

static int
xfs_root(struct super_block *sb, struct inode **vpp)
{
  struct xfs *xfsp = VFS_TO_XFS(sb);
  struct xfs_message_getroot msg;
  int errno;

  XFSDEB(XDEBVFOPS, ("xfs_root sp = 0x%x\n", (u_int) sb));

  do {
    if (xfsp->root != NULL) {
        *vpp = XNODE_TO_VNODE(xfsp->root);
        return 0;
    }
    
    msg.header.opcode = XFS_MSG_GETROOT;

    /*
     * Mounting should done by root, so get the root node with
     * root's priviliges (usually none, and none is needed).
     */

    msg.cred.uid = 0;
    msg.cred.pag = 0;

    errno = xfs_message_rpc(xfsp->fd, &msg.header, sizeof(msg));
    XFSDEB(XDEBVFOPS, ("xfs_root xfs_message_rpc errno = %d\n", errno));
    if (errno == 0)
        errno = ((struct xfs_message_wakeup *) &msg)->errno;
    XFSDEB(XDEBVFOPS, ("xfs_root xfs_message_wakeup errno = %d\n", errno));
  } while (errno == 0);

  /*
   * Failed to get message through, need to pretend that all went well
   * and return a fake dead vnode to be able to unmount.
   */

  *vpp = make_dead_vnode(sb);

  return 0;
}

static struct inode *
make_dead_vnode(struct super_block *sb)
{
    struct inode *dead;

    XFSDEB(XDEBNODE, ("make_dead_vnode sb = 0x%x\n", (u_int) sb));

    dead = iget (sb,0);
    if(!dead) { /* XXX - ugly.. better handling */
       panic("make_dead_vnode: iget failed\n");
    }

    dead->i_op = &xfs_dead_inode_operations;
    
    return dead;
}

struct super_block * xfs_read_super (struct super_block * sb, void * data,
				      int silent)
{
  int minordevice=0;

  printk("xfs_read_super starting\n");
  printk("xfs_read_super: sb: %p data: %p silent: %d\n",sb,data,silent);
  printk("xfs_read_super: %d:%d\n",(int)MAJOR(sb->s_dev),(int)MINOR(sb->s_dev));

  printk("xfs_read_super: begin setting variables\n");
  MOD_INC_USE_COUNT;

  xfs[minordevice].status = XFS_MOUNTED;
  xfs[minordevice].sb = sb;
  xfs[minordevice].root = 0;
  xfs[minordevice].nnodes = 0;
  xfs[minordevice].nodes = 0;
  xfs[minordevice].fd = minordevice;
  VFS_TO_XFS(sb) = &xfs[minordevice];

  if (xfs_root(sb,&sb->s_mounted)) {
    printk("xfs_read_super: get root inode failed\n");
    sb->s_dev=0;
    MOD_DEC_USE_COUNT;
    return NULL;
  }
  sb->s_op = &xfs_sops;

  sb->s_blocksize = 512;
  sb->s_blocksize_bits = 9;
  /*  sb->s_flags |= MS_RDONLY;*/
  
  printk("xfs_read_super: returning\n");
  return sb;

}

static void xfs_put_super(struct super_block *sb)
{
  printk("xfs_put_super starting\n");
  sb->s_dev = 0;
  printk("xfs_put_super exiting\n");
  MOD_DEC_USE_COUNT;
}

static void xfs_read_inode(struct inode *inode)
{
  
  XFSDEB(XDEBVFOPS,("xfs_read_inode starting inode: %p\n",inode));
  XFSDEB(XDEBVFOPS,("xfs_read_inode inode->i_ino: %ld\n",inode->i_ino));
  inode->i_blksize = 1024;
  inode->i_mode = 0;
  inode->i_op = NULL;
  XFSDEB(XDEBVFOPS,("xfs_read_inode exiting\n"));
}

static void xfs_put_inode(struct inode *inode)
{
    struct xfs_message_inactivenode msg;
    struct xfs *xfsp = XFS_FROM_VNODE(inode);
    struct xfs_node *xn = VNODE_TO_XNODE(inode);

    XFSDEB(XDEBNODE, ("xfs_put_inode inode = 0x%p\n", inode));

    if (xn == NULL)
      return;

    msg.header.opcode = XFS_MSG_INACTIVENODE;
    msg.handle = xn->handle;
    xfs_message_send(xfsp->fd, &msg.header, sizeof(msg));

    xfs_cache_delete(xn);
    free_xfs_node(xn);
}
