2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
5 * Copyright (c) 2008-2009, Lawrence Stewart <lstewart@freebsd.org>
6 * Copyright (c) 2009-2010, The FreeBSD Foundation
9 * Portions of this software were developed at the Centre for Advanced
10 * Internet Architectures, Swinburne University of Technology, Melbourne,
11 * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice unmodified, this list of conditions, and the following
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/namei.h>
49 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52 #include <sys/unistd.h>
53 #include <sys/fcntl.h>
54 #include <sys/eventhandler.h>
56 #include <security/mac/mac_framework.h>
58 /* Async. Logging Queue */
60 char *aq_entbuf; /* Buffer for stored entries */
61 int aq_entmax; /* Max entries */
62 int aq_entlen; /* Entry length */
63 int aq_freebytes; /* Bytes available in buffer */
64 int aq_buflen; /* Total length of our buffer */
65 int aq_writehead; /* Location for next write */
66 int aq_writetail; /* Flush starts at this location */
67 int aq_wrapearly; /* # bytes left blank at end of buf */
68 int aq_flags; /* Queue flags */
69 int aq_waiters; /* Num threads waiting for resources
70 * NB: Used as a wait channel so must
71 * not be first field in the alq struct
73 struct ale aq_getpost; /* ALE for use by get/post */
74 struct mtx aq_mtx; /* Queue lock */
75 struct vnode *aq_vp; /* Open vnode handle */
76 struct ucred *aq_cred; /* Credentials of the opening thread */
77 LIST_ENTRY(alq) aq_act; /* List of active queues */
78 LIST_ENTRY(alq) aq_link; /* List of all queues */
81 #define AQ_WANTED 0x0001 /* Wakeup sleeper when io is done */
82 #define AQ_ACTIVE 0x0002 /* on the active list */
83 #define AQ_FLUSHING 0x0004 /* doing IO */
84 #define AQ_SHUTDOWN 0x0008 /* Queue no longer valid */
85 #define AQ_ORDERED 0x0010 /* Queue enforces ordered writes */
86 #define AQ_LEGACY 0x0020 /* Legacy queue (fixed length writes) */
88 #define ALQ_LOCK(alq) mtx_lock_spin(&(alq)->aq_mtx)
89 #define ALQ_UNLOCK(alq) mtx_unlock_spin(&(alq)->aq_mtx)
91 #define HAS_PENDING_DATA(alq) ((alq)->aq_freebytes != (alq)->aq_buflen)
93 static MALLOC_DEFINE(M_ALD, "ALD", "ALD");
96 * The ald_mtx protects the ald_queues list and the ald_active list.
98 static struct mtx ald_mtx;
99 static LIST_HEAD(, alq) ald_queues;
100 static LIST_HEAD(, alq) ald_active;
101 static int ald_shutingdown = 0;
102 struct thread *ald_thread;
103 static struct proc *ald_proc;
104 static eventhandler_tag alq_eventhandler_tag = NULL;
106 #define ALD_LOCK() mtx_lock(&ald_mtx)
107 #define ALD_UNLOCK() mtx_unlock(&ald_mtx)
109 /* Daemon functions */
110 static int ald_add(struct alq *);
111 static int ald_rem(struct alq *);
112 static void ald_startup(void *);
113 static void ald_daemon(void);
114 static void ald_shutdown(void *, int);
115 static void ald_activate(struct alq *);
116 static void ald_deactivate(struct alq *);
118 /* Internal queue functions */
119 static void alq_shutdown(struct alq *);
120 static void alq_destroy(struct alq *);
121 static int alq_doio(struct alq *);
125 * Add a new queue to the global list. Fail if we're shutting down.
128 ald_add(struct alq *alq)
135 if (ald_shutingdown) {
139 LIST_INSERT_HEAD(&ald_queues, alq, aq_link);
146 * Remove a queue from the global list unless we're shutting down. If so,
147 * the ald will take care of cleaning up it's resources.
150 ald_rem(struct alq *alq)
157 if (ald_shutingdown) {
161 LIST_REMOVE(alq, aq_link);
168 * Put a queue on the active list. This will schedule it for writing.
171 ald_activate(struct alq *alq)
173 LIST_INSERT_HEAD(&ald_active, alq, aq_act);
178 ald_deactivate(struct alq *alq)
180 LIST_REMOVE(alq, aq_act);
181 alq->aq_flags &= ~AQ_ACTIVE;
185 ald_startup(void *unused)
187 mtx_init(&ald_mtx, "ALDmtx", NULL, MTX_DEF|MTX_QUIET);
188 LIST_INIT(&ald_queues);
189 LIST_INIT(&ald_active);
198 ald_thread = FIRST_THREAD_IN_PROC(ald_proc);
200 alq_eventhandler_tag = EVENTHANDLER_REGISTER(shutdown_pre_sync,
201 ald_shutdown, NULL, SHUTDOWN_PRI_FIRST);
206 while ((alq = LIST_FIRST(&ald_active)) == NULL &&
208 mtx_sleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);
210 /* Don't shutdown until all active ALQs are flushed. */
211 if (ald_shutingdown && alq == NULL) {
219 needwakeup = alq_doio(alq);
230 ald_shutdown(void *arg, int howto)
236 /* Ensure no new queues can be created. */
239 /* Shutdown all ALQs prior to terminating the ald_daemon. */
240 while ((alq = LIST_FIRST(&ald_queues)) != NULL) {
241 LIST_REMOVE(alq, aq_link);
247 /* At this point, all ALQs are flushed and shutdown. */
250 * Wake ald_daemon so that it exits. It won't be able to do
251 * anything until we mtx_sleep because we hold the ald_mtx.
255 /* Wait for ald_daemon to exit. */
256 mtx_sleep(ald_proc, &ald_mtx, PWAIT, "aldslp", 0);
262 alq_shutdown(struct alq *alq)
266 /* Stop any new writers. */
267 alq->aq_flags |= AQ_SHUTDOWN;
270 * If the ALQ isn't active but has unwritten data (possible if
271 * the ALQ_NOACTIVATE flag has been used), explicitly activate the
272 * ALQ here so that the pending data gets flushed by the ald_daemon.
274 if (!(alq->aq_flags & AQ_ACTIVE) && HAS_PENDING_DATA(alq)) {
275 alq->aq_flags |= AQ_ACTIVE;
284 while (alq->aq_flags & AQ_ACTIVE) {
285 alq->aq_flags |= AQ_WANTED;
286 msleep_spin(alq, &alq->aq_mtx, "aldclose", 0);
290 vn_close(alq->aq_vp, FWRITE, alq->aq_cred,
292 crfree(alq->aq_cred);
296 alq_destroy(struct alq *alq)
298 /* Drain all pending IO. */
301 mtx_destroy(&alq->aq_mtx);
302 free(alq->aq_entbuf, M_ALD);
307 * Flush all pending data to disk. This operation will block.
310 alq_doio(struct alq *alq)
316 struct iovec aiov[2];
321 KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));
327 wrapearly = alq->aq_wrapearly;
329 bzero(&aiov, sizeof(aiov));
330 bzero(&auio, sizeof(auio));
332 /* Start the write from the location of our buffer tail pointer. */
333 aiov[0].iov_base = alq->aq_entbuf + alq->aq_writetail;
335 if (alq->aq_writetail < alq->aq_writehead) {
336 /* Buffer not wrapped. */
337 totlen = aiov[0].iov_len = alq->aq_writehead - alq->aq_writetail;
338 } else if (alq->aq_writehead == 0) {
339 /* Buffer not wrapped (special case to avoid an empty iov). */
340 totlen = aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail -
344 * Buffer wrapped, requires 2 aiov entries:
345 * - first is from writetail to end of buffer
346 * - second is from start of buffer to writehead
348 aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail -
351 aiov[1].iov_base = alq->aq_entbuf;
352 aiov[1].iov_len = alq->aq_writehead;
353 totlen = aiov[0].iov_len + aiov[1].iov_len;
356 alq->aq_flags |= AQ_FLUSHING;
359 auio.uio_iov = &aiov[0];
361 auio.uio_segflg = UIO_SYSSPACE;
362 auio.uio_rw = UIO_WRITE;
363 auio.uio_iovcnt = iov;
364 auio.uio_resid = totlen;
368 * Do all of the junk required to write now.
370 vn_start_write(vp, &mp, V_WAIT);
371 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
373 * XXX: VOP_WRITE error checks are ignored.
376 if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
378 VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
380 vn_finished_write(mp);
383 alq->aq_flags &= ~AQ_FLUSHING;
385 /* Adjust writetail as required, taking into account wrapping. */
386 alq->aq_writetail = (alq->aq_writetail + totlen + wrapearly) %
388 alq->aq_freebytes += totlen + wrapearly;
391 * If we just flushed part of the buffer which wrapped, reset the
392 * wrapearly indicator.
395 alq->aq_wrapearly = 0;
398 * If we just flushed the buffer completely, reset indexes to 0 to
399 * minimise buffer wraps.
400 * This is also required to ensure alq_getn() can't wedge itself.
402 if (!HAS_PENDING_DATA(alq))
403 alq->aq_writehead = alq->aq_writetail = 0;
405 KASSERT((alq->aq_writetail >= 0 && alq->aq_writetail < alq->aq_buflen),
406 ("%s: aq_writetail < 0 || aq_writetail >= aq_buflen", __func__));
408 if (alq->aq_flags & AQ_WANTED) {
409 alq->aq_flags &= ~AQ_WANTED;
416 static struct kproc_desc ald_kp = {
422 SYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp);
423 SYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL);
426 /* User visible queue functions */
429 * Create the queue data structure, allocate the buffer, and open the file.
433 alq_open_flags(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
442 KASSERT((size > 0), ("%s: size <= 0", __func__));
447 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, file, td);
448 oflags = FWRITE | O_NOFOLLOW | O_CREAT;
450 error = vn_open_cred(&nd, &oflags, cmode, 0, cred, NULL);
454 NDFREE(&nd, NDF_ONLY_PNBUF);
455 /* We just unlock so we hold a reference */
456 VOP_UNLOCK(nd.ni_vp, 0);
458 alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO);
459 alq->aq_vp = nd.ni_vp;
460 alq->aq_cred = crhold(cred);
462 mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET);
464 alq->aq_buflen = size;
468 alq->aq_freebytes = alq->aq_buflen;
469 alq->aq_entbuf = malloc(alq->aq_buflen, M_ALD, M_WAITOK|M_ZERO);
470 alq->aq_writehead = alq->aq_writetail = 0;
471 if (flags & ALQ_ORDERED)
472 alq->aq_flags |= AQ_ORDERED;
474 if ((error = ald_add(alq)) != 0) {
485 alq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
490 KASSERT((count >= 0), ("%s: count < 0", __func__));
493 if ((ret = alq_open_flags(alqp, file, cred, cmode,
494 size*count, 0)) == 0) {
495 (*alqp)->aq_flags |= AQ_LEGACY;
496 (*alqp)->aq_entmax = count;
497 (*alqp)->aq_entlen = size;
500 ret = alq_open_flags(alqp, file, cred, cmode, size, 0);
507 * Copy a new entry into the queue. If the operation would block either
508 * wait or return an error depending on the value of waitok.
511 alq_writen(struct alq *alq, void *data, int len, int flags)
513 int activate, copy, ret;
516 KASSERT((len > 0 && len <= alq->aq_buflen),
517 ("%s: len <= 0 || len > aq_buflen", __func__));
526 * Fail to perform the write and return EWOULDBLOCK if:
527 * - The message is larger than our underlying buffer.
528 * - The ALQ is being shutdown.
529 * - There is insufficient free space in our underlying buffer
530 * to accept the message and the user can't wait for space.
531 * - There is insufficient free space in our underlying buffer
532 * to accept the message and the alq is inactive due to prior
533 * use of the ALQ_NOACTIVATE flag (which would lead to deadlock).
535 if (len > alq->aq_buflen ||
536 alq->aq_flags & AQ_SHUTDOWN ||
537 (((flags & ALQ_NOWAIT) || (!(alq->aq_flags & AQ_ACTIVE) &&
538 HAS_PENDING_DATA(alq))) && alq->aq_freebytes < len)) {
540 return (EWOULDBLOCK);
544 * If we want ordered writes and there is already at least one thread
545 * waiting for resources to become available, sleep until we're woken.
547 if (alq->aq_flags & AQ_ORDERED && alq->aq_waiters > 0) {
548 KASSERT(!(flags & ALQ_NOWAIT),
549 ("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
551 msleep_spin(&alq->aq_waiters, &alq->aq_mtx, "alqwnord", 0);
556 * (ALQ_WAITOK && aq_freebytes < len) or aq_freebytes >= len, either
557 * enter while loop and sleep until we have enough free bytes (former)
558 * or skip (latter). If AQ_ORDERED is set, only 1 thread at a time will
559 * be in this loop. Otherwise, multiple threads may be sleeping here
560 * competing for ALQ resources.
562 while (alq->aq_freebytes < len && !(alq->aq_flags & AQ_SHUTDOWN)) {
563 KASSERT(!(flags & ALQ_NOWAIT),
564 ("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
565 alq->aq_flags |= AQ_WANTED;
569 msleep_spin(alq, &alq->aq_mtx, "alqwnres", 0);
573 * If we're the first thread to wake after an AQ_WANTED wakeup
574 * but there isn't enough free space for us, we're going to loop
575 * and sleep again. If there are other threads waiting in this
576 * loop, schedule a wakeup so that they can see if the space
577 * they require is available.
579 if (alq->aq_waiters > 0 && !(alq->aq_flags & AQ_ORDERED) &&
580 alq->aq_freebytes < len && !(alq->aq_flags & AQ_WANTED))
587 * If there are waiters, we need to signal the waiting threads after we
588 * complete our work. The alq ptr is used as a wait channel for threads
589 * requiring resources to be freed up. In the AQ_ORDERED case, threads
590 * are not allowed to concurrently compete for resources in the above
591 * while loop, so we use a different wait channel in this case.
593 if (alq->aq_waiters > 0) {
594 if (alq->aq_flags & AQ_ORDERED)
595 waitchan = &alq->aq_waiters;
601 /* Bail if we're shutting down. */
602 if (alq->aq_flags & AQ_SHUTDOWN) {
608 * If we need to wrap the buffer to accommodate the write,
609 * we'll need 2 calls to bcopy.
611 if ((alq->aq_buflen - alq->aq_writehead) < len)
612 copy = alq->aq_buflen - alq->aq_writehead;
614 /* Copy message (or part thereof if wrap required) to the buffer. */
615 bcopy(data, alq->aq_entbuf + alq->aq_writehead, copy);
616 alq->aq_writehead += copy;
618 if (alq->aq_writehead >= alq->aq_buflen) {
619 KASSERT((alq->aq_writehead == alq->aq_buflen),
620 ("%s: alq->aq_writehead (%d) > alq->aq_buflen (%d)",
624 alq->aq_writehead = 0;
629 * Wrap the buffer by copying the remainder of our message
630 * to the start of the buffer and resetting aq_writehead.
632 bcopy(((uint8_t *)data)+copy, alq->aq_entbuf, len - copy);
633 alq->aq_writehead = len - copy;
636 KASSERT((alq->aq_writehead >= 0 && alq->aq_writehead < alq->aq_buflen),
637 ("%s: aq_writehead < 0 || aq_writehead >= aq_buflen", __func__));
639 alq->aq_freebytes -= len;
641 if (!(alq->aq_flags & AQ_ACTIVE) && !(flags & ALQ_NOACTIVATE)) {
642 alq->aq_flags |= AQ_ACTIVE;
646 KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));
657 /* NB: We rely on wakeup_one waking threads in a FIFO manner. */
658 if (waitchan != NULL)
659 wakeup_one(waitchan);
665 alq_write(struct alq *alq, void *data, int flags)
667 /* Should only be called in fixed length message (legacy) mode. */
668 KASSERT((alq->aq_flags & AQ_LEGACY),
669 ("%s: fixed length write on variable length queue", __func__));
670 return (alq_writen(alq, data, alq->aq_entlen, flags));
674 * Retrieve a pointer for the ALQ to write directly into, avoiding bcopy.
677 alq_getn(struct alq *alq, int len, int flags)
682 KASSERT((len > 0 && len <= alq->aq_buflen),
683 ("%s: len <= 0 || len > alq->aq_buflen", __func__));
690 * Determine the number of free contiguous bytes.
691 * We ensure elsewhere that if aq_writehead == aq_writetail because
692 * the buffer is empty, they will both be set to 0 and therefore
693 * aq_freebytes == aq_buflen and is fully contiguous.
694 * If they are equal and the buffer is not empty, aq_freebytes will
695 * be 0 indicating the buffer is full.
697 if (alq->aq_writehead <= alq->aq_writetail)
698 contigbytes = alq->aq_freebytes;
700 contigbytes = alq->aq_buflen - alq->aq_writehead;
702 if (contigbytes < len) {
704 * Insufficient space at end of buffer to handle a
705 * contiguous write. Wrap early if there's space at
706 * the beginning. This will leave a hole at the end
707 * of the buffer which we will have to skip over when
708 * flushing the buffer to disk.
710 if (alq->aq_writetail >= len || flags & ALQ_WAITOK) {
711 /* Keep track of # bytes left blank. */
712 alq->aq_wrapearly = contigbytes;
713 /* Do the wrap and adjust counters. */
714 contigbytes = alq->aq_freebytes =
716 alq->aq_writehead = 0;
722 * Return a NULL ALE if:
723 * - The message is larger than our underlying buffer.
724 * - The ALQ is being shutdown.
725 * - There is insufficient free space in our underlying buffer
726 * to accept the message and the user can't wait for space.
727 * - There is insufficient free space in our underlying buffer
728 * to accept the message and the alq is inactive due to prior
729 * use of the ALQ_NOACTIVATE flag (which would lead to deadlock).
731 if (len > alq->aq_buflen ||
732 alq->aq_flags & AQ_SHUTDOWN ||
733 (((flags & ALQ_NOWAIT) || (!(alq->aq_flags & AQ_ACTIVE) &&
734 HAS_PENDING_DATA(alq))) && contigbytes < len)) {
740 * If we want ordered writes and there is already at least one thread
741 * waiting for resources to become available, sleep until we're woken.
743 if (alq->aq_flags & AQ_ORDERED && alq->aq_waiters > 0) {
744 KASSERT(!(flags & ALQ_NOWAIT),
745 ("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
747 msleep_spin(&alq->aq_waiters, &alq->aq_mtx, "alqgnord", 0);
752 * (ALQ_WAITOK && contigbytes < len) or contigbytes >= len, either enter
753 * while loop and sleep until we have enough contiguous free bytes
754 * (former) or skip (latter). If AQ_ORDERED is set, only 1 thread at a
755 * time will be in this loop. Otherwise, multiple threads may be
756 * sleeping here competing for ALQ resources.
758 while (contigbytes < len && !(alq->aq_flags & AQ_SHUTDOWN)) {
759 KASSERT(!(flags & ALQ_NOWAIT),
760 ("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
761 alq->aq_flags |= AQ_WANTED;
765 msleep_spin(alq, &alq->aq_mtx, "alqgnres", 0);
768 if (alq->aq_writehead <= alq->aq_writetail)
769 contigbytes = alq->aq_freebytes;
771 contigbytes = alq->aq_buflen - alq->aq_writehead;
774 * If we're the first thread to wake after an AQ_WANTED wakeup
775 * but there isn't enough free space for us, we're going to loop
776 * and sleep again. If there are other threads waiting in this
777 * loop, schedule a wakeup so that they can see if the space
778 * they require is available.
780 if (alq->aq_waiters > 0 && !(alq->aq_flags & AQ_ORDERED) &&
781 contigbytes < len && !(alq->aq_flags & AQ_WANTED))
788 * If there are waiters, we need to signal the waiting threads after we
789 * complete our work. The alq ptr is used as a wait channel for threads
790 * requiring resources to be freed up. In the AQ_ORDERED case, threads
791 * are not allowed to concurrently compete for resources in the above
792 * while loop, so we use a different wait channel in this case.
794 if (alq->aq_waiters > 0) {
795 if (alq->aq_flags & AQ_ORDERED)
796 waitchan = &alq->aq_waiters;
802 /* Bail if we're shutting down. */
803 if (alq->aq_flags & AQ_SHUTDOWN) {
805 if (waitchan != NULL)
806 wakeup_one(waitchan);
811 * If we are here, we have a contiguous number of bytes >= len
812 * available in our buffer starting at aq_writehead.
814 alq->aq_getpost.ae_data = alq->aq_entbuf + alq->aq_writehead;
815 alq->aq_getpost.ae_bytesused = len;
817 return (&alq->aq_getpost);
821 alq_get(struct alq *alq, int flags)
823 /* Should only be called in fixed length message (legacy) mode. */
824 KASSERT((alq->aq_flags & AQ_LEGACY),
825 ("%s: fixed length get on variable length queue", __func__));
826 return (alq_getn(alq, alq->aq_entlen, flags));
830 alq_post_flags(struct alq *alq, struct ale *ale, int flags)
837 if (ale->ae_bytesused > 0) {
838 if (!(alq->aq_flags & AQ_ACTIVE) &&
839 !(flags & ALQ_NOACTIVATE)) {
840 alq->aq_flags |= AQ_ACTIVE;
844 alq->aq_writehead += ale->ae_bytesused;
845 alq->aq_freebytes -= ale->ae_bytesused;
847 /* Wrap aq_writehead if we filled to the end of the buffer. */
848 if (alq->aq_writehead == alq->aq_buflen)
849 alq->aq_writehead = 0;
851 KASSERT((alq->aq_writehead >= 0 &&
852 alq->aq_writehead < alq->aq_buflen),
853 ("%s: aq_writehead < 0 || aq_writehead >= aq_buflen",
856 KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));
860 * If there are waiters, we need to signal the waiting threads after we
861 * complete our work. The alq ptr is used as a wait channel for threads
862 * requiring resources to be freed up. In the AQ_ORDERED case, threads
863 * are not allowed to concurrently compete for resources in the
864 * alq_getn() while loop, so we use a different wait channel in this case.
866 if (alq->aq_waiters > 0) {
867 if (alq->aq_flags & AQ_ORDERED)
868 waitchan = &alq->aq_waiters;
882 /* NB: We rely on wakeup_one waking threads in a FIFO manner. */
883 if (waitchan != NULL)
884 wakeup_one(waitchan);
888 alq_flush(struct alq *alq)
896 * Pull the lever iff there is data to flush and we're
897 * not already in the middle of a flush operation.
899 if (HAS_PENDING_DATA(alq) && !(alq->aq_flags & AQ_FLUSHING)) {
900 if (alq->aq_flags & AQ_ACTIVE)
904 needwakeup = alq_doio(alq);
915 * Flush remaining data, close the file and free all resources.
918 alq_close(struct alq *alq)
920 /* Only flush and destroy alq if not already shutting down. */
921 if (ald_rem(alq) == 0)
926 alq_load_handler(module_t mod, int what, void *arg)
939 /* Only allow unload if there are no open queues. */
940 if (LIST_FIRST(&ald_queues) == NULL) {
943 EVENTHANDLER_DEREGISTER(shutdown_pre_sync,
944 alq_eventhandler_tag);
945 ald_shutdown(NULL, 0);
946 mtx_destroy(&ald_mtx);
954 /* If MOD_QUIESCE failed we must fail here too. */
955 if (ald_shutingdown == 0)
967 static moduledata_t alq_mod =
974 DECLARE_MODULE(alq, alq_mod, SI_SUB_LAST, SI_ORDER_ANY);
975 MODULE_VERSION(alq, 1);