kern_alq.c revision 205959
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 205959 2010-03-31 03:58:57Z 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 *);
106103785Sjeffstatic int alq_doio(struct alq *);
107103785Sjeff
108103785Sjeff
109103785Sjeff/*
110103785Sjeff * Add a new queue to the global list.  Fail if we're shutting down.
111103785Sjeff */
112103785Sjeffstatic int
113103785Sjeffald_add(struct alq *alq)
114103785Sjeff{
115103785Sjeff	int error;
116103785Sjeff
117103785Sjeff	error = 0;
118103785Sjeff
119103785Sjeff	ALD_LOCK();
120103785Sjeff	if (ald_shutingdown) {
121103785Sjeff		error = EBUSY;
122103785Sjeff		goto done;
123103785Sjeff	}
124103785Sjeff	LIST_INSERT_HEAD(&ald_queues, alq, aq_link);
125103785Sjeffdone:
126103785Sjeff	ALD_UNLOCK();
127103785Sjeff	return (error);
128103785Sjeff}
129103785Sjeff
130103785Sjeff/*
131103785Sjeff * Remove a queue from the global list unless we're shutting down.  If so,
132103785Sjeff * the ald will take care of cleaning up it's resources.
133103785Sjeff */
134103785Sjeffstatic int
135103785Sjeffald_rem(struct alq *alq)
136103785Sjeff{
137103785Sjeff	int error;
138103785Sjeff
139103785Sjeff	error = 0;
140103785Sjeff
141103785Sjeff	ALD_LOCK();
142103785Sjeff	if (ald_shutingdown) {
143103785Sjeff		error = EBUSY;
144103785Sjeff		goto done;
145103785Sjeff	}
146103785Sjeff	LIST_REMOVE(alq, aq_link);
147103785Sjeffdone:
148103785Sjeff	ALD_UNLOCK();
149103785Sjeff	return (error);
150103785Sjeff}
151103785Sjeff
152103785Sjeff/*
153103785Sjeff * Put a queue on the active list.  This will schedule it for writing.
154103785Sjeff */
155103785Sjeffstatic void
156103785Sjeffald_activate(struct alq *alq)
157103785Sjeff{
158103785Sjeff	LIST_INSERT_HEAD(&ald_active, alq, aq_act);
159103785Sjeff	wakeup(&ald_active);
160103785Sjeff}
161103785Sjeff
162103785Sjeffstatic void
163103785Sjeffald_deactivate(struct alq *alq)
164103785Sjeff{
165103785Sjeff	LIST_REMOVE(alq, aq_act);
166103785Sjeff	alq->aq_flags &= ~AQ_ACTIVE;
167103785Sjeff}
168103785Sjeff
169103785Sjeffstatic void
170103785Sjeffald_startup(void *unused)
171103785Sjeff{
172103785Sjeff	mtx_init(&ald_mtx, "ALDmtx", NULL, MTX_DEF|MTX_QUIET);
173103785Sjeff	LIST_INIT(&ald_queues);
174103785Sjeff	LIST_INIT(&ald_active);
175103785Sjeff}
176103785Sjeff
177103785Sjeffstatic void
178103785Sjeffald_daemon(void)
179103785Sjeff{
180103785Sjeff	int needwakeup;
181103785Sjeff	struct alq *alq;
182103785Sjeff
183103995Sjeff	ald_thread = FIRST_THREAD_IN_PROC(ald_proc);
184103995Sjeff
185103785Sjeff	EVENTHANDLER_REGISTER(shutdown_pre_sync, ald_shutdown, NULL,
186103785Sjeff	    SHUTDOWN_PRI_FIRST);
187103785Sjeff
188103785Sjeff	ALD_LOCK();
189103785Sjeff
190103785Sjeff	for (;;) {
191205959Slstewart		while ((alq = LIST_FIRST(&ald_active)) == NULL &&
192205959Slstewart		    !ald_shutingdown)
193103785Sjeff			msleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);
194103785Sjeff
195205959Slstewart		/* Don't shutdown until all active ALQs are flushed. */
196205959Slstewart		if (ald_shutingdown && alq == NULL) {
197205959Slstewart			ALD_UNLOCK();
198205959Slstewart			break;
199205959Slstewart		}
200205959Slstewart
201103785Sjeff		ALQ_LOCK(alq);
202103785Sjeff		ald_deactivate(alq);
203103785Sjeff		ALD_UNLOCK();
204103785Sjeff		needwakeup = alq_doio(alq);
205103785Sjeff		ALQ_UNLOCK(alq);
206103785Sjeff		if (needwakeup)
207103785Sjeff			wakeup(alq);
208103785Sjeff		ALD_LOCK();
209103785Sjeff	}
210205959Slstewart
211205959Slstewart	kproc_exit(0);
212103785Sjeff}
213103785Sjeff
214103785Sjeffstatic void
215103785Sjeffald_shutdown(void *arg, int howto)
216103785Sjeff{
217103785Sjeff	struct alq *alq;
218103785Sjeff
219103785Sjeff	ALD_LOCK();
220205959Slstewart
221205959Slstewart	/* Ensure no new queues can be created. */
222103785Sjeff	ald_shutingdown = 1;
223103785Sjeff
224205959Slstewart	/* Shutdown all ALQs prior to terminating the ald_daemon. */
225103785Sjeff	while ((alq = LIST_FIRST(&ald_queues)) != NULL) {
226103785Sjeff		LIST_REMOVE(alq, aq_link);
227103785Sjeff		ALD_UNLOCK();
228103785Sjeff		alq_shutdown(alq);
229103785Sjeff		ALD_LOCK();
230103785Sjeff	}
231205959Slstewart
232205959Slstewart	/* At this point, all ALQs are flushed and shutdown. */
233205959Slstewart
234205959Slstewart	/*
235205959Slstewart	 * Wake ald_daemon so that it exits. It won't be able to do
236205959Slstewart	 * anything until we msleep because we hold the ald_mtx.
237205959Slstewart	 */
238205959Slstewart	wakeup(&ald_active);
239205959Slstewart
240205959Slstewart	/* Wait for ald_daemon to exit. */
241205959Slstewart	msleep(ald_proc, &ald_mtx, PWAIT, "aldslp", 0);
242205959Slstewart
243103785Sjeff	ALD_UNLOCK();
244103785Sjeff}
245103785Sjeff
246103785Sjeffstatic void
247103785Sjeffalq_shutdown(struct alq *alq)
248103785Sjeff{
249103785Sjeff	ALQ_LOCK(alq);
250103785Sjeff
251103785Sjeff	/* Stop any new writers. */
252103785Sjeff	alq->aq_flags |= AQ_SHUTDOWN;
253103785Sjeff
254103785Sjeff	/* Drain IO */
255103785Sjeff	while (alq->aq_flags & (AQ_FLUSHING|AQ_ACTIVE)) {
256103785Sjeff		alq->aq_flags |= AQ_WANTED;
257180196Srdivacky		msleep_spin(alq, &alq->aq_mtx, "aldclose", 0);
258103785Sjeff	}
259103785Sjeff	ALQ_UNLOCK(alq);
260103785Sjeff
261103995Sjeff	vn_close(alq->aq_vp, FWRITE, alq->aq_cred,
262103830Sjeff	    curthread);
263103830Sjeff	crfree(alq->aq_cred);
264103785Sjeff}
265103785Sjeff
266103785Sjeff/*
267103785Sjeff * Flush all pending data to disk.  This operation will block.
268103785Sjeff */
269103785Sjeffstatic int
270103785Sjeffalq_doio(struct alq *alq)
271103785Sjeff{
272103785Sjeff	struct thread *td;
273103785Sjeff	struct mount *mp;
274103785Sjeff	struct vnode *vp;
275103785Sjeff	struct uio auio;
276103785Sjeff	struct iovec aiov[2];
277103785Sjeff	struct ale *ale;
278103785Sjeff	struct ale *alstart;
279103785Sjeff	int totlen;
280103785Sjeff	int iov;
281157233Sjhb	int vfslocked;
282103785Sjeff
283103785Sjeff	vp = alq->aq_vp;
284103785Sjeff	td = curthread;
285103785Sjeff	totlen = 0;
286103785Sjeff	iov = 0;
287103785Sjeff
288103785Sjeff	alstart = ale = alq->aq_entvalid;
289103785Sjeff	alq->aq_entvalid = NULL;
290103785Sjeff
291103785Sjeff	bzero(&aiov, sizeof(aiov));
292103785Sjeff	bzero(&auio, sizeof(auio));
293103785Sjeff
294103785Sjeff	do {
295103785Sjeff		if (aiov[iov].iov_base == NULL)
296103785Sjeff			aiov[iov].iov_base = ale->ae_data;
297103785Sjeff		aiov[iov].iov_len += alq->aq_entlen;
298103785Sjeff		totlen += alq->aq_entlen;
299103785Sjeff		/* Check to see if we're wrapping the buffer */
300103785Sjeff		if (ale->ae_data + alq->aq_entlen != ale->ae_next->ae_data)
301103785Sjeff			iov++;
302103785Sjeff		ale->ae_flags &= ~AE_VALID;
303103785Sjeff		ale = ale->ae_next;
304103785Sjeff	} while (ale->ae_flags & AE_VALID);
305103785Sjeff
306103785Sjeff	alq->aq_flags |= AQ_FLUSHING;
307103785Sjeff	ALQ_UNLOCK(alq);
308103785Sjeff
309103785Sjeff	if (iov == 2 || aiov[iov].iov_base == NULL)
310103785Sjeff		iov--;
311103785Sjeff
312103785Sjeff	auio.uio_iov = &aiov[0];
313103785Sjeff	auio.uio_offset = 0;
314103785Sjeff	auio.uio_segflg = UIO_SYSSPACE;
315103785Sjeff	auio.uio_rw = UIO_WRITE;
316103785Sjeff	auio.uio_iovcnt = iov + 1;
317103785Sjeff	auio.uio_resid = totlen;
318103785Sjeff	auio.uio_td = td;
319103785Sjeff
320103785Sjeff	/*
321103785Sjeff	 * Do all of the junk required to write now.
322103785Sjeff	 */
323157233Sjhb	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
324103785Sjeff	vn_start_write(vp, &mp, V_WAIT);
325175202Sattilio	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
326121508Srwatson	/*
327121508Srwatson	 * XXX: VOP_WRITE error checks are ignored.
328121508Srwatson	 */
329121508Srwatson#ifdef MAC
330172930Srwatson	if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0)
331121508Srwatson#endif
332121508Srwatson		VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
333175294Sattilio	VOP_UNLOCK(vp, 0);
334103785Sjeff	vn_finished_write(mp);
335157233Sjhb	VFS_UNLOCK_GIANT(vfslocked);
336103785Sjeff
337103785Sjeff	ALQ_LOCK(alq);
338103785Sjeff	alq->aq_flags &= ~AQ_FLUSHING;
339103785Sjeff
340103785Sjeff	if (alq->aq_entfree == NULL)
341103785Sjeff		alq->aq_entfree = alstart;
342103785Sjeff
343103785Sjeff	if (alq->aq_flags & AQ_WANTED) {
344103785Sjeff		alq->aq_flags &= ~AQ_WANTED;
345103785Sjeff		return (1);
346103785Sjeff	}
347103785Sjeff
348103785Sjeff	return(0);
349103785Sjeff}
350103785Sjeff
351103785Sjeffstatic struct kproc_desc ald_kp = {
352103785Sjeff        "ALQ Daemon",
353103785Sjeff        ald_daemon,
354103995Sjeff        &ald_proc
355103785Sjeff};
356103785Sjeff
357177253SrwatsonSYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp);
358177253SrwatsonSYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL);
359103785Sjeff
360103785Sjeff
361103785Sjeff/* User visible queue functions */
362103785Sjeff
363103785Sjeff/*
364103785Sjeff * Create the queue data structure, allocate the buffer, and open the file.
365103785Sjeff */
366103785Sjeffint
367145142Srwatsonalq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
368145142Srwatson    int size, int count)
369103785Sjeff{
370103785Sjeff	struct thread *td;
371103785Sjeff	struct nameidata nd;
372103785Sjeff	struct ale *ale;
373103785Sjeff	struct ale *alp;
374103785Sjeff	struct alq *alq;
375103785Sjeff	char *bufp;
376103785Sjeff	int flags;
377103785Sjeff	int error;
378157233Sjhb	int i, vfslocked;
379103785Sjeff
380103785Sjeff	*alqp = NULL;
381103785Sjeff	td = curthread;
382103785Sjeff
383157233Sjhb	NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, file, td);
384103995Sjeff	flags = FWRITE | O_NOFOLLOW | O_CREAT;
385103785Sjeff
386194586Skib	error = vn_open_cred(&nd, &flags, cmode, 0, cred, NULL);
387103785Sjeff	if (error)
388103785Sjeff		return (error);
389154902Spjd
390157233Sjhb	vfslocked = NDHASGIANT(&nd);
391154903Spjd	NDFREE(&nd, NDF_ONLY_PNBUF);
392103785Sjeff	/* We just unlock so we hold a reference */
393175294Sattilio	VOP_UNLOCK(nd.ni_vp, 0);
394157233Sjhb	VFS_UNLOCK_GIANT(vfslocked);
395103785Sjeff
396111119Simp	alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO);
397111119Simp	alq->aq_entbuf = malloc(count * size, M_ALD, M_WAITOK|M_ZERO);
398111119Simp	alq->aq_first = malloc(sizeof(*ale) * count, M_ALD, M_WAITOK|M_ZERO);
399103785Sjeff	alq->aq_vp = nd.ni_vp;
400116697Srwatson	alq->aq_cred = crhold(cred);
401103785Sjeff	alq->aq_entmax = count;
402103785Sjeff	alq->aq_entlen = size;
403103785Sjeff	alq->aq_entfree = alq->aq_first;
404103785Sjeff
405103785Sjeff	mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET);
406103785Sjeff
407103785Sjeff	bufp = alq->aq_entbuf;
408103785Sjeff	ale = alq->aq_first;
409103785Sjeff	alp = NULL;
410103785Sjeff
411103785Sjeff	/* Match up entries with buffers */
412103785Sjeff	for (i = 0; i < count; i++) {
413103785Sjeff		if (alp)
414103785Sjeff			alp->ae_next = ale;
415103785Sjeff		ale->ae_data = bufp;
416103785Sjeff		alp = ale;
417103785Sjeff		ale++;
418103785Sjeff		bufp += size;
419103785Sjeff	}
420103785Sjeff
421103785Sjeff	alp->ae_next = alq->aq_first;
422103785Sjeff
423103785Sjeff	if ((error = ald_add(alq)) != 0)
424103785Sjeff		return (error);
425103785Sjeff	*alqp = alq;
426103785Sjeff
427103785Sjeff	return (0);
428103785Sjeff}
429103785Sjeff
430103785Sjeff/*
431103785Sjeff * Copy a new entry into the queue.  If the operation would block either
432103785Sjeff * wait or return an error depending on the value of waitok.
433103785Sjeff */
434103785Sjeffint
435103785Sjeffalq_write(struct alq *alq, void *data, int waitok)
436103785Sjeff{
437103785Sjeff	struct ale *ale;
438103785Sjeff
439103785Sjeff	if ((ale = alq_get(alq, waitok)) == NULL)
440103785Sjeff		return (EWOULDBLOCK);
441103785Sjeff
442103785Sjeff	bcopy(data, ale->ae_data, alq->aq_entlen);
443103785Sjeff	alq_post(alq, ale);
444103785Sjeff
445103785Sjeff	return (0);
446103785Sjeff}
447103785Sjeff
448103785Sjeffstruct ale *
449103785Sjeffalq_get(struct alq *alq, int waitok)
450103785Sjeff{
451103785Sjeff	struct ale *ale;
452103785Sjeff	struct ale *aln;
453103785Sjeff
454103785Sjeff	ale = NULL;
455103785Sjeff
456103785Sjeff	ALQ_LOCK(alq);
457103785Sjeff
458103785Sjeff	/* Loop until we get an entry or we're shutting down */
459103785Sjeff	while ((alq->aq_flags & AQ_SHUTDOWN) == 0 &&
460103785Sjeff	    (ale = alq->aq_entfree) == NULL &&
461103785Sjeff	    (waitok & ALQ_WAITOK)) {
462103785Sjeff		alq->aq_flags |= AQ_WANTED;
463180196Srdivacky		msleep_spin(alq, &alq->aq_mtx, "alqget", 0);
464103785Sjeff	}
465103785Sjeff
466103785Sjeff	if (ale != NULL) {
467103785Sjeff		aln = ale->ae_next;
468103785Sjeff		if ((aln->ae_flags & AE_VALID) == 0)
469103785Sjeff			alq->aq_entfree = aln;
470115308Sjeff		else
471115308Sjeff			alq->aq_entfree = NULL;
472103785Sjeff	} else
473103785Sjeff		ALQ_UNLOCK(alq);
474103785Sjeff
475103785Sjeff
476103785Sjeff	return (ale);
477103785Sjeff}
478103785Sjeff
479103785Sjeffvoid
480103785Sjeffalq_post(struct alq *alq, struct ale *ale)
481103785Sjeff{
482103785Sjeff	int activate;
483103785Sjeff
484103785Sjeff	ale->ae_flags |= AE_VALID;
485103785Sjeff
486103785Sjeff	if (alq->aq_entvalid == NULL)
487103785Sjeff		alq->aq_entvalid = ale;
488103785Sjeff
489103785Sjeff	if ((alq->aq_flags & AQ_ACTIVE) == 0) {
490103785Sjeff		alq->aq_flags |= AQ_ACTIVE;
491103785Sjeff		activate = 1;
492103785Sjeff	} else
493103785Sjeff		activate = 0;
494103785Sjeff
495103785Sjeff	ALQ_UNLOCK(alq);
496103785Sjeff	if (activate) {
497103785Sjeff		ALD_LOCK();
498103785Sjeff		ald_activate(alq);
499103785Sjeff		ALD_UNLOCK();
500103785Sjeff	}
501103785Sjeff}
502103785Sjeff
503103785Sjeffvoid
504103785Sjeffalq_flush(struct alq *alq)
505103785Sjeff{
506103785Sjeff	int needwakeup = 0;
507103785Sjeff
508103785Sjeff	ALD_LOCK();
509103785Sjeff	ALQ_LOCK(alq);
510103785Sjeff	if (alq->aq_flags & AQ_ACTIVE) {
511103785Sjeff		ald_deactivate(alq);
512103785Sjeff		ALD_UNLOCK();
513103785Sjeff		needwakeup = alq_doio(alq);
514103785Sjeff	} else
515103785Sjeff		ALD_UNLOCK();
516103785Sjeff	ALQ_UNLOCK(alq);
517103785Sjeff
518103785Sjeff	if (needwakeup)
519103785Sjeff		wakeup(alq);
520103785Sjeff}
521103785Sjeff
522103785Sjeff/*
523103785Sjeff * Flush remaining data, close the file and free all resources.
524103785Sjeff */
525103785Sjeffvoid
526103785Sjeffalq_close(struct alq *alq)
527103785Sjeff{
528103785Sjeff	/*
529103785Sjeff	 * If we're already shuting down someone else will flush and close
530103785Sjeff	 * the vnode.
531103785Sjeff	 */
532103785Sjeff	if (ald_rem(alq) != 0)
533103785Sjeff		return;
534103785Sjeff
535103785Sjeff	/*
536103785Sjeff	 * Drain all pending IO.
537103785Sjeff	 */
538103785Sjeff	alq_shutdown(alq);
539103785Sjeff
540103785Sjeff	mtx_destroy(&alq->aq_mtx);
541103785Sjeff	free(alq->aq_first, M_ALD);
542103785Sjeff	free(alq->aq_entbuf, M_ALD);
543103785Sjeff	free(alq, M_ALD);
544103785Sjeff}
545205959Slstewart
546205959Slstewartstatic int
547205959Slstewartalq_load_handler(module_t mod, int what, void *arg)
548205959Slstewart{
549205959Slstewart	int ret;
550205959Slstewart
551205959Slstewart	ret = 0;
552205959Slstewart
553205959Slstewart	switch (what) {
554205959Slstewart	case MOD_LOAD:
555205959Slstewart	case MOD_SHUTDOWN:
556205959Slstewart		break;
557205959Slstewart
558205959Slstewart	case MOD_QUIESCE:
559205959Slstewart		ALD_LOCK();
560205959Slstewart		/* Only allow unload if there are no open queues. */
561205959Slstewart		if (LIST_FIRST(&ald_queues) == NULL) {
562205959Slstewart			ald_shutingdown = 1;
563205959Slstewart			ALD_UNLOCK();
564205959Slstewart			ald_shutdown(NULL, 0);
565205959Slstewart			mtx_destroy(&ald_mtx);
566205959Slstewart		} else {
567205959Slstewart			ALD_UNLOCK();
568205959Slstewart			ret = EBUSY;
569205959Slstewart		}
570205959Slstewart		break;
571205959Slstewart
572205959Slstewart	case MOD_UNLOAD:
573205959Slstewart		/* If MOD_QUIESCE failed we must fail here too. */
574205959Slstewart		if (ald_shutingdown == 0)
575205959Slstewart			ret = EBUSY;
576205959Slstewart		break;
577205959Slstewart
578205959Slstewart	default:
579205959Slstewart		ret = EINVAL;
580205959Slstewart		break;
581205959Slstewart	}
582205959Slstewart
583205959Slstewart	return (ret);
584205959Slstewart}
585205959Slstewart
586205959Slstewartstatic moduledata_t alq_mod =
587205959Slstewart{
588205959Slstewart	"alq",
589205959Slstewart	alq_load_handler,
590205959Slstewart	NULL
591205959Slstewart};
592205959Slstewart
593205959SlstewartDECLARE_MODULE(alq, alq_mod, SI_SUB_SMP, SI_ORDER_ANY);
594205959SlstewartMODULE_VERSION(alq, 1);
595