#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/cred.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/gfs.h>
#include <sys/vnode.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/sysmacros.h>
#include <fs/fs_subr.h>
#include <sys/contract.h>
#include <sys/contract_impl.h>
#include <sys/ctfs.h>
#include <sys/ctfs_impl.h>
#include <sys/file.h>
#include <sys/policy.h>
static int
ctfs_endpoint_open(ctfs_endpoint_t *endpt, ct_equeue_t *q, int flag)
{
if ((flag & ~FNONBLOCK) != (FREAD | FOFFMAX))
return (EINVAL);
mutex_enter(&endpt->ctfs_endpt_lock);
if ((endpt->ctfs_endpt_flags & CTFS_ENDPT_SETUP) == 0) {
endpt->ctfs_endpt_flags |= CTFS_ENDPT_SETUP;
if (flag & FNONBLOCK)
endpt->ctfs_endpt_flags |= CTFS_ENDPT_NBLOCK;
cte_add_listener(q, &endpt->ctfs_endpt_listener);
}
mutex_exit(&endpt->ctfs_endpt_lock);
return (0);
}
static void
ctfs_endpoint_inactive(ctfs_endpoint_t *endpt)
{
mutex_enter(&endpt->ctfs_endpt_lock);
if (endpt->ctfs_endpt_flags & CTFS_ENDPT_SETUP) {
endpt->ctfs_endpt_flags = 0;
cte_remove_listener(&endpt->ctfs_endpt_listener);
}
pollhead_clean(&endpt->ctfs_endpt_listener.ctl_pollhead);
mutex_exit(&endpt->ctfs_endpt_lock);
}
static int
ctfs_endpoint_ioctl(ctfs_endpoint_t *endpt, int cmd, intptr_t arg, cred_t *cr,
zone_t *zone, int rprivchk)
{
uint64_t id, zuniqid;
zuniqid = zone->zone_uniqid;
switch (cmd) {
case CT_ERESET:
cte_reset_listener(&endpt->ctfs_endpt_listener);
break;
case CT_ERECV:
return (cte_get_event(&endpt->ctfs_endpt_listener,
endpt->ctfs_endpt_flags & CTFS_ENDPT_NBLOCK,
(void *)arg, rprivchk ? cr : NULL, zuniqid, 0));
case CT_ECRECV:
return (cte_get_event(&endpt->ctfs_endpt_listener,
endpt->ctfs_endpt_flags & CTFS_ENDPT_NBLOCK,
(void *)arg, rprivchk ? cr : NULL, zuniqid, 1));
case CT_ENEXT:
if (copyin((void *)arg, &id, sizeof (uint64_t)))
return (EFAULT);
return (cte_next_event(&endpt->ctfs_endpt_listener, id));
case CT_ERELIABLE:
return (cte_set_reliable(&endpt->ctfs_endpt_listener, cr));
default:
return (EINVAL);
}
return (0);
}
static int
ctfs_endpoint_poll(ctfs_endpoint_t *endpt, short events, int anyyet,
short *reventsp, pollhead_t **php)
{
if ((events & POLLIN) && endpt->ctfs_endpt_listener.ctl_position) {
*reventsp = POLLIN;
} else {
*reventsp = 0;
if (!anyyet)
*php = &endpt->ctfs_endpt_listener.ctl_pollhead;
}
return (0);
}
vnode_t *
ctfs_create_evnode(vnode_t *pvp)
{
vnode_t *vp;
ctfs_evnode_t *evnode;
ctfs_cdirnode_t *cdirnode = pvp->v_data;
vp = gfs_file_create(sizeof (ctfs_evnode_t), pvp, ctfs_ops_event);
evnode = vp->v_data;
evnode->ctfs_ev_contract = cdirnode->ctfs_cn_contract;
return (vp);
}
static int
ctfs_ev_access(
vnode_t *vp,
int mode,
int flags,
cred_t *cr,
caller_context_t *cct)
{
ctfs_evnode_t *evnode = vp->v_data;
contract_t *ct = evnode->ctfs_ev_contract;
int error;
if (mode & (VWRITE | VEXEC))
return (EACCES);
if (error = secpolicy_contract_observer(cr, ct))
return (error);
return (0);
}
static int
ctfs_ev_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *cct)
{
ctfs_evnode_t *evnode = (*vpp)->v_data;
contract_t *ct = evnode->ctfs_ev_contract;
int error;
if (error = secpolicy_contract_observer(cr, ct))
return (error);
return (ctfs_endpoint_open(&evnode->ctfs_ev_listener,
&evnode->ctfs_ev_contract->ct_events, flag));
}
static void
ctfs_ev_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
{
ctfs_evnode_t *evnode;
vnode_t *pvp = gfs_file_parent(vp);
VN_HOLD(pvp);
if ((evnode = gfs_file_inactive(vp)) != NULL) {
ctfs_endpoint_inactive(&evnode->ctfs_ev_listener);
kmem_free(evnode, sizeof (ctfs_evnode_t));
}
VN_RELE(pvp);
}
static int
ctfs_ev_getattr(
vnode_t *vp,
vattr_t *vap,
int flags,
cred_t *cr,
caller_context_t *ct)
{
ctfs_evnode_t *evnode = vp->v_data;
vap->va_type = VREG;
vap->va_mode = 0444;
vap->va_nlink = 1;
vap->va_size = 0;
vap->va_ctime = evnode->ctfs_ev_contract->ct_ctime;
mutex_enter(&evnode->ctfs_ev_contract->ct_events.ctq_lock);
vap->va_atime = vap->va_mtime =
evnode->ctfs_ev_contract->ct_events.ctq_atime;
mutex_exit(&evnode->ctfs_ev_contract->ct_events.ctq_lock);
ctfs_common_getattr(vp, vap);
return (0);
}
static int
ctfs_ev_ioctl(
vnode_t *vp,
int cmd,
intptr_t arg,
int flag,
cred_t *cr,
int *rvalp,
caller_context_t *ct)
{
ctfs_evnode_t *evnode = vp->v_data;
return (ctfs_endpoint_ioctl(&evnode->ctfs_ev_listener, cmd, arg, cr,
VTOZONE(vp), 0));
}
static int
ctfs_ev_poll(
vnode_t *vp,
short events,
int anyyet,
short *reventsp,
pollhead_t **php,
caller_context_t *ct)
{
ctfs_evnode_t *evnode = vp->v_data;
return (ctfs_endpoint_poll(&evnode->ctfs_ev_listener, events, anyyet,
reventsp, php));
}
const fs_operation_def_t ctfs_tops_event[] = {
{ VOPNAME_OPEN, { .vop_open = ctfs_ev_open } },
{ VOPNAME_CLOSE, { .vop_close = ctfs_close } },
{ VOPNAME_IOCTL, { .vop_ioctl = ctfs_ev_ioctl } },
{ VOPNAME_GETATTR, { .vop_getattr = ctfs_ev_getattr } },
{ VOPNAME_ACCESS, { .vop_access = ctfs_ev_access } },
{ VOPNAME_READDIR, { .error = fs_notdir } },
{ VOPNAME_LOOKUP, { .error = fs_notdir } },
{ VOPNAME_INACTIVE, { .vop_inactive = ctfs_ev_inactive } },
{ VOPNAME_POLL, { .vop_poll = ctfs_ev_poll } },
{ NULL, NULL }
};
vnode_t *
ctfs_create_pbundle(vnode_t *pvp)
{
vnode_t *vp;
ctfs_bunode_t *bundle;
vp = gfs_file_create(sizeof (ctfs_bunode_t), pvp, ctfs_ops_bundle);
bundle = vp->v_data;
bundle->ctfs_bu_queue =
contract_type_pbundle(ct_types[gfs_file_index(pvp)], curproc);
return (vp);
}
vnode_t *
ctfs_create_bundle(vnode_t *pvp)
{
vnode_t *vp;
ctfs_bunode_t *bundle;
vp = gfs_file_create(sizeof (ctfs_bunode_t), pvp, ctfs_ops_bundle);
bundle = vp->v_data;
bundle->ctfs_bu_queue =
contract_type_bundle(ct_types[gfs_file_index(pvp)]);
return (vp);
}
static int
ctfs_bu_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
{
ctfs_bunode_t *bunode = (*vpp)->v_data;
return (ctfs_endpoint_open(&bunode->ctfs_bu_listener,
bunode->ctfs_bu_queue, flag));
}
static void
ctfs_bu_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
{
ctfs_bunode_t *bunode;
vnode_t *pvp = gfs_file_parent(vp);
VN_HOLD(pvp);
if ((bunode = gfs_file_inactive(vp)) != NULL) {
ctfs_endpoint_inactive(&bunode->ctfs_bu_listener);
kmem_free(bunode, sizeof (ctfs_bunode_t));
}
VN_RELE(pvp);
}
static int
ctfs_bu_getattr(
vnode_t *vp,
vattr_t *vap,
int flags,
cred_t *cr,
caller_context_t *ct)
{
ctfs_bunode_t *bunode = vp->v_data;
vap->va_type = VREG;
vap->va_mode = 0444;
vap->va_nodeid = gfs_file_index(vp);
vap->va_nlink = 1;
vap->va_size = 0;
vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
vap->va_ctime.tv_nsec = 0;
mutex_enter(&bunode->ctfs_bu_queue->ctq_lock);
vap->va_mtime = vap->va_atime = bunode->ctfs_bu_queue->ctq_atime;
mutex_exit(&bunode->ctfs_bu_queue->ctq_lock);
ctfs_common_getattr(vp, vap);
return (0);
}
static int
ctfs_bu_ioctl(
vnode_t *vp,
int cmd,
intptr_t arg,
int flag,
cred_t *cr,
int *rvalp,
caller_context_t *ct)
{
ctfs_bunode_t *bunode = vp->v_data;
return (ctfs_endpoint_ioctl(&bunode->ctfs_bu_listener, cmd, arg, cr,
VTOZONE(vp), bunode->ctfs_bu_queue->ctq_listno == CTEL_BUNDLE));
}
static int
ctfs_bu_poll(
vnode_t *vp,
short events,
int anyyet,
short *reventsp,
pollhead_t **php,
caller_context_t *ct)
{
ctfs_bunode_t *bunode = vp->v_data;
return (ctfs_endpoint_poll(&bunode->ctfs_bu_listener, events, anyyet,
reventsp, php));
}
const fs_operation_def_t ctfs_tops_bundle[] = {
{ VOPNAME_OPEN, { .vop_open = ctfs_bu_open } },
{ VOPNAME_CLOSE, { .vop_close = ctfs_close } },
{ VOPNAME_IOCTL, { .vop_ioctl = ctfs_bu_ioctl } },
{ VOPNAME_GETATTR, { .vop_getattr = ctfs_bu_getattr } },
{ VOPNAME_ACCESS, { .vop_access = ctfs_access_readonly } },
{ VOPNAME_READDIR, { .error = fs_notdir } },
{ VOPNAME_LOOKUP, { .error = fs_notdir } },
{ VOPNAME_INACTIVE, { .vop_inactive = ctfs_bu_inactive } },
{ VOPNAME_POLL, { .vop_poll = ctfs_bu_poll } },
{ NULL, NULL }
};