kern_alq.c revision 172930
1139804Simp/*-
2103785Sjeff * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
3103785Sjeff * All rights reserved.
4103785Sjeff *
5103785Sjeff * Redistribution and use in source and binary forms, with or without
6103785Sjeff * modification, are permitted provided that the following conditions
7103785Sjeff * are met:
8103785Sjeff * 1. Redistributions of source code must retain the above copyright
9103785Sjeff *    notice unmodified, this list of conditions, and the following
10103785Sjeff *    disclaimer.
11103785Sjeff * 2. Redistributions in binary form must reproduce the above copyright
12103785Sjeff *    notice, this list of conditions and the following disclaimer in the
13103785Sjeff *    documentation and/or other materials provided with the distribution.
14103785Sjeff *
15103785Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16103785Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17103785Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18103785Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19103785Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20103785Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21103785Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22103785Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23103785Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24103785Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25103785Sjeff */
26103785Sjeff
27116182Sobrien#include <sys/cdefs.h>
28116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_alq.c 172930 2007-10-24 19:04:04Z rwatson $");
29116182Sobrien
30121508Srwatson#include "opt_mac.h"
31121508Srwatson
32103785Sjeff#include <sys/param.h>
33103785Sjeff#include <sys/systm.h>
34103785Sjeff#include <sys/kernel.h>
35103785Sjeff#include <sys/kthread.h>
36103785Sjeff#include <sys/lock.h>
37157233Sjhb#include <sys/mount.h>
38103785Sjeff#include <sys/mutex.h>
39103785Sjeff#include <sys/namei.h>
40103785Sjeff#include <sys/proc.h>
41103785Sjeff#include <sys/vnode.h>
42103785Sjeff#include <sys/alq.h>
43103785Sjeff#include <sys/malloc.h>
44103785Sjeff#include <sys/unistd.h>
45103785Sjeff#include <sys/fcntl.h>
46103785Sjeff#include <sys/eventhandler.h>
47103785Sjeff
48163606Srwatson#include <security/mac/mac_framework.h>
49163606Srwatson
50103785Sjeff/* Async. Logging Queue */
51103785Sjeffstruct alq {
52103785Sjeff	int	aq_entmax;		/* Max entries */
53103785Sjeff	int	aq_entlen;		/* Entry length */
54103785Sjeff	char	*aq_entbuf;		/* Buffer for stored entries */
55103785Sjeff	int	aq_flags;		/* Queue flags */
56103785Sjeff	struct mtx	aq_mtx;		/* Queue lock */
57103785Sjeff	struct vnode	*aq_vp;		/* Open vnode handle */
58103830Sjeff	struct ucred	*aq_cred;	/* Credentials of the opening thread */
59103785Sjeff	struct ale	*aq_first;	/* First ent */
60103785Sjeff	struct ale	*aq_entfree;	/* First free ent */
61103785Sjeff	struct ale	*aq_entvalid;	/* First ent valid for writing */
62103785Sjeff	LIST_ENTRY(alq)	aq_act;		/* List of active queues */
63103785Sjeff	LIST_ENTRY(alq)	aq_link;	/* List of all queues */
64103785Sjeff};
65103785Sjeff
66103785Sjeff#define	AQ_WANTED	0x0001		/* Wakeup sleeper when io is done */
67103785Sjeff#define	AQ_ACTIVE	0x0002		/* on the active list */
68103785Sjeff#define	AQ_FLUSHING	0x0004		/* doing IO */
69103785Sjeff#define	AQ_SHUTDOWN	0x0008		/* Queue no longer valid */
70103785Sjeff
71103785Sjeff#define	ALQ_LOCK(alq)	mtx_lock_spin(&(alq)->aq_mtx)
72103785Sjeff#define	ALQ_UNLOCK(alq)	mtx_unlock_spin(&(alq)->aq_mtx)
73103785Sjeff
74103785Sjeffstatic MALLOC_DEFINE(M_ALD, "ALD", "ALD");
75103785Sjeff
76103785Sjeff/*
77103785Sjeff * The ald_mtx protects the ald_queues list and the ald_active list.
78103785Sjeff */
79103785Sjeffstatic struct mtx ald_mtx;
80103785Sjeffstatic LIST_HEAD(, alq) ald_queues;
81103785Sjeffstatic LIST_HEAD(, alq) ald_active;
82103785Sjeffstatic int ald_shutingdown = 0;
83103995Sjeffstruct thread *ald_thread;
84103995Sjeffstatic struct proc *ald_proc;
85103785Sjeff
86103785Sjeff#define	ALD_LOCK()	mtx_lock(&ald_mtx)
87103785Sjeff#define	ALD_UNLOCK()	mtx_unlock(&ald_mtx)
88103785Sjeff
89103785Sjeff/* Daemon functions */
90103785Sjeffstatic int ald_add(struct alq *);
91103785Sjeffstatic int ald_rem(struct alq *);
92103785Sjeffstatic void ald_startup(void *);
93103785Sjeffstatic void ald_daemon(void);
94103785Sjeffstatic void ald_shutdown(void *, int);
95103785Sjeffstatic void ald_activate(struct alq *);
96103785Sjeffstatic void ald_deactivate(struct alq *);
97103785Sjeff
98103785Sjeff/* Internal queue functions */
99103785Sjeffstatic void alq_shutdown(struct alq *);
100103785Sjeffstatic int alq_doio(struct alq *);
101103785Sjeff
102103785Sjeff
103103785Sjeff/*
104103785Sjeff * Add a new queue to the global list.  Fail if we're shutting down.
105103785Sjeff */
106103785Sjeffstatic int
107103785Sjeffald_add(struct alq *alq)
108103785Sjeff{
109103785Sjeff	int error;
110103785Sjeff
111103785Sjeff	error = 0;
112103785Sjeff
113103785Sjeff	ALD_LOCK();
114103785Sjeff	if (ald_shutingdown) {
115103785Sjeff		error = EBUSY;
116103785Sjeff		goto done;
117103785Sjeff	}
118103785Sjeff	LIST_INSERT_HEAD(&ald_queues, alq, aq_link);
119103785Sjeffdone:
120103785Sjeff	ALD_UNLOCK();
121103785Sjeff	return (error);
122103785Sjeff}
123103785Sjeff
124103785Sjeff/*
125103785Sjeff * Remove a queue from the global list unless we're shutting down.  If so,
126103785Sjeff * the ald will take care of cleaning up it's resources.
127103785Sjeff */
128103785Sjeffstatic int
129103785Sjeffald_rem(struct alq *alq)
130103785Sjeff{
131103785Sjeff	int error;
132103785Sjeff
133103785Sjeff	error = 0;
134103785Sjeff
135103785Sjeff	ALD_LOCK();
136103785Sjeff	if (ald_shutingdown) {
137103785Sjeff		error = EBUSY;
138103785Sjeff		goto done;
139103785Sjeff	}
140103785Sjeff	LIST_REMOVE(alq, aq_link);
141103785Sjeffdone:
142103785Sjeff	ALD_UNLOCK();
143103785Sjeff	return (error);
144103785Sjeff}
145103785Sjeff
146103785Sjeff/*
147103785Sjeff * Put a queue on the active list.  This will schedule it for writing.
148103785Sjeff */
149103785Sjeffstatic void
150103785Sjeffald_activate(struct alq *alq)
151103785Sjeff{
152103785Sjeff	LIST_INSERT_HEAD(&ald_active, alq, aq_act);
153103785Sjeff	wakeup(&ald_active);
154103785Sjeff}
155103785Sjeff
156103785Sjeffstatic void
157103785Sjeffald_deactivate(struct alq *alq)
158103785Sjeff{
159103785Sjeff	LIST_REMOVE(alq, aq_act);
160103785Sjeff	alq->aq_flags &= ~AQ_ACTIVE;
161103785Sjeff}
162103785Sjeff
163103785Sjeffstatic void
164103785Sjeffald_startup(void *unused)
165103785Sjeff{
166103785Sjeff	mtx_init(&ald_mtx, "ALDmtx", NULL, MTX_DEF|MTX_QUIET);
167103785Sjeff	LIST_INIT(&ald_queues);
168103785Sjeff	LIST_INIT(&ald_active);
169103785Sjeff}
170103785Sjeff
171103785Sjeffstatic void
172103785Sjeffald_daemon(void)
173103785Sjeff{
174103785Sjeff	int needwakeup;
175103785Sjeff	struct alq *alq;
176103785Sjeff
177103995Sjeff	ald_thread = FIRST_THREAD_IN_PROC(ald_proc);
178103995Sjeff
179103785Sjeff	EVENTHANDLER_REGISTER(shutdown_pre_sync, ald_shutdown, NULL,
180103785Sjeff	    SHUTDOWN_PRI_FIRST);
181103785Sjeff
182103785Sjeff	ALD_LOCK();
183103785Sjeff
184103785Sjeff	for (;;) {
185103785Sjeff		while ((alq = LIST_FIRST(&ald_active)) == NULL)
186103785Sjeff			msleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);
187103785Sjeff
188103785Sjeff		ALQ_LOCK(alq);
189103785Sjeff		ald_deactivate(alq);
190103785Sjeff		ALD_UNLOCK();
191103785Sjeff		needwakeup = alq_doio(alq);
192103785Sjeff		ALQ_UNLOCK(alq);
193103785Sjeff		if (needwakeup)
194103785Sjeff			wakeup(alq);
195103785Sjeff		ALD_LOCK();
196103785Sjeff	}
197103785Sjeff}
198103785Sjeff
199103785Sjeffstatic void
200103785Sjeffald_shutdown(void *arg, int howto)
201103785Sjeff{
202103785Sjeff	struct alq *alq;
203103785Sjeff
204103785Sjeff	ALD_LOCK();
205103785Sjeff	ald_shutingdown = 1;
206103785Sjeff
207103785Sjeff	while ((alq = LIST_FIRST(&ald_queues)) != NULL) {
208103785Sjeff		LIST_REMOVE(alq, aq_link);
209103785Sjeff		ALD_UNLOCK();
210103785Sjeff		alq_shutdown(alq);
211103785Sjeff		ALD_LOCK();
212103785Sjeff	}
213103785Sjeff	ALD_UNLOCK();
214103785Sjeff}
215103785Sjeff
216103785Sjeffstatic void
217103785Sjeffalq_shutdown(struct alq *alq)
218103785Sjeff{
219103785Sjeff	ALQ_LOCK(alq);
220103785Sjeff
221103785Sjeff	/* Stop any new writers. */
222103785Sjeff	alq->aq_flags |= AQ_SHUTDOWN;
223103785Sjeff
224103785Sjeff	/* Drain IO */
225103785Sjeff	while (alq->aq_flags & (AQ_FLUSHING|AQ_ACTIVE)) {
226103785Sjeff		alq->aq_flags |= AQ_WANTED;
227167266Scognet		ALQ_UNLOCK(alq);
228167266Scognet		tsleep(alq, PWAIT, "aldclose", 0);
229167266Scognet		ALQ_LOCK(alq);
230103785Sjeff	}
231103785Sjeff	ALQ_UNLOCK(alq);
232103785Sjeff
233103995Sjeff	vn_close(alq->aq_vp, FWRITE, alq->aq_cred,
234103830Sjeff	    curthread);
235103830Sjeff	crfree(alq->aq_cred);
236103785Sjeff}
237103785Sjeff
238103785Sjeff/*
239103785Sjeff * Flush all pending data to disk.  This operation will block.
240103785Sjeff */
241103785Sjeffstatic int
242103785Sjeffalq_doio(struct alq *alq)
243103785Sjeff{
244103785Sjeff	struct thread *td;
245103785Sjeff	struct mount *mp;
246103785Sjeff	struct vnode *vp;
247103785Sjeff	struct uio auio;
248103785Sjeff	struct iovec aiov[2];
249103785Sjeff	struct ale *ale;
250103785Sjeff	struct ale *alstart;
251103785Sjeff	int totlen;
252103785Sjeff	int iov;
253157233Sjhb	int vfslocked;
254103785Sjeff
255103785Sjeff	vp = alq->aq_vp;
256103785Sjeff	td = curthread;
257103785Sjeff	totlen = 0;
258103785Sjeff	iov = 0;
259103785Sjeff
260103785Sjeff	alstart = ale = alq->aq_entvalid;
261103785Sjeff	alq->aq_entvalid = NULL;
262103785Sjeff
263103785Sjeff	bzero(&aiov, sizeof(aiov));
264103785Sjeff	bzero(&auio, sizeof(auio));
265103785Sjeff
266103785Sjeff	do {
267103785Sjeff		if (aiov[iov].iov_base == NULL)
268103785Sjeff			aiov[iov].iov_base = ale->ae_data;
269103785Sjeff		aiov[iov].iov_len += alq->aq_entlen;
270103785Sjeff		totlen += alq->aq_entlen;
271103785Sjeff		/* Check to see if we're wrapping the buffer */
272103785Sjeff		if (ale->ae_data + alq->aq_entlen != ale->ae_next->ae_data)
273103785Sjeff			iov++;
274103785Sjeff		ale->ae_flags &= ~AE_VALID;
275103785Sjeff		ale = ale->ae_next;
276103785Sjeff	} while (ale->ae_flags & AE_VALID);
277103785Sjeff
278103785Sjeff	alq->aq_flags |= AQ_FLUSHING;
279103785Sjeff	ALQ_UNLOCK(alq);
280103785Sjeff
281103785Sjeff	if (iov == 2 || aiov[iov].iov_base == NULL)
282103785Sjeff		iov--;
283103785Sjeff
284103785Sjeff	auio.uio_iov = &aiov[0];
285103785Sjeff	auio.uio_offset = 0;
286103785Sjeff	auio.uio_segflg = UIO_SYSSPACE;
287103785Sjeff	auio.uio_rw = UIO_WRITE;
288103785Sjeff	auio.uio_iovcnt = iov + 1;
289103785Sjeff	auio.uio_resid = totlen;
290103785Sjeff	auio.uio_td = td;
291103785Sjeff
292103785Sjeff	/*
293103785Sjeff	 * Do all of the junk required to write now.
294103785Sjeff	 */
295157233Sjhb	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
296103785Sjeff	vn_start_write(vp, &mp, V_WAIT);
297103785Sjeff	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
298103830Sjeff	VOP_LEASE(vp, td, alq->aq_cred, LEASE_WRITE);
299121508Srwatson	/*
300121508Srwatson	 * XXX: VOP_WRITE error checks are ignored.
301121508Srwatson	 */
302121508Srwatson#ifdef MAC
303172930Srwatson	if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
304121508Srwatson#endif
305121508Srwatson		VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
306103785Sjeff	VOP_UNLOCK(vp, 0, td);
307103785Sjeff	vn_finished_write(mp);
308157233Sjhb	VFS_UNLOCK_GIANT(vfslocked);
309103785Sjeff
310103785Sjeff	ALQ_LOCK(alq);
311103785Sjeff	alq->aq_flags &= ~AQ_FLUSHING;
312103785Sjeff
313103785Sjeff	if (alq->aq_entfree == NULL)
314103785Sjeff		alq->aq_entfree = alstart;
315103785Sjeff
316103785Sjeff	if (alq->aq_flags & AQ_WANTED) {
317103785Sjeff		alq->aq_flags &= ~AQ_WANTED;
318103785Sjeff		return (1);
319103785Sjeff	}
320103785Sjeff
321103785Sjeff	return(0);
322103785Sjeff}
323103785Sjeff
324103785Sjeffstatic struct kproc_desc ald_kp = {
325103785Sjeff        "ALQ Daemon",
326103785Sjeff        ald_daemon,
327103995Sjeff        &ald_proc
328103785Sjeff};
329103785Sjeff
330103785SjeffSYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp)
331103785SjeffSYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL)
332103785Sjeff
333103785Sjeff
334103785Sjeff/* User visible queue functions */
335103785Sjeff
336103785Sjeff/*
337103785Sjeff * Create the queue data structure, allocate the buffer, and open the file.
338103785Sjeff */
339103785Sjeffint
340145142Srwatsonalq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
341145142Srwatson    int size, int count)
342103785Sjeff{
343103785Sjeff	struct thread *td;
344103785Sjeff	struct nameidata nd;
345103785Sjeff	struct ale *ale;
346103785Sjeff	struct ale *alp;
347103785Sjeff	struct alq *alq;
348103785Sjeff	char *bufp;
349103785Sjeff	int flags;
350103785Sjeff	int error;
351157233Sjhb	int i, vfslocked;
352103785Sjeff
353103785Sjeff	*alqp = NULL;
354103785Sjeff	td = curthread;
355103785Sjeff
356157233Sjhb	NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, file, td);
357103995Sjeff	flags = FWRITE | O_NOFOLLOW | O_CREAT;
358103785Sjeff
359170183Skib	error = vn_open_cred(&nd, &flags, cmode, cred, NULL);
360103785Sjeff	if (error)
361103785Sjeff		return (error);
362154902Spjd
363157233Sjhb	vfslocked = NDHASGIANT(&nd);
364154903Spjd	NDFREE(&nd, NDF_ONLY_PNBUF);
365103785Sjeff	/* We just unlock so we hold a reference */
366103785Sjeff	VOP_UNLOCK(nd.ni_vp, 0, td);
367157233Sjhb	VFS_UNLOCK_GIANT(vfslocked);
368103785Sjeff
369111119Simp	alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO);
370111119Simp	alq->aq_entbuf = malloc(count * size, M_ALD, M_WAITOK|M_ZERO);
371111119Simp	alq->aq_first = malloc(sizeof(*ale) * count, M_ALD, M_WAITOK|M_ZERO);
372103785Sjeff	alq->aq_vp = nd.ni_vp;
373116697Srwatson	alq->aq_cred = crhold(cred);
374103785Sjeff	alq->aq_entmax = count;
375103785Sjeff	alq->aq_entlen = size;
376103785Sjeff	alq->aq_entfree = alq->aq_first;
377103785Sjeff
378103785Sjeff	mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET);
379103785Sjeff
380103785Sjeff	bufp = alq->aq_entbuf;
381103785Sjeff	ale = alq->aq_first;
382103785Sjeff	alp = NULL;
383103785Sjeff
384103785Sjeff	/* Match up entries with buffers */
385103785Sjeff	for (i = 0; i < count; i++) {
386103785Sjeff		if (alp)
387103785Sjeff			alp->ae_next = ale;
388103785Sjeff		ale->ae_data = bufp;
389103785Sjeff		alp = ale;
390103785Sjeff		ale++;
391103785Sjeff		bufp += size;
392103785Sjeff	}
393103785Sjeff
394103785Sjeff	alp->ae_next = alq->aq_first;
395103785Sjeff
396103785Sjeff	if ((error = ald_add(alq)) != 0)
397103785Sjeff		return (error);
398103785Sjeff	*alqp = alq;
399103785Sjeff
400103785Sjeff	return (0);
401103785Sjeff}
402103785Sjeff
403103785Sjeff/*
404103785Sjeff * Copy a new entry into the queue.  If the operation would block either
405103785Sjeff * wait or return an error depending on the value of waitok.
406103785Sjeff */
407103785Sjeffint
408103785Sjeffalq_write(struct alq *alq, void *data, int waitok)
409103785Sjeff{
410103785Sjeff	struct ale *ale;
411103785Sjeff
412103785Sjeff	if ((ale = alq_get(alq, waitok)) == NULL)
413103785Sjeff		return (EWOULDBLOCK);
414103785Sjeff
415103785Sjeff	bcopy(data, ale->ae_data, alq->aq_entlen);
416103785Sjeff	alq_post(alq, ale);
417103785Sjeff
418103785Sjeff	return (0);
419103785Sjeff}
420103785Sjeff
421103785Sjeffstruct ale *
422103785Sjeffalq_get(struct alq *alq, int waitok)
423103785Sjeff{
424103785Sjeff	struct ale *ale;
425103785Sjeff	struct ale *aln;
426103785Sjeff
427103785Sjeff	ale = NULL;
428103785Sjeff
429103785Sjeff	ALQ_LOCK(alq);
430103785Sjeff
431103785Sjeff	/* Loop until we get an entry or we're shutting down */
432103785Sjeff	while ((alq->aq_flags & AQ_SHUTDOWN) == 0 &&
433103785Sjeff	    (ale = alq->aq_entfree) == NULL &&
434103785Sjeff	    (waitok & ALQ_WAITOK)) {
435103785Sjeff		alq->aq_flags |= AQ_WANTED;
436167266Scognet		ALQ_UNLOCK(alq);
437167266Scognet		tsleep(alq, PWAIT, "alqget", 0);
438167266Scognet		ALQ_LOCK(alq);
439103785Sjeff	}
440103785Sjeff
441103785Sjeff	if (ale != NULL) {
442103785Sjeff		aln = ale->ae_next;
443103785Sjeff		if ((aln->ae_flags & AE_VALID) == 0)
444103785Sjeff			alq->aq_entfree = aln;
445115308Sjeff		else
446115308Sjeff			alq->aq_entfree = NULL;
447103785Sjeff	} else
448103785Sjeff		ALQ_UNLOCK(alq);
449103785Sjeff
450103785Sjeff
451103785Sjeff	return (ale);
452103785Sjeff}
453103785Sjeff
454103785Sjeffvoid
455103785Sjeffalq_post(struct alq *alq, struct ale *ale)
456103785Sjeff{
457103785Sjeff	int activate;
458103785Sjeff
459103785Sjeff	ale->ae_flags |= AE_VALID;
460103785Sjeff
461103785Sjeff	if (alq->aq_entvalid == NULL)
462103785Sjeff		alq->aq_entvalid = ale;
463103785Sjeff
464103785Sjeff	if ((alq->aq_flags & AQ_ACTIVE) == 0) {
465103785Sjeff		alq->aq_flags |= AQ_ACTIVE;
466103785Sjeff		activate = 1;
467103785Sjeff	} else
468103785Sjeff		activate = 0;
469103785Sjeff
470103785Sjeff	ALQ_UNLOCK(alq);
471103785Sjeff	if (activate) {
472103785Sjeff		ALD_LOCK();
473103785Sjeff		ald_activate(alq);
474103785Sjeff		ALD_UNLOCK();
475103785Sjeff	}
476103785Sjeff}
477103785Sjeff
478103785Sjeffvoid
479103785Sjeffalq_flush(struct alq *alq)
480103785Sjeff{
481103785Sjeff	int needwakeup = 0;
482103785Sjeff
483103785Sjeff	ALD_LOCK();
484103785Sjeff	ALQ_LOCK(alq);
485103785Sjeff	if (alq->aq_flags & AQ_ACTIVE) {
486103785Sjeff		ald_deactivate(alq);
487103785Sjeff		ALD_UNLOCK();
488103785Sjeff		needwakeup = alq_doio(alq);
489103785Sjeff	} else
490103785Sjeff		ALD_UNLOCK();
491103785Sjeff	ALQ_UNLOCK(alq);
492103785Sjeff
493103785Sjeff	if (needwakeup)
494103785Sjeff		wakeup(alq);
495103785Sjeff}
496103785Sjeff
497103785Sjeff/*
498103785Sjeff * Flush remaining data, close the file and free all resources.
499103785Sjeff */
500103785Sjeffvoid
501103785Sjeffalq_close(struct alq *alq)
502103785Sjeff{
503103785Sjeff	/*
504103785Sjeff	 * If we're already shuting down someone else will flush and close
505103785Sjeff	 * the vnode.
506103785Sjeff	 */
507103785Sjeff	if (ald_rem(alq) != 0)
508103785Sjeff		return;
509103785Sjeff
510103785Sjeff	/*
511103785Sjeff	 * Drain all pending IO.
512103785Sjeff	 */
513103785Sjeff	alq_shutdown(alq);
514103785Sjeff
515103785Sjeff	mtx_destroy(&alq->aq_mtx);
516103785Sjeff	free(alq->aq_first, M_ALD);
517103785Sjeff	free(alq->aq_entbuf, M_ALD);
518103785Sjeff	free(alq, M_ALD);
519103785Sjeff}
520