kern_alq.c revision 103830
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 103830 2002-09-23 05:20:00Z 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 */ 53103830Sjeff struct ucred *aq_cred; /* Credentials of the opening thread */ 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 227103830Sjeff vn_close(alq->aq_vp, FREAD|FWRITE, alq->aq_cred, 228103830Sjeff curthread); 229103830Sjeff crfree(alq->aq_cred); 230103785Sjeff} 231103785Sjeff 232103785Sjeff/* 233103785Sjeff * Flush all pending data to disk. This operation will block. 234103785Sjeff */ 235103785Sjeffstatic int 236103785Sjeffalq_doio(struct alq *alq) 237103785Sjeff{ 238103785Sjeff struct thread *td; 239103785Sjeff struct mount *mp; 240103785Sjeff struct vnode *vp; 241103785Sjeff struct uio auio; 242103785Sjeff struct iovec aiov[2]; 243103785Sjeff struct ale *ale; 244103785Sjeff struct ale *alstart; 245103785Sjeff int totlen; 246103785Sjeff int iov; 247103785Sjeff 248103785Sjeff vp = alq->aq_vp; 249103785Sjeff td = curthread; 250103785Sjeff totlen = 0; 251103785Sjeff iov = 0; 252103785Sjeff 253103785Sjeff alstart = ale = alq->aq_entvalid; 254103785Sjeff alq->aq_entvalid = NULL; 255103785Sjeff 256103785Sjeff bzero(&aiov, sizeof(aiov)); 257103785Sjeff bzero(&auio, sizeof(auio)); 258103785Sjeff 259103785Sjeff do { 260103785Sjeff if (aiov[iov].iov_base == NULL) 261103785Sjeff aiov[iov].iov_base = ale->ae_data; 262103785Sjeff aiov[iov].iov_len += alq->aq_entlen; 263103785Sjeff totlen += alq->aq_entlen; 264103785Sjeff /* Check to see if we're wrapping the buffer */ 265103785Sjeff if (ale->ae_data + alq->aq_entlen != ale->ae_next->ae_data) 266103785Sjeff iov++; 267103785Sjeff ale->ae_flags &= ~AE_VALID; 268103785Sjeff ale = ale->ae_next; 269103785Sjeff } while (ale->ae_flags & AE_VALID); 270103785Sjeff 271103785Sjeff alq->aq_flags |= AQ_FLUSHING; 272103785Sjeff ALQ_UNLOCK(alq); 273103785Sjeff 274103785Sjeff if (iov == 2 || aiov[iov].iov_base == NULL) 275103785Sjeff iov--; 276103785Sjeff 277103785Sjeff auio.uio_iov = &aiov[0]; 278103785Sjeff auio.uio_offset = 0; 279103785Sjeff auio.uio_segflg = UIO_SYSSPACE; 280103785Sjeff auio.uio_rw = UIO_WRITE; 281103785Sjeff auio.uio_iovcnt = iov + 1; 282103785Sjeff auio.uio_resid = totlen; 283103785Sjeff auio.uio_td = td; 284103785Sjeff 285103785Sjeff /* 286103785Sjeff * Do all of the junk required to write now. 287103785Sjeff */ 288103785Sjeff vn_start_write(vp, &mp, V_WAIT); 289103785Sjeff vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 290103830Sjeff VOP_LEASE(vp, td, alq->aq_cred, LEASE_WRITE); 291103785Sjeff /* XXX error ignored */ 292103830Sjeff VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred); 293103785Sjeff VOP_UNLOCK(vp, 0, td); 294103785Sjeff vn_finished_write(mp); 295103785Sjeff 296103785Sjeff ALQ_LOCK(alq); 297103785Sjeff alq->aq_flags &= ~AQ_FLUSHING; 298103785Sjeff 299103785Sjeff if (alq->aq_entfree == NULL) 300103785Sjeff alq->aq_entfree = alstart; 301103785Sjeff 302103785Sjeff if (alq->aq_flags & AQ_WANTED) { 303103785Sjeff alq->aq_flags &= ~AQ_WANTED; 304103785Sjeff return (1); 305103785Sjeff } 306103785Sjeff 307103785Sjeff return(0); 308103785Sjeff} 309103785Sjeff 310103785Sjeffstatic struct kproc_desc ald_kp = { 311103785Sjeff "ALQ Daemon", 312103785Sjeff ald_daemon, 313103785Sjeff &ald_thread 314103785Sjeff}; 315103785Sjeff 316103785SjeffSYSINIT(aldthread, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &ald_kp) 317103785SjeffSYSINIT(ald, SI_SUB_LOCK, SI_ORDER_ANY, ald_startup, NULL) 318103785Sjeff 319103785Sjeff 320103785Sjeff/* User visible queue functions */ 321103785Sjeff 322103785Sjeff/* 323103785Sjeff * Create the queue data structure, allocate the buffer, and open the file. 324103785Sjeff */ 325103785Sjeffint 326103785Sjeffalq_open(struct alq **alqp, const char *file, int size, int count) 327103785Sjeff{ 328103785Sjeff struct thread *td; 329103785Sjeff struct nameidata nd; 330103785Sjeff struct ale *ale; 331103785Sjeff struct ale *alp; 332103785Sjeff struct alq *alq; 333103785Sjeff char *bufp; 334103785Sjeff int flags; 335103785Sjeff int error; 336103785Sjeff int i; 337103785Sjeff 338103785Sjeff *alqp = NULL; 339103785Sjeff td = curthread; 340103785Sjeff 341103785Sjeff NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, file, td); 342103785Sjeff flags = FREAD | FWRITE | O_NOFOLLOW | O_CREAT; 343103785Sjeff 344103785Sjeff error = vn_open(&nd, &flags, 0); 345103785Sjeff if (error) 346103785Sjeff return (error); 347103785Sjeff 348103785Sjeff NDFREE(&nd, NDF_ONLY_PNBUF); 349103785Sjeff /* We just unlock so we hold a reference */ 350103785Sjeff VOP_UNLOCK(nd.ni_vp, 0, td); 351103785Sjeff 352103785Sjeff alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO); 353103785Sjeff alq->aq_entbuf = malloc(count * size, M_ALD, M_WAITOK|M_ZERO); 354103785Sjeff alq->aq_first = malloc(sizeof(*ale) * count, M_ALD, M_WAITOK|M_ZERO); 355103785Sjeff alq->aq_vp = nd.ni_vp; 356103830Sjeff alq->aq_cred = crhold(td->td_ucred); 357103785Sjeff alq->aq_entmax = count; 358103785Sjeff alq->aq_entlen = size; 359103785Sjeff alq->aq_entfree = alq->aq_first; 360103785Sjeff 361103785Sjeff mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET); 362103785Sjeff 363103785Sjeff bufp = alq->aq_entbuf; 364103785Sjeff ale = alq->aq_first; 365103785Sjeff alp = NULL; 366103785Sjeff 367103785Sjeff /* Match up entries with buffers */ 368103785Sjeff for (i = 0; i < count; i++) { 369103785Sjeff if (alp) 370103785Sjeff alp->ae_next = ale; 371103785Sjeff ale->ae_data = bufp; 372103785Sjeff alp = ale; 373103785Sjeff ale++; 374103785Sjeff bufp += size; 375103785Sjeff } 376103785Sjeff 377103785Sjeff alp->ae_next = alq->aq_first; 378103785Sjeff 379103785Sjeff if ((error = ald_add(alq)) != 0) 380103785Sjeff return (error); 381103785Sjeff *alqp = alq; 382103785Sjeff 383103785Sjeff return (0); 384103785Sjeff} 385103785Sjeff 386103785Sjeff/* 387103785Sjeff * Copy a new entry into the queue. If the operation would block either 388103785Sjeff * wait or return an error depending on the value of waitok. 389103785Sjeff */ 390103785Sjeffint 391103785Sjeffalq_write(struct alq *alq, void *data, int waitok) 392103785Sjeff{ 393103785Sjeff struct ale *ale; 394103785Sjeff 395103785Sjeff if ((ale = alq_get(alq, waitok)) == NULL) 396103785Sjeff return (EWOULDBLOCK); 397103785Sjeff 398103785Sjeff bcopy(data, ale->ae_data, alq->aq_entlen); 399103785Sjeff alq_post(alq, ale); 400103785Sjeff 401103785Sjeff return (0); 402103785Sjeff} 403103785Sjeff 404103785Sjeffstruct ale * 405103785Sjeffalq_get(struct alq *alq, int waitok) 406103785Sjeff{ 407103785Sjeff struct ale *ale; 408103785Sjeff struct ale *aln; 409103785Sjeff 410103785Sjeff ale = NULL; 411103785Sjeff 412103785Sjeff ALQ_LOCK(alq); 413103785Sjeff 414103785Sjeff /* Loop until we get an entry or we're shutting down */ 415103785Sjeff while ((alq->aq_flags & AQ_SHUTDOWN) == 0 && 416103785Sjeff (ale = alq->aq_entfree) == NULL && 417103785Sjeff (waitok & ALQ_WAITOK)) { 418103785Sjeff alq->aq_flags |= AQ_WANTED; 419103785Sjeff ALQ_UNLOCK(alq); 420103785Sjeff tsleep(alq, PWAIT, "alqget", 0); 421103785Sjeff ALQ_LOCK(alq); 422103785Sjeff } 423103785Sjeff 424103785Sjeff if (ale != NULL) { 425103785Sjeff aln = ale->ae_next; 426103785Sjeff if ((aln->ae_flags & AE_VALID) == 0) 427103785Sjeff alq->aq_entfree = aln; 428103785Sjeff } else 429103785Sjeff ALQ_UNLOCK(alq); 430103785Sjeff 431103785Sjeff 432103785Sjeff return (ale); 433103785Sjeff} 434103785Sjeff 435103785Sjeffvoid 436103785Sjeffalq_post(struct alq *alq, struct ale *ale) 437103785Sjeff{ 438103785Sjeff int activate; 439103785Sjeff 440103785Sjeff ale->ae_flags |= AE_VALID; 441103785Sjeff 442103785Sjeff if (alq->aq_entvalid == NULL) 443103785Sjeff alq->aq_entvalid = ale; 444103785Sjeff 445103785Sjeff if ((alq->aq_flags & AQ_ACTIVE) == 0) { 446103785Sjeff alq->aq_flags |= AQ_ACTIVE; 447103785Sjeff activate = 1; 448103785Sjeff } else 449103785Sjeff activate = 0; 450103785Sjeff 451103785Sjeff ALQ_UNLOCK(alq); 452103785Sjeff if (activate) { 453103785Sjeff ALD_LOCK(); 454103785Sjeff ald_activate(alq); 455103785Sjeff ALD_UNLOCK(); 456103785Sjeff } 457103785Sjeff} 458103785Sjeff 459103785Sjeffvoid 460103785Sjeffalq_flush(struct alq *alq) 461103785Sjeff{ 462103785Sjeff int needwakeup = 0; 463103785Sjeff 464103785Sjeff ALD_LOCK(); 465103785Sjeff ALQ_LOCK(alq); 466103785Sjeff if (alq->aq_flags & AQ_ACTIVE) { 467103785Sjeff ald_deactivate(alq); 468103785Sjeff ALD_UNLOCK(); 469103785Sjeff needwakeup = alq_doio(alq); 470103785Sjeff } else 471103785Sjeff ALD_UNLOCK(); 472103785Sjeff ALQ_UNLOCK(alq); 473103785Sjeff 474103785Sjeff if (needwakeup) 475103785Sjeff wakeup(alq); 476103785Sjeff} 477103785Sjeff 478103785Sjeff/* 479103785Sjeff * Flush remaining data, close the file and free all resources. 480103785Sjeff */ 481103785Sjeffvoid 482103785Sjeffalq_close(struct alq *alq) 483103785Sjeff{ 484103785Sjeff /* 485103785Sjeff * If we're already shuting down someone else will flush and close 486103785Sjeff * the vnode. 487103785Sjeff */ 488103785Sjeff if (ald_rem(alq) != 0) 489103785Sjeff return; 490103785Sjeff 491103785Sjeff /* 492103785Sjeff * Drain all pending IO. 493103785Sjeff */ 494103785Sjeff alq_shutdown(alq); 495103785Sjeff 496103785Sjeff mtx_destroy(&alq->aq_mtx); 497103785Sjeff free(alq->aq_first, M_ALD); 498103785Sjeff free(alq->aq_entbuf, M_ALD); 499103785Sjeff free(alq, M_ALD); 500103785Sjeff} 501