/* adosfs_anode.c 930820 Niklas Hallvist <niklas@appli.se> */

/* Inspired by (but not copied from) Frank J. Edwards and Paul Popelkas work
   on AmigaDOS FS for Amiga Unix and MS-DOS FS for NetBSD respectively.
   There are also code based on ufs/ufs_inode.c in here so the usual
   BSD copyright applies.  */

/*
 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
 * 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 University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.
 *
 */

#include "param.h"
#include "types.h"
#include "systm.h"
#include "errno.h"
#include "mount.h"
#include "vnode.h"
#include "time.h"
#include "proc.h"

#include "adosfs_defs.h"
#include "adosfs_date.h"
#include "adosfs_anode.h"
#include "adosfsmount.h"
#include "adosfs_proto.h"

#define	AHSZ	512
#if	((AHSZ & (AHSZ - 1)) == 0)
#define	AHASH(dev,ino)	(((dev) + (ino)) & (AHSZ - 1))
#else
#define	AHASH(dev,ino)	(((unsigned)((dev) + (ino))) % AHSZ)
#endif

union ahead {
  union  ahead *ah_head[2];
  struct anode *ah_chain[2];
} ahead[AHSZ];

extern struct vnodeops adosfs_vnodeops;

int
adosfs_init ()
{
  int i;
  union ahead *ah = ahead;

  if (VN_MAXPRIVATE < sizeof (struct anode))
    panic("adosfs_init: vnode too small");
  for (i = AHSZ; --i >= 0; ah++)
    {
      ah->ah_head[0] = ah;
      ah->ah_head[1] = ah;
    }
  return 0;
}

/*
 * Look up an AmigaDOS FS anode number to find its incore vnode.
 * If it is not in core, read it in from the specified device.
 * If it is in core, wait for the lock bit to clear, then
 * return the anode locked. Detection and handling of mount
 * points must be done by the calling routine.
 */
int
aget (struct adosfsmount *amp, ados_u_long key, struct anode **app)
{
  struct mount *mp = amp->am_mountp;
  struct vnode *vp;
  struct anode *ap;
  union ahead *ah;
  dev_t dev = amp->am_dev;
  int error;

  ah = &ahead[AHASH (dev, key)];
 loop:
  for (ap = ah->ah_chain[0]; ap != (struct anode *)ah; ap = ap->a_forw)
    {
      if (key != ap->a_keyblk || dev != ap->a_dev)
	continue;
      if ((ap->a_flag & ALOCKED) != 0)
	{
	  ap->a_flag |= AWANT;
	  tsleep ((caddr_t)ap, PINOD, "aget", 0);
	  goto loop;
	}
      if (vget (ATOV (ap)))
	goto loop;
      *app = ap;
      return 0;
    }

  /*
   * Allocate a new inode.
   */
  if (error = getnewvnode (VT_ADOSFS, mp, &adosfs_vnodeops, &vp))
    {
      *app = 0;
      return error;
    }
  ap = VTOA (vp);
  ATOV (ap) = vp;
  insque (ap, ah);
  ALOCK (ap);

  if (error = adosfs_read_anode (ap, key, amp))
    {
      remque (ap);
      ap->a_forw = ap;
      ap->a_back = ap;
      aput (ap);
      *app = 0;
      return error;
    }

  ap->a_dev = dev;
  VREF (amp->am_devvp);
  *app = ap;
  return 0;
}

void
aput (struct anode *ap)
{
  if ((ap->a_flag & ALOCKED) == 0)
    panic ("aput");
  AUNLOCK (ap);
  vrele (ATOV (ap));
}

/*
 * Lock an inode. If its already locked, set the WANT bit and sleep.
 */
void
alock (struct anode *ap)
{
  while (ap->a_flag & ALOCKED)
    {
      ap->a_flag |= AWANT;
      if (ap->a_spare0 == curproc->p_pid)
	panic( "locking against myself");
      ap->a_spare1 = curproc->p_pid;
      (void) tsleep ((caddr_t)ap, PINOD, "alock", 0);
    }
  ap->a_spare1 = 0;
  ap->a_spare0 = curproc->p_pid;
  ap->a_flag |= ALOCKED;
}

/*
 * Unlock an inode.  If WANT bit is on, wakeup.
 */
void
aunlock (struct anode *ap)
{
  if ((ap->a_flag & ALOCKED) == 0)
    vprint ("aunlock: unlocked anode", ATOV (ap));
  ap->a_spare0 = 0;
  ap->a_flag &= ~ALOCKED;
  if (ap->a_flag & AWANT)
    {
      ap->a_flag &= ~AWANT;
      wakeup ((caddr_t)ap);
    }
}
