Ejemplo n.º 1
0
static void
ald_shutdown(void *arg, int howto)
{
    struct alq *alq;

    ALD_LOCK();

    /* Ensure no new queues can be created. */
    ald_shutingdown = 1;

    /* Shutdown all ALQs prior to terminating the ald_daemon. */
    while ((alq = BSD_LIST_FIRST(&ald_queues)) != NULL) {
        BSD_LIST_REMOVE(alq, aq_link);
        ALD_UNLOCK();
        alq_shutdown(alq);
        ALD_LOCK();
    }

    /* At this point, all ALQs are flushed and shutdown. */

    /*
     * Wake ald_daemon so that it exits. It won't be able to do
     * anything until we mtx_sleep because we hold the ald_mtx.
     */
    wakeup(&ald_active);

    /* Wait for ald_daemon to exit. */
    mtx_sleep(ald_proc, &ald_mtx, PWAIT, "aldslp", 0);

    ALD_UNLOCK();
}
Ejemplo n.º 2
0
static int
ald_daemon(void *arg)
{
	int needwakeup;
	struct alq *alq;

	daemonize("ALQ Daemon");
	allow_signal(SIGKILL);

	ALD_LOCK();

	for (;;) {
		if ((alq = LIST_FIRST(&ald_active)) == NULL)
			ALD_WAIT((alq = LIST_FIRST(&ald_active)) != NULL);

		if (signal_pending(current))
			break;

		spin_lock_irq(&alq->aq_lock);
		ald_deactivate(alq);
		ALD_UNLOCK();
		needwakeup = alq_doio(alq);
		spin_unlock_irq(&alq->aq_lock);
		if (needwakeup)
			wake_up_interruptible(&alq->aq_waitq);
		ALD_LOCK();
	}

	ALD_UNLOCK();

	return 0;
}
Ejemplo n.º 3
0
static void
ald_daemon(void)
{
	int needwakeup;
	struct alq *alq;

	ald_thread = FIRST_THREAD_IN_PROC(ald_proc);

	EVENTHANDLER_REGISTER(shutdown_pre_sync, ald_shutdown, NULL,
	    SHUTDOWN_PRI_FIRST);

	ALD_LOCK();

	for (;;) {
		while ((alq = LIST_FIRST(&ald_active)) == NULL)
			msleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);

		ALQ_LOCK(alq);
		ald_deactivate(alq);
		ALD_UNLOCK();
		needwakeup = alq_doio(alq);
		ALQ_UNLOCK(alq);
		if (needwakeup)
			wakeup(alq);
		ALD_LOCK();
	}
}
Ejemplo n.º 4
0
void
alq_flush(struct alq *alq)
{
    int needwakeup = 0;

    ALD_LOCK();
    ALQ_LOCK(alq);

    /*
     * Pull the lever iff there is data to flush and we're
     * not already in the middle of a flush operation.
     */
    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);
}
Ejemplo n.º 5
0
static void
alq_shutdown(struct alq *alq)
{
    ALQ_LOCK(alq);

    /* Stop any new writers. */
    alq->aq_flags |= AQ_SHUTDOWN;

    /*
     * If the ALQ isn't active but has unwritten data (possible if
     * the ALQ_NOACTIVATE flag has been used), explicitly activate the
     * ALQ here so that the pending data gets flushed by the ald_daemon.
     */
    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);
    }

    /* Drain IO */
    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);
}
Ejemplo n.º 6
0
static void
ald_shutdown(void *arg, int howto)
{
	struct alq *alq;

	ALD_LOCK();
	ald_shutingdown = 1;

	while ((alq = LIST_FIRST(&ald_queues)) != NULL) {
		LIST_REMOVE(alq, aq_link);
		ALD_UNLOCK();
		alq_shutdown(alq);
		ALD_LOCK();
	}
	ALD_UNLOCK();
}
Ejemplo n.º 7
0
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;

        /* Wrap aq_writehead if we filled to the end of the buffer. */
        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 there are waiters, we need to signal the waiting threads after we
     * complete our work. The alq ptr is used as a wait channel for threads
     * requiring resources to be freed up. In the AQ_ORDERED case, threads
     * are not allowed to concurrently compete for resources in the
     * alq_getn() while loop, so we use a different wait channel in this case.
     */
    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();
    }

    /* NB: We rely on wakeup_one waking threads in a FIFO manner. */
    if (waitchan != NULL)
        wakeup_one(waitchan);
}
Ejemplo n.º 8
0
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 = BSD_LIST_FIRST(&ald_active)) == NULL &&
                !ald_shutingdown)
            mtx_sleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);

        /* Don't shutdown until all active ALQs are flushed. */
        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);
}
Ejemplo n.º 9
0
/*
 * Remove a queue from the global list unless we're shutting down.  If so,
 * the ald will take care of cleaning up it's resources.
 */
static int
ald_rem(struct alq *alq)
{
    int error;

    error = 0;

    ALD_LOCK();
    if (ald_shutingdown) {
        error = EBUSY;
        goto done;
    }
    BSD_LIST_REMOVE(alq, aq_link);
done:
    ALD_UNLOCK();
    return (error);
}
Ejemplo n.º 10
0
/*
 * Add a new queue to the global list.  Fail if we're shutting down.
 */
