2 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
3 * Copyright (c) 2008-2009, Lawrence Stewart <lstewart@freebsd.org>
4 * Copyright (c) 2009-2010, The FreeBSD Foundation
7 * Portions of this software were developed at the Centre for Advanced
8 * Internet Architectures, Swinburne University of Technology, Melbourne,
9 * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice unmodified, this list of conditions, and the following
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
43 #include <sys/mount.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
47 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <sys/unistd.h>
51 #include <sys/fcntl.h>
52 #include <sys/eventhandler.h>
54 #include <security/mac/mac_framework.h>
56 /* Async. Logging Queue */
58 char *aq_entbuf; /* Buffer for stored entries */
59 int aq_entmax; /* Max entries */
60 int aq_entlen; /* Entry length */
61 int aq_freebytes; /* Bytes available in buffer */
62 int aq_buflen; /* Total length of our buffer */
63 int aq_writehead; /* Location for next write */
64 int aq_writetail; /* Flush starts at this location */
65 int aq_wrapearly; /* # bytes left blank at end of buf */
66 int aq_flags; /* Queue flags */
67 int aq_waiters; /* Num threads waiting for resources
68 * NB: Used as a wait channel so must
69 * not be first field in the alq struct
71 struct ale aq_getpost; /* ALE for use by get/post */
72 struct mtx aq_mtx; /* Queue lock */
73 struct vnode *aq_vp; /* Open vnode handle */
74 struct ucred *aq_cred; /* Credentials of the opening thread */
75 LIST_ENTRY(alq) aq_act; /* List of active queues */
76 LIST_ENTRY(alq) aq_link; /* List of all queues */
79 #define AQ_WANTED 0x0001 /* Wakeup sleeper when io is done */
80 #define AQ_ACTIVE 0x0002 /* on the active list */
81 #define AQ_FLUSHING 0x0004 /* doing IO */
82 #define AQ_SHUTDOWN 0x0008 /* Queue no longer valid */
83 #define AQ_ORDERED 0x0010 /* Queue enforces ordered writes */
84 #define AQ_LEGACY 0x0020 /* Legacy queue (fixed length writes) */
86 #define ALQ_LOCK(alq) mtx_lock_spin(&(alq)->aq_mtx)
87 #define ALQ_UNLOCK(alq) mtx_unlock_spin(&(alq)->aq_mtx)
89 #define HAS_PENDING_DATA(alq) ((alq)->aq_freebytes != (alq)->aq_buflen)
91 static MALLOC_DEFINE(M_ALD, "ALD", "ALD");
94 * The ald_mtx protects the ald_queues list and the ald_active list.
96 static struct mtx ald_mtx;
97 static LIST_HEAD(, alq) ald_queues;
98 static LIST_HEAD(, alq) ald_active;
99 static int ald_shutingdown = 0;
100 struct thread *ald_thread;
101 static struct proc *ald_proc;
102 static eventhandler_tag alq_eventhandler_tag = NULL;
104 #define ALD_LOCK() mtx_lock(&ald_mtx)
105 #define ALD_UNLOCK() mtx_unlock(&ald_mtx)
107 /* Daemon functions */
108 static int ald_add(struct alq *);
109 static int ald_rem(struct alq *);
110 static void ald_startup(void *);
111 static void ald_daemon(void);
112 static void ald_shutdown(void *, int);
113 static void ald_activate(struct alq *);
114 static void ald_deactivate(struct alq *);
116 /* Internal queue functions */
117 static void alq_shutdown(struct alq *);
118 static void alq_destroy(struct alq *);
119 static int alq_doio(struct alq *);
123 * Add a new queue to the global list. Fail if we're shutting down.
126 ald_add(struct alq *alq)
133 if (ald_shutingdown) {
137 LIST_INSERT_HEAD(&ald_queues, alq, aq_link);
144 * Remove a queue from the global list unless we're shutting down. If so,
145 * the ald will take care of cleaning up it's resources.
148 ald_rem(struct alq *alq)
155 if (ald_shutingdown) {
159 LIST_REMOVE(alq, aq_link);
166 * Put a queue on the active list. This will schedule it for writing.
169 ald_activate(struct alq *alq)
171 LIST_INSERT_HEAD(&ald_active, alq, aq_act);
176 ald_deactivate(struct alq *alq)
178 LIST_REMOVE(alq, aq_act);
179 alq->aq_flags &= ~AQ_ACTIVE;
183 ald_startup(void *unused)
185 mtx_init(&ald_mtx, "ALDmtx", NULL, MTX_DEF|MTX_QUIET);
186 LIST_INIT(&ald_queues);
187 LIST_INIT(&ald_active);
196 ald_thread = FIRST_THREAD_IN_PROC(ald_proc);
198 alq_eventhandler_tag = EVENTHANDLER_REGISTER(shutdown_pre_sync,
199 ald_shutdown, NULL, SHUTDOWN_PRI_FIRST);
204 while ((alq = LIST_FIRST(&ald_active)) == NULL &&
206 mtx_sleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);
208 /* Don't shutdown until all active ALQs are flushed. */
209 if (ald_shutingdown && alq == NULL) {
217 needwakeup = alq_doio(alq);
228 ald_shutdown(void *arg, int howto)
234 /* Ensure no new queues can be created. */
237 /* Shutdown all ALQs prior to terminating the ald_daemon. */
238 while ((alq = LIST_FIRST(&ald_queues)) != NULL) {
239 LIST_REMOVE(alq, aq_link);
245 /* At this point, all ALQs are flushed and shutdown. */
248 * Wake ald_daemon so that it exits. It won't be able to do
249 * anything until we mtx_sleep because we hold the ald_mtx.
253 /* Wait for ald_daemon to exit. */
254 mtx_sleep(ald_proc, &ald_mtx, PWAIT, "aldslp", 0);
260 alq_shutdown(struct alq *alq)
264 /* Stop any new writers. */
265 alq->aq_flags |= AQ_SHUTDOWN;
268 * If the ALQ isn't active but has unwritten data (possible if
269 * the ALQ_NOACTIVATE flag has been used), explicitly activate the
270 * ALQ here so that the pending data gets flushed by the ald_daemon.
272 if (!(alq->aq_flags & AQ_ACTIVE) && HAS_PENDING_DATA(alq)) {
273 alq->aq_flags |= AQ_ACTIVE;
282 while (alq->aq_flags & AQ_ACTIVE) {
283 alq->aq_flags |= AQ_WANTED;
284 msleep_spin(alq, &alq->aq_mtx, "aldclose", 0);
288 vn_close(alq->aq_vp, FWRITE, alq->aq_cred,
290 crfree(alq->aq_cred);
294 alq_destroy(struct alq *alq)
296 /* Drain all pending IO. */
299 mtx_destroy(&alq->aq_mtx);
300 free(alq->aq_entbuf, M_ALD);
305 * Flush all pending data to disk. This operation will block.
308 alq_doio(struct alq *alq)
314 struct iovec aiov[2];
319 KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));
325 wrapearly = alq->aq_wrapearly;
327 bzero(&aiov, sizeof(aiov));
328 bzero(&auio, sizeof(auio));
330 /* Start the write from the location of our buffer tail pointer. */
331 aiov[0].iov_base = alq->aq_entbuf + alq->aq_writetail;
333 if (alq->aq_writetail < alq->aq_writehead) {
334 /* Buffer not wrapped. */
335 totlen = aiov[0].iov_len = alq->aq_writehead - alq->aq_writetail;
336 } else if (alq->aq_writehead == 0) {
337 /* Buffer not wrapped (special case to avoid an empty iov). */
338 totlen = aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail -
342 * Buffer wrapped, requires 2 aiov entries:
343 * - first is from writetail to end of buffer
344 * - second is from start of buffer to writehead
346 aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail -
349 aiov[1].iov_base = alq->aq_entbuf;
350 aiov[1].iov_len = alq->aq_writehead;
351 totlen = aiov[0].iov_len + aiov[1].iov_len;
354 alq->aq_flags |= AQ_FLUSHING;
357 auio.uio_iov = &aiov[0];
359 auio.uio_segflg = UIO_SYSSPACE;
360 auio.uio_rw = UIO_WRITE;
361 auio.uio_iovcnt = iov;
362 auio.uio_resid = totlen;
366 * Do all of the junk required to write now.
368 vn_start_write(vp, &mp, V_WAIT);
369 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
371 * XXX: VOP_WRITE error checks are ignored.
374 if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
376 VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
378 vn_finished_write(mp);
381 alq->aq_flags &= ~AQ_FLUSHING;
383 /* Adjust writetail as required, taking into account wrapping. */
384 alq->aq_writetail = (alq->aq_writetail + totlen + wrapearly) %
386 alq->aq_freebytes += totlen + wrapearly;
389 * If we just flushed part of the buffer which wrapped, reset the
390 * wrapearly indicator.
393 alq->aq_wrapearly = 0;
396 * If we just flushed the buffer completely, reset indexes to 0 to
397 * minimise buffer wraps.
398 * This is also required to ensure alq_getn() can't wedge itself.
400 if (!HAS_PENDING_DATA(alq))
401 alq->aq_writehead = alq->aq_writetail = 0;
403 KASSERT((alq->aq_writetail >= 0 && alq->aq_writetail < alq->aq_buflen),
404 ("%s: aq_writetail < 0 || aq_writetail >= aq_buflen", __func__));
406 if (alq->aq_flags & AQ_WANTED) {
407 alq->aq_flags &= ~AQ_WANTED;
414 static struct kproc_desc ald_kp = {
420 SYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp);
421 SYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL);
424 /* User visible queue functions */
427 * Create the queue data structure, allocate the buffer, and open the file.
431 alq_open_flags(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
440 KASSERT((size > 0), ("%s: size <= 0", __func__));
445 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, file, td);
446 oflags = FWRITE | O_NOFOLLOW | O_CREAT;
448 error = vn_open_cred(&nd, &oflags, cmode, 0, cred, NULL);
452 NDFREE(&nd, NDF_ONLY_PNBUF);
453 /* We just unlock so we hold a reference */
454 VOP_UNLOCK(nd.ni_vp, 0);
456 alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO);
457 alq->aq_vp = nd.ni_vp;
458 alq->aq_cred = crhold(cred);
460 mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET);
462 alq->aq_buflen = size;
466 alq->aq_freebytes = alq->aq_buflen;
467 alq->aq_entbuf = malloc(alq->aq_buflen, M_ALD, M_WAITOK|M_ZERO);
468 alq->aq_writehead = alq->aq_writetail = 0;
469 if (flags & ALQ_ORDERED)
470 alq->aq_flags |= AQ_ORDERED;
472 if ((error = ald_add(alq)) != 0) {
483 alq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
488 KASSERT((count >= 0), ("%s: count < 0", __func__));
491 if ((ret = alq_open_flags(alqp, file, cred, cmode,
492 size*count, 0)) == 0) {
493 (*alqp)->aq_flags |= AQ_LEGACY;
494 (*alqp)->aq_entmax = count;
495 (*alqp)->aq_entlen = size;
498 ret = alq_open_flags(alqp, file, cred, cmode, size, 0);
505 * Copy a new entry into the queue. If the operation would block either
506 * wait or return an error depending on the value of waitok.
509 alq_writen(struct alq *alq, void *data, int len, int flags)
511 int activate, copy, ret;
514 KASSERT((len > 0 && len <= alq->aq_buflen),
515 ("%s: len <= 0 || len > aq_buflen", __func__));
524 * Fail to perform the write and return EWOULDBLOCK if:
525 * - The message is larger than our underlying buffer.
526 * - The ALQ is being shutdown.
527 * - There is insufficient free space in our underlying buffer
528 * to accept the message and the user can't wait for space.
529 * - There is insufficient free space in our underlying buffer
530 * to accept the message and the alq is inactive due to prior
531 * use of the ALQ_NOACTIVATE flag (which would lead to deadlock).
533 if (len > alq->aq_buflen ||
534 alq->aq_flags & AQ_SHUTDOWN ||
535 (((flags & ALQ_NOWAIT) || (!(alq->aq_flags & AQ_ACTIVE) &&
536 HAS_PENDING_DATA(alq))) && alq->aq_freebytes < len)) {
538 return (EWOULDBLOCK);
542 * If we want ordered writes and there is already at least one thread
543 * waiting for resources to become available, sleep until we're woken.
545 if (alq->aq_flags & AQ_ORDERED && alq->aq_waiters > 0) {
546 KASSERT(!(flags & ALQ_NOWAIT),
547 ("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
549 msleep_spin(&alq->aq_waiters, &alq->aq_mtx, "alqwnord", 0);
554 * (ALQ_WAITOK && aq_freebytes < len) or aq_freebytes >= len, either
555 * enter while loop and sleep until we have enough free bytes (former)
556 * or skip (latter). If AQ_ORDERED is set, only 1 thread at a time will
557 * be in this loop. Otherwise, multiple threads may be sleeping here
558 * competing for ALQ resources.
560 while (alq->aq_freebytes < len && !(alq->aq_flags & AQ_SHUTDOWN)) {
561 KASSERT(!(flags & ALQ_NOWAIT),
562 ("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
563 alq->aq_flags |= AQ_WANTED;
567 msleep_spin(alq, &alq->aq_mtx, "alqwnres", 0);
571 * If we're the first thread to wake after an AQ_WANTED wakeup
572 * but there isn't enough free space for us, we're going to loop
573 * and sleep again. If there are other threads waiting in this
574 * loop, schedule a wakeup so that they can see if the space
575 * they require is available.
577 if (alq->aq_waiters > 0 && !(alq->aq_flags & AQ_ORDERED) &&
578 alq->aq_freebytes < len && !(alq->aq_flags & AQ_WANTED))
585 * If there are waiters, we need to signal the waiting threads after we
586 * complete our work. The alq ptr is used as a wait channel for threads
587 * requiring resources to be freed up. In the AQ_ORDERED case, threads
588 * are not allowed to concurrently compete for resources in the above
589 * while loop, so we use a different wait channel in this case.
591 if (alq->aq_waiters > 0) {
592 if (alq->aq_flags & AQ_ORDERED)
593 waitchan = &alq->aq_waiters;
599 /* Bail if we're shutting down. */
600 if (alq->aq_flags & AQ_SHUTDOWN) {
606 * If we need to wrap the buffer to accommodate the write,
607 * we'll need 2 calls to bcopy.
609 if ((alq->aq_buflen - alq->aq_writehead) < len)
610 copy = alq->aq_buflen - alq->aq_writehead;
612 /* Copy message (or part thereof if wrap required) to the buffer. */
613 bcopy(data, alq->aq_entbuf + alq->aq_writehead, copy);
614 alq->aq_writehead += copy;
616 if (alq->aq_writehead >= alq->aq_buflen) {
617 KASSERT((alq->aq_writehead == alq->aq_buflen),
618 ("%s: alq->aq_writehead (%d) > alq->aq_buflen (%d)",
622 alq->aq_writehead = 0;
627 * Wrap the buffer by copying the remainder of our message
628 * to the start of the buffer and resetting aq_writehead.
630 bcopy(((uint8_t *)data)+copy, alq->aq_entbuf, len - copy);
631 alq->aq_writehead = len - copy;
634 KASSERT((alq->aq_writehead >= 0 && alq->aq_writehead < alq->aq_buflen),
635 ("%s: aq_writehead < 0 || aq_writehead >= aq_buflen", __func__));
637 alq->aq_freebytes -= len;
639 if (!(alq->aq_flags & AQ_ACTIVE) && !(flags & ALQ_NOACTIVATE)) {
640 alq->aq_flags |= AQ_ACTIVE;
644 KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));
655 /* NB: We rely on wakeup_one waking threads in a FIFO manner. */
656 if (waitchan != NULL)
657 wakeup_one(waitchan);
663 alq_write(struct alq *alq, void *data, int flags)
665 /* Should only be called in fixed length message (legacy) mode. */
666 KASSERT((alq->aq_flags & AQ_LEGACY),
667 ("%s: fixed length write on variable length queue", __func__));
668 return (alq_writen(alq, data, alq->aq_entlen, flags));
672 * Retrieve a pointer for the ALQ to write directly into, avoiding bcopy.
675 alq_getn(struct alq *alq, int len, int flags)
680 KASSERT((len > 0 && len <= alq->aq_buflen),
681 ("%s: len <= 0 || len > alq->aq_buflen", __func__));
688 * Determine the number of free contiguous bytes.
689 * We ensure elsewhere that if aq_writehead == aq_writetail because
690 * the buffer is empty, they will both be set to 0 and therefore
691 * aq_freebytes == aq_buflen and is fully contiguous.
692 * If they are equal and the buffer is not empty, aq_freebytes will
693 * be 0 indicating the buffer is full.
695 if (alq->aq_writehead <= alq->aq_writetail)
696 contigbytes = alq->aq_freebytes;
698 contigbytes = alq->aq_buflen - alq->aq_writehead;
700 if (contigbytes < len) {
702 * Insufficient space at end of buffer to handle a
703 * contiguous write. Wrap early if there's space at
704 * the beginning. This will leave a hole at the end
705 * of the buffer which we will have to skip over when
706 * flushing the buffer to disk.
708 if (alq->aq_writetail >= len || flags & ALQ_WAITOK) {
709 /* Keep track of # bytes left blank. */
710 alq->aq_wrapearly = contigbytes;
711 /* Do the wrap and adjust counters. */
712 contigbytes = alq->aq_freebytes =
714 alq->aq_writehead = 0;
720 * Return a NULL ALE if:
721 * - The message is larger than our underlying buffer.
722 * - The ALQ is being shutdown.
723 * - There is insufficient free space in our underlying buffer
724 * to accept the message and the user can't wait for space.
725 * - There is insufficient free space in our underlying buffer
726 * to accept the message and the alq is inactive due to prior
727 * use of the ALQ_NOACTIVATE flag (which would lead to deadlock).
729 if (len > alq->aq_buflen ||
730 alq->aq_flags & AQ_SHUTDOWN ||
731 (((flags & ALQ_NOWAIT) || (!(alq->aq_flags & AQ_ACTIVE) &&
732 HAS_PENDING_DATA(alq))) && contigbytes < len)) {
738 * If we want ordered writes and there is already at least one thread
739 * waiting for resources to become available, sleep until we're woken.
741 if (alq->aq_flags & AQ_ORDERED && alq->aq_waiters > 0) {
742 KASSERT(!(flags & ALQ_NOWAIT),
743 ("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
745 msleep_spin(&alq->aq_waiters, &alq->aq_mtx, "alqgnord", 0);
750 * (ALQ_WAITOK && contigbytes < len) or contigbytes >= len, either enter
751 * while loop and sleep until we have enough contiguous free bytes
752 * (former) or skip (latter). If AQ_ORDERED is set, only 1 thread at a
753 * time will be in this loop. Otherwise, multiple threads may be
754 * sleeping here competing for ALQ resources.
756 while (contigbytes < len && !(alq->aq_flags & AQ_SHUTDOWN)) {
757 KASSERT(!(flags & ALQ_NOWAIT),
758 ("%s: ALQ_NOWAIT set but incorrectly ignored!", __func__));
759 alq->aq_flags |= AQ_WANTED;
763 msleep_spin(alq, &alq->aq_mtx, "alqgnres", 0);
766 if (alq->aq_writehead <= alq->aq_writetail)
767 contigbytes = alq->aq_freebytes;
769 contigbytes = alq->aq_buflen - alq->aq_writehead;
772 * If we're the first thread to wake after an AQ_WANTED wakeup
773 * but there isn't enough free space for us, we're going to loop
774 * and sleep again. If there are other threads waiting in this
775 * loop, schedule a wakeup so that they can see if the space
776 * they require is available.
778 if (alq->aq_waiters > 0 && !(alq->aq_flags & AQ_ORDERED) &&
779 contigbytes < len && !(alq->aq_flags & AQ_WANTED))
786 * If there are waiters, we need to signal the waiting threads after we
787 * complete our work. The alq ptr is used as a wait channel for threads
788 * requiring resources to be freed up. In the AQ_ORDERED case, threads
789 * are not allowed to concurrently compete for resources in the above
790 * while loop, so we use a different wait channel in this case.
792 if (alq->aq_waiters > 0) {
793 if (alq->aq_flags & AQ_ORDERED)
794 waitchan = &alq->aq_waiters;
800 /* Bail if we're shutting down. */
801 if (alq->aq_flags & AQ_SHUTDOWN) {
803 if (waitchan != NULL)
804 wakeup_one(waitchan);
809 * If we are here, we have a contiguous number of bytes >= len
810 * available in our buffer starting at aq_writehead.
812 alq->aq_getpost.ae_data = alq->aq_entbuf + alq->aq_writehead;
813 alq->aq_getpost.ae_bytesused = len;
815 return (&alq->aq_getpost);
819 alq_get(struct alq *alq, int flags)
821 /* Should only be called in fixed length message (legacy) mode. */
822 KASSERT((alq->aq_flags & AQ_LEGACY),
823 ("%s: fixed length get on variable length queue", __func__));
824 return (alq_getn(alq, alq->aq_entlen, flags));
828 alq_post_flags(struct alq *alq, struct ale *ale, int flags)
835 if (ale->ae_bytesused > 0) {
836 if (!(alq->aq_flags & AQ_ACTIVE) &&
837 !(flags & ALQ_NOACTIVATE)) {
838 alq->aq_flags |= AQ_ACTIVE;
842 alq->aq_writehead += ale->ae_bytesused;
843 alq->aq_freebytes -= ale->ae_bytesused;
845 /* Wrap aq_writehead if we filled to the end of the buffer. */
846 if (alq->aq_writehead == alq->aq_buflen)
847 alq->aq_writehead = 0;
849 KASSERT((alq->aq_writehead >= 0 &&
850 alq->aq_writehead < alq->aq_buflen),
851 ("%s: aq_writehead < 0 || aq_writehead >= aq_buflen",
854 KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__));
858 * If there are waiters, we need to signal the waiting threads after we
859 * complete our work. The alq ptr is used as a wait channel for threads
860 * requiring resources to be freed up. In the AQ_ORDERED case, threads
861 * are not allowed to concurrently compete for resources in the
862 * alq_getn() while loop, so we use a different wait channel in this case.
864 if (alq->aq_waiters > 0) {
865 if (alq->aq_flags & AQ_ORDERED)
866 waitchan = &alq->aq_waiters;
880 /* NB: We rely on wakeup_one waking threads in a FIFO manner. */
881 if (waitchan != NULL)
882 wakeup_one(waitchan);
886 alq_flush(struct alq *alq)
894 * Pull the lever iff there is data to flush and we're
895 * not already in the middle of a flush operation.
897 if (HAS_PENDING_DATA(alq) && !(alq->aq_flags & AQ_FLUSHING)) {
898 if (alq->aq_flags & AQ_ACTIVE)
902 needwakeup = alq_doio(alq);
913 * Flush remaining data, close the file and free all resources.
916 alq_close(struct alq *alq)
918 /* Only flush and destroy alq if not already shutting down. */
919 if (ald_rem(alq) == 0)
924 alq_load_handler(module_t mod, int what, void *arg)
937 /* Only allow unload if there are no open queues. */
938 if (LIST_FIRST(&ald_queues) == NULL) {
941 EVENTHANDLER_DEREGISTER(shutdown_pre_sync,
942 alq_eventhandler_tag);
943 ald_shutdown(NULL, 0);
944 mtx_destroy(&ald_mtx);
952 /* If MOD_QUIESCE failed we must fail here too. */
953 if (ald_shutingdown == 0)
965 static moduledata_t alq_mod =
972 DECLARE_MODULE(alq, alq_mod, SI_SUB_SMP, SI_ORDER_ANY);
973 MODULE_VERSION(alq, 1);