2 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/kthread.h>
35 #include <sys/mutex.h>
36 #include <sys/namei.h>
38 #include <sys/vnode.h>
40 #include <sys/malloc.h>
41 #include <sys/unistd.h>
42 #include <sys/fcntl.h>
43 #include <sys/eventhandler.h>
45 /* Async. Logging Queue */
47 int aq_entmax; /* Max entries */
48 int aq_entlen; /* Entry length */
49 char *aq_entbuf; /* Buffer for stored entries */
50 int aq_flags; /* Queue flags */
51 struct mtx aq_mtx; /* Queue lock */
52 struct vnode *aq_vp; /* Open vnode handle */
53 struct ucred *aq_cred; /* Credentials of the opening thread */
54 struct ale *aq_first; /* First ent */
55 struct ale *aq_entfree; /* First free ent */
56 struct ale *aq_entvalid; /* First ent valid for writing */
57 LIST_ENTRY(alq) aq_act; /* List of active queues */
58 LIST_ENTRY(alq) aq_link; /* List of all queues */
61 #define AQ_WANTED 0x0001 /* Wakeup sleeper when io is done */
62 #define AQ_ACTIVE 0x0002 /* on the active list */
63 #define AQ_FLUSHING 0x0004 /* doing IO */
64 #define AQ_SHUTDOWN 0x0008 /* Queue no longer valid */
66 #define ALQ_LOCK(alq) mtx_lock_spin(&(alq)->aq_mtx)
67 #define ALQ_UNLOCK(alq) mtx_unlock_spin(&(alq)->aq_mtx)
69 static MALLOC_DEFINE(M_ALD, "ALD", "ALD");
72 * The ald_mtx protects the ald_queues list and the ald_active list.
74 static struct mtx ald_mtx;
75 static LIST_HEAD(, alq) ald_queues;
76 static LIST_HEAD(, alq) ald_active;
77 static int ald_shutingdown = 0;
78 struct thread *ald_thread;
79 static struct proc *ald_proc;
81 #define ALD_LOCK() mtx_lock(&ald_mtx)
82 #define ALD_UNLOCK() mtx_unlock(&ald_mtx)
84 /* Daemon functions */
85 static int ald_add(struct alq *);
86 static int ald_rem(struct alq *);
87 static void ald_startup(void *);
88 static void ald_daemon(void);
89 static void ald_shutdown(void *, int);
90 static void ald_activate(struct alq *);
91 static void ald_deactivate(struct alq *);
93 /* Internal queue functions */
94 static void alq_shutdown(struct alq *);
95 static int alq_doio(struct alq *);
99 * Add a new queue to the global list. Fail if we're shutting down.
102 ald_add(struct alq *alq)
109 if (ald_shutingdown) {
113 LIST_INSERT_HEAD(&ald_queues, alq, aq_link);
120 * Remove a queue from the global list unless we're shutting down. If so,
121 * the ald will take care of cleaning up it's resources.
124 ald_rem(struct alq *alq)
131 if (ald_shutingdown) {
135 LIST_REMOVE(alq, aq_link);
142 * Put a queue on the active list. This will schedule it for writing.
145 ald_activate(struct alq *alq)
147 LIST_INSERT_HEAD(&ald_active, alq, aq_act);
152 ald_deactivate(struct alq *alq)
154 LIST_REMOVE(alq, aq_act);
155 alq->aq_flags &= ~AQ_ACTIVE;
159 ald_startup(void *unused)
161 mtx_init(&ald_mtx, "ALDmtx", NULL, MTX_DEF|MTX_QUIET);
162 LIST_INIT(&ald_queues);
163 LIST_INIT(&ald_active);
174 ald_thread = FIRST_THREAD_IN_PROC(ald_proc);
176 EVENTHANDLER_REGISTER(shutdown_pre_sync, ald_shutdown, NULL,
182 while ((alq = LIST_FIRST(&ald_active)) == NULL)
183 msleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0);
188 needwakeup = alq_doio(alq);
197 ald_shutdown(void *arg, int howto)
204 while ((alq = LIST_FIRST(&ald_queues)) != NULL) {
205 LIST_REMOVE(alq, aq_link);
214 alq_shutdown(struct alq *alq)
218 /* Stop any new writers. */
219 alq->aq_flags |= AQ_SHUTDOWN;
222 while (alq->aq_flags & (AQ_FLUSHING|AQ_ACTIVE)) {
223 alq->aq_flags |= AQ_WANTED;
225 tsleep(alq, PWAIT, "aldclose", 0);
230 vn_close(alq->aq_vp, FWRITE, alq->aq_cred,
232 crfree(alq->aq_cred);
236 * Flush all pending data to disk. This operation will block.
239 alq_doio(struct alq *alq)
245 struct iovec aiov[2];
256 alstart = ale = alq->aq_entvalid;
257 alq->aq_entvalid = NULL;
259 bzero(&aiov, sizeof(aiov));
260 bzero(&auio, sizeof(auio));
263 if (aiov[iov].iov_base == NULL)
264 aiov[iov].iov_base = ale->ae_data;
265 aiov[iov].iov_len += alq->aq_entlen;
266 totlen += alq->aq_entlen;
267 /* Check to see if we're wrapping the buffer */
268 if (ale->ae_data + alq->aq_entlen != ale->ae_next->ae_data)
270 ale->ae_flags &= ~AE_VALID;
272 } while (ale->ae_flags & AE_VALID);
274 alq->aq_flags |= AQ_FLUSHING;
277 if (iov == 2 || aiov[iov].iov_base == NULL)
280 auio.uio_iov = &aiov[0];
282 auio.uio_segflg = UIO_SYSSPACE;
283 auio.uio_rw = UIO_WRITE;
284 auio.uio_iovcnt = iov + 1;
285 auio.uio_resid = totlen;
289 * Do all of the junk required to write now.
291 vn_start_write(vp, &mp, V_WAIT);
292 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
293 VOP_LEASE(vp, td, alq->aq_cred, LEASE_WRITE);
294 /* XXX error ignored */
295 VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred);
296 VOP_UNLOCK(vp, 0, td);
297 vn_finished_write(mp);
300 alq->aq_flags &= ~AQ_FLUSHING;
302 if (alq->aq_entfree == NULL)
303 alq->aq_entfree = alstart;
305 if (alq->aq_flags & AQ_WANTED) {
306 alq->aq_flags &= ~AQ_WANTED;
313 static struct kproc_desc ald_kp = {
319 SYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp)
320 SYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL)
323 /* User visible queue functions */
326 * Create the queue data structure, allocate the buffer, and open the file.
329 alq_open(struct alq **alqp, const char *file, int size, int count)
344 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, file, td);
345 flags = FWRITE | O_NOFOLLOW | O_CREAT;
347 error = vn_open(&nd, &flags, 0);
351 NDFREE(&nd, NDF_ONLY_PNBUF);
352 /* We just unlock so we hold a reference */
353 VOP_UNLOCK(nd.ni_vp, 0, td);
355 alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO);
356 alq->aq_entbuf = malloc(count * size, M_ALD, M_WAITOK|M_ZERO);
357 alq->aq_first = malloc(sizeof(*ale) * count, M_ALD, M_WAITOK|M_ZERO);
358 alq->aq_vp = nd.ni_vp;
359 alq->aq_cred = crhold(td->td_ucred);
360 alq->aq_entmax = count;
361 alq->aq_entlen = size;
362 alq->aq_entfree = alq->aq_first;
364 mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET);
366 bufp = alq->aq_entbuf;
370 /* Match up entries with buffers */
371 for (i = 0; i < count; i++) {
380 alp->ae_next = alq->aq_first;
382 if ((error = ald_add(alq)) != 0)
390 * Copy a new entry into the queue. If the operation would block either
391 * wait or return an error depending on the value of waitok.
394 alq_write(struct alq *alq, void *data, int waitok)
398 if ((ale = alq_get(alq, waitok)) == NULL)
399 return (EWOULDBLOCK);
401 bcopy(data, ale->ae_data, alq->aq_entlen);
408 alq_get(struct alq *alq, int waitok)
417 /* Loop until we get an entry or we're shutting down */
418 while ((alq->aq_flags & AQ_SHUTDOWN) == 0 &&
419 (ale = alq->aq_entfree) == NULL &&
420 (waitok & ALQ_WAITOK)) {
421 alq->aq_flags |= AQ_WANTED;
423 tsleep(alq, PWAIT, "alqget", 0);
429 if ((aln->ae_flags & AE_VALID) == 0)
430 alq->aq_entfree = aln;
439 alq_post(struct alq *alq, struct ale *ale)
443 ale->ae_flags |= AE_VALID;
445 if (alq->aq_entvalid == NULL)
446 alq->aq_entvalid = ale;
448 if ((alq->aq_flags & AQ_ACTIVE) == 0) {
449 alq->aq_flags |= AQ_ACTIVE;
463 alq_flush(struct alq *alq)
469 if (alq->aq_flags & AQ_ACTIVE) {
472 needwakeup = alq_doio(alq);
482 * Flush remaining data, close the file and free all resources.
485 alq_close(struct alq *alq)
488 * If we're already shuting down someone else will flush and close
491 if (ald_rem(alq) != 0)
495 * Drain all pending IO.
499 mtx_destroy(&alq->aq_mtx);
500 free(alq->aq_first, M_ALD);
501 free(alq->aq_entbuf, M_ALD);