/*
 * 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_node.c,v 1.10 1998/02/27 23:27:21 map Exp $	 */

#ifndef lint
static char vcid[] = "$Id: xfs_node.c,v 1.10 1998/02/27 23:27:21 map Exp $";
#endif /* lint */

#define __NO_VERSION__

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

struct inode *
xfs_iget(struct super_block *sb, struct xfs_msg_node *node,
	 struct xfs_node *newnode)
{
    struct inode *inode;
    struct xfs_attr *attr = &node->attr;

    XFSDEB(XDEBNODE, ("xfs_iget sb: %p node: %p newnode: %p", sb, node, newnode));
    if (sb == NULL) {
     	printk("xfs_iget: super block is NULL\n");
     	return NULL;
    }
    if ((inode = iget(sb, (ino_t)newnode)) == NULL) {
    	printk("xfs_iget: iget failed\n");
    	return NULL;
    }
    
    inode->i_mode = XA_VALID_MODE(attr) ? attr->xa_mode : 0;
    inode->i_uid = XA_VALID_UID(attr) ? attr->xa_uid : 0;
    inode->i_gid = XA_VALID_GID(attr) ? attr->xa_gid : 0;
    inode->i_nlink = XA_VALID_NLINK(attr) ? attr->xa_nlink : 1;
    inode->i_size = XA_VALID_SIZE(attr) ? attr->xa_size : 0;
    inode->i_atime = XA_VALID_ATIME(attr) ? attr->xa_atime : 0;
    inode->i_mtime = XA_VALID_MTIME(attr) ? attr->xa_mtime : 0;
    inode->i_ctime = XA_VALID_CTIME(attr) ? attr->xa_ctime : 0;
    
    if (!XA_VALID_TYPE(attr))
	inode->i_op = &xfs_dead_inode_operations;
    else if (attr->xa_type == XFS_FILE_REG)
	inode->i_op = &xfs_file_inode_operations;
    else if (attr->xa_type == XFS_FILE_DIR)
	inode->i_op = &xfs_dir_inode_operations;
    else if (attr->xa_type == XFS_FILE_LNK)
	inode->i_op = &xfs_link_inode_operations;
    else
	inode->i_op = &xfs_dead_inode_operations;
    
    
    inode->u.generic_ip = newnode;

    return inode;
}

/*
 * Create a new xfs_node and make a VN_HOLD()!
 *
 * Also prevents creation of duplicates. This happens
 * whenever there are more than one name to a file,
 * "." and ".." are common cases.
 */
/*
 * xfsp = pointer to the current xfs file system (maps onto super_block)
 *
 */

struct xfs_node *
new_xfs_node(struct xfs *xfsp, struct xfs_msg_node *node)
{
  struct xfs_node *result;

  XFSDEB(XDEBNODE, ("new_xfs_node %d.%d.%d.%d\n",
		   node->handle.a,
		   node->handle.b,
		   node->handle.c,
		   node->handle.d));

  /* Does not allow duplicates */
  result = xfs_node_find(xfsp, &node->handle);
  if (result == 0) {
      struct inode *v;

      result = xfs_alloc(sizeof(*result));
      if (result == 0) {
	  printk("xfs_alloc(%d) failed\n", (int)sizeof(*result));
	  panic("new_xfs_node: You Loose!");
      }
      
      memset(result, 0, sizeof(*result));
      
#if 0
      error = getnewvnode(VT_AFS, XFS_TO_VFS(xfsp), xfs_vnodeop_p, &v);
#endif
/*      i_ino = node->attr.fileid;*/

      /* Save reference on list */
#if 1
      result->next = xfsp->nodes;
      xfsp->nodes = result;
#endif
      xfsp->nnodes++;

      result->handle = node->handle;
      result->flags = 0;
      result->tokens = 0;
      
#if 0
      result->vn->v_type = node->attr.va_type;
#endif
      
      v=xfs_iget(XFS_TO_VFS(xfsp),node,result);
      result->vn = v;
      /* XXX - handle this case better */
#if 0
      if (error) {
	  panic("new_xfs_node: getnewvnode failed: error = %d\n", error);
      }
#endif

  } else {
      /* Node is already cached */
      XNODE_TO_VNODE(result)->i_count++;
  }

  /* Init other fields */
  result->attr = node->attr;
  XFS_TOKEN_SET(result, XFS_ATTR_R, XFS_ATTR_MASK);
  memmove(result->id, node->id, sizeof(result->id));
  memmove(result->rights, node->rights, sizeof(result->rights));

  return result;
}

void
free_xfs_node(struct xfs_node *node)
{
  struct xfs *xfsp;
  struct xfs_node *t;

  XFSDEB(XDEBNODE, ("free_xfs_node starting\n"));

  if (node == NULL)
    return;

  xfsp = XFS_FROM_XNODE(node);

#if 1
  /* First remove from chain. */
  if (node == xfsp->nodes) {
      xfsp->nodes = node->next;
  } else {
      if (xfsp->nodes == NULL)
	  goto done;
      for (t = xfsp->nodes; t->next; t = t->next)
	if (t->next == node) {
	    t->next = t->next->next;
	    goto done;
	}
      printk("XFS PANIC Error: free_xfs_node(0x%x) failed?\n", (int) node);
      return;
  }
#endif

 done:
  /* XXX Really need to put back dirty data first. */
  if (DATA_FROM_XNODE(node)) {
      iput(DATA_FROM_XNODE(node));
  }
  xfsp->nnodes--;
  XNODE_TO_VNODE(node)->u.generic_ip = NULL;
  xfs_free(node);

  XFSDEB(XDEBNODE, ("free_xfs_node done\n"));
}

void
free_all_xfs_nodes(struct xfs *xfsp)
{
  struct xfs_node *t;

  XFSDEB(XDEBNODE, ("free_all_xfs_nodes starting\n"));

  xfs_cache_purge();
  XFSDEB(XDEBNODE, ("free_all_xfs_nodes now removing root\n"));
  if (xfsp->root) {
      iput(XNODE_TO_VNODE(xfsp->root));
      xfsp->root = 0;
  }

#if 0 /* Is this really needed? */
  /* There might still be a few nodes out there, invalidate them */
  for (t = xfsp->nodes; t; t = t->next) {
      t->tokens = 0;
      if (DATA_FROM_XNODE(t)) {
	  vrele(DATA_FROM_XNODE(t));
	  DATA_FROM_XNODE(t) = (struct vnode *) 0;
      }
  }
#endif

  XFSDEB(XDEBNODE, ("free_all_xfs_nodes done\n"));
}

struct xfs_node *
xfs_node_find(struct xfs *xfsp, xfs_handle *handlep)
{
  struct xfs_node *x;
  
  XFSDEB(XDEBNODE, ("xfs_node_find\n"));

  for(x = xfsp->nodes;
      x != NULL;
      x = x->next)
    if (xfs_handle_eq(&x->handle, handlep))
      break;
  if(x != NULL)
      return x;
  else
      return NULL;
}