static int
ald_add(struct alq *alq)
{
    int error;

    error = 0;

    ALD_LOCK();
    if (ald_shutingdown) {
        error = EBUSY;
        goto done;
    }
    BSD_LIST_INSERT_HEAD(&ald_queues, alq, aq_link);
done:
    ALD_UNLOCK();
    return (error);
}
Ejemplo n.º 11
0
void
alq_flush(struct alq *alq)
{
	int needwakeup = 0;

	ALD_LOCK();
	spin_lock_irq(&alq->aq_lock);
	if (alq->aq_flags & AQ_ACTIVE) {
		ald_deactivate(alq);
		ALD_UNLOCK();
		needwakeup = alq_doio(alq);
	} else
		ALD_UNLOCK();
	spin_unlock_irq(&alq->aq_lock);

	if (needwakeup)
		wake_up_interruptible(&alq->aq_waitq);
}
Ejemplo n.º 12
0
void
alq_flush(struct alq *alq)
{
	int needwakeup = 0;

	ALD_LOCK();
	ALQ_LOCK(alq);
	if (alq->aq_flags & AQ_ACTIVE) {
		ald_deactivate(alq);
		ALD_UNLOCK();
		needwakeup = alq_doio(alq);
	} else
		ALD_UNLOCK();
	ALQ_UNLOCK(alq);

	if (needwakeup)
		wakeup(alq);
}
Ejemplo n.º 13
0
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();
        /* Only allow unload if there are no open queues. */
        if (BSD_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 MOD_QUIESCE failed we must fail here too. */
        if (ald_shutingdown == 0)
            ret = EBUSY;
        break;

    default:
        ret = EINVAL;
        break;
    }

    return (ret);
}
Ejemplo n.º 14
0
void
alq_post(struct alq *alq, struct ale *ale)
{
	int activate;

	ale->ae_flags |= AE_VALID;

	if (alq->aq_entvalid == NULL)
		alq->aq_entvalid = ale;

	if ((alq->aq_flags & AQ_ACTIVE) == 0) {
		alq->aq_flags |= AQ_ACTIVE;
		activate = 1;
	} else
		activate = 0;

	spin_unlock(&alq->aq_lock);
	if (activate) {
		ALD_LOCK();
		ald_activate(alq);
		ALD_UNLOCK();
	}
}
Ejemplo n.º 15
0
/*
 * Copy a new entry into the queue.  If the operation would block either
 * wait or return an error depending on the value of waitok.
 */
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);

    /*
     * Fail to perform the write and return EWOULDBLOCK if:
     * - The message is larger than our underlying buffer.
     * - The ALQ is being shutdown.
     * - There is insufficient free space in our underlying buffer
     *   to accept the message and the user can't wait for space.
     * - There is insufficient free space in our underlying buffer
     *   to accept the message and the alq is inactive due to prior
     *   use of the ALQ_NOACTIVATE flag (which would lead to deadlock).
     */
    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 we want ordered writes and there is already at least one thread
     * waiting for resources to become available, sleep until we're woken.
     */
    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--;
    }

    /*
     * (ALQ_WAITOK && aq_freebytes < len) or aq_freebytes >= len, either
     * enter while loop and sleep until we have enough free bytes (former)
     * or skip (latter). If AQ_ORDERED is set, only 1 thread at a time will
     * be in this loop. Otherwise, multiple threads may be sleeping here
     * competing for ALQ resources.
     */
    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 we're the first thread to wake after an AQ_WANTED wakeup
         * but there isn't enough free space for us, we're going to loop
         * and sleep again. If there are other threads waiting in this
         * loop, schedule a wakeup so that they can see if the space
         * they require is available.
         */
        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 there are waiters, we need to signal the waiting threads after we
     * complete our work. The alq ptr is used as a wait channel for threads
     * requiring resources to be freed up. In the AQ_ORDERED case, threads
     * are not allowed to concurrently compete for resources in the above
     * while loop, so we use a different wait channel in this case.
     */
    if (alq->aq_waiters > 0) {
        if (alq->aq_flags & AQ_ORDERED)
            waitchan = &alq->aq_waiters;
        else
            waitchan = alq;
    } else
        waitchan = NULL;

    /* Bail if we're shutting down. */
    if (alq->aq_flags & AQ_SHUTDOWN) {
        ret = EWOULDBLOCK;
        goto unlock;
    }

    /*
     * If we need to wrap the buffer to accommodate the write,
     * we'll need 2 calls to bcopy.
     */
    if ((alq->aq_buflen - alq->aq_writehead) < len)
        copy = alq->aq_buflen - alq->aq_writehead;

    /* Copy message (or part thereof if wrap required) to the buffer. */
    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) {
        /*
         * Wrap the buffer by copying the remainder of our message
         * to the start of the buffer and resetting aq_writehead.
         */
        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();
    }

    /* NB: We rely on wakeup_one waking threads in a FIFO manner. */
    if (waitchan != NULL)
        wakeup_one(waitchan);

    return (ret);
}