kern_alq.c revision 206026
1103785Sjeff/*-
2103785Sjeff * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
3103785Sjeff * Copyright (c) 2008-2009, Lawrence Stewart <lstewart@freebsd.org>
4103785Sjeff * Copyright (c) 2009-2010, The FreeBSD Foundation
5103785Sjeff * All rights reserved.
6103785Sjeff *
7103785Sjeff * Portions of this software were developed at the Centre for Advanced
8103785Sjeff * Internet Architectures, Swinburne University of Technology, Melbourne,
9103785Sjeff * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation.
10103785Sjeff *
11103785Sjeff * Redistribution and use in source and binary forms, with or without
12103785Sjeff * modification, are permitted provided that the following conditions
13103785Sjeff * are met:
14103785Sjeff * 1. Redistributions of source code must retain the above copyright
15103785Sjeff *    notice unmodified, this list of conditions, and the following
16103785Sjeff *    disclaimer.
17103785Sjeff * 2. Redistributions in binary form must reproduce the above copyright
18103785Sjeff *    notice, this list of conditions and the following disclaimer in the
19103785Sjeff *    documentation and/or other materials provided with the distribution.
20103785Sjeff *
21103785Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22103785Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23103785Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24103785Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25103785Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26103785Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27116182Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28116182Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29116182Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30121508Srwatson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31121508Srwatson */
32103785Sjeff
33103785Sjeff#include <sys/cdefs.h>
34103785Sjeff__FBSDID("$FreeBSD: head/sys/kern/kern_alq.c 206026 2010-04-01 01:16:00Z lstewart $");
35103785Sjeff
36103785Sjeff#include "opt_mac.h"
37121508Srwatson
38103785Sjeff#include <sys/param.h>
39103785Sjeff#include <sys/systm.h>
40103785Sjeff#include <sys/kernel.h>
41103785Sjeff#include <sys/kthread.h>
42103785Sjeff#include <sys/lock.h>
43103785Sjeff#include <sys/mount.h>
44103785Sjeff#include <sys/mutex.h>
45103785Sjeff#include <sys/namei.h>
46103785Sjeff#include <sys/proc.h>
47103785Sjeff#include <sys/vnode.h>
48103785Sjeff#include <sys/alq.h>
49103785Sjeff#include <sys/malloc.h>
50103785Sjeff#include <sys/unistd.h>
51103785Sjeff#include <sys/fcntl.h>
52103785Sjeff#include <sys/eventhandler.h>
53103785Sjeff
54103785Sjeff#include <security/mac/mac_framework.h>
55103785Sjeff
56103830Sjeff/* Async. Logging Queue */
57103785Sjeffstruct alq {
58103785Sjeff	int	aq_entmax;		/* Max entries */
59103785Sjeff	int	aq_entlen;		/* Entry length */
60103785Sjeff	char	*aq_entbuf;		/* Buffer for stored entries */
61103785Sjeff	int	aq_flags;		/* Queue flags */
62103785Sjeff	struct mtx	aq_mtx;		/* Queue lock */
63103785Sjeff	struct vnode	*aq_vp;		/* Open vnode handle */
64103785Sjeff	struct ucred	*aq_cred;	/* Credentials of the opening thread */
65103785Sjeff	struct ale	*aq_first;	/* First ent */
66103785Sjeff	struct ale	*aq_entfree;	/* First free ent */
67103785Sjeff	struct ale	*aq_entvalid;	/* First ent valid for writing */
68103785Sjeff	LIST_ENTRY(alq)	aq_act;		/* List of active queues */
69103785Sjeff	LIST_ENTRY(alq)	aq_link;	/* List of all queues */
70103785Sjeff};
71103785Sjeff
72103785Sjeff#define	AQ_WANTED	0x0001		/* Wakeup sleeper when io is done */
73103785Sjeff#define	AQ_ACTIVE	0x0002		/* on the active list */
74103785Sjeff#define	AQ_FLUSHING	0x0004		/* doing IO */
75103785Sjeff#define	AQ_SHUTDOWN	0x0008		/* Queue no longer valid */
76103785Sjeff
77103785Sjeff#define	ALQ_LOCK(alq)	mtx_lock_spin(&(alq)->aq_mtx)
78103785Sjeff#define	ALQ_UNLOCK(alq)	mtx_unlock_spin(&(alq)->aq_mtx)
79103785Sjeff
80103785Sjeffstatic MALLOC_DEFINE(M_ALD, "ALD", "ALD");
81103995Sjeff
82103995Sjeff/*
83103785Sjeff * The ald_mtx protects the ald_queues list and the ald_active list.
84103785Sjeff */
85103785Sjeffstatic struct mtx ald_mtx;
86103785Sjeffstatic LIST_HEAD(, alq) ald_queues;
87103785Sjeffstatic LIST_HEAD(, alq) ald_active;
88103785Sjeffstatic int ald_shutingdown = 0;
89103785Sjeffstruct thread *ald_thread;
90103785Sjeffstatic struct proc *ald_proc;
91103785Sjeff
92103785Sjeff#define	ALD_LOCK()	mtx_lock(&ald_mtx)
93103785Sjeff#define	ALD_UNLOCK()	mtx_unlock(&ald_mtx)
94103785Sjeff
95103785Sjeff/* Daemon functions */
96103785Sjeffstatic int ald_add(struct alq *);
97103785Sjeffstatic int ald_rem(struct alq *);
98103785Sjeffstatic void ald_startup(void *);
99103785Sjeffstatic void ald_daemon(void);
100103785Sjeffstatic void ald_shutdown(void *, int);
101103785Sjeffstatic void ald_activate(struct alq *);
102103785Sjeffstatic void ald_deactivate(struct alq *);
103103785Sjeff
104103785Sjeff/* Internal queue functions */
105103785Sjeffstatic void alq_shutdown(struct alq *);
106103785Sjeffstatic void alq_destroy(struct alq *);
107103785Sjeffstatic int alq_doio(struct alq *);
108103785Sjeff
109103785Sjeff
110103785Sjeff/*
111103785Sjeff * Add a new queue to the global list.  Fail if we're shutting down.
112103785Sjeff */
113103785Sjeffstatic int
114103785Sjeffald_add(struct alq *alq)
115103785Sjeff{
116103785Sjeff	int error;
117103785Sjeff
118103785Sjeff	error = 0;
119103785Sjeff
120103785Sjeff	ALD_LOCK();
121103785Sjeff	if (ald_shutingdown) {
122103785Sjeff		error = EBUSY;
123103785Sjeff		goto done;
124103785Sjeff	}
125103785Sjeff	LIST_INSERT_HEAD(&ald_queues, alq, aq_link);
126103785Sjeffdone:
127103785Sjeff	ALD_UNLOCK();
128103785Sjeff	return (error);
129103785Sjeff}
130103785Sjeff
131103785Sjeff/*
132103785Sjeff * Remove a queue from the global list unless we're shutting down.  If so,
133103785Sjeff * the ald will take care of cleaning up it's resources.
134103785Sjeff */
135103785Sjeffstatic int
136103785Sjeffald_rem(struct alq *alq)
137103785Sjeff{
138103785Sjeff	int error;
139103785Sjeff
140103785Sjeff	error = 0;
141103785Sjeff
142103785Sjeff	ALD_LOCK();
143103785Sjeff	if (ald_shutingdown) {
144103785Sjeff		error = EBUSY;
145103785Sjeff		goto done;
146103785Sjeff	}
147103785Sjeff	LIST_REMOVE(alq, aq_link);
148103785Sjeffdone:
149103785Sjeff	ALD_UNLOCK();
150103785Sjeff	return (error);
151103785Sjeff}
152103785Sjeff
153103785Sjeff/*
154103785Sjeff * Put a queue on the active list.  This will schedule it for writing.
155103785Sjeff */
156103785Sjeffstatic void
157103785Sjeffald_activate(struct alq *alq)
158103785Sjeff{
159103785Sjeff	LIST_INSERT_HEAD(&ald_active, alq, aq_act);
160103785Sjeff	wakeup(&ald_active);
161103785Sjeff}
162103785Sjeff
163103785Sjeffstatic void
164103785Sjeffald_deactivate(struct alq *alq)
165103785Sjeff{
166103785Sjeff	LIST_REMOVE(alq, aq_act);
167103785Sjeff	alq->aq_flags &= ~AQ_ACTIVE;
168103785Sjeff}
169103785Sjeff
170103785Sjeffstatic void
171103785Sjeffald_startup(void *unused)
172103785Sjeff{
173103785Sjeff	mtx_init(&ald_mtx, "ALDmtx", NULL, MTX_DEF|MTX_QUIET);
174103785Sjeff	LIST_INIT(&ald_queues);
175103785Sjeff	LIST_INIT(&ald_active);
176103785Sjeff}
177103995Sjeff
178103995Sjeffstatic void
179103785Sjeffald_daemon(void)
180103785Sjeff{
181103785Sjeff	int needwakeup;
182103785Sjeff	struct alq *alq;
183103785Sjeff
184103785Sjeff	ald_thread = FIRST_THREAD_IN_PROC(ald_proc);
185103785Sjeff
186103785Sjeff	EVENTHANDLER_REGISTER(shutdown_pre_sync, ald_shutdown, NULL,
187103785Sjeff	    SHUTDOWN_PRI_FIRST);
188103785Sjeff
189103785Sjeff	ALD_LOCK();
190103785Sjeff
191103785Sjeff	for (;;) {
192103785Sjeff		while ((alq = LIST_FIRST(&ald_active)) == NULL &&
193103785Sjeff		    !ald_shutingdown)
194103785Sjeff			msleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);
195103785Sjeff
196103785Sjeff		/* Don't shutdown until all active ALQs are flushed. */
197103785Sjeff		if (ald_shutingdown && alq == NULL) {
198103785Sjeff			ALD_UNLOCK();
199103785Sjeff			break;
200103785Sjeff		}
201103785Sjeff
202103785Sjeff		ALQ_LOCK(alq);
203103785Sjeff		ald_deactivate(alq);
204103785Sjeff		ALD_UNLOCK();
205103785Sjeff		needwakeup = alq_doio(alq);
206103785Sjeff		ALQ_UNLOCK(alq);
207103785Sjeff		if (needwakeup)
208103785Sjeff			wakeup(alq);
209103785Sjeff		ALD_LOCK();
210103785Sjeff	}
211103785Sjeff
212103785Sjeff	kproc_exit(0);
213103785Sjeff}
214103785Sjeff
215103785Sjeffstatic void
216103785Sjeffald_shutdown(void *arg, int howto)
217103785Sjeff{
218103785Sjeff	struct alq *alq;
219103785Sjeff
220103785Sjeff	ALD_LOCK();
221103785Sjeff
222103785Sjeff	/* Ensure no new queues can be created. */
223103785Sjeff	ald_shutingdown = 1;
224103785Sjeff
225103785Sjeff	/* Shutdown all ALQs prior to terminating the ald_daemon. */
226103785Sjeff	while ((alq = LIST_FIRST(&ald_queues)) != NULL) {
227103785Sjeff		LIST_REMOVE(alq, aq_link);
228103785Sjeff		ALD_UNLOCK();
229103785Sjeff		alq_shutdown(alq);
230103785Sjeff		ALD_LOCK();
231103785Sjeff	}
232103785Sjeff
233103995Sjeff	/* At this point, all ALQs are flushed and shutdown. */
234103830Sjeff
235103830Sjeff	/*
236103785Sjeff	 * Wake ald_daemon so that it exits. It won't be able to do
237103785Sjeff	 * anything until we msleep because we hold the ald_mtx.
238103785Sjeff	 */
239103785Sjeff	wakeup(&ald_active);
240103785Sjeff
241103785Sjeff	/* Wait for ald_daemon to exit. */
242103785Sjeff	msleep(ald_proc, &ald_mtx, PWAIT, "aldslp", 0);
243103785Sjeff
244103785Sjeff	ALD_UNLOCK();
245103785Sjeff}
246103785Sjeff
247103785Sjeffstatic void
248103785Sjeffalq_shutdown(struct alq *alq)
249103785Sjeff{
250103785Sjeff	ALQ_LOCK(alq);
251103785Sjeff
252103785Sjeff	/* Stop any new writers. */
253103785Sjeff	alq->aq_flags |= AQ_SHUTDOWN;
254103785Sjeff
255103785Sjeff	/* Drain IO */
256103785Sjeff	while (alq->aq_flags & (AQ_FLUSHING|AQ_ACTIVE)) {
257103785Sjeff		alq->aq_flags |= AQ_WANTED;
258103785Sjeff		msleep_spin(alq, &alq->aq_mtx, "aldclose", 0);
259103785Sjeff	}
260103785Sjeff	ALQ_UNLOCK(alq);
261103785Sjeff
262103785Sjeff	vn_close(alq->aq_vp, FWRITE, alq->aq_cred,
263103785Sjeff	    curthread);
264103785Sjeff	crfree(alq->aq_cred);
265103785Sjeff}
266103785Sjeff
267103785Sjeffvoid
268103785Sjeffalq_destroy(struct alq *alq)
269103785Sjeff{
270103785Sjeff	/* Drain all pending IO. */
271103785Sjeff	alq_shutdown(alq);
272103785Sjeff
273103785Sjeff	mtx_destroy(&alq->aq_mtx);
274103785Sjeff	free(alq->aq_first, M_ALD);
275103785Sjeff	free(alq->aq_entbuf, M_ALD);
276103785Sjeff	free(alq, M_ALD);
277103785Sjeff}
278103785Sjeff
279103785Sjeff/*
280103785Sjeff * Flush all pending data to disk.  This operation will block.
281103785Sjeff */
282103785Sjeffstatic int
283103785Sjeffalq_doio(struct alq *alq)
284103785Sjeff{
285103785Sjeff	struct thread *td;
286103785Sjeff	struct mount *mp;
287103785Sjeff	struct vnode *vp;
288103785Sjeff	struct uio auio;
289103785Sjeff	struct iovec aiov[2];
290103785Sjeff	struct ale *ale;
291103785Sjeff	struct ale *alstart;
292103785Sjeff	int totlen;
293103785Sjeff	int iov;
294103785Sjeff	int vfslocked;
295103785Sjeff
296103830Sjeff	vp = alq->aq_vp;
297121508Srwatson	td = curthread;
298121508Srwatson	totlen = 0;
299121508Srwatson	iov = 0;
300121508Srwatson
301121508Srwatson	alstart = ale = alq->aq_entvalid;
302121508Srwatson	alq->aq_entvalid = NULL;
303121508Srwatson
304103785Sjeff	bzero(&aiov, sizeof(aiov));
305103785Sjeff	bzero(&auio, sizeof(auio));
306103785Sjeff
307103785Sjeff	do {
308103785Sjeff		if (aiov[iov].iov_base == NULL)
309103785Sjeff			aiov[iov].iov_base = ale->ae_data;
310103785Sjeff		aiov[iov].iov_len += alq->aq_entlen;
311103785Sjeff		totlen += alq->aq_entlen;
312103785Sjeff		/* Check to see if we're wrapping the buffer */
313103785Sjeff		if (ale->ae_data + alq->aq_entlen != ale->ae_next->ae_data)
314103785Sjeff			iov++;
315103785Sjeff		ale->ae_flags &= ~AE_VALID;
316103785Sjeff		ale = ale->ae_next;
317103785Sjeff	} while (ale->ae_flags & AE_VALID);
318103785Sjeff
319103785Sjeff	alq->aq_flags |= AQ_FLUSHING;
320103785Sjeff	ALQ_UNLOCK(alq);
321103785Sjeff
322103785Sjeff	if (iov == 2 || aiov[iov].iov_base == NULL)
323103785Sjeff		iov--;
324103995Sjeff
325103785Sjeff	auio.uio_iov = &aiov[0];
326103785Sjeff	auio.uio_offset = 0;
327103785Sjeff	auio.uio_segflg = UIO_SYSSPACE;
328103785Sjeff	auio.uio_rw = UIO_WRITE;
329103785Sjeff	auio.uio_iovcnt = iov + 1;
330103785Sjeff	auio.uio_resid = totlen;
331103785Sjeff	auio.uio_td = td;
332103785Sjeff
333103785Sjeff	/*
334103785Sjeff	 * Do all of the junk required to write now.
335103785Sjeff	 */
336103785Sjeff	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
337116697Srwatson	vn_start_write(vp, &mp, V_WAIT);
338116697Srwatson	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
339103785Sjeff	/*
340103785Sjeff	 * XXX: VOP_WRITE error checks are ignored.
341103785Sjeff	 */
342103785Sjeff#ifdef MAC
343103785Sjeff	if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
344103785Sjeff#endif
345103785Sjeff		VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
346103785Sjeff	VOP_UNLOCK(vp, 0);
347103785Sjeff	vn_finished_write(mp);
348103785Sjeff	VFS_UNLOCK_GIANT(vfslocked);
349103785Sjeff
350103785Sjeff	ALQ_LOCK(alq);
351103785Sjeff	alq->aq_flags &= ~AQ_FLUSHING;
352103785Sjeff
353103785Sjeff	if (alq->aq_entfree == NULL)
354103995Sjeff		alq->aq_entfree = alstart;
355103785Sjeff
356118094Sphk	if (alq->aq_flags & AQ_WANTED) {
357103785Sjeff		alq->aq_flags &= ~AQ_WANTED;
358103785Sjeff		return (1);
359103785Sjeff	}
360103785Sjeff
361103785Sjeff	return(0);
362103785Sjeff}
363103785Sjeff
364111119Simpstatic struct kproc_desc ald_kp = {
365111119Simp        "ALQ Daemon",
366111119Simp        ald_daemon,
367103785Sjeff        &ald_proc
368116697Srwatson};
369103785Sjeff
370103785SjeffSYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp);
371103785SjeffSYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL);
372103785Sjeff
373103785Sjeff
374103785Sjeff/* User visible queue functions */
375103785Sjeff
376103785Sjeff/*
377103785Sjeff * Create the queue data structure, allocate the buffer, and open the file.
378103785Sjeff */
379103785Sjeffint
380103785Sjeffalq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
381103785Sjeff    int size, int count)
382103785Sjeff{
383103785Sjeff	struct thread *td;
384103785Sjeff	struct nameidata nd;
385103785Sjeff	struct ale *ale;
386103785Sjeff	struct ale *alp;
387103785Sjeff	struct alq *alq;
388103785Sjeff	char *bufp;
389103785Sjeff	int flags;
390103785Sjeff	int error;
391103785Sjeff	int i, vfslocked;
392103785Sjeff
393103785Sjeff	*alqp = NULL;
394103785Sjeff	td = curthread;
395103785Sjeff
396103785Sjeff	NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, file, td);
397103785Sjeff	flags = FWRITE | O_NOFOLLOW | O_CREAT;
398103785Sjeff
399103785Sjeff	error = vn_open_cred(&nd, &flags, cmode, 0, cred, NULL);
400103785Sjeff	if (error)
401103785Sjeff		return (error);
402103785Sjeff
403103785Sjeff	vfslocked = NDHASGIANT(&nd);
404103785Sjeff	NDFREE(&nd, NDF_ONLY_PNBUF);
405103785Sjeff	/* We just unlock so we hold a reference */
406103785Sjeff	VOP_UNLOCK(nd.ni_vp, 0);
407103785Sjeff	VFS_UNLOCK_GIANT(vfslocked);
408103785Sjeff
409103785Sjeff	alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO);
410103785Sjeff	alq->aq_entbuf = malloc(count * size, M_ALD, M_WAITOK|M_ZERO);
411103785Sjeff	alq->aq_first = malloc(sizeof(*ale) * count, M_ALD, M_WAITOK|M_ZERO);
412103785Sjeff	alq->aq_vp = nd.ni_vp;
413103785Sjeff	alq->aq_cred = crhold(cred);
414103785Sjeff	alq->aq_entmax = count;
415103785Sjeff	alq->aq_entlen = size;
416103785Sjeff	alq->aq_entfree = alq->aq_first;
417103785Sjeff
418103785Sjeff	mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET);
419103785Sjeff
420103785Sjeff	bufp = alq->aq_entbuf;
421103785Sjeff	ale = alq->aq_first;
422103785Sjeff	alp = NULL;
423103785Sjeff
424103785Sjeff	/* Match up entries with buffers */
425103785Sjeff	for (i = 0; i < count; i++) {
426103785Sjeff		if (alp)
427103785Sjeff			alp->ae_next = ale;
428103785Sjeff		ale->ae_data = bufp;
429103785Sjeff		alp = ale;
430103785Sjeff		ale++;
431103785Sjeff		bufp += size;
432103785Sjeff	}
433103785Sjeff
434103785Sjeff	alp->ae_next = alq->aq_first;
435103785Sjeff
436103785Sjeff	if ((error = ald_add(alq)) != 0) {
437103785Sjeff		alq_destroy(alq);
438103785Sjeff		return (error);
439103785Sjeff	}
440115308Sjeff
441115308Sjeff	*alqp = alq;
442103785Sjeff
443103785Sjeff	return (0);
444103785Sjeff}
445103785Sjeff
446103785Sjeff/*
447103785Sjeff * Copy a new entry into the queue.  If the operation would block either
448103785Sjeff * wait or return an error depending on the value of waitok.
449103785Sjeff */
450103785Sjeffint
451103785Sjeffalq_write(struct alq *alq, void *data, int waitok)
452103785Sjeff{
453103785Sjeff	struct ale *ale;
454103785Sjeff
455103785Sjeff	if ((ale = alq_get(alq, waitok)) == NULL)
456103785Sjeff		return (EWOULDBLOCK);
457103785Sjeff
458103785Sjeff	bcopy(data, ale->ae_data, alq->aq_entlen);
459103785Sjeff	alq_post(alq, ale);
460103785Sjeff
461103785Sjeff	return (0);
462103785Sjeff}
463103785Sjeff
464103785Sjeffstruct ale *
465103785Sjeffalq_get(struct alq *alq, int waitok)
466103785Sjeff{
467103785Sjeff	struct ale *ale;
468103785Sjeff	struct ale *aln;
469103785Sjeff
470103785Sjeff	ale = NULL;
471103785Sjeff
472103785Sjeff	ALQ_LOCK(alq);
473103785Sjeff
474103785Sjeff	/* Loop until we get an entry or we're shutting down */
475103785Sjeff	while ((alq->aq_flags & AQ_SHUTDOWN) == 0 &&
476103785Sjeff	    (ale = alq->aq_entfree) == NULL &&
477103785Sjeff	    (waitok & ALQ_WAITOK)) {
478103785Sjeff		alq->aq_flags |= AQ_WANTED;
479103785Sjeff		msleep_spin(alq, &alq->aq_mtx, "alqget", 0);
480103785Sjeff	}
481103785Sjeff
482103785Sjeff	if (ale != NULL) {
483103785Sjeff		aln = ale->ae_next;
484103785Sjeff		if ((aln->ae_flags & AE_VALID) == 0)
485103785Sjeff			alq->aq_entfree = aln;
486103785Sjeff		else
487103785Sjeff			alq->aq_entfree = NULL;
488103785Sjeff	} else
489103785Sjeff		ALQ_UNLOCK(alq);
490103785Sjeff
491103785Sjeff
492103785Sjeff	return (ale);
493103785Sjeff}
494103785Sjeff
495103785Sjeffvoid
496103785Sjeffalq_post(struct alq *alq, struct ale *ale)
497103785Sjeff{
498103785Sjeff	int activate;
499103785Sjeff
500103785Sjeff	ale->ae_flags |= AE_VALID;
501103785Sjeff
502103785Sjeff	if (alq->aq_entvalid == NULL)
503103785Sjeff		alq->aq_entvalid = ale;
504103785Sjeff
505103785Sjeff	if ((alq->aq_flags & AQ_ACTIVE) == 0) {
506103785Sjeff		alq->aq_flags |= AQ_ACTIVE;
507103785Sjeff		activate = 1;
508103785Sjeff	} else
509103785Sjeff		activate = 0;
510103785Sjeff
511103785Sjeff	ALQ_UNLOCK(alq);
512103785Sjeff	if (activate) {
513103785Sjeff		ALD_LOCK();
514103785Sjeff		ald_activate(alq);
515		ALD_UNLOCK();
516	}
517}
518
519void
520alq_flush(struct alq *alq)
521{
522	int needwakeup = 0;
523
524	ALD_LOCK();
525	ALQ_LOCK(alq);
526	if (alq->aq_flags & AQ_ACTIVE) {
527		ald_deactivate(alq);
528		ALD_UNLOCK();
529		needwakeup = alq_doio(alq);
530	} else
531		ALD_UNLOCK();
532	ALQ_UNLOCK(alq);
533
534	if (needwakeup)
535		wakeup(alq);
536}
537
538/*
539 * Flush remaining data, close the file and free all resources.
540 */
541void
542alq_close(struct alq *alq)
543{
544	/* Only flush and destroy alq if not already shutting down. */
545	if (ald_rem(alq) == 0)
546		alq_destroy(alq);
547}
548
549static int
550alq_load_handler(module_t mod, int what, void *arg)
551{
552	int ret;
553
554	ret = 0;
555
556	switch (what) {
557	case MOD_LOAD:
558	case MOD_SHUTDOWN:
559		break;
560
561	case MOD_QUIESCE:
562		ALD_LOCK();
563		/* Only allow unload if there are no open queues. */
564		if (LIST_FIRST(&ald_queues) == NULL) {
565			ald_shutingdown = 1;
566			ALD_UNLOCK();
567			ald_shutdown(NULL, 0);
568			mtx_destroy(&ald_mtx);
569		} else {
570			ALD_UNLOCK();
571			ret = EBUSY;
572		}
573		break;
574
575	case MOD_UNLOAD:
576		/* If MOD_QUIESCE failed we must fail here too. */
577		if (ald_shutingdown == 0)
578			ret = EBUSY;
579		break;
580
581	default:
582		ret = EINVAL;
583		break;
584	}
585
586	return (ret);
587}
588
589static moduledata_t alq_mod =
590{
591	"alq",
592	alq_load_handler,
593	NULL
594};
595
596DECLARE_MODULE(alq, alq_mod, SI_SUB_SMP, SI_ORDER_ANY);
597MODULE_VERSION(alq, 1);
598