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

#define __NO_VERSION__

#include <xfs/xfs_locl.h>
#include <xfs/xfs_common.h>
#include <xfs/xfs_fs.h>
#include <xfs/xfs_deb.h>
#include <xfs/xfs_cache.h>

RCSID("$Id: xfs_node.c,v 1.30 1998/12/22 13:16:34 lha Exp $");

void
xfs_attr2inode(const struct xfs_attr *attr, struct inode *inode)
{
    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;
    inode->i_blocks = inode->i_size / I_BLOCKS_UNIT;
}

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\n", sb, node, newnode));
    if (sb == NULL) {
     	printk(KERN_EMERG "XFS Panic: xfs_iget: super block is NULL\n");
     	return NULL;
    }
    if ((inode = iget(sb, (ino_t)newnode)) == NULL) {
    	printk(KERN_EMERG "XFS Panic: xfs_iget: iget failed\n");
    	return NULL;
    }
    
    xfs_attr2inode (attr, inode);
    
    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), XFS_MEM_XNODE);
	if (result == 0) {
	    printk(KERN_EMERG "xfs_alloc(%d) failed\n", (int)sizeof(*result));
	    panic("new_xfs_node: You Loose!");
	}
	
	memset(result, 0, sizeof(*result));
	
	/* Save reference on list */
	result->next = xfsp->nodes;
	xfsp->nodes = result;
	
	xfsp->nnodes++;
	
	result->anonrights = node->anonrights;
	result->handle = node->handle;
	result->flags = 0;
	result->tokens = 0;
	
	v=xfs_iget(XFS_TO_VFS(xfsp),node,result);
	result->vn = v;
	/* XXX - handle this case better */
    } 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);
    
    /* 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(KERN_EMERG "XFS Panic: free_xfs_node(0x%p) failed?\n", node);
	return;
    }
    
done:
    /* XXX Really need to put back dirty data first. */
    if (DATA_FROM_XNODE(node)) {
#ifndef LINUX2_1
	iput(DATA_FROM_XNODE(node));
	DATA_FROM_XNODE(node) = NULL;
#else
	dput(DATA_FROM_XNODE(node));
	DATA_FROM_XNODE(node) = NULL;
#endif /* LINUX2_1 */
    }
    xfsp->nnodes--;
    XFSDEB(XDEBNODE, ("free_xfs_node xnode: %p inode:%p\n",
		      node, XNODE_TO_VNODE(node)));
    XNODE_TO_VNODE(node)->u.generic_ip = NULL;
    xfs_free(node, XFS_MEM_XNODE);
    
    XFSDEB(XDEBNODE, ("free_xfs_node done\n"));
}

void
free_all_xfs_nodes(struct xfs *xfsp)
{
    struct xfs_node *xnode;
    XFSDEB(XDEBNODE, ("free_all_xfs_nodes starting\n"));
    
#ifndef LINUX2_1
    xfs_cache_purge();
#endif /* LINUX2_1 */

    /* There might still be a few nodes out there, invalidate them */
    for (xnode = xfsp->nodes; xnode; xnode = xnode->next) {
	xfs_invalid_xnode(xnode);
    }
    
    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;
}

/*
 * Returns 1 if pag has any rights set in the node
 */
int
xfs_has_pag(const struct xfs_node *xn, pag_t pag)
{
    int i;
    
    for (i = 0; i < MAXRIGHTS; i++)
	if (xn->id[i] == pag)
	    return 1;
    
    return 0;
}
