#include <sys/cdefs.h>
#include "opt_mac.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/alq.h>
#include <sys/eventhandler.h>
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/mutex.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/reboot.h>
#include <sys/unistd.h>
#include <sys/vnode.h>
#include <security/mac/mac_framework.h>
struct alq {
char *aq_entbuf;
int aq_entmax;
int aq_entlen;
int aq_freebytes;
int aq_buflen;
int aq_writehead;
int aq_writetail;
int aq_wrapearly;
int aq_flags;
int aq_waiters;
struct ale aq_getpost;
struct mtx aq_mtx;
struct vnode *aq_vp;
struct ucred *aq_cred;
LIST_ENTRY(alq) aq_act;
LIST_ENTRY(alq) aq_link;
};
#define AQ_WANTED 0x0001
#define AQ_ACTIVE 0x0002
#define AQ_FLUSHING 0x0004
#define AQ_SHUTDOWN 0x0008
#define AQ_ORDERED 0x0010
#define AQ_LEGACY 0x0020
#define ALQ_LOCK(alq) mtx_lock_spin(&(alq)->aq_mtx)
#define ALQ_UNLOCK(alq) mtx_unlock_spin(&(alq)->aq_mtx)
#define HAS_PENDING_DATA(alq) ((alq)->aq_freebytes != (alq)->aq_buflen)
static MALLOC_DEFINE(M_ALD, "ALD", "ALD");
static struct mtx ald_mtx;
static LIST_HEAD(, alq) ald_queues;
static LIST_HEAD(, alq) ald_active;
static int ald_shutingdown = 0;
struct thread *ald_thread;
static struct proc *ald_proc;
static eventhandler_tag alq_eventhandler_tag = NULL;
#define ALD_LOCK() mtx_lock(&ald_mtx)
#define ALD_UNLOCK() mtx_unlock(&ald_mtx)
static int ald_add(struct alq *);
static int ald_rem(struct alq *);
static void ald_startup(void *);
static void ald_daemon(void);
static void ald_shutdown(void *, int);
static void ald_activate(struct alq *);
static void ald_deactivate(struct alq *);
static void alq_shutdown(struct alq *);
static void alq_destroy(struct alq *);
static int alq_doio(struct alq *);
static int
ald_add(struct alq *alq)
{
int error;
error = 0;
ALD_LOCK();
if (ald_shutingdown) {
error = EBUSY;
goto done;
}
LIST_INSERT_HEAD(&ald_queues, alq, aq_link);
done:
ALD_UNLOCK();
return (error);
}
static int
ald_rem(struct alq *alq)
{
int error;
error = 0;
ALD_LOCK();
if (ald_shutingdown) {
error = EBUSY;
goto done;
}
LIST_REMOVE(alq, aq_link);
done:
ALD_UNLOCK();
return (error);
}
static void
ald_activate(struct alq *alq)
{
LIST_INSERT_HEAD(&ald_active, alq, aq_act);
wakeup(&ald_active);
}
static void
ald_deactivate(struct alq *alq)
{
LIST_REMOVE(alq, aq_act);
alq->aq_flags &= ~AQ_ACTIVE;
}
static void
ald_startup(void *unused)
{
mtx_init(&ald_mtx, "ALDmtx", NULL, MTX_DEF|MTX_QUIET);
LIST_INIT(&ald_queues);
LIST_INIT(&ald_active);
}
static void
ald_daemon(void)
{
int needwakeup;
struct alq *alq;
ald_thread = FIRST_THREAD_IN_PROC(ald_proc);
alq_eventhandler_tag = EVENTHANDLER_REGISTER(shutdown_pre_sync,
ald_shutdown, NULL, SHUTDOWN_PRI_FIRST);
ALD_LOCK();
for (;;) {
while ((alq = LIST_FIRST(&ald_active)) == NULL &&
!ald_shutingdown)
mtx_sleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);
if (ald_shutingdown && alq == NULL) {
ALD_UNLOCK();
break;
}
ALQ_LOCK(alq);
ald_deactivate(alq);
ALD_UNLOCK();
needwakeup = alq_doio(alq);
ALQ_UNLOCK(alq);
if (needwakeup)
wakeup_one(alq);
ALD_LOCK();
}
kproc_exit(0);
}
static void
ald_shutdown(void *arg, int howto)
{
struct alq *alq;
if ((howto & RB_NOSYNC) != 0 || SCHEDULER_STOPPED())
return;
ALD_LOCK();
ald_shutingdown = 1;
while ((alq = LIST_FIRST(&ald_queues)) != NULL) {
LIST_REMOVE(alq, aq_link);
ALD_UNLOCK();
alq_shutdown(alq);
ALD_LOCK();
}
wakeup(&ald_active);
mtx_sleep(ald_proc, &ald_mtx, PWAIT, "aldslp", 0);
ALD_UNLOCK();
}
static void
alq_shutdown(struct alq *alq)
{
ALQ_LOCK(alq);
alq->aq_flags |= AQ_SHUTDOWN;
if (!(alq->aq_flags & AQ_ACTIVE) && HAS_PENDING_DATA(alq)) {
alq->aq_flags |= AQ_ACTIVE;
ALQ_UNLOCK(alq);
ALD_LOCK();
ald_activate(alq);
ALD_UNLOCK();
ALQ_LOCK(alq);
}
while (alq->aq_flags & AQ_ACTIVE) {
alq->aq_flags |= AQ_WANTED;
msleep_spin(alq, &alq->aq_mtx, "aldclose", 0);
}
ALQ_UNLOCK(alq);
vn_close(alq->aq_vp, FWRITE, alq->aq_cred,
curthread);
crfree(alq->aq_cred);
}
void
alq_destroy(struct alq *alq)
{
alq_shutdown(alq);
mtx_destroy(&alq->aq_mtx);
free(alq->aq_entbuf, M_ALD);
free(alq, M_ALD);
}
static int
alq_doio(struct alq *alq)
{
struct thread *td;
struct mount *mp;
struct vnode *vp;
struct uio auio;
struct iovec aiov[2];
int totlen;
int iov;
int wrapearly;
KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));
vp = alq->aq_vp;
td = curthread;
totlen = 0;
iov = 1;
wrapearly = alq->aq_wrapearly;
bzero(&aiov, sizeof(aiov));
bzero(&auio, sizeof(auio));
aiov[0].iov_base = alq->aq_entbuf + alq->aq_writetail;
if (alq->aq_writetail < alq->aq_writehead) {
totlen = aiov[0].iov_len = alq->aq_writehead - alq->aq_writetail;
} else if (alq->aq_writehead == 0) {
totlen = aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail -
wrapearly;
} else {
aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail -
wrapearly;
iov++;
aiov[1].iov_base = alq->aq_entbuf;
aiov[1].iov_len = alq->aq_writehead;
totlen = aiov[0].iov_len + aiov[1].iov_len;
}
alq->aq_flags |= AQ_FLUSHING;
ALQ_UNLOCK(alq);
auio.uio_iov = &aiov[0];
auio.uio_offset = 0;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_rw = UIO_WRITE;
auio.uio_iovcnt = iov;
auio.uio_resid = totlen;
auio.uio_td = td;
vn_start_write(vp, &mp, V_WAIT);
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
#ifdef MAC
if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
#endif
VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
VOP_UNLOCK(vp);
vn_finished_write(mp);
ALQ_LOCK(alq);
alq->aq_flags &= ~AQ_FLUSHING;
alq->aq_writetail = (alq->aq_writetail + totlen + wrapearly) %
alq->aq_buflen;
alq->aq_freebytes += totlen + wrapearly;
if (wrapearly)
alq->aq_wrapearly = 0;
if (!HAS_PENDING_DATA(alq))
alq->aq_writehead = alq->aq_writetail = 0;
KASSERT((alq->aq_writetail >= 0 && alq->aq_writetail < alq->aq_buflen),
("%s: aq_writetail < 0 || aq_writetail >= aq_buflen", __func__));
if (alq->aq_flags & AQ_WANTED) {
alq->aq_flags &= ~AQ_WANTED;
return (1);
}
return(0);
}
static struct kproc_desc ald_kp = {
"ALQ Daemon",
ald_daemon,
&ald_proc
};
SYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp);
SYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL);
int
alq_open_flags(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
int size, int flags)
{
struct nameidata nd;
struct alq *alq;
int oflags;
int error;
KASSERT((size > 0), ("%s: size <= 0", __func__));
*alqp = NULL;
NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, file);
oflags = FWRITE | O_NOFOLLOW | O_CREAT;
error = vn_open_cred(&nd, &oflags, cmode, 0, cred, NULL);
if (error)
return (error);
NDFREE_PNBUF(&nd);
VOP_UNLOCK(nd.ni_vp);
alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO);
alq->aq_vp = nd.ni_vp;
alq->aq_cred = crhold(cred);
mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET);
alq->aq_buflen = size;
alq->aq_entmax = 0;
alq->aq_entlen = 0;
alq->aq_freebytes = alq->aq_buflen;
alq->aq_entbuf = malloc(alq->aq_buflen, M_ALD, M_WAITOK|M_ZERO);
alq->aq_writehead = alq->aq_writetail = 0;
if (flags & ALQ_ORDERED)
alq->aq_flags |= AQ_ORDERED;
if ((error = ald_add(alq)) != 0) {
alq_destroy(alq);
return (error);
}
*alqp = alq;
return (0);
}
int
alq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
int size, int count)
{
int ret;
KASSERT((count >= 0), ("%s: count < 0", __func__));
if (count > 0) {
if ((ret = alq_open_flags(alqp, file, cred, cmode,
size*count, 0)) == 0) {
(*alqp)->aq_flags |= AQ_LEGACY;
(*alqp)->aq_entmax = count;
(*alqp)->aq_entlen = size;
}
} else
ret = alq_open_flags(alqp, file, cred, cmode, size, 0);
return (ret);
}
int
alq_writen(struct alq *alq, void *data, int len, int flags)
{
int activate, copy, ret;
void *waitchan;
KASSERT((len > 0 && len <= alq->aq_buflen),
("%s: len <= 0 || len > aq_buflen", __func__));
activate = ret = 0;
copy = len;
waitchan = NULL;
ALQ_LOCK(alq);
if (len > alq->aq_buflen ||
alq->aq_flags & AQ_SHUTDOWN ||
(((flags & ALQ_NOWAIT) || (!(alq->aq_flags & AQ_ACTIVE) &&
HAS_PENDING_DATA(alq))) && alq->aq_freebytes < len)) {
ALQ_UNLOCK(alq);
return (EWOULDBLOCK);
}
if (alq->aq_flags & AQ_ORDERED && alq->aq_waiters > 0) {
KASSERT(!(flags & ALQ_NOWAIT),
("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
alq->aq_waiters++;
msleep_spin(&alq->aq_waiters, &alq->aq_mtx, "alqwnord", 0);
alq->aq_waiters--;
}
while (alq->aq_freebytes < len && !(alq->aq_flags & AQ_SHUTDOWN)) {
KASSERT(!(flags & ALQ_NOWAIT),
("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
alq->aq_flags |= AQ_WANTED;
alq->aq_waiters++;
if (waitchan)
wakeup(waitchan);
msleep_spin(alq, &alq->aq_mtx, "alqwnres", 0);
alq->aq_waiters--;
if (alq->aq_waiters > 0 && !(alq->aq_flags & AQ_ORDERED) &&
alq->aq_freebytes < len && !(alq->aq_flags & AQ_WANTED))
waitchan = alq;
else
waitchan = NULL;
}
if (alq->aq_waiters > 0) {
if (alq->aq_flags & AQ_ORDERED)
waitchan = &alq->aq_waiters;
else
waitchan = alq;
} else
waitchan = NULL;
if (alq->aq_flags & AQ_SHUTDOWN) {
ret = EWOULDBLOCK;
goto unlock;
}
if ((alq->aq_buflen - alq->aq_writehead) < len)
copy = alq->aq_buflen - alq->aq_writehead;
bcopy(data, alq->aq_entbuf + alq->aq_writehead, copy);
alq->aq_writehead += copy;
if (alq->aq_writehead >= alq->aq_buflen) {
KASSERT((alq->aq_writehead == alq->aq_buflen),
("%s: alq->aq_writehead (%d) > alq->aq_buflen (%d)",
__func__,
alq->aq_writehead,
alq->aq_buflen));
alq->aq_writehead = 0;
}
if (copy != len) {
bcopy(((uint8_t *)data)+copy, alq->aq_entbuf, len - copy);
alq->aq_writehead = len - copy;
}
KASSERT((alq->aq_writehead >= 0 && alq->aq_writehead < alq->aq_buflen),
("%s: aq_writehead < 0 || aq_writehead >= aq_buflen", __func__));
alq->aq_freebytes -= len;
if (!(alq->aq_flags & AQ_ACTIVE) && !(flags & ALQ_NOACTIVATE)) {
alq->aq_flags |= AQ_ACTIVE;
activate = 1;
}
KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));
unlock:
ALQ_UNLOCK(alq);
if (activate) {
ALD_LOCK();
ald_activate(alq);
ALD_UNLOCK();
}
if (waitchan != NULL)
wakeup_one(waitchan);
return (ret);
}
int
alq_write(struct alq *alq, void *data, int flags)
{
KASSERT((alq->aq_flags & AQ_LEGACY),
("%s: fixed length write on variable length queue", __func__));
return (alq_writen(alq, data, alq->aq_entlen, flags));
}
struct ale *
alq_getn(struct alq *alq, int len, int flags)
{
int contigbytes;
void *waitchan;
KASSERT((len > 0 && len <= alq->aq_buflen),
("%s: len <= 0 || len > alq->aq_buflen", __func__));
waitchan = NULL;
ALQ_LOCK(alq);
if (alq->aq_writehead <= alq->aq_writetail)
contigbytes = alq->aq_freebytes;
else {
contigbytes = alq->aq_buflen - alq->aq_writehead;
if (contigbytes < len) {
if (alq->aq_writetail >= len || flags & ALQ_WAITOK) {
alq->aq_wrapearly = contigbytes;
contigbytes = alq->aq_freebytes =
alq->aq_writetail;
alq->aq_writehead = 0;
}
}
}
if (len > alq->aq_buflen ||
alq->aq_flags & AQ_SHUTDOWN ||
(((flags & ALQ_NOWAIT) || (!(alq->aq_flags & AQ_ACTIVE) &&
HAS_PENDING_DATA(alq))) && contigbytes < len)) {
ALQ_UNLOCK(alq);
return (NULL);
}
if (alq->aq_flags & AQ_ORDERED && alq->aq_waiters > 0) {
KASSERT(!(flags & ALQ_NOWAIT),
("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
alq->aq_waiters++;
msleep_spin(&alq->aq_waiters, &alq->aq_mtx, "alqgnord", 0);
alq->aq_waiters--;
}
while (contigbytes < len && !(alq->aq_flags & AQ_SHUTDOWN)) {
KASSERT(!(flags & ALQ_NOWAIT),
("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
alq->aq_flags |= AQ_WANTED;
alq->aq_waiters++;
if (waitchan)
wakeup(waitchan);
msleep_spin(alq, &alq->aq_mtx, "alqgnres", 0);
alq->aq_waiters--;
if (alq->aq_writehead <= alq->aq_writetail)
contigbytes = alq->aq_freebytes;
else
contigbytes = alq->aq_buflen - alq->aq_writehead;
if (alq->aq_waiters > 0 && !(alq->aq_flags & AQ_ORDERED) &&
contigbytes < len && !(alq->aq_flags & AQ_WANTED))
waitchan = alq;
else
waitchan = NULL;
}
if (alq->aq_waiters > 0) {
if (alq->aq_flags & AQ_ORDERED)
waitchan = &alq->aq_waiters;
else
waitchan = alq;
} else
waitchan = NULL;
if (alq->aq_flags & AQ_SHUTDOWN) {
ALQ_UNLOCK(alq);
if (waitchan != NULL)
wakeup_one(waitchan);
return (NULL);
}
alq->aq_getpost.ae_data = alq->aq_entbuf + alq->aq_writehead;
alq->aq_getpost.ae_bytesused = len;
return (&alq->aq_getpost);
}
struct ale *
alq_get(struct alq *alq, int flags)
{
KASSERT((alq->aq_flags & AQ_LEGACY),
("%s: fixed length get on variable length queue", __func__));
return (alq_getn(alq, alq->aq_entlen, flags));
}
void
alq_post_flags(struct alq *alq, struct ale *ale, int flags)
{
int activate;
void *waitchan;
activate = 0;
if (ale->ae_bytesused > 0) {
if (!(alq->aq_flags & AQ_ACTIVE) &&
!(flags & ALQ_NOACTIVATE)) {
alq->aq_flags |= AQ_ACTIVE;
activate = 1;
}
alq->aq_writehead += ale->ae_bytesused;
alq->aq_freebytes -= ale->ae_bytesused;
if (alq->aq_writehead == alq->aq_buflen)
alq->aq_writehead = 0;
KASSERT((alq->aq_writehead >= 0 &&
alq->aq_writehead < alq->aq_buflen),
("%s: aq_writehead < 0 || aq_writehead >= aq_buflen",
__func__));
KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));
}
if (alq->aq_waiters > 0) {
if (alq->aq_flags & AQ_ORDERED)
waitchan = &alq->aq_waiters;
else
waitchan = alq;
} else
waitchan = NULL;
ALQ_UNLOCK(alq);
if (activate) {
ALD_LOCK();
ald_activate(alq);
ALD_UNLOCK();
}
if (waitchan != NULL)
wakeup_one(waitchan);
}
void
alq_flush(struct alq *alq)
{
int needwakeup = 0;
ALD_LOCK();
ALQ_LOCK(alq);
if (HAS_PENDING_DATA(alq) && !(alq->aq_flags & AQ_FLUSHING)) {
if (alq->aq_flags & AQ_ACTIVE)
ald_deactivate(alq);
ALD_UNLOCK();
needwakeup = alq_doio(alq);
} else
ALD_UNLOCK();
ALQ_UNLOCK(alq);
if (needwakeup)
wakeup_one(alq);
}
void
alq_close(struct alq *alq)
{
if (ald_rem(alq) == 0)
alq_destroy(alq);
}
static int
alq_load_handler(module_t mod, int what, void *arg)
{
int ret;
ret = 0;
switch (what) {
case MOD_LOAD:
case MOD_SHUTDOWN:
break;
case MOD_QUIESCE:
ALD_LOCK();
if (LIST_FIRST(&ald_queues) == NULL) {
ald_shutingdown = 1;
ALD_UNLOCK();
EVENTHANDLER_DEREGISTER(shutdown_pre_sync,
alq_eventhandler_tag);
ald_shutdown(NULL, 0);
mtx_destroy(&ald_mtx);
} else {
ALD_UNLOCK();
ret = EBUSY;
}
break;
case MOD_UNLOAD:
if (ald_shutingdown == 0)
ret = EBUSY;
break;
default:
ret = EINVAL;
break;
}
return (ret);
}
static moduledata_t alq_mod =
{
"alq",
alq_load_handler,
NULL
};
DECLARE_MODULE(alq, alq_mod, SI_SUB_LAST, SI_ORDER_ANY);
MODULE_VERSION(alq, 1);