kern_alq.c revision 206027
1139804Simp/*-
2103785Sjeff * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
3205959Slstewart * Copyright (c) 2008-2009, Lawrence Stewart <lstewart@freebsd.org>
4205959Slstewart * Copyright (c) 2009-2010, The FreeBSD Foundation
5103785Sjeff * All rights reserved.
6103785Sjeff *
7205959Slstewart * Portions of this software were developed at the Centre for Advanced
8205959Slstewart * Internet Architectures, Swinburne University of Technology, Melbourne,
9205959Slstewart * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation.
10205959Slstewart *
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,
27103785Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28103785Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29103785Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30103785Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31103785Sjeff */
32103785Sjeff
33116182Sobrien#include <sys/cdefs.h>
34116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_alq.c 206027 2010-04-01 01:23:36Z lstewart $");
35116182Sobrien
36205959Slstewart#include "opt_mac.h"
37205959Slstewart
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>
43157233Sjhb#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
54163606Srwatson#include <security/mac/mac_framework.h>
55163606Srwatson
56103785Sjeff/* 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 */
64103830Sjeff	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");
81103785Sjeff
82103785Sjeff/*
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;
89103995Sjeffstruct thread *ald_thread;
90103995Sjeffstatic 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 *);
106206026Slstewartstatic 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}
177103785Sjeff
178103785Sjeffstatic void
179103785Sjeffald_daemon(void)
180103785Sjeff{
181103785Sjeff	int needwakeup;
182103785Sjeff	struct alq *alq;
183103785Sjeff
184103995Sjeff	ald_thread = FIRST_THREAD_IN_PROC(ald_proc);
185103995Sjeff
186103785Sjeff	EVENTHANDLER_REGISTER(shutdown_pre_sync, ald_shutdown, NULL,
187103785Sjeff	    SHUTDOWN_PRI_FIRST);
188103785Sjeff
189103785Sjeff	ALD_LOCK();
190103785Sjeff
191103785Sjeff	for (;;) {
192205959Slstewart		while ((alq = LIST_FIRST(&ald_active)) == NULL &&
193205959Slstewart		    !ald_shutingdown)
194206027Slstewart			mtx_sleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);
195103785Sjeff
196205959Slstewart		/* Don't shutdown until all active ALQs are flushed. */
197205959Slstewart		if (ald_shutingdown && alq == NULL) {
198205959Slstewart			ALD_UNLOCK();
199205959Slstewart			break;
200205959Slstewart		}
201205959Slstewart
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	}
211205959Slstewart
212205959Slstewart	kproc_exit(0);
213103785Sjeff}
214103785Sjeff
215103785Sjeffstatic void
216103785Sjeffald_shutdown(void *arg, int howto)
217103785Sjeff{
218103785Sjeff	struct alq *alq;
219103785Sjeff
220103785Sjeff	ALD_LOCK();
221205959Slstewart
222205959Slstewart	/* Ensure no new queues can be created. */
223103785Sjeff	ald_shutingdown = 1;
224103785Sjeff
225205959Slstewart	/* 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	}
232205959Slstewart
233205959Slstewart	/* At this point, all ALQs are flushed and shutdown. */
234205959Slstewart
235205959Slstewart	/*
236205959Slstewart	 * Wake ald_daemon so that it exits. It won't be able to do
237206027Slstewart	 * anything until we mtx_sleep because we hold the ald_mtx.
238205959Slstewart	 */
239205959Slstewart	wakeup(&ald_active);
240205959Slstewart
241205959Slstewart	/* Wait for ald_daemon to exit. */
242206027Slstewart	mtx_sleep(ald_proc, &ald_mtx, PWAIT, "aldslp", 0);
243205959Slstewart
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;
258180196Srdivacky		msleep_spin(alq, &alq->aq_mtx, "aldclose", 0);
259103785Sjeff	}
260103785Sjeff	ALQ_UNLOCK(alq);
261103785Sjeff
262103995Sjeff	vn_close(alq->aq_vp, FWRITE, alq->aq_cred,
263103830Sjeff	    curthread);
264103830Sjeff	crfree(alq->aq_cred);
265103785Sjeff}
266103785Sjeff
267206026Slstewartvoid
268206026Slstewartalq_destroy(struct alq *alq)
269206026Slstewart{
270206026Slstewart	/* Drain all pending IO. */
271206026Slstewart	alq_shutdown(alq);
272206026Slstewart
273206026Slstewart	mtx_destroy(&alq->aq_mtx);
274206026Slstewart	free(alq->aq_first, M_ALD);
275206026Slstewart	free(alq->aq_entbuf, M_ALD);
276206026Slstewart	free(alq, M_ALD);
277206026Slstewart}
278206026Slstewart
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;
294157233Sjhb	int vfslocked;
295103785Sjeff
296103785Sjeff	vp = alq->aq_vp;
297103785Sjeff	td = curthread;
298103785Sjeff	totlen = 0;
299103785Sjeff	iov = 0;
300103785Sjeff
301103785Sjeff	alstart = ale = alq->aq_entvalid;
302103785Sjeff	alq->aq_entvalid = NULL;
303103785Sjeff
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--;
324103785Sjeff
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	 */
336157233Sjhb	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
337103785Sjeff	vn_start_write(vp, &mp, V_WAIT);
338175202Sattilio	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
339121508Srwatson	/*
340121508Srwatson	 * XXX: VOP_WRITE error checks are ignored.
341121508Srwatson	 */
342121508Srwatson#ifdef MAC
343172930Srwatson	if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
344121508Srwatson#endif
345121508Srwatson		VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
346175294Sattilio	VOP_UNLOCK(vp, 0);
347103785Sjeff	vn_finished_write(mp);
348157233Sjhb	VFS_UNLOCK_GIANT(vfslocked);
349103785Sjeff
350103785Sjeff	ALQ_LOCK(alq);
351103785Sjeff	alq->aq_flags &= ~AQ_FLUSHING;
352103785Sjeff
353103785Sjeff	if (alq->aq_entfree == NULL)
354103785Sjeff		alq->aq_entfree = alstart;
355103785Sjeff
356103785Sjeff	if (alq->aq_flags & AQ_WANTED) {
357103785Sjeff		alq->aq_flags &= ~AQ_WANTED;
358103785Sjeff		return (1);
359103785Sjeff	}
360103785Sjeff
361103785Sjeff	return(0);
362103785Sjeff}
363103785Sjeff
364103785Sjeffstatic struct kproc_desc ald_kp = {
365103785Sjeff        "ALQ Daemon",
366103785Sjeff        ald_daemon,
367103995Sjeff        &ald_proc
368103785Sjeff};
369103785Sjeff
370177253SrwatsonSYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp);
371177253SrwatsonSYSINIT(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
380145142Srwatsonalq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
381145142Srwatson    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;
391157233Sjhb	int i, vfslocked;
392103785Sjeff
393103785Sjeff	*alqp = NULL;
394103785Sjeff	td = curthread;
395103785Sjeff
396157233Sjhb	NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, file, td);
397103995Sjeff	flags = FWRITE | O_NOFOLLOW | O_CREAT;
398103785Sjeff
399194586Skib	error = vn_open_cred(&nd, &flags, cmode, 0, cred, NULL);
400103785Sjeff	if (error)
401103785Sjeff		return (error);
402154902Spjd
403157233Sjhb	vfslocked = NDHASGIANT(&nd);
404154903Spjd	NDFREE(&nd, NDF_ONLY_PNBUF);
405103785Sjeff	/* We just unlock so we hold a reference */
406175294Sattilio	VOP_UNLOCK(nd.ni_vp, 0);
407157233Sjhb	VFS_UNLOCK_GIANT(vfslocked);
408103785Sjeff
409111119Simp	alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO);
410111119Simp	alq->aq_entbuf = malloc(count * size, M_ALD, M_WAITOK|M_ZERO);
411111119Simp	alq->aq_first = malloc(sizeof(*ale) * count, M_ALD, M_WAITOK|M_ZERO);
412103785Sjeff	alq->aq_vp = nd.ni_vp;
413116697Srwatson	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
436206026Slstewart	if ((error = ald_add(alq)) != 0) {
437206026Slstewart		alq_destroy(alq);
438103785Sjeff		return (error);
439206026Slstewart	}
440206026Slstewart
441103785Sjeff	*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;
479180196Srdivacky		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;
486115308Sjeff		else
487115308Sjeff			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);
515103785Sjeff		ALD_UNLOCK();
516103785Sjeff	}
517103785Sjeff}
518103785Sjeff
519103785Sjeffvoid
520103785Sjeffalq_flush(struct alq *alq)
521103785Sjeff{
522103785Sjeff	int needwakeup = 0;
523103785Sjeff
524103785Sjeff	ALD_LOCK();
525103785Sjeff	ALQ_LOCK(alq);
526103785Sjeff	if (alq->aq_flags & AQ_ACTIVE) {
527103785Sjeff		ald_deactivate(alq);
528103785Sjeff		ALD_UNLOCK();
529103785Sjeff		needwakeup = alq_doio(alq);
530103785Sjeff	} else
531103785Sjeff		ALD_UNLOCK();
532103785Sjeff	ALQ_UNLOCK(alq);
533103785Sjeff
534103785Sjeff	if (needwakeup)
535103785Sjeff		wakeup(alq);
536103785Sjeff}
537103785Sjeff
538103785Sjeff/*
539103785Sjeff * Flush remaining data, close the file and free all resources.
540103785Sjeff */
541103785Sjeffvoid
542103785Sjeffalq_close(struct alq *alq)
543103785Sjeff{
544206026Slstewart	/* Only flush and destroy alq if not already shutting down. */
545206026Slstewart	if (ald_rem(alq) == 0)
546206026Slstewart		alq_destroy(alq);
547103785Sjeff}
548205959Slstewart
549205959Slstewartstatic int
550205959Slstewartalq_load_handler(module_t mod, int what, void *arg)
551205959Slstewart{
552205959Slstewart	int ret;
553205959Slstewart
554205959Slstewart	ret = 0;
555205959Slstewart
556205959Slstewart	switch (what) {
557205959Slstewart	case MOD_LOAD:
558205959Slstewart	case MOD_SHUTDOWN:
559205959Slstewart		break;
560205959Slstewart
561205959Slstewart	case MOD_QUIESCE:
562205959Slstewart		ALD_LOCK();
563205959Slstewart		/* Only allow unload if there are no open queues. */
564205959Slstewart		if (LIST_FIRST(&ald_queues) == NULL) {
565205959Slstewart			ald_shutingdown = 1;
566205959Slstewart			ALD_UNLOCK();
567205959Slstewart			ald_shutdown(NULL, 0);
568205959Slstewart			mtx_destroy(&ald_mtx);
569205959Slstewart		} else {
570205959Slstewart			ALD_UNLOCK();
571205959Slstewart			ret = EBUSY;
572205959Slstewart		}
573205959Slstewart		break;
574205959Slstewart
575205959Slstewart	case MOD_UNLOAD:
576205959Slstewart		/* If MOD_QUIESCE failed we must fail here too. */
577205959Slstewart		if (ald_shutingdown == 0)
578205959Slstewart			ret = EBUSY;
579205959Slstewart		break;
580205959Slstewart
581205959Slstewart	default:
582205959Slstewart		ret = EINVAL;
583205959Slstewart		break;
584205959Slstewart	}
585205959Slstewart
586205959Slstewart	return (ret);
587205959Slstewart}
588205959Slstewart
589205959Slstewartstatic moduledata_t alq_mod =
590205959Slstewart{
591205959Slstewart	"alq",
592205959Slstewart	alq_load_handler,
593205959Slstewart	NULL
594205959Slstewart};
595205959Slstewart
596205959SlstewartDECLARE_MODULE(alq, alq_mod, SI_SUB_SMP, SI_ORDER_ANY);
597205959SlstewartMODULE_VERSION(alq, 1);
598