#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/buf.h>
#include <sys/mount.h>
#include <sys/queue.h>
#define VU_DIROP 0x01000000
#define vnode uvnode
#include <ufs/lfs/lfs.h>
#include <ufs/lfs/lfs_inode.h>
#undef vnode
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include "bufcache.h"
#include "extern.h"
#include "kernelops.h"
struct uvnodelst vnodelist;
struct uvnodelst getvnodelist[VNODE_HASH_MAX];
struct vgrlst vgrlist;
int nvnodes;
#ifndef VTOI
#define VTOI(vp) ((struct inode *)(vp)->v_data)
#endif
int
raw_vop_strategy(struct ubuf * bp)
{
if (bp->b_flags & B_READ) {
return kops.ko_pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
bp->b_blkno * dev_bsize);
} else {
return kops.ko_pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
bp->b_blkno * dev_bsize);
}
}
int
raw_vop_bwrite(struct ubuf * bp)
{
bp->b_flags &= ~(B_READ | B_DELWRI | B_DONE | B_ERROR);
raw_vop_strategy(bp);
brelse(bp, 0);
return 0;
}
int
raw_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
{
*daddrp = lbn;
return 0;
}
void
register_vget(void *fs, struct uvnode *func(void *, ino_t))
{
struct vget_reg *vgr;
vgr = emalloc(sizeof(*vgr));
vgr->vgr_fs = fs;
vgr->vgr_func = func;
LIST_INSERT_HEAD(&vgrlist, vgr, vgr_list);
}
static struct uvnode *
VFS_VGET(void *fs, ino_t ino)
{
struct vget_reg *vgr;
LIST_FOREACH(vgr, &vgrlist, vgr_list) {
if (vgr->vgr_fs == fs)
return vgr->vgr_func(fs, ino);
}
return NULL;
}
void
vnode_destroy(struct uvnode *tossvp)
{
struct ubuf *bp;
--nvnodes;
LIST_REMOVE(tossvp, v_getvnodes);
LIST_REMOVE(tossvp, v_mntvnodes);
while ((bp = LIST_FIRST(&tossvp->v_dirtyblkhd)) != NULL) {
LIST_REMOVE(bp, b_vnbufs);
bremfree(bp);
buf_destroy(bp);
}
while ((bp = LIST_FIRST(&tossvp->v_cleanblkhd)) != NULL) {
LIST_REMOVE(bp, b_vnbufs);
bremfree(bp);
buf_destroy(bp);
}
free(VTOI(tossvp)->inode_ext.lfs);
free(VTOI(tossvp)->i_din);
memset(VTOI(tossvp), 0, sizeof(struct inode));
free(tossvp->v_data);
memset(tossvp, 0, sizeof(*tossvp));
free(tossvp);
}
int hits, misses;
struct uvnode *
vget(void *fs, ino_t ino)
{
struct uvnode *vp, *tossvp;
int hash;
tossvp = NULL;
hash = ((unsigned long)fs + ino) & (VNODE_HASH_MAX - 1);
LIST_FOREACH(vp, &getvnodelist[hash], v_getvnodes) {
if (vp->v_fs != fs)
continue;
if (VTOI(vp)->i_number == ino) {
LIST_REMOVE(vp, v_getvnodes);
LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
++hits;
break;
}
if (LIST_EMPTY(&vp->v_dirtyblkhd) &&
vp->v_usecount == 0 &&
!(vp->v_uflag & VU_DIROP))
tossvp = vp;
}
if (nvnodes > VNODE_CACHE_SIZE && tossvp) {
vnode_destroy(tossvp);
}
if (vp)
return vp;
++misses;
return VFS_VGET(fs, ino);
}
void
vfs_init(void)
{
int i;
nvnodes = 0;
LIST_INIT(&vnodelist);
for (i = 0; i < VNODE_HASH_MAX; i++)
LIST_INIT(&getvnodelist[i]);
LIST_INIT(&vgrlist);
}