#include <sys/param.h>
#include <sys/systm.h>
#include <sys/vnode.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include "hfs.h"
#include "hfs_cnode.h"
extern lck_attr_t * hfs_lock_attr;
extern lck_grp_t * hfs_mutex_group;
extern lck_grp_t * hfs_rwlock_group;
lck_grp_t * chash_lck_grp;
lck_grp_attr_t * chash_lck_grp_attr;
lck_attr_t * chash_lck_attr;
LIST_HEAD(cnodehashhead, cnode) *cnodehashtbl;
u_long cnodehash;
#define CNODEHASH(device, inum) (&cnodehashtbl[((device) + (inum)) & cnodehash])
lck_mtx_t hfs_chash_mutex;
__private_extern__
void
hfs_chashinit()
{
cnodehashtbl = hashinit(desiredvnodes, M_HFSMNT, &cnodehash);
chash_lck_grp_attr= lck_grp_attr_alloc_init();
lck_grp_attr_setstat(chash_lck_grp_attr);
chash_lck_grp = lck_grp_alloc_init("cnode_hash", chash_lck_grp_attr);
chash_lck_attr = lck_attr_alloc_init();
lck_mtx_init(&hfs_chash_mutex, chash_lck_grp, chash_lck_attr);
}
__private_extern__
struct vnode *
hfs_chash_getvnode(dev_t dev, ino_t inum, int wantrsrc, int skiplock)
{
struct cnode *cp;
struct vnode *vp;
int error;
uint32_t vid;
loop:
lck_mtx_lock(&hfs_chash_mutex);
for (cp = CNODEHASH(dev, inum)->lh_first; cp; cp = cp->c_hash.le_next) {
if ((cp->c_fileid != inum) || (cp->c_dev != dev))
continue;
if (ISSET(cp->c_hflag, H_ALLOC | H_TRANSIT | H_ATTACH)) {
SET(cp->c_hflag, H_WAITING);
(void) msleep(cp, &hfs_chash_mutex, PDROP | PINOD,
"hfs_chash_getvnode", 0);
goto loop;
}
if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
continue;
}
vp = wantrsrc ? cp->c_rsrc_vp : cp->c_vp;
if (vp == NULLVP)
goto exit;
vid = vnode_vid(vp);
lck_mtx_unlock(&hfs_chash_mutex);
if ((error = vnode_getwithvid(vp, vid))) {
return (NULL);
}
if (!skiplock && hfs_lock(cp, HFS_EXCLUSIVE_LOCK) != 0) {
vnode_put(vp);
return (NULL);
}
if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
if (!skiplock)
hfs_unlock(cp);
vnode_put(vp);
return (NULL);
}
return (vp);
}
exit:
lck_mtx_unlock(&hfs_chash_mutex);
return (NULL);
}
__private_extern__
int
hfs_chash_snoop(dev_t dev, ino_t inum, int (*callout)(const struct cat_desc *,
const struct cat_attr *, void *), void * arg)
{
struct cnode *cp;
int result = ENOENT;
lck_mtx_lock(&hfs_chash_mutex);
for (cp = CNODEHASH(dev, inum)->lh_first; cp; cp = cp->c_hash.le_next) {
if ((cp->c_fileid != inum) || (cp->c_dev != dev))
continue;
if (!ISSET(cp->c_hflag, H_ALLOC | H_TRANSIT | H_ATTACH)) {
result = callout(&cp->c_desc, &cp->c_attr, arg);
}
break;
}
lck_mtx_unlock(&hfs_chash_mutex);
return (result);
}
__private_extern__
struct cnode *
hfs_chash_getcnode(dev_t dev, ino_t inum, struct vnode **vpp, int wantrsrc, int skiplock)
{
struct cnode *cp;
struct cnode *ncp = NULL;
vnode_t vp;
uint32_t vid;
loop:
lck_mtx_lock(&hfs_chash_mutex);
loop_with_lock:
for (cp = CNODEHASH(dev, inum)->lh_first; cp; cp = cp->c_hash.le_next) {
if ((cp->c_fileid != inum) || (cp->c_dev != dev))
continue;
if (ISSET(cp->c_hflag, H_ALLOC | H_ATTACH | H_TRANSIT)) {
SET(cp->c_hflag, H_WAITING);
(void) msleep(cp, &hfs_chash_mutex, PINOD,
"hfs_chash_getcnode", 0);
goto loop_with_lock;
}
if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
continue;
}
vp = wantrsrc ? cp->c_rsrc_vp : cp->c_vp;
if (vp == NULL) {
SET(cp->c_hflag, H_ATTACH);
lck_mtx_unlock(&hfs_chash_mutex);
} else {
vid = vnode_vid(vp);
lck_mtx_unlock(&hfs_chash_mutex);
if (vnode_getwithvid(vp, vid))
goto loop;
}
if (ncp) {
FREE_ZONE(ncp, sizeof(struct cnode), M_HFSNODE);
ncp = NULL;
}
if (!skiplock && hfs_lock(cp, HFS_EXCLUSIVE_LOCK) != 0) {
if (vp != NULLVP)
vnode_put(vp);
lck_mtx_lock(&hfs_chash_mutex);
if (vp == NULLVP)
CLR(cp->c_hflag, H_ATTACH);
goto loop_with_lock;
}
if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
if (!skiplock)
hfs_unlock(cp);
if (vp != NULLVP)
vnode_put(vp);
lck_mtx_lock(&hfs_chash_mutex);
if (vp == NULLVP)
CLR(cp->c_hflag, H_ATTACH);
goto loop_with_lock;
}
*vpp = vp;
return (cp);
}
if (skiplock)
panic("%s - should never get here when skiplock is set \n", __FUNCTION__);
if (ncp == NULL) {
lck_mtx_unlock(&hfs_chash_mutex);
MALLOC_ZONE(ncp, struct cnode *, sizeof(struct cnode), M_HFSNODE, M_WAITOK);
goto loop;
}
bzero(ncp, sizeof(struct cnode));
SET(ncp->c_hflag, H_ALLOC);
ncp->c_fileid = inum;
ncp->c_dev = dev;
TAILQ_INIT(&ncp->c_hintlist);
lck_rw_init(&ncp->c_rwlock, hfs_rwlock_group, hfs_lock_attr);
if (!skiplock)
(void) hfs_lock(ncp, HFS_EXCLUSIVE_LOCK);
LIST_INSERT_HEAD(CNODEHASH(dev, inum), ncp, c_hash);
lck_mtx_unlock(&hfs_chash_mutex);
*vpp = NULL;
return (ncp);
}
__private_extern__
void
hfs_chashwakeup(struct cnode *cp, int hflags)
{
lck_mtx_lock(&hfs_chash_mutex);
CLR(cp->c_hflag, hflags);
if (ISSET(cp->c_hflag, H_WAITING)) {
CLR(cp->c_hflag, H_WAITING);
wakeup((caddr_t)cp);
}
lck_mtx_unlock(&hfs_chash_mutex);
}
__private_extern__
void
hfs_chash_rehash(struct cnode *cp1, struct cnode *cp2)
{
lck_mtx_lock(&hfs_chash_mutex);
LIST_REMOVE(cp1, c_hash);
LIST_REMOVE(cp2, c_hash);
LIST_INSERT_HEAD(CNODEHASH(cp1->c_dev, cp1->c_fileid), cp1, c_hash);
LIST_INSERT_HEAD(CNODEHASH(cp2->c_dev, cp2->c_fileid), cp2, c_hash);
lck_mtx_unlock(&hfs_chash_mutex);
}
__private_extern__
int
hfs_chashremove(struct cnode *cp)
{
lck_mtx_lock(&hfs_chash_mutex);
if (ISSET(cp->c_hflag, H_ATTACH)) {
lck_mtx_unlock(&hfs_chash_mutex);
return (EBUSY);
}
LIST_REMOVE(cp, c_hash);
cp->c_hash.le_next = NULL;
cp->c_hash.le_prev = NULL;
lck_mtx_unlock(&hfs_chash_mutex);
return (0);
}
__private_extern__
void
hfs_chash_abort(struct cnode *cp)
{
lck_mtx_lock(&hfs_chash_mutex);
LIST_REMOVE(cp, c_hash);
cp->c_hash.le_next = NULL;
cp->c_hash.le_prev = NULL;
CLR(cp->c_hflag, H_ATTACH | H_ALLOC);
if (ISSET(cp->c_hflag, H_WAITING)) {
CLR(cp->c_hflag, H_WAITING);
wakeup((caddr_t)cp);
}
lck_mtx_unlock(&hfs_chash_mutex);
}
__private_extern__
void
hfs_chash_mark_in_transit(struct cnode *cp)
{
lck_mtx_lock(&hfs_chash_mutex);
SET(cp->c_hflag, H_TRANSIT);
lck_mtx_unlock(&hfs_chash_mutex);
}