| 1 | /*- |
| 2 | * SPDX-License-Identifier: BSD-2-Clause |
| 3 | * |
| 4 | * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org> |
| 5 | * Copyright (c) 2008-2009, Lawrence Stewart <lstewart@freebsd.org> |
| 6 | * Copyright (c) 2010, The FreeBSD Foundation |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Portions of this software were developed at the Centre for Advanced |
| 10 | * Internet Architectures, Swinburne University of Technology, Melbourne, |
| 11 | * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation. |
| 12 | * |
| 13 | * Redistribution and use in source and binary forms, with or without |
| 14 | * modification, are permitted provided that the following conditions |
| 15 | * are met: |
| 16 | * 1. Redistributions of source code must retain the above copyright |
| 17 | * notice unmodified, this list of conditions, and the following |
| 18 | * disclaimer. |
| 19 | * 2. Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in the |
| 21 | * documentation and/or other materials provided with the distribution. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 24 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 25 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 26 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | * |
| 34 | */ |
| 35 | #ifndef _SYS_ALQ_H_ |
| 36 | #define	_SYS_ALQ_H_ |
| 37 | |
| 38 | #ifdef _KERNEL |
| 39 | /* |
| 40 | * Opaque type for the Async. Logging Queue |
| 41 | */ |
| 42 | struct alq; |
| 43 | |
| 44 | /* The thread for the logging daemon */ |
| 45 | extern struct thread *ald_thread; |
| 46 | |
| 47 | /* |
| 48 | * Async. Logging Entry |
| 49 | */ |
| 50 | struct ale { |
| 51 | 	intptr_t	ae_bytesused;	/* # bytes written to ALE. */ |
| 52 | 	char		*ae_data;	/* Write ptr. */ |
| 53 | 	int		ae_pad;		/* Unused, compat. */ |
| 54 | }; |
| 55 | |
| 56 | /* Flag options. */ |
| 57 | #define	ALQ_NOWAIT	0x0001	/* ALQ may not sleep. */ |
| 58 | #define	ALQ_WAITOK	0x0002	/* ALQ may sleep. */ |
| 59 | #define	ALQ_NOACTIVATE	0x0004	/* Don't activate ALQ after write. */ |
| 60 | #define	ALQ_ORDERED	0x0010	/* Maintain write ordering between threads. */ |
| 61 | |
| 62 | /* Suggested mode for file creation. */ |
| 63 | #define	ALQ_DEFAULT_CMODE	0600 |
| 64 | |
| 65 | /* |
| 66 | * alq_open_flags: Creates a new queue |
| 67 | * |
| 68 | * Arguments: |
| 69 | *	alq	Storage for a pointer to the newly created queue. |
| 70 | *	file	The filename to open for logging. |
| 71 | *	cred	Credential to authorize open and I/O with. |
| 72 | *	cmode	Creation mode for file, if new. |
| 73 | *	size	The size of the queue in bytes. |
| 74 | *	flags	ALQ_ORDERED |
| 75 | * Returns: |
| 76 | *	error from open or 0 on success |
| 77 | */ |
| 78 | struct ucred; |
| 79 | int alq_open_flags(struct alq **alqp, const char *file, struct ucred *cred, int cmode, |
| 80 | 	 int size, int flags); |
| 81 | int alq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode, |
| 82 | 	 int size, int count); |
| 83 | |
| 84 | /* |
| 85 | * alq_writen: Write data into the queue |
| 86 | * |
| 87 | * Arguments: |
| 88 | *	alq	The queue we're writing to |
| 89 | *	data	The entry to be recorded |
| 90 | *	len	The number of bytes to write from *data |
| 91 | *	flags	(ALQ_NOWAIT || ALQ_WAITOK), ALQ_NOACTIVATE |
| 92 | * |
| 93 | * Returns: |
| 94 | *	EWOULDBLOCK if: |
| 95 | *		Waitok is ALQ_NOWAIT and the queue is full. |
| 96 | *		The system is shutting down. |
| 97 | *	0 on success. |
| 98 | */ |
| 99 | int alq_writen(struct alq *alq, void *data, int len, int flags); |
| 100 | int alq_write(struct alq *alq, void *data, int flags); |
| 101 | |
| 102 | /* |
| 103 | * alq_flush: Flush the queue out to disk |
| 104 | */ |
| 105 | void alq_flush(struct alq *alq); |
| 106 | |
| 107 | /* |
| 108 | * alq_close: Flush the queue and free all resources. |
| 109 | */ |
| 110 | void alq_close(struct alq *alq); |
| 111 | |
| 112 | /* |
| 113 | * alq_getn: Return an entry for direct access |
| 114 | * |
| 115 | * Arguments: |
| 116 | *	alq	The queue to retrieve an entry from |
| 117 | *	len	Max number of bytes required |
| 118 | *	flags	(ALQ_NOWAIT || ALQ_WAITOK) |
| 119 | * |
| 120 | * Returns: |
| 121 | *	The next available ale on success. |
| 122 | *	NULL if: |
| 123 | *		flags is ALQ_NOWAIT and the queue is full. |
| 124 | *		The system is shutting down. |
| 125 | * |
| 126 | * This leaves the queue locked until a subsequent alq_post. |
| 127 | */ |
| 128 | struct ale *alq_getn(struct alq *alq, int len, int flags); |
| 129 | struct ale *alq_get(struct alq *alq, int flags); |
| 130 | |
| 131 | /* |
| 132 | * alq_post_flags: Schedule the ale retrieved by alq_get/alq_getn for writing. |
| 133 | *	alq	The queue to post the entry to. |
| 134 | *	ale	An asynch logging entry returned by alq_get. |
| 135 | *	flags	ALQ_NOACTIVATE |
| 136 | */ |
| 137 | void alq_post_flags(struct alq *alq, struct ale *ale, int flags); |
| 138 | |
| 139 | static __inline void |
| 140 | alq_post(struct alq *alq, struct ale *ale) |
| 141 | { |
| 142 | 	alq_post_flags(alq, ale, 0); |
| 143 | } |
| 144 | |
| 145 | #endif	/* _KERNEL */ |
| 146 | #endif	/* _SYS_ALQ_H_ */ |