kern_alq.c revision 180196
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 180196 2008-07-02 20:44:33Z rdivacky $");
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;
227180196Srdivacky		msleep_spin(alq, &alq->aq_mtx, "aldclose", 0);
228103785Sjeff	}
229103785Sjeff	ALQ_UNLOCK(alq);
230103785Sjeff
231103995Sjeff	vn_close(alq->aq_vp, FWRITE, alq->aq_cred,
232103830Sjeff	    curthread);
233103830Sjeff	crfree(alq->aq_cred);
234103785Sjeff}
235103785Sjeff
236103785Sjeff/*
237103785Sjeff * Flush all pending data to disk.  This operation will block.
238103785Sjeff */
239103785Sjeffstatic int
240103785Sjeffalq_doio(struct alq *alq)
241103785Sjeff{
242103785Sjeff	struct thread *td;
243103785Sjeff	struct mount *mp;
244103785Sjeff	struct vnode *vp;
245103785Sjeff	struct uio auio;
246103785Sjeff	struct iovec aiov[2];
247103785Sjeff	struct ale *ale;
248103785Sjeff	struct ale *alstart;
249103785Sjeff	int totlen;
250103785Sjeff	int iov;
251157233Sjhb	int vfslocked;
252103785Sjeff
253103785Sjeff	vp = alq->aq_vp;
254103785Sjeff	td = curthread;
255103785Sjeff	totlen = 0;
256103785Sjeff	iov = 0;
257103785Sjeff
258103785Sjeff	alstart = ale = alq->aq_entvalid;
259103785Sjeff	alq->aq_entvalid = NULL;
260103785Sjeff
261103785Sjeff	bzero(&aiov, sizeof(aiov));
262103785Sjeff	bzero(&auio, sizeof(auio));
263103785Sjeff
264103785Sjeff	do {
265103785Sjeff		if (aiov[iov].iov_base == NULL)
266103785Sjeff			aiov[iov].iov_base = ale->ae_data;
267103785Sjeff		aiov[iov].iov_len += alq->aq_entlen;
268103785Sjeff		totlen += alq->aq_entlen;
269103785Sjeff		/* Check to see if we're wrapping the buffer */
270103785Sjeff		if (ale->ae_data + alq->aq_entlen != ale->ae_next->ae_data)
271103785Sjeff			iov++;
272103785Sjeff		ale->ae_flags &= ~AE_VALID;
273103785Sjeff		ale = ale->ae_next;
274103785Sjeff	} while (ale->ae_flags & AE_VALID);
275103785Sjeff
276103785Sjeff	alq->aq_flags |= AQ_FLUSHING;
277103785Sjeff	ALQ_UNLOCK(alq);
278103785Sjeff
279103785Sjeff	if (iov == 2 || aiov[iov].iov_base == NULL)
280103785Sjeff		iov--;
281103785Sjeff
282103785Sjeff	auio.uio_iov = &aiov[0];
283103785Sjeff	auio.uio_offset = 0;
284103785Sjeff	auio.uio_segflg = UIO_SYSSPACE;
285103785Sjeff	auio.uio_rw = UIO_WRITE;
286103785Sjeff	auio.uio_iovcnt = iov + 1;
287103785Sjeff	auio.uio_resid = totlen;
288103785Sjeff	auio.uio_td = td;
289103785Sjeff
290103785Sjeff	/*
291103785Sjeff	 * Do all of the junk required to write now.
292103785Sjeff	 */
293157233Sjhb	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
294103785Sjeff	vn_start_write(vp, &mp, V_WAIT);
295175202Sattilio	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
296103830Sjeff	VOP_LEASE(vp, td, alq->aq_cred, LEASE_WRITE);
297121508Srwatson	/*
298121508Srwatson	 * XXX: VOP_WRITE error checks are ignored.
299121508Srwatson	 */
300121508Srwatson#ifdef MAC
301172930Srwatson	if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
302121508Srwatson#endif
303121508Srwatson		VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
304175294Sattilio	VOP_UNLOCK(vp, 0);
305103785Sjeff	vn_finished_write(mp);
306157233Sjhb	VFS_UNLOCK_GIANT(vfslocked);
307103785Sjeff
308103785Sjeff	ALQ_LOCK(alq);
309103785Sjeff	alq->aq_flags &= ~AQ_FLUSHING;
310103785Sjeff
311103785Sjeff	if (alq->aq_entfree == NULL)
312103785Sjeff		alq->aq_entfree = alstart;
313103785Sjeff
314103785Sjeff	if (alq->aq_flags & AQ_WANTED) {
315103785Sjeff		alq->aq_flags &= ~AQ_WANTED;
316103785Sjeff		return (1);
317103785Sjeff	}
318103785Sjeff
319103785Sjeff	return(0);
320103785Sjeff}
321103785Sjeff
322103785Sjeffstatic struct kproc_desc ald_kp = {
323103785Sjeff        "ALQ Daemon",
324103785Sjeff        ald_daemon,
325103995Sjeff        &ald_proc
326103785Sjeff};
327103785Sjeff
328177253SrwatsonSYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp);
329177253SrwatsonSYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL);
330103785Sjeff
331103785Sjeff
332103785Sjeff/* User visible queue functions */
333103785Sjeff
334103785Sjeff/*
335103785Sjeff * Create the queue data structure, allocate the buffer, and open the file.
336103785Sjeff */
337103785Sjeffint
338145142Srwatsonalq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
339145142Srwatson    int size, int count)
340103785Sjeff{
341103785Sjeff	struct thread *td;
342103785Sjeff	struct nameidata nd;
343103785Sjeff	struct ale *ale;
344103785Sjeff	struct ale *alp;
345103785Sjeff	struct alq *alq;
346103785Sjeff	char *bufp;
347103785Sjeff	int flags;
348103785Sjeff	int error;
349157233Sjhb	int i, vfslocked;
350103785Sjeff
351103785Sjeff	*alqp = NULL;
352103785Sjeff	td = curthread;
353103785Sjeff
354157233Sjhb	NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, file, td);
355103995Sjeff	flags = FWRITE | O_NOFOLLOW | O_CREAT;
356103785Sjeff
357170183Skib	error = vn_open_cred(&nd, &flags, cmode, cred, NULL);
358103785Sjeff	if (error)
359103785Sjeff		return (error);
360154902Spjd
361157233Sjhb	vfslocked = NDHASGIANT(&nd);
362154903Spjd	NDFREE(&nd, NDF_ONLY_PNBUF);
363103785Sjeff	/* We just unlock so we hold a reference */
364175294Sattilio	VOP_UNLOCK(nd.ni_vp, 0);
365157233Sjhb	VFS_UNLOCK_GIANT(vfslocked);
366103785Sjeff
367111119Simp	alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO);
368111119Simp	alq->aq_entbuf = malloc(count * size, M_ALD, M_WAITOK|M_ZERO);
369111119Simp	alq->aq_first = malloc(sizeof(*ale) * count, M_ALD, M_WAITOK|M_ZERO);
370103785Sjeff	alq->aq_vp = nd.ni_vp;
371116697Srwatson	alq->aq_cred = crhold(cred);
372103785Sjeff	alq->aq_entmax = count;
373103785Sjeff	alq->aq_entlen = size;
374103785Sjeff	alq->aq_entfree = alq->aq_first;
375103785Sjeff
376103785Sjeff	mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET);
377103785Sjeff
378103785Sjeff	bufp = alq->aq_entbuf;
379103785Sjeff	ale = alq->aq_first;
380103785Sjeff	alp = NULL;
381103785Sjeff
382103785Sjeff	/* Match up entries with buffers */
383103785Sjeff	for (i = 0; i < count; i++) {
384103785Sjeff		if (alp)
385103785Sjeff			alp->ae_next = ale;
386103785Sjeff		ale->ae_data = bufp;
387103785Sjeff		alp = ale;
388103785Sjeff		ale++;
389103785Sjeff		bufp += size;
390103785Sjeff	}
391103785Sjeff
392103785Sjeff	alp->ae_next = alq->aq_first;
393103785Sjeff
394103785Sjeff	if ((error = ald_add(alq)) != 0)
395103785Sjeff		return (error);
396103785Sjeff	*alqp = alq;
397103785Sjeff
398103785Sjeff	return (0);
399103785Sjeff}
400103785Sjeff
401103785Sjeff/*
402103785Sjeff * Copy a new entry into the queue.  If the operation would block either
403103785Sjeff * wait or return an error depending on the value of waitok.
404103785Sjeff */
405103785Sjeffint
406103785Sjeffalq_write(struct alq *alq, void *data, int waitok)
407103785Sjeff{
408103785Sjeff	struct ale *ale;
409103785Sjeff
410103785Sjeff	if ((ale = alq_get(alq, waitok)) == NULL)
411103785Sjeff		return (EWOULDBLOCK);
412103785Sjeff
413103785Sjeff	bcopy(data, ale->ae_data, alq->aq_entlen);
414103785Sjeff	alq_post(alq, ale);
415103785Sjeff
416103785Sjeff	return (0);
417103785Sjeff}
418103785Sjeff
419103785Sjeffstruct ale *
420103785Sjeffalq_get(struct alq *alq, int waitok)
421103785Sjeff{
422103785Sjeff	struct ale *ale;
423103785Sjeff	struct ale *aln;
424103785Sjeff
425103785Sjeff	ale = NULL;
426103785Sjeff
427103785Sjeff	ALQ_LOCK(alq);
428103785Sjeff
429103785Sjeff	/* Loop until we get an entry or we're shutting down */
430103785Sjeff	while ((alq->aq_flags & AQ_SHUTDOWN) == 0 &&
431103785Sjeff	    (ale = alq->aq_entfree) == NULL &&
432103785Sjeff	    (waitok & ALQ_WAITOK)) {
433103785Sjeff		alq->aq_flags |= AQ_WANTED;
434180196Srdivacky		msleep_spin(alq, &alq->aq_mtx, "alqget", 0);
435103785Sjeff	}
436103785Sjeff
437103785Sjeff	if (ale != NULL) {
438103785Sjeff		aln = ale->ae_next;
439103785Sjeff		if ((aln->ae_flags & AE_VALID) == 0)
440103785Sjeff			alq->aq_entfree = aln;
441115308Sjeff		else
442115308Sjeff			alq->aq_entfree = NULL;
443103785Sjeff	} else
444103785Sjeff		ALQ_UNLOCK(alq);
445103785Sjeff
446103785Sjeff
447103785Sjeff	return (ale);
448103785Sjeff}
449103785Sjeff
450103785Sjeffvoid
451103785Sjeffalq_post(struct alq *alq, struct ale *ale)
452103785Sjeff{
453103785Sjeff	int activate;
454103785Sjeff
455103785Sjeff	ale->ae_flags |= AE_VALID;
456103785Sjeff
457103785Sjeff	if (alq->aq_entvalid == NULL)
458103785Sjeff		alq->aq_entvalid = ale;
459103785Sjeff
460103785Sjeff	if ((alq->aq_flags & AQ_ACTIVE) == 0) {
461103785Sjeff		alq->aq_flags |= AQ_ACTIVE;
462103785Sjeff		activate = 1;
463103785Sjeff	} else
464103785Sjeff		activate = 0;
465103785Sjeff
466103785Sjeff	ALQ_UNLOCK(alq);
467103785Sjeff	if (activate) {
468103785Sjeff		ALD_LOCK();
469103785Sjeff		ald_activate(alq);
470103785Sjeff		ALD_UNLOCK();
471103785Sjeff	}
472103785Sjeff}
473103785Sjeff
474103785Sjeffvoid
475103785Sjeffalq_flush(struct alq *alq)
476103785Sjeff{
477103785Sjeff	int needwakeup = 0;
478103785Sjeff
479103785Sjeff	ALD_LOCK();
480103785Sjeff	ALQ_LOCK(alq);
481103785Sjeff	if (alq->aq_flags & AQ_ACTIVE) {
482103785Sjeff		ald_deactivate(alq);
483103785Sjeff		ALD_UNLOCK();
484103785Sjeff		needwakeup = alq_doio(alq);
485103785Sjeff	} else
486103785Sjeff		ALD_UNLOCK();
487103785Sjeff	ALQ_UNLOCK(alq);
488103785Sjeff
489103785Sjeff	if (needwakeup)
490103785Sjeff		wakeup(alq);
491103785Sjeff}
492103785Sjeff
493103785Sjeff/*
494103785Sjeff * Flush remaining data, close the file and free all resources.
495103785Sjeff */
496103785Sjeffvoid
497103785Sjeffalq_close(struct alq *alq)
498103785Sjeff{
499103785Sjeff	/*
500103785Sjeff	 * If we're already shuting down someone else will flush and close
501103785Sjeff	 * the vnode.
502103785Sjeff	 */
503103785Sjeff	if (ald_rem(alq) != 0)
504103785Sjeff		return;
505103785Sjeff
506103785Sjeff	/*
507103785Sjeff	 * Drain all pending IO.
508103785Sjeff	 */
509103785Sjeff	alq_shutdown(alq);
510103785Sjeff
511103785Sjeff	mtx_destroy(&alq->aq_mtx);
512103785Sjeff	free(alq->aq_first, M_ALD);
513103785Sjeff	free(alq->aq_entbuf, M_ALD);
514103785Sjeff	free(alq, M_ALD);
515103785Sjeff}
516