/*
 * 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_message.c,v 1.7 1998/01/19 02:18:32 map Exp $	 */

#ifndef lint
static char vcid[] = "$Id: xfs_message.c,v 1.7 1998/01/19 02:18:32 map Exp $";
#endif /* lint */

#define __NO_VERSION__

#include "xfs_locl.h"
#include "xfs_cache.h"
#include "xfs_message.h"
#include "xdeb.h"
#include "xfs_fs.h"

int xfs_message_installroot(int fd,
			struct xfs_message_installroot *message,
			u_int size)
{
  int errno = 0;

  XFSDEB(XDEBMSG, ("xfs_message_installroot\n"));

  if (xfs[fd].root != 0)
    {
      printk("XFS PANIC Warning: xfs_message_installroot again\n");
      errno = EBUSY;
    }
  else
    {
      xfs[fd].root = new_xfs_node(&xfs[fd], &message->node); /* VN_HOLD's */
#if 0
      xfs[fd].root->vn.v_flag |= VROOT;
#endif
    }
  return errno;
}

int xfs_message_installnode(int fd, struct xfs_message_installnode *message, u_int size)
{
  int errno = 0;
  struct xfs_node *n, *dp;

  dp = xfs_node_find(&xfs[fd], &message->parent_handle);
  if (dp)
    {
      n = new_xfs_node(&xfs[fd], &message->node); /* VN_HOLD's */
      xfs_cache_add(dp, message->name, strlen(message->name), n);
      /* XXX there should be some way of telling when to deallocate */
    }
  else
    {
      printk("XFS PANIC Warning: xfs_message_install could not find parent\n");
      errno = ENOENT;
    }
  return errno;
}

int xfs_message_installattr(int fd, struct xfs_message_installattr *message, u_int size)
{
  int errno = 0;
  struct xfs_node *t;

  t = xfs_node_find(&xfs[fd], &message->node.handle);
  if (t != 0)
    {
      t->tokens = message->node.tokens;
      t->attr = message->node.attr;
      memmove(t->id, message->node.id, sizeof(t->id));
      memmove(t->rights, message->node.rights, sizeof(t->rights));
    }
  else
    {
      printk("XFS PANIC Warning: xfs_message_installattr didn't find node!\n");
      errno = ENOENT;
    }
  return errno;
}

int xfs_message_installdata(int fd, struct xfs_message_installdata *message, u_int size)
{
  struct xfs_node *t;
  struct inode *vp;
  int errno = 0;

  XFSDEB(XDEBMSG, ("xfs_message_installdata\n"));

  t = xfs_node_find(&xfs[fd], &message->node.handle);
  if (t != 0)
    {
      char tmp[sizeof(message->cache_handle) + 1];
      XFSDEB(XDEBMSG,("cache_handle = '%c%c%c%c'\n",
		      (int) message->cache_handle.data[0],
		      (int) message->cache_handle.data[1],
		      (int) message->cache_handle.data[2],
		      (int) message->cache_handle.data[3]));
      
      memmove (tmp, &message->cache_handle, sizeof(message->cache_handle));
      tmp[4] = '\0';
      current->fs->pwd->i_count++;
      errno = open_namei(tmp, 3, 0, &vp, current->fs->pwd);
#if 0
      errno = VOP_LOOKUP(u.u_cdir, tmp, &vp, u.u_cred, 0, 0);
#endif
      if (errno == 0)
	{
	  if (DATA_FROM_XNODE(t))
	    {
	      iput(DATA_FROM_XNODE(t));
#if 0
	      VN_RELE(DATA_FROM_XNODE(t));
#endif
	    }
	  DATA_FROM_XNODE(t) = vp; /* VOP_LOOKUP does an implicit VN_HOLD? */
	  t->tokens = message->node.tokens;
	  t->attr = message->node.attr;
	  memmove(t->id, message->node.id, sizeof(t->id));
	  memmove(t->rights, message->node.rights, sizeof(t->rights));
	}
      else
	printk("XFS PANIC Warning: xfs_message_installdata failed to lookup cache file = %s, errno = %d\n", tmp, errno);
    }
  else
    {
      printk("XFS PANIC Warning: xfs_message_installdata didn't find node!\n");
      errno = ENOENT;
    }
  return errno;
}

int xfs_message_invalidnode(int fd, struct xfs_message_invalidnode *message, u_int size)
{
  int errno = 0;
  struct xfs_node *t;
  
  t = xfs_node_find(&xfs[fd], &message->handle);
  if (t != 0)
    {
      /* XXX Really need to put back dirty data first. */
      if (DATA_FROM_XNODE(t))
	{
	  iput(DATA_FROM_XNODE(t));
	  DATA_FROM_XNODE(t) = (struct inode *) 0;
	}
      XFS_TOKEN_CLEAR(t, ~0,
		      XFS_OPEN_MASK | XFS_ATTR_MASK |
		      XFS_DATA_MASK | XFS_LOCK_MASK);
    }
  else
    {
      printk("XFS PANIC Warning: xfs_message_invalidnode didn't find node!\n");
      errno = ENOENT;
    }
  return errno;
}
