Lines Matching refs:alq
58 struct alq { struct
76 LIST_ENTRY(alq) aq_act; /* List of active queues */ argument
77 LIST_ENTRY(alq) aq_link; /* List of all queues */
87 #define ALQ_LOCK(alq) mtx_lock_spin(&(alq)->aq_mtx) argument
88 #define ALQ_UNLOCK(alq) mtx_unlock_spin(&(alq)->aq_mtx) argument
90 #define HAS_PENDING_DATA(alq) ((alq)->aq_freebytes != (alq)->aq_buflen) argument
98 static LIST_HEAD(, alq) ald_queues;
99 static LIST_HEAD(, alq) ald_active;
109 static int ald_add(struct alq *);
110 static int ald_rem(struct alq *);
114 static void ald_activate(struct alq *);
115 static void ald_deactivate(struct alq *);
118 static void alq_shutdown(struct alq *);
119 static void alq_destroy(struct alq *);
120 static int alq_doio(struct alq *);
126 ald_add(struct alq *alq) in ald_add() argument
137 LIST_INSERT_HEAD(&ald_queues, alq, aq_link); in ald_add()
148 ald_rem(struct alq *alq) in ald_rem() argument
159 LIST_REMOVE(alq, aq_link); in ald_rem()
169 ald_activate(struct alq *alq) in ald_activate() argument
171 LIST_INSERT_HEAD(&ald_active, alq, aq_act); in ald_activate()
176 ald_deactivate(struct alq *alq) in ald_deactivate() argument
178 LIST_REMOVE(alq, aq_act); in ald_deactivate()
179 alq->aq_flags &= ~AQ_ACTIVE; in ald_deactivate()
194 struct alq *alq; in ald_daemon() local
204 while ((alq = LIST_FIRST(&ald_active)) == NULL && in ald_daemon()
209 if (ald_shutingdown && alq == NULL) { in ald_daemon()
214 ALQ_LOCK(alq); in ald_daemon()
215 ald_deactivate(alq); in ald_daemon()
217 needwakeup = alq_doio(alq); in ald_daemon()
218 ALQ_UNLOCK(alq); in ald_daemon()
220 wakeup_one(alq); in ald_daemon()
230 struct alq *alq; in ald_shutdown() local
241 while ((alq = LIST_FIRST(&ald_queues)) != NULL) { in ald_shutdown()
242 LIST_REMOVE(alq, aq_link); in ald_shutdown()
244 alq_shutdown(alq); in ald_shutdown()
263 alq_shutdown(struct alq *alq) in alq_shutdown() argument
265 ALQ_LOCK(alq); in alq_shutdown()
268 alq->aq_flags |= AQ_SHUTDOWN; in alq_shutdown()
275 if (!(alq->aq_flags & AQ_ACTIVE) && HAS_PENDING_DATA(alq)) { in alq_shutdown()
276 alq->aq_flags |= AQ_ACTIVE; in alq_shutdown()
277 ALQ_UNLOCK(alq); in alq_shutdown()
279 ald_activate(alq); in alq_shutdown()
281 ALQ_LOCK(alq); in alq_shutdown()
285 while (alq->aq_flags & AQ_ACTIVE) { in alq_shutdown()
286 alq->aq_flags |= AQ_WANTED; in alq_shutdown()
287 msleep_spin(alq, &alq->aq_mtx, "aldclose", 0); in alq_shutdown()
289 ALQ_UNLOCK(alq); in alq_shutdown()
291 vn_close(alq->aq_vp, FWRITE, alq->aq_cred, in alq_shutdown()
293 crfree(alq->aq_cred); in alq_shutdown()
297 alq_destroy(struct alq *alq) in alq_destroy() argument
300 alq_shutdown(alq); in alq_destroy()
302 mtx_destroy(&alq->aq_mtx); in alq_destroy()
303 free(alq->aq_entbuf, M_ALD); in alq_destroy()
304 free(alq, M_ALD); in alq_destroy()
311 alq_doio(struct alq *alq) in alq_doio() argument
322 KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__)); in alq_doio()
324 vp = alq->aq_vp; in alq_doio()
328 wrapearly = alq->aq_wrapearly; in alq_doio()
334 aiov[0].iov_base = alq->aq_entbuf + alq->aq_writetail; in alq_doio()
336 if (alq->aq_writetail < alq->aq_writehead) { in alq_doio()
338 totlen = aiov[0].iov_len = alq->aq_writehead - alq->aq_writetail; in alq_doio()
339 } else if (alq->aq_writehead == 0) { in alq_doio()
341 totlen = aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail - in alq_doio()
349 aiov[0].iov_len = alq->aq_buflen - alq->aq_writetail - in alq_doio()
352 aiov[1].iov_base = alq->aq_entbuf; in alq_doio()
353 aiov[1].iov_len = alq->aq_writehead; in alq_doio()
357 alq->aq_flags |= AQ_FLUSHING; in alq_doio()
358 ALQ_UNLOCK(alq); in alq_doio()
377 if (mac_vnode_check_write(alq->aq_cred, NOCRED, vp) == 0) in alq_doio()
379 VOP_WRITE(vp, &auio, IO_UNIT | IO_APPEND, alq->aq_cred); in alq_doio()
383 ALQ_LOCK(alq); in alq_doio()
384 alq->aq_flags &= ~AQ_FLUSHING; in alq_doio()
387 alq->aq_writetail = (alq->aq_writetail + totlen + wrapearly) % in alq_doio()
388 alq->aq_buflen; in alq_doio()
389 alq->aq_freebytes += totlen + wrapearly; in alq_doio()
396 alq->aq_wrapearly = 0; in alq_doio()
403 if (!HAS_PENDING_DATA(alq)) in alq_doio()
404 alq->aq_writehead = alq->aq_writetail = 0; in alq_doio()
406 KASSERT((alq->aq_writetail >= 0 && alq->aq_writetail < alq->aq_buflen), in alq_doio()
409 if (alq->aq_flags & AQ_WANTED) { in alq_doio()
410 alq->aq_flags &= ~AQ_WANTED; in alq_doio()
433 alq_open_flags(struct alq **alqp, const char *file, struct ucred *cred, int cmode, in alq_open_flags()
437 struct alq *alq; in alq_open_flags() local
456 alq = malloc(sizeof(*alq), M_ALD, M_WAITOK|M_ZERO); in alq_open_flags()
457 alq->aq_vp = nd.ni_vp; in alq_open_flags()
458 alq->aq_cred = crhold(cred); in alq_open_flags()
460 mtx_init(&alq->aq_mtx, "ALD Queue", NULL, MTX_SPIN|MTX_QUIET); in alq_open_flags()
462 alq->aq_buflen = size; in alq_open_flags()
463 alq->aq_entmax = 0; in alq_open_flags()
464 alq->aq_entlen = 0; in alq_open_flags()
466 alq->aq_freebytes = alq->aq_buflen; in alq_open_flags()
467 alq->aq_entbuf = malloc(alq->aq_buflen, M_ALD, M_WAITOK|M_ZERO); in alq_open_flags()
468 alq->aq_writehead = alq->aq_writetail = 0; in alq_open_flags()
470 alq->aq_flags |= AQ_ORDERED; in alq_open_flags()
472 if ((error = ald_add(alq)) != 0) { in alq_open_flags()
473 alq_destroy(alq); in alq_open_flags()
477 *alqp = alq; in alq_open_flags()
483 alq_open(struct alq **alqp, const char *file, struct ucred *cred, int cmode, in alq_open()
508 alq_writen(struct alq *alq, void *data, int len, int flags) in alq_writen() argument
513 KASSERT((len > 0 && len <= alq->aq_buflen), in alq_writen()
520 ALQ_LOCK(alq); in alq_writen()
532 if (len > alq->aq_buflen || in alq_writen()
533 alq->aq_flags & AQ_SHUTDOWN || in alq_writen()
534 (((flags & ALQ_NOWAIT) || (!(alq->aq_flags & AQ_ACTIVE) && in alq_writen()
535 HAS_PENDING_DATA(alq))) && alq->aq_freebytes < len)) { in alq_writen()
536 ALQ_UNLOCK(alq); in alq_writen()
544 if (alq->aq_flags & AQ_ORDERED && alq->aq_waiters > 0) { in alq_writen()
547 alq->aq_waiters++; in alq_writen()
548 msleep_spin(&alq->aq_waiters, &alq->aq_mtx, "alqwnord", 0); in alq_writen()
549 alq->aq_waiters--; in alq_writen()
559 while (alq->aq_freebytes < len && !(alq->aq_flags & AQ_SHUTDOWN)) { in alq_writen()
562 alq->aq_flags |= AQ_WANTED; in alq_writen()
563 alq->aq_waiters++; in alq_writen()
566 msleep_spin(alq, &alq->aq_mtx, "alqwnres", 0); in alq_writen()
567 alq->aq_waiters--; in alq_writen()
576 if (alq->aq_waiters > 0 && !(alq->aq_flags & AQ_ORDERED) && in alq_writen()
577 alq->aq_freebytes < len && !(alq->aq_flags & AQ_WANTED)) in alq_writen()
578 waitchan = alq; in alq_writen()
590 if (alq->aq_waiters > 0) { in alq_writen()
591 if (alq->aq_flags & AQ_ORDERED) in alq_writen()
592 waitchan = &alq->aq_waiters; in alq_writen()
594 waitchan = alq; in alq_writen()
599 if (alq->aq_flags & AQ_SHUTDOWN) { in alq_writen()
608 if ((alq->aq_buflen - alq->aq_writehead) < len) in alq_writen()
609 copy = alq->aq_buflen - alq->aq_writehead; in alq_writen()
612 bcopy(data, alq->aq_entbuf + alq->aq_writehead, copy); in alq_writen()
613 alq->aq_writehead += copy; in alq_writen()
615 if (alq->aq_writehead >= alq->aq_buflen) { in alq_writen()
616 KASSERT((alq->aq_writehead == alq->aq_buflen), in alq_writen()
619 alq->aq_writehead, in alq_writen()
620 alq->aq_buflen)); in alq_writen()
621 alq->aq_writehead = 0; in alq_writen()
629 bcopy(((uint8_t *)data)+copy, alq->aq_entbuf, len - copy); in alq_writen()
630 alq->aq_writehead = len - copy; in alq_writen()
633 KASSERT((alq->aq_writehead >= 0 && alq->aq_writehead < alq->aq_buflen), in alq_writen()
636 alq->aq_freebytes -= len; in alq_writen()
638 if (!(alq->aq_flags & AQ_ACTIVE) && !(flags & ALQ_NOACTIVATE)) { in alq_writen()
639 alq->aq_flags |= AQ_ACTIVE; in alq_writen()
643 KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__)); in alq_writen()
646 ALQ_UNLOCK(alq); in alq_writen()
650 ald_activate(alq); in alq_writen()
662 alq_write(struct alq *alq, void *data, int flags) in alq_write() argument
665 KASSERT((alq->aq_flags & AQ_LEGACY), in alq_write()
667 return (alq_writen(alq, data, alq->aq_entlen, flags)); in alq_write()
674 alq_getn(struct alq *alq, int len, int flags) in alq_getn() argument
679 KASSERT((len > 0 && len <= alq->aq_buflen), in alq_getn()
684 ALQ_LOCK(alq); in alq_getn()
694 if (alq->aq_writehead <= alq->aq_writetail) in alq_getn()
695 contigbytes = alq->aq_freebytes; in alq_getn()
697 contigbytes = alq->aq_buflen - alq->aq_writehead; in alq_getn()
707 if (alq->aq_writetail >= len || flags & ALQ_WAITOK) { in alq_getn()
709 alq->aq_wrapearly = contigbytes; in alq_getn()
711 contigbytes = alq->aq_freebytes = in alq_getn()
712 alq->aq_writetail; in alq_getn()
713 alq->aq_writehead = 0; in alq_getn()
728 if (len > alq->aq_buflen || in alq_getn()
729 alq->aq_flags & AQ_SHUTDOWN || in alq_getn()
730 (((flags & ALQ_NOWAIT) || (!(alq->aq_flags & AQ_ACTIVE) && in alq_getn()
731 HAS_PENDING_DATA(alq))) && contigbytes < len)) { in alq_getn()
732 ALQ_UNLOCK(alq); in alq_getn()
740 if (alq->aq_flags & AQ_ORDERED && alq->aq_waiters > 0) { in alq_getn()
743 alq->aq_waiters++; in alq_getn()
744 msleep_spin(&alq->aq_waiters, &alq->aq_mtx, "alqgnord", 0); in alq_getn()
745 alq->aq_waiters--; in alq_getn()
755 while (contigbytes < len && !(alq->aq_flags & AQ_SHUTDOWN)) { in alq_getn()
758 alq->aq_flags |= AQ_WANTED; in alq_getn()
759 alq->aq_waiters++; in alq_getn()
762 msleep_spin(alq, &alq->aq_mtx, "alqgnres", 0); in alq_getn()
763 alq->aq_waiters--; in alq_getn()
765 if (alq->aq_writehead <= alq->aq_writetail) in alq_getn()
766 contigbytes = alq->aq_freebytes; in alq_getn()
768 contigbytes = alq->aq_buflen - alq->aq_writehead; in alq_getn()
777 if (alq->aq_waiters > 0 && !(alq->aq_flags & AQ_ORDERED) && in alq_getn()
778 contigbytes < len && !(alq->aq_flags & AQ_WANTED)) in alq_getn()
779 waitchan = alq; in alq_getn()
791 if (alq->aq_waiters > 0) { in alq_getn()
792 if (alq->aq_flags & AQ_ORDERED) in alq_getn()
793 waitchan = &alq->aq_waiters; in alq_getn()
795 waitchan = alq; in alq_getn()
800 if (alq->aq_flags & AQ_SHUTDOWN) { in alq_getn()
801 ALQ_UNLOCK(alq); in alq_getn()
811 alq->aq_getpost.ae_data = alq->aq_entbuf + alq->aq_writehead; in alq_getn()
812 alq->aq_getpost.ae_bytesused = len; in alq_getn()
814 return (&alq->aq_getpost); in alq_getn()
818 alq_get(struct alq *alq, int flags) in alq_get() argument
821 KASSERT((alq->aq_flags & AQ_LEGACY), in alq_get()
823 return (alq_getn(alq, alq->aq_entlen, flags)); in alq_get()
827 alq_post_flags(struct alq *alq, struct ale *ale, int flags) in alq_post_flags() argument
835 if (!(alq->aq_flags & AQ_ACTIVE) && in alq_post_flags()
837 alq->aq_flags |= AQ_ACTIVE; in alq_post_flags()
841 alq->aq_writehead += ale->ae_bytesused; in alq_post_flags()
842 alq->aq_freebytes -= ale->ae_bytesused; in alq_post_flags()
845 if (alq->aq_writehead == alq->aq_buflen) in alq_post_flags()
846 alq->aq_writehead = 0; in alq_post_flags()
848 KASSERT((alq->aq_writehead >= 0 && in alq_post_flags()
849 alq->aq_writehead < alq->aq_buflen), in alq_post_flags()
853 KASSERT((HAS_PENDING_DATA(alq)), ("%s: queue empty!", __func__)); in alq_post_flags()
863 if (alq->aq_waiters > 0) { in alq_post_flags()
864 if (alq->aq_flags & AQ_ORDERED) in alq_post_flags()
865 waitchan = &alq->aq_waiters; in alq_post_flags()
867 waitchan = alq; in alq_post_flags()
871 ALQ_UNLOCK(alq); in alq_post_flags()
875 ald_activate(alq); in alq_post_flags()
885 alq_flush(struct alq *alq) in alq_flush() argument
890 ALQ_LOCK(alq); in alq_flush()
896 if (HAS_PENDING_DATA(alq) && !(alq->aq_flags & AQ_FLUSHING)) { in alq_flush()
897 if (alq->aq_flags & AQ_ACTIVE) in alq_flush()
898 ald_deactivate(alq); in alq_flush()
901 needwakeup = alq_doio(alq); in alq_flush()
905 ALQ_UNLOCK(alq); in alq_flush()
908 wakeup_one(alq); in alq_flush()
915 alq_close(struct alq *alq) in alq_close() argument
918 if (ald_rem(alq) == 0) in alq_close()
919 alq_destroy(alq); in alq_close()
971 DECLARE_MODULE(alq, alq_mod, SI_SUB_LAST, SI_ORDER_ANY);
972 MODULE_VERSION(alq, 1);