kern_alq.c revision 103785
1103785Sjeff/* 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 * $FreeBSD: head/sys/kern/kern_alq.c 103785 2002-09-22 07:11:14Z jeff $ 27103785Sjeff * 28103785Sjeff */ 29103785Sjeff 30103785Sjeff#include <sys/param.h> 31103785Sjeff#include <sys/systm.h> 32103785Sjeff#include <sys/kernel.h> 33103785Sjeff#include <sys/kthread.h> 34103785Sjeff#include <sys/lock.h> 35103785Sjeff#include <sys/mutex.h> 36103785Sjeff#include <sys/namei.h> 37103785Sjeff#include <sys/proc.h> 38103785Sjeff#include <sys/vnode.h> 39103785Sjeff#include <sys/alq.h> 40103785Sjeff#include <sys/malloc.h> 41103785Sjeff#include <sys/unistd.h> 42103785Sjeff#include <sys/fcntl.h> 43103785Sjeff#include <sys/eventhandler.h> 44103785Sjeff 45103785Sjeff/* Async. Logging Queue */ 46103785Sjeffstruct alq { 47103785Sjeff int aq_entmax; /* Max entries */ 48103785Sjeff int aq_entlen; /* Entry length */ 49103785Sjeff char *aq_entbuf; /* Buffer for stored entries */ 50103785Sjeff int aq_flags; /* Queue flags */ 51103785Sjeff struct mtx aq_mtx; /* Queue lock */ 52103785Sjeff struct vnode *aq_vp; /* Open vnode handle */ 53103785Sjeff struct thread *aq_td; /* Thread that opened the vnode */ 54103785Sjeff struct ale *aq_first; /* First ent */ 55103785Sjeff struct ale *aq_entfree; /* First free ent */ 56103785Sjeff struct ale *aq_entvalid; /* First ent valid for writing */ 57103785Sjeff LIST_ENTRY(alq) aq_act; /* List of active queues */ 58103785Sjeff LIST_ENTRY(alq) aq_link; /* List of all queues */ 59103785Sjeff}; 60103785Sjeff 61103785Sjeff#define AQ_WANTED 0x0001 /* Wakeup sleeper when io is done */ 62103785Sjeff#define AQ_ACTIVE 0x0002 /* on the active list */ 63103785Sjeff#define AQ_FLUSHING 0x0004 /* doing IO */ 64103785Sjeff#define AQ_SHUTDOWN 0x0008 /* Queue no longer valid */ 65103785Sjeff 66103785Sjeff#define ALQ_LOCK(alq) mtx_lock_spin(&(alq)->aq_mtx) 67103785Sjeff#define ALQ_UNLOCK(alq) mtx_unlock_spin(&(alq)->aq_mtx) 68103785Sjeff 69103785Sjeffstatic MALLOC_DEFINE(M_ALD, "ALD", "ALD"); 70103785Sjeff 71103785Sjeff/* 72103785Sjeff * The ald_mtx protects the ald_queues list and the ald_active list. 73103785Sjeff */ 74103785Sjeffstatic struct mtx ald_mtx; 75103785Sjeffstatic LIST_HEAD(, alq) ald_queues; 76103785Sjeffstatic LIST_HEAD(, alq) ald_active; 77103785Sjeffstatic struct proc *ald_thread; 78103785Sjeffstatic int ald_shutingdown = 0; 79103785Sjeff 80103785Sjeff#define ALD_LOCK() mtx_lock(&ald_mtx) 81103785Sjeff#define ALD_UNLOCK() mtx_unlock(&ald_mtx) 82103785Sjeff 83103785Sjeff/* Daemon functions */ 84103785Sjeffstatic int ald_add(struct alq *); 85103785Sjeffstatic int ald_rem(struct alq *); 86103785Sjeffstatic void ald_startup(void *); 87103785Sjeffstatic void ald_daemon(void); 88103785Sjeffstatic void ald_shutdown(void *, int); 89103785Sjeffstatic void ald_activate(struct alq *); 90103785Sjeffstatic void ald_deactivate(struct alq *); 91103785Sjeff 92103785Sjeff/* Internal queue functions */ 93103785Sjeffstatic void alq_shutdown(struct alq *); 94103785Sjeffstatic int alq_doio(struct alq *); 95103785Sjeff 96103785Sjeff 97103785Sjeff/* 98103785Sjeff * Add a new queue to the global list. Fail if we're shutting down. 99103785Sjeff */ 100103785Sjeffstatic int 101103785Sjeffald_add(struct alq *alq) 102103785Sjeff{ 103103785Sjeff int error; 104103785Sjeff 105103785Sjeff error = 0; 106103785Sjeff 107103785Sjeff ALD_LOCK(); 108103785Sjeff if (ald_shutingdown) { 109103785Sjeff error = EBUSY; 110103785Sjeff goto done; 111103785Sjeff } 112103785Sjeff LIST_INSERT_HEAD(&ald_queues, alq, aq_link); 113103785Sjeffdone: 114103785Sjeff ALD_UNLOCK(); 115103785Sjeff return (error); 116103785Sjeff} 117103785Sjeff 118103785Sjeff/* 119103785Sjeff * Remove a queue from the global list unless we're shutting down. If so, 120103785Sjeff * the ald will take care of cleaning up it's resources. 121103785Sjeff */ 122103785Sjeffstatic int 123103785Sjeffald_rem(struct alq *alq) 124103785Sjeff{ 125103785Sjeff int error; 126103785Sjeff 127103785Sjeff error = 0; 128103785Sjeff 129103785Sjeff ALD_LOCK(); 130103785Sjeff if (ald_shutingdown) { 131103785Sjeff error = EBUSY; 132103785Sjeff goto done; 133103785Sjeff } 134103785Sjeff LIST_REMOVE(alq, aq_link); 135103785Sjeffdone: 136103785Sjeff ALD_UNLOCK(); 137103785Sjeff return (error); 138103785Sjeff} 139103785Sjeff 140103785Sjeff/* 141103785Sjeff * Put a queue on the active list. This will schedule it for writing. 142103785Sjeff */ 143103785Sjeffstatic void 144103785Sjeffald_activate(struct alq *alq) 145103785Sjeff{ 146103785Sjeff LIST_INSERT_HEAD(&ald_active, alq, aq_act); 147103785Sjeff wakeup(&ald_active); 148103785Sjeff} 149103785Sjeff 150103785Sjeffstatic void 151103785Sjeffald_deactivate(struct alq *alq) 152103785Sjeff{ 153103785Sjeff LIST_REMOVE(alq, aq_act); 154103785Sjeff alq->aq_flags &= ~AQ_ACTIVE; 155103785Sjeff} 156103785Sjeff 157103785Sjeffstatic void 158103785Sjeffald_startup(void *unused) 159103785Sjeff{ 160103785Sjeff mtx_init(&ald_mtx, "ALDmtx", NULL, MTX_DEF|MTX_QUIET); 161103785Sjeff LIST_INIT(&ald_queues); 162103785Sjeff LIST_INIT(&ald_active); 163103785Sjeff} 164103785Sjeff 165103785Sjeffstatic void 166103785Sjeffald_daemon(void) 167103785Sjeff{ 168103785Sjeff int needwakeup; 169103785Sjeff struct alq *alq; 170103785Sjeff 171103785Sjeff mtx_lock(&Giant); 172103785Sjeff 173103785Sjeff EVENTHANDLER_REGISTER(shutdown_pre_sync, ald_shutdown, NULL, 174103785Sjeff SHUTDOWN_PRI_FIRST); 175103785Sjeff 176103785Sjeff ALD_LOCK(); 177103785Sjeff 178103785Sjeff for (;;) { 179103785Sjeff while ((alq = LIST_FIRST(&ald_active)) == NULL) 180103785Sjeff msleep(&ald_active, &ald_mtx, PWAIT, "aldslp", 0); 181103785Sjeff 182103785Sjeff ALQ_LOCK(alq); 183103785Sjeff ald_deactivate(alq); 184103785Sjeff ALD_UNLOCK(); 185103785Sjeff needwakeup = alq_doio(alq); 186103785Sjeff ALQ_UNLOCK(alq); 187103785Sjeff if (needwakeup) 188103785Sjeff wakeup(alq); 189103785Sjeff ALD_LOCK(); 190103785Sjeff } 191103785Sjeff} 192103785Sjeff 193103785Sjeffstatic void 194103785Sjeffald_shutdown(void *arg, int howto) 195103785Sjeff{ 196103785Sjeff struct alq *alq; 197103785Sjeff 198103785Sjeff ALD_LOCK(); 199103785Sjeff ald_shutingdown = 1; 200103785Sjeff 201103785Sjeff while ((alq = LIST_FIRST(&ald_queues)) != NULL) { 202103785Sjeff LIST_REMOVE(alq, aq_link); 203103785Sjeff ALD_UNLOCK(); 204103785Sjeff alq_shutdown(alq); 205103785Sjeff ALD_LOCK(); 206103785Sjeff } 207103785Sjeff ALD_UNLOCK(); 208103785Sjeff} 209103785Sjeff 210103785Sjeffstatic void 211103785Sjeffalq_shutdown(struct alq *alq) 212103785Sjeff{ 213103785Sjeff ALQ_LOCK(alq); 214103785Sjeff 215103785Sjeff /* Stop any new writers. */ 216103785Sjeff alq->aq_flags |= AQ_SHUTDOWN; 217103785Sjeff 218103785Sjeff /* Drain IO */ 219103785Sjeff while (alq->aq_flags & (AQ_FLUSHING|AQ_ACTIVE)) { 220103785Sjeff alq->aq_flags |= AQ_WANTED; 221103785Sjeff ALQ_UNLOCK(alq); 222103785Sjeff tsleep(alq, PWAIT, "aldclose", 0); 223103785Sjeff ALQ_LOCK(alq); 224103785Sjeff } 225103785Sjeff ALQ_UNLOCK(alq); 226103785Sjeff 227103785Sjeff vn_close(alq->aq_vp, FREAD|FWRITE, alq->aq_td->td_ucred, 228103785Sjeff alq->aq_td); 229103785Sjeff} 230103785Sjeff 231103785Sjeff/* 232103785Sjeff * Flush all pending data to disk. This operation will block. 233103785Sjeff */ 234103785Sjeffstatic int 235103785Sjeffalq_doio(struct alq *alq) 236103785Sjeff{ 237103785Sjeff struct thread *td; 238103785Sjeff struct mount *mp; 239103785Sjeff struct vnode *vp; 240103785Sjeff struct uio auio; 241103785Sjeff struct iovec aiov[2]; 242103785Sjeff struct ale *ale; 243103785Sjeff struct ale *alstart; 244103785Sjeff int totlen; 245103785Sjeff int iov; 246103785Sjeff 247103785Sjeff vp = alq->aq_vp; 248103785Sjeff td = curthread; 249103785Sjeff totlen = 0; 250103785Sjeff iov = 0; 251103785Sjeff 252103785Sjeff alstart = ale = alq->aq_entvalid; 253103785Sjeff alq->aq_entvalid = NULL; 254103785Sjeff 255103785Sjeff bzero(&aiov, sizeof(aiov)); 256103785Sjeff bzero(&auio, sizeof(auio)); 257103785Sjeff 258103785Sjeff do { 259103785Sjeff if (aiov[iov].iov_base == NULL) 260103785Sjeff aiov[iov].iov_base = ale->ae_data; 261103785Sjeff aiov[iov].iov_len += alq->aq_entlen; 262103785Sjeff totlen += alq->aq_entlen; 263103785Sjeff /* Check to see if we're wrapping the buffer */ 264103785Sjeff if (ale->ae_data + alq->aq_entlen != ale->ae_next->ae_data) 265103785Sjeff iov++; 266103785Sjeff ale->ae_flags &= ~AE_VALID; 267103785Sjeff ale = ale->ae_next; 268103785Sjeff } while (ale->ae_flags & AE_VALID); 269103785Sjeff 270103785Sjeff alq->aq_flags |= AQ_FLUSHING; 271103785Sjeff ALQ_UNLOCK(alq); 272103785Sjeff 273103785Sjeff if (iov == 2 || aiov[iov].iov_base == NULL) 274103785Sjeff iov--; 275103785Sjeff 276103785Sjeff auio.uio_iov = &aiov[0]; 277103785Sjeff auio.uio_offset = 0; 278103785Sjeff auio.uio_segflg = UIO_SYSSPACE; 279103785Sjeff auio.uio_rw = UIO_WRITE; 280103785Sjeff auio.uio_iovcnt = iov + 1; 281103785Sjeff auio.uio_resid = totlen; 282103785Sjeff auio.uio_td = td; 283103785Sjeff 284103785Sjeff /* 285103785Sjeff * Do all of the junk required to write now. 286103785Sjeff */ 287103785Sjeff vn_start_write(vp, &mp, V_WAIT); 288103785Sjeff vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 289103785Sjeff VOP_LEASE(vp, td, td->td_ucred, LEASE_WRITE); 290103785Sjeff /* XXX error ignored */ 291103785Sjeff VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, td->td_ucred); 292103785Sjeff VOP_UNLOCK(vp, 0, td); 293103785Sjeff vn_finished_write(mp); 294103785Sjeff 295103785Sjeff ALQ_LOCK(alq); 296103785Sjeff alq->aq_flags &= ~AQ_FLUSHING; 297103785Sjeff 298103785Sjeff if (alq->aq_entfree == NULL) 299103785Sjeff alq->aq_entfree = alstart; 300103785Sjeff 301103785Sjeff if (alq->aq_flags & AQ_WANTED) { 302103785Sjeff alq->aq_flags &= ~AQ_WANTED; 303103785Sjeff return (1); 304103785Sjeff } 305103785Sjeff 306103785Sjeff return(0); 307103785Sjeff} 308103785Sjeff 309103785Sjeffstatic struct kproc_desc ald_kp = { 310103785Sjeff "ALQ Daemon", 311103785Sjeff ald_daemon, 312103785Sjeff &ald_thread 313103785Sjeff}; 314103785Sjeff 315103785SjeffSYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp) 316103785SjeffSYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL) 317103785Sjeff 318103785Sjeff 319103785Sjeff/* User visible queue functions */ 320103785Sjeff 321103785Sjeff/* 322103785Sjeff * Create the queue data structure, allocate the buffer, and open the file. 323103785Sjeff */ 324103785Sjeffint 325103785Sjeffalq_open(struct alq **alqp, const char *file, int size, int count) 326103785Sjeff{ 327103785Sjeff struct thread *td; 328103785Sjeff struct nameidata nd; 329103785Sjeff struct ale *ale; 330103785Sjeff struct ale *alp; 331103785Sjeff struct alq *alq; 332103785Sjeff char *bufp; 333103785Sjeff int flags; 334103785Sjeff int error; 335103785Sjeff int i; 336103785Sjeff 337103785Sjeff *alqp = NULL; 338103785Sjeff td = curthread; 339103785Sjeff 340103785Sjeff NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, file, td); 341103785Sjeff flags = FREAD | FWRITE | O_NOFOLLOW | O_CREAT; 342103785Sjeff 343103785Sjeff error = vn_open(&nd, &flags, 0); 344103785Sjeff if (error) 345103785Sjeff return (error); 346103785Sjeff 347103785Sjeff NDFREE(&nd, NDF_ONLY_PNBUF); 348103785Sjeff /* We just unlock so we hold a reference */ 349103785Sjeff VOP_UNLOCK(nd.ni_vp, 0, td); 350103785Sjeff 351103785Sjeff alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO); 352103785Sjeff alq->aq_entbuf = malloc(count * size, M_ALD, M_WAITOK|M_ZERO); 353103785Sjeff alq->aq_first = malloc(sizeof(*ale) * count, M_ALD, M_WAITOK|M_ZERO); 354103785Sjeff alq->aq_vp = nd.ni_vp; 355103785Sjeff alq->aq_td = td; 356103785Sjeff alq->aq_entmax = count; 357103785Sjeff alq->aq_entlen = size; 358103785Sjeff alq->aq_entfree = alq->aq_first; 359103785Sjeff 360103785Sjeff mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET); 361103785Sjeff 362103785Sjeff bufp = alq->aq_entbuf; 363103785Sjeff ale = alq->aq_first; 364103785Sjeff alp = NULL; 365103785Sjeff 366103785Sjeff /* Match up entries with buffers */ 367103785Sjeff for (i = 0; i < count; i++) { 368103785Sjeff if (alp) 369103785Sjeff alp->ae_next = ale; 370103785Sjeff ale->ae_data = bufp; 371103785Sjeff alp = ale; 372103785Sjeff ale++; 373103785Sjeff bufp += size; 374103785Sjeff } 375103785Sjeff 376103785Sjeff alp->ae_next = alq->aq_first; 377103785Sjeff 378103785Sjeff if ((error = ald_add(alq)) != 0) 379103785Sjeff return (error); 380103785Sjeff *alqp = alq; 381103785Sjeff 382103785Sjeff return (0); 383103785Sjeff} 384103785Sjeff 385103785Sjeff/* 386103785Sjeff * Copy a new entry into the queue. If the operation would block either 387103785Sjeff * wait or return an error depending on the value of waitok. 388103785Sjeff */ 389103785Sjeffint 390103785Sjeffalq_write(struct alq *alq, void *data, int waitok) 391103785Sjeff{ 392103785Sjeff struct ale *ale; 393103785Sjeff 394103785Sjeff if ((ale = alq_get(alq, waitok)) == NULL) 395103785Sjeff return (EWOULDBLOCK); 396103785Sjeff 397103785Sjeff bcopy(data, ale->ae_data, alq->aq_entlen); 398103785Sjeff alq_post(alq, ale); 399103785Sjeff 400103785Sjeff return (0); 401103785Sjeff} 402103785Sjeff 403103785Sjeffstruct ale * 404103785Sjeffalq_get(struct alq *alq, int waitok) 405103785Sjeff{ 406103785Sjeff struct ale *ale; 407103785Sjeff struct ale *aln; 408103785Sjeff 409103785Sjeff ale = NULL; 410103785Sjeff 411103785Sjeff ALQ_LOCK(alq); 412103785Sjeff 413103785Sjeff /* Loop until we get an entry or we're shutting down */ 414103785Sjeff while ((alq->aq_flags & AQ_SHUTDOWN) == 0 && 415103785Sjeff (ale = alq->aq_entfree) == NULL && 416103785Sjeff (waitok & ALQ_WAITOK)) { 417103785Sjeff alq->aq_flags |= AQ_WANTED; 418103785Sjeff ALQ_UNLOCK(alq); 419103785Sjeff tsleep(alq, PWAIT, "alqget", 0); 420103785Sjeff ALQ_LOCK(alq); 421103785Sjeff } 422103785Sjeff 423103785Sjeff if (ale != NULL) { 424103785Sjeff aln = ale->ae_next; 425103785Sjeff if ((aln->ae_flags & AE_VALID) == 0) 426103785Sjeff alq->aq_entfree = aln; 427103785Sjeff } else 428103785Sjeff ALQ_UNLOCK(alq); 429103785Sjeff 430103785Sjeff 431103785Sjeff return (ale); 432103785Sjeff} 433103785Sjeff 434103785Sjeffvoid 435103785Sjeffalq_post(struct alq *alq, struct ale *ale) 436103785Sjeff{ 437103785Sjeff int activate; 438103785Sjeff 439103785Sjeff ale->ae_flags |= AE_VALID; 440103785Sjeff 441103785Sjeff if (alq->aq_entvalid == NULL) 442103785Sjeff alq->aq_entvalid = ale; 443103785Sjeff 444103785Sjeff if ((alq->aq_flags & AQ_ACTIVE) == 0) { 445103785Sjeff alq->aq_flags |= AQ_ACTIVE; 446103785Sjeff activate = 1; 447103785Sjeff } else 448103785Sjeff activate = 0; 449103785Sjeff 450103785Sjeff ALQ_UNLOCK(alq); 451103785Sjeff if (activate) { 452103785Sjeff ALD_LOCK(); 453103785Sjeff ald_activate(alq); 454103785Sjeff ALD_UNLOCK(); 455103785Sjeff } 456103785Sjeff} 457103785Sjeff 458103785Sjeffvoid 459103785Sjeffalq_flush(struct alq *alq) 460103785Sjeff{ 461103785Sjeff int needwakeup = 0; 462103785Sjeff 463103785Sjeff ALD_LOCK(); 464103785Sjeff ALQ_LOCK(alq); 465103785Sjeff if (alq->aq_flags & AQ_ACTIVE) { 466103785Sjeff ald_deactivate(alq); 467103785Sjeff ALD_UNLOCK(); 468103785Sjeff needwakeup = alq_doio(alq); 469103785Sjeff } else 470103785Sjeff ALD_UNLOCK(); 471103785Sjeff ALQ_UNLOCK(alq); 472103785Sjeff 473103785Sjeff if (needwakeup) 474103785Sjeff wakeup(alq); 475103785Sjeff} 476103785Sjeff 477103785Sjeff/* 478103785Sjeff * Flush remaining data, close the file and free all resources. 479103785Sjeff */ 480103785Sjeffvoid 481103785Sjeffalq_close(struct alq *alq) 482103785Sjeff{ 483103785Sjeff /* 484103785Sjeff * If we're already shuting down someone else will flush and close 485103785Sjeff * the vnode. 486103785Sjeff */ 487103785Sjeff if (ald_rem(alq) != 0) 488103785Sjeff return; 489103785Sjeff 490103785Sjeff /* 491103785Sjeff * Drain all pending IO. 492103785Sjeff */ 493103785Sjeff alq_shutdown(alq); 494103785Sjeff 495103785Sjeff mtx_destroy(&alq->aq_mtx); 496103785Sjeff free(alq->aq_first, M_ALD); 497103785Sjeff free(alq->aq_entbuf, M_ALD); 498103785Sjeff free(alq, M_ALD); 499103785Sjeff} 500