#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/cred.h>
#include <sys/kmem.h>
#include <sys/sysmacros.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/vnode.h>
#include <sys/fs/snode.h>
#include <sys/fs/fifonode.h>
#include <sys/debug.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/file.h>
#include <sys/open.h>
#include <sys/user.h>
#include <sys/termios.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/autoconf.h>
#include <sys/esunddi.h>
#include <sys/flock.h>
#include <sys/modctl.h>
struct vfs spec_vfs;
static dev_t specdev;
struct kmem_cache *snode_cache;
int spec_debug = 0;
static struct snode *sfind(dev_t, vtype_t, struct vnode *);
static struct vnode *get_cvp(dev_t, vtype_t, struct snode *, int *);
static void sinsert(struct snode *);
struct vnode *
specvp_devfs(
struct vnode *realvp,
dev_t dev,
vtype_t vtyp,
struct cred *cr,
dev_info_t *dip)
{
struct vnode *vp;
ASSERT(realvp && dip);
vp = specvp(realvp, dev, vtyp, cr);
ASSERT(vp);
spec_assoc_vp_with_devi(vp, dip);
return (vp);
}
struct vnode *
specvp(
struct vnode *vp,
dev_t dev,
vtype_t type,
struct cred *cr)
{
struct snode *sp;
struct snode *nsp;
struct snode *csp;
struct vnode *svp;
struct vattr va;
int rc;
int used_csp = 0;
if (vp == NULL)
return (NULL);
if (vp->v_type == VFIFO)
return (fifovp(vp, cr));
ASSERT(vp->v_type == type);
ASSERT(vp->v_rdev == dev);
nsp = kmem_cache_alloc(snode_cache, KM_SLEEP);
csp = kmem_cache_alloc(snode_cache, KM_SLEEP);
va.va_mask = AT_FSID | AT_TIMES;
rc = VOP_GETATTR(vp, &va, 0, cr, NULL);
mutex_enter(&stable_lock);
if ((sp = sfind(dev, type, vp)) == NULL) {
struct vnode *cvp;
sp = nsp;
svp = STOV(sp);
sp->s_realvp = vp;
VN_HOLD(vp);
sp->s_commonvp = NULL;
sp->s_dev = dev;
sp->s_dip = NULL;
sp->s_nextr = 0;
sp->s_list = NULL;
sp->s_plcy = NULL;
sp->s_size = 0;
sp->s_flag = 0;
if (rc == 0) {
sp->s_fsid = va.va_fsid;
sp->s_atime = va.va_atime.tv_sec;
sp->s_mtime = va.va_mtime.tv_sec;
sp->s_ctime = va.va_ctime.tv_sec;
} else {
sp->s_fsid = specdev;
sp->s_atime = 0;
sp->s_mtime = 0;
sp->s_ctime = 0;
}
sp->s_count = 0;
sp->s_mapcnt = 0;
vn_reinit(svp);
svp->v_flag = (vp->v_flag & VROOT);
svp->v_vfsp = vp->v_vfsp;
VFS_HOLD(svp->v_vfsp);
svp->v_type = type;
svp->v_rdev = dev;
(void) vn_copypath(vp, svp);
if (type == VBLK || type == VCHR) {
cvp = get_cvp(dev, type, csp, &used_csp);
svp->v_stream = cvp->v_stream;
sp->s_commonvp = cvp;
}
vn_exists(svp);
sinsert(sp);
mutex_exit(&stable_lock);
if (used_csp == 0) {
kmem_cache_free(snode_cache, csp);
}
} else {
mutex_exit(&stable_lock);
kmem_cache_free(snode_cache, nsp);
kmem_cache_free(snode_cache, csp);
}
return (STOV(sp));
}
struct vnode *
makespecvp(dev_t dev, vtype_t type)
{
struct snode *sp;
struct vnode *svp, *cvp;
time_t now;
sp = kmem_cache_alloc(snode_cache, KM_SLEEP);
svp = STOV(sp);
cvp = commonvp(dev, type);
now = gethrestime_sec();
sp->s_realvp = NULL;
sp->s_commonvp = cvp;
sp->s_dev = dev;
sp->s_dip = NULL;
sp->s_nextr = 0;
sp->s_list = NULL;
sp->s_plcy = NULL;
sp->s_size = 0;
sp->s_flag = 0;
sp->s_fsid = specdev;
sp->s_atime = now;
sp->s_mtime = now;
sp->s_ctime = now;
sp->s_count = 0;
sp->s_mapcnt = 0;
vn_reinit(svp);
svp->v_vfsp = &spec_vfs;
svp->v_stream = cvp->v_stream;
svp->v_type = type;
svp->v_rdev = dev;
vn_exists(svp);
mutex_enter(&stable_lock);
sinsert(sp);
mutex_exit(&stable_lock);
return (svp);
}
static void
spec_assoc_fence(dev_info_t *ndip, vnode_t *vp)
{
int fence;
struct snode *csp;
ASSERT(vp);
ASSERT(vn_matchops(vp, spec_getvnodeops()));
fence = 0;
if (ndip != NULL) {
mutex_enter(&DEVI(ndip)->devi_lock);
if (DEVI(ndip)->devi_flags & DEVI_RETIRED)
fence = 1;
mutex_exit(&DEVI(ndip)->devi_lock);
}
csp = VTOCS(vp);
ASSERT(csp);
mutex_enter(&csp->s_lock);
if (fence)
csp->s_flag |= SFENCED;
else
csp->s_flag &= ~SFENCED;
mutex_exit(&csp->s_lock);
FENDBG((CE_NOTE, "%sfenced common snode (%p) for new dip=%p",
fence ? "" : "un", (void *)csp, (void *)ndip));
}
void
spec_assoc_vp_with_devi(struct vnode *vp, dev_info_t *dip)
{
struct snode *csp;
dev_info_t *olddip;
ASSERT(vp);
if ((dip == NULL) && (getmajor(vp->v_rdev) == clone_major))
return;
if (dip)
e_ddi_hold_devi(dip);
csp = VTOS(VTOS(vp)->s_commonvp);
mutex_enter(&csp->s_lock);
olddip = csp->s_dip;
csp->s_dip = dip;
csp->s_flag |= SDIPSET;
if (olddip != dip)
csp->s_flag &= ~SSIZEVALID;
mutex_exit(&csp->s_lock);
spec_assoc_fence(dip, vp);
if (olddip)
ddi_release_devi(olddip);
}
dev_info_t *
spec_hold_devi_by_vp(struct vnode *vp)
{
struct snode *csp;
dev_info_t *dip;
ASSERT(vn_matchops(vp, spec_getvnodeops()));
csp = VTOS(VTOS(vp)->s_commonvp);
dip = csp->s_dip;
if (dip)
e_ddi_hold_devi(dip);
return (dip);
}
struct vnode *
specfind(dev_t dev, vtype_t type)
{
struct snode *st;
struct vnode *nvp;
mutex_enter(&stable_lock);
st = stable[STABLEHASH(dev)];
while (st != NULL) {
if (st->s_dev == dev) {
nvp = STOV(st);
if (nvp->v_type == type && st->s_commonvp != nvp) {
VN_HOLD(nvp);
if (nvp->v_path != NULL &&
ZONE_PATH_VISIBLE(nvp->v_path, curzone)) {
mutex_exit(&stable_lock);
return (nvp);
}
VN_RELE(nvp);
}
}
st = st->s_next;
}
mutex_exit(&stable_lock);
return (NULL);
}
int
devi_stillreferenced(dev_info_t *dip)
{
struct snode *sp;
int i;
if (e_ddi_devi_holdcnt(dip) == 0)
return (DEVI_NOT_REFERENCED);
mutex_enter(&stable_lock);
for (i = 0; i < STABLESIZE; i++) {
for (sp = stable[i]; sp != NULL; sp = sp->s_next) {
if (sp->s_dip == dip) {
mutex_exit(&stable_lock);
return (DEVI_REFERENCED);
}
}
}
mutex_exit(&stable_lock);
return (DEVI_NOT_REFERENCED);
}
int
spec_devi_open_count(struct snode *sp, dev_info_t **dipp)
{
dev_info_t *dip;
uint_t count;
struct vnode *vp;
ASSERT(sp);
ASSERT(dipp);
vp = STOV(sp);
*dipp = NULL;
if (sp->s_commonvp != vp || (dip = sp->s_dip) == NULL)
return (-1);
mutex_enter(&sp->s_lock);
count = sp->s_count + sp->s_mapcnt;
if (sp->s_flag & SLOCKED)
count++;
mutex_exit(&sp->s_lock);
*dipp = dip;
return (count);
}
struct vnode *
common_specvp(struct vnode *vp)
{
struct snode *sp;
if ((vp->v_type != VBLK) && (vp->v_type != VCHR) ||
!vn_matchops(vp, spec_getvnodeops()))
return (vp);
sp = VTOS(vp);
return (sp->s_commonvp);
}
static struct vnode *
get_cvp(
dev_t dev,
vtype_t type,
struct snode *nsp,
int *used_nsp)
{
struct snode *sp;
struct vnode *svp;
ASSERT(MUTEX_HELD(&stable_lock));
if ((sp = sfind(dev, type, NULL)) == NULL) {
sp = nsp;
*used_nsp = 1;
svp = STOV(sp);
sp->s_realvp = NULL;
sp->s_commonvp = svp;
sp->s_dev = dev;
sp->s_dip = NULL;
sp->s_nextr = 0;
sp->s_list = NULL;
sp->s_plcy = NULL;
sp->s_size = UNKNOWN_SIZE;
sp->s_flag = 0;
sp->s_fsid = specdev;
sp->s_atime = 0;
sp->s_mtime = 0;
sp->s_ctime = 0;
sp->s_count = 0;
sp->s_mapcnt = 0;
vn_reinit(svp);
svp->v_vfsp = &spec_vfs;
svp->v_type = type;
svp->v_rdev = dev;
vn_exists(svp);
sinsert(sp);
} else
*used_nsp = 0;
return (STOV(sp));
}
struct vnode *
commonvp(dev_t dev, vtype_t type)
{
struct snode *sp, *nsp;
struct vnode *svp;
nsp = kmem_cache_alloc(snode_cache, KM_SLEEP);
mutex_enter(&stable_lock);
if ((sp = sfind(dev, type, NULL)) == NULL) {
sp = nsp;
svp = STOV(sp);
sp->s_realvp = NULL;
sp->s_commonvp = svp;
sp->s_dev = dev;
sp->s_dip = NULL;
sp->s_nextr = 0;
sp->s_list = NULL;
sp->s_plcy = NULL;
sp->s_size = UNKNOWN_SIZE;
sp->s_flag = 0;
sp->s_fsid = specdev;
sp->s_atime = 0;
sp->s_mtime = 0;
sp->s_ctime = 0;
sp->s_count = 0;
sp->s_mapcnt = 0;
vn_reinit(svp);
svp->v_vfsp = &spec_vfs;
svp->v_type = type;
svp->v_rdev = dev;
vn_exists(svp);
sinsert(sp);
mutex_exit(&stable_lock);
} else {
mutex_exit(&stable_lock);
kmem_cache_free(snode_cache, nsp);
}
return (STOV(sp));
}
struct snode *stable[STABLESIZE];
int stablesz = STABLESIZE;
kmutex_t stable_lock;
static void
sinsert(struct snode *sp)
{
ASSERT(MUTEX_HELD(&stable_lock));
sp->s_next = stable[STABLEHASH(sp->s_dev)];
stable[STABLEHASH(sp->s_dev)] = sp;
}
void
sdelete(struct snode *sp)
{
struct snode *st;
struct snode *stprev = NULL;
ASSERT(MUTEX_HELD(&stable_lock));
st = stable[STABLEHASH(sp->s_dev)];
while (st != NULL) {
if (st == sp) {
if (stprev == NULL)
stable[STABLEHASH(sp->s_dev)] = st->s_next;
else
stprev->s_next = st->s_next;
break;
}
stprev = st;
st = st->s_next;
}
}
static struct snode *
sfind(
dev_t dev,
vtype_t type,
struct vnode *vp)
{
struct snode *st;
struct vnode *svp;
ASSERT(MUTEX_HELD(&stable_lock));
st = stable[STABLEHASH(dev)];
while (st != NULL) {
svp = STOV(st);
if (st->s_dev == dev && svp->v_type == type &&
VN_CMP(st->s_realvp, vp) &&
(vp != NULL || st->s_commonvp == svp) &&
(vp == NULL || st->s_realvp->v_vfsp == vp->v_vfsp)) {
VN_HOLD(svp);
return (st);
}
st = st->s_next;
}
return (NULL);
}
void
smark(struct snode *sp, int flag)
{
time_t now = gethrestime_sec();
ASSERT((flag & ~(SACC|SUPD|SCHG)) == 0);
if (((flag & sp->s_flag) != flag) ||
((flag & SACC) && (sp->s_atime != now)) ||
((flag & SUPD) && (sp->s_mtime != now)) ||
((flag & SCHG) && (sp->s_ctime != now))) {
mutex_enter(&sp->s_lock);
sp->s_flag |= flag;
if (flag & SACC)
sp->s_atime = now;
if (flag & SUPD)
sp->s_mtime = now;
if (flag & SCHG)
sp->s_ctime = now;
mutex_exit(&sp->s_lock);
}
}
offset_t
spec_maxoffset(struct vnode *vp)
{
struct snode *sp = VTOS(vp);
struct snode *csp = VTOS(sp->s_commonvp);
if (vp->v_stream)
return ((offset_t)-1);
else if (csp->s_flag & SANYOFFSET)
return ((offset_t)-1);
#ifdef _ILP32
if (csp->s_flag & SLOFFSET)
return (SPEC_MAXOFFSET_T);
#endif
return (MAXOFF_T);
}
static int
snode_constructor(void *buf, void *cdrarg, int kmflags)
{
struct snode *sp = buf;
struct vnode *vp;
vp = sp->s_vnode = vn_alloc(kmflags);
if (vp == NULL) {
return (-1);
}
vn_setops(vp, spec_getvnodeops());
vp->v_data = sp;
mutex_init(&sp->s_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&sp->s_cv, NULL, CV_DEFAULT, NULL);
return (0);
}
static void
snode_destructor(void *buf, void *cdrarg)
{
struct snode *sp = buf;
struct vnode *vp = STOV(sp);
mutex_destroy(&sp->s_lock);
cv_destroy(&sp->s_cv);
vn_free(vp);
}
int
specinit(int fstype, char *name)
{
static const fs_operation_def_t spec_vfsops_template[] = {
VFSNAME_SYNC, { .vfs_sync = spec_sync },
NULL, NULL
};
extern struct vnodeops *spec_vnodeops;
extern const fs_operation_def_t spec_vnodeops_template[];
struct vfsops *spec_vfsops;
int error;
dev_t dev;
error = vfs_setfsops(fstype, spec_vfsops_template, &spec_vfsops);
if (error != 0) {
cmn_err(CE_WARN, "specinit: bad vfs ops template");
return (error);
}
error = vn_make_ops(name, spec_vnodeops_template, &spec_vnodeops);
if (error != 0) {
(void) vfs_freevfsops_by_type(fstype);
cmn_err(CE_WARN, "specinit: bad vnode ops template");
return (error);
}
mutex_init(&stable_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&spec_syncbusy, NULL, MUTEX_DEFAULT, NULL);
snode_cache = kmem_cache_create("snode_cache", sizeof (struct snode),
0, snode_constructor, snode_destructor, NULL, NULL, NULL, 0);
VFS_INIT(&spec_vfs, spec_vfsops, (caddr_t)NULL);
if ((dev = getudev()) == -1)
dev = 0;
specdev = makedevice(dev, 0);
return (0);
}
int
device_close(struct vnode *vp, int flag, struct cred *cr)
{
struct snode *sp = VTOS(vp);
enum vtype type = vp->v_type;
struct vnode *cvp;
dev_t dev;
int error;
dev = sp->s_dev;
cvp = sp->s_commonvp;
switch (type) {
case VCHR:
if (vp->v_stream) {
if (cvp->v_stream != NULL)
error = strclose(cvp, flag, cr);
vp->v_stream = NULL;
} else
error = dev_close(dev, flag, OTYP_CHR, cr);
break;
case VBLK:
(void) spec_putpage(cvp, (offset_t)0,
(size_t)0, B_INVAL|B_FORCE, cr, NULL);
bflush(dev);
binval(dev);
error = dev_close(dev, flag, OTYP_BLK, cr);
break;
default:
panic("device_close: not a device");
}
return (error);
}
struct vnode *
makectty(vnode_t *ovp)
{
vnode_t *vp;
if (vp = makespecvp(ovp->v_rdev, VCHR)) {
struct snode *sp;
struct snode *csp;
struct vnode *cvp;
sp = VTOS(vp);
cvp = sp->s_commonvp;
csp = VTOS(cvp);
mutex_enter(&csp->s_lock);
csp->s_count++;
mutex_exit(&csp->s_lock);
}
return (vp);
}
void
spec_snode_walk(int (*callback)(struct snode *sp, void *arg), void *arg)
{
struct snode *sp;
int i;
ASSERT(callback);
mutex_enter(&stable_lock);
for (i = 0; i < STABLESIZE; i++) {
for (sp = stable[i]; sp; sp = sp->s_next) {
if (callback(sp, arg) != DDI_WALK_CONTINUE)
goto out;
}
}
out:
mutex_exit(&stable_lock);
}
int
spec_is_clone(vnode_t *vp)
{
struct snode *sp;
if (vn_matchops(vp, spec_getvnodeops())) {
sp = VTOS(vp);
return ((sp->s_flag & SCLONE) ? 1 : 0);
}
return (0);
}
int
spec_is_selfclone(vnode_t *vp)
{
struct snode *sp;
if (vn_matchops(vp, spec_getvnodeops())) {
sp = VTOS(vp);
return ((sp->s_flag & SSELFCLONE) ? 1 : 0);
}
return (0);
}
int
spec_fence_snode(dev_info_t *dip, struct vnode *vp)
{
struct snode *sp;
struct snode *csp;
int retired;
int i;
char *path;
int emitted;
ASSERT(dip);
retired = 0;
mutex_enter(&DEVI(dip)->devi_lock);
if (DEVI(dip)->devi_flags & DEVI_RETIRED)
retired = 1;
mutex_exit(&DEVI(dip)->devi_lock);
if (!retired)
return (0);
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
if (vp != NULL) {
ASSERT(vn_matchops(vp, spec_getvnodeops()));
csp = VTOCS(vp);
ASSERT(csp);
mutex_enter(&csp->s_lock);
csp->s_flag |= SFENCED;
mutex_exit(&csp->s_lock);
FENDBG((CE_NOTE, "fenced off snode(%p) for dip: %s",
(void *)csp, path));
kmem_free(path, MAXPATHLEN);
return (0);
}
emitted = 0;
mutex_enter(&stable_lock);
for (i = 0; i < STABLESIZE; i++) {
for (sp = stable[i]; sp != NULL; sp = sp->s_next) {
ASSERT(sp->s_commonvp);
csp = VTOS(sp->s_commonvp);
if (csp->s_dip == dip) {
mutex_enter(&csp->s_lock);
csp->s_flag |= SFENCED;
mutex_exit(&csp->s_lock);
if (!emitted) {
FENDBG((CE_NOTE, "fenced 1 of N"));
emitted++;
}
}
}
}
mutex_exit(&stable_lock);
FENDBG((CE_NOTE, "fenced off all snodes for dip: %s", path));
kmem_free(path, MAXPATHLEN);
return (0);
}
int
spec_unfence_snode(dev_info_t *dip)
{
struct snode *sp;
struct snode *csp;
int i;
char *path;
int emitted;
ASSERT(dip);
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
emitted = 0;
mutex_enter(&stable_lock);
for (i = 0; i < STABLESIZE; i++) {
for (sp = stable[i]; sp != NULL; sp = sp->s_next) {
ASSERT(sp->s_commonvp);
csp = VTOS(sp->s_commonvp);
ASSERT(csp);
if (csp->s_dip == dip) {
mutex_enter(&csp->s_lock);
csp->s_flag &= ~SFENCED;
mutex_exit(&csp->s_lock);
if (!emitted) {
FENDBG((CE_NOTE, "unfenced 1 of N"));
emitted++;
}
}
}
}
mutex_exit(&stable_lock);
FENDBG((CE_NOTE, "unfenced all snodes for dip: %s", path));
kmem_free(path, MAXPATHLEN);
return (0);
}
void
spec_size_invalidate(dev_t dev, vtype_t type)
{
struct snode *csp;
mutex_enter(&stable_lock);
if ((csp = sfind(dev, type, NULL)) != NULL) {
mutex_enter(&csp->s_lock);
csp->s_flag &= ~SSIZEVALID;
VN_RELE_ASYNC(STOV(csp), system_taskq);
mutex_exit(&csp->s_lock);
}
mutex_exit(&stable_lock);
}