1139825Simp/*-
2103785Sjeff * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
3207223Slstewart * Copyright (c) 2008-2009, Lawrence Stewart <lstewart@freebsd.org>
4207223Slstewart * Copyright (c) 2010, The FreeBSD Foundation
5103785Sjeff * All rights reserved.
6103785Sjeff *
7207223Slstewart * Portions of this software were developed at the Centre for Advanced
8207223Slstewart * Internet Architectures, Swinburne University of Technology, Melbourne,
9207223Slstewart * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation.
10207223Slstewart *
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 * $FreeBSD$
33103785Sjeff *
34103785Sjeff */
35103812Sjeff#ifndef _SYS_ALQ_H_
36103812Sjeff#define	_SYS_ALQ_H_
37103785Sjeff
38103785Sjeff/*
39103785Sjeff * Opaque type for the Async. Logging Queue
40103785Sjeff */
41103785Sjeffstruct alq;
42103785Sjeff
43103995Sjeff/* The thread for the logging daemon */
44103995Sjeffextern struct thread *ald_thread;
45103995Sjeff
46103785Sjeff/*
47103785Sjeff * Async. Logging Entry
48103785Sjeff */
49103785Sjeffstruct ale {
50207223Slstewart	intptr_t	ae_bytesused;	/* # bytes written to ALE. */
51207223Slstewart	char		*ae_data;	/* Write ptr. */
52207223Slstewart	int		ae_pad;		/* Unused, compat. */
53103785Sjeff};
54103785Sjeff
55207223Slstewart/* Flag options. */
56207223Slstewart#define	ALQ_NOWAIT	0x0001	/* ALQ may not sleep. */
57207223Slstewart#define	ALQ_WAITOK	0x0002	/* ALQ may sleep. */
58207223Slstewart#define	ALQ_NOACTIVATE	0x0004	/* Don't activate ALQ after write. */
59207223Slstewart#define	ALQ_ORDERED	0x0010	/* Maintain write ordering between threads. */
60103785Sjeff
61145142Srwatson/* Suggested mode for file creation. */
62145142Srwatson#define	ALQ_DEFAULT_CMODE	0600
63145142Srwatson
64103785Sjeff/*
65207223Slstewart * alq_open_flags:  Creates a new queue
66103785Sjeff *
67103785Sjeff * Arguments:
68103785Sjeff *	alq	Storage for a pointer to the newly created queue.
69103785Sjeff *	file	The filename to open for logging.
70145142Srwatson *	cred	Credential to authorize open and I/O with.
71145142Srwatson *	cmode	Creation mode for file, if new.
72207223Slstewart *	size	The size of the queue in bytes.
73207223Slstewart *	flags	ALQ_ORDERED
74103785Sjeff * Returns:
75103785Sjeff *	error from open or 0 on success
76103785Sjeff */
77116697Srwatsonstruct ucred;
78207223Slstewartint alq_open_flags(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
79207223Slstewart	    int size, int flags);
80207223Slstewartint alq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode,
81145142Srwatson	    int size, int count);
82103785Sjeff
83103785Sjeff/*
84207223Slstewart * alq_writen:  Write data into the queue
85103785Sjeff *
86103785Sjeff * Arguments:
87103785Sjeff *	alq	The queue we're writing to
88103785Sjeff *	data	The entry to be recorded
89207223Slstewart *	len	The number of bytes to write from *data
90207223Slstewart *	flags	(ALQ_NOWAIT || ALQ_WAITOK), ALQ_NOACTIVATE
91103785Sjeff *
92103785Sjeff * Returns:
93103785Sjeff *	EWOULDBLOCK if:
94103785Sjeff *		Waitok is ALQ_NOWAIT and the queue is full.
95103785Sjeff *		The system is shutting down.
96103785Sjeff *	0 on success.
97103785Sjeff */
98207223Slstewartint alq_writen(struct alq *alq, void *data, int len, int flags);
99207223Slstewartint alq_write(struct alq *alq, void *data, int flags);
100103785Sjeff
101103785Sjeff/*
102103785Sjeff * alq_flush:  Flush the queue out to disk
103103785Sjeff */
104103785Sjeffvoid alq_flush(struct alq *alq);
105103785Sjeff
106103785Sjeff/*
107103785Sjeff * alq_close:  Flush the queue and free all resources.
108103785Sjeff */
109103785Sjeffvoid alq_close(struct alq *alq);
110103785Sjeff
111103785Sjeff/*
112207223Slstewart * alq_getn:  Return an entry for direct access
113103785Sjeff *
114103785Sjeff * Arguments:
115103785Sjeff *	alq	The queue to retrieve an entry from
116207223Slstewart *	len	Max number of bytes required
117207223Slstewart *	flags	(ALQ_NOWAIT || ALQ_WAITOK)
118103785Sjeff *
119103785Sjeff * Returns:
120103785Sjeff *	The next available ale on success.
121103785Sjeff *	NULL if:
122207223Slstewart *		flags is ALQ_NOWAIT and the queue is full.
123103785Sjeff *		The system is shutting down.
124103785Sjeff *
125103785Sjeff * This leaves the queue locked until a subsequent alq_post.
126103785Sjeff */
127207223Slstewartstruct ale *alq_getn(struct alq *alq, int len, int flags);
128207223Slstewartstruct ale *alq_get(struct alq *alq, int flags);
129103785Sjeff
130103785Sjeff/*
131207223Slstewart * alq_post_flags:  Schedule the ale retrieved by alq_get/alq_getn for writing.
132103785Sjeff *	alq	The queue to post the entry to.
133103785Sjeff *	ale	An asynch logging entry returned by alq_get.
134207223Slstewart *	flags	ALQ_NOACTIVATE
135103785Sjeff */
136207223Slstewartvoid alq_post_flags(struct alq *alq, struct ale *ale, int flags);
137103785Sjeff
138207223Slstewartstatic __inline void
139207223Slstewartalq_post(struct alq *alq, struct ale *ale)
140207223Slstewart{
141207223Slstewart	alq_post_flags(alq, ale, 0);
142207223Slstewart}
143207223Slstewart
144103812Sjeff#endif	/* _SYS_ALQ_H_ */
145