2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2010 Fabio Checconi
5 * Copyright (c) 2009-2010 Luigi Rizzo, Universita` di Pisa
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Main control module for geom-based disk schedulers ('sched').
37 * A 'sched' node is typically inserted transparently between
38 * an existing provider pp and its original geom gp
42 * using the command "geom sched insert <provider>" and
43 * resulting in the following topology
45 * [pp --> sched_gp --> cp] [new_pp --> gp ... ]
47 * Deletion "geom sched destroy <provider>.sched." restores the
48 * original chain. The normal "geom sched create <provide>"
52 * Internally, the 'sched' uses the following data structures
54 * geom{} g_sched_softc{} g_gsched{}
55 * +----------+ +---------------+ +-------------+
56 * | softc *-|--->| sc_gsched *-|-->| gs_init |
57 * | ... | | | | gs_fini |
58 * | | | [ hash table] | | gs_start |
59 * +----------+ | | | ... |
65 * +---------------+ | algorithm- |
69 * A g_sched_softc{} is created with a "geom sched insert" call.
70 * In turn this instantiates a specific scheduling algorithm,
71 * which sets sc_gsched to point to the algorithm callbacks,
72 * and calls gs_init() to create the g_*_softc{} .
73 * The other callbacks (gs_start, gs_next, ...) are invoked
76 * g_sched_softc{} is defined in g_sched.h and mostly used here;
77 * g_gsched{}, and the gs_callbacks, are documented in gs_scheduler.h;
78 * g_*_softc{} is defined/implemented by each algorithm (gs_*.c)
81 * When a bio is received on the provider, it goes to the
82 * g_sched_start() which calls gs_start() to initially queue it;
83 * then we call g_sched_dispatch() that loops around gs_next()
84 * to select zero or more bio's to be sent downstream.
86 * g_sched_dispatch() can also be called as a result of a timeout,
87 * e.g. when doing anticipation or pacing requests.
89 * When a bio comes back, it goes to g_sched_done() which in turn
90 * calls gs_done(). The latter does any necessary housekeeping in
91 * the scheduling algorithm, and may decide to call g_sched_dispatch()
92 * to send more bio's downstream.
94 * If an algorithm needs per-flow queues, these are created
95 * calling gs_init_class() and destroyed with gs_fini_class(),
96 * and they are also inserted in the hash table implemented in
99 * If an algorithm is replaced, or a transparently-inserted node is
100 * removed with "geom sched destroy", we need to remove all references
101 * to the g_*_softc{} and g_sched_softc from the bio's still in
102 * the scheduler. g_sched_forced_dispatch() helps doing this.
103 * XXX need to explain better.
106 #include <sys/cdefs.h>
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/kernel.h>
110 #include <sys/module.h>
111 #include <sys/lock.h>
112 #include <sys/mutex.h>
114 #include <sys/limits.h>
115 #include <sys/hash.h>
116 #include <sys/sbuf.h>
117 #include <sys/sysctl.h>
118 #include <sys/malloc.h>
119 #include <sys/proc.h> /* we access curthread */
120 #include <geom/geom.h>
121 #include "gs_scheduler.h"
122 #include "g_sched.h" /* geom hooks */
125 * Size of the per-geom hash table storing traffic classes.
126 * We may decide to change it at a later time, it has no ABI
127 * implications as it is only used for run-time allocations.
129 #define G_SCHED_HASH_SIZE 32
131 static int g_sched_destroy(struct g_geom *gp, boolean_t force);
132 static int g_sched_destroy_geom(struct gctl_req *req,
133 struct g_class *mp, struct g_geom *gp);
134 static void g_sched_config(struct gctl_req *req, struct g_class *mp,
136 static struct g_geom *g_sched_taste(struct g_class *mp,
137 struct g_provider *pp, int flags __unused);
138 static void g_sched_dumpconf(struct sbuf *sb, const char *indent,
139 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
140 static void g_sched_init(struct g_class *mp);
141 static void g_sched_fini(struct g_class *mp);
142 static int g_sched_ioctl(struct g_provider *pp, u_long cmd, void *data,
143 int fflag, struct thread *td);
145 struct g_class g_sched_class = {
146 .name = G_SCHED_CLASS_NAME,
147 .version = G_VERSION,
148 .ctlreq = g_sched_config,
149 .taste = g_sched_taste,
150 .destroy_geom = g_sched_destroy_geom,
151 .init = g_sched_init,
152 .ioctl = g_sched_ioctl,
156 MALLOC_DEFINE(M_GEOM_SCHED, "GEOM_SCHED", "Geom schedulers data structures");
159 * Global variables describing the state of the geom_sched module.
160 * There is only one static instance of this structure.
162 LIST_HEAD(gs_list, g_gsched); /* type, link field */
163 struct geom_sched_vars {
165 struct gs_list gs_scheds; /* list of algorithms */
167 u_int gs_sched_count; /* how many algorithms ? */
168 u_int gs_patched; /* g_io_request was patched */
170 u_int gs_initialized;
171 u_int gs_expire_secs; /* expiration of hash entries */
173 struct bio_queue_head gs_pending;
176 /* The following are for stats, usually protected by gs_mtx. */
177 u_long gs_requests; /* total requests */
178 u_long gs_done; /* total done */
179 u_int gs_in_flight; /* requests in flight */
180 u_int gs_writes_in_flight;
181 u_int gs_bytes_in_flight;
182 u_int gs_write_bytes_in_flight;
184 char gs_names[256]; /* names of schedulers */
187 static struct geom_sched_vars me = {
188 .gs_expire_secs = 10,
191 SYSCTL_DECL(_kern_geom);
192 SYSCTL_NODE(_kern_geom, OID_AUTO, sched, CTLFLAG_RW, 0,
195 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, in_flight_wb, CTLFLAG_RD,
196 &me.gs_write_bytes_in_flight, 0, "Write bytes in flight");
198 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, in_flight_b, CTLFLAG_RD,
199 &me.gs_bytes_in_flight, 0, "Bytes in flight");
201 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, in_flight_w, CTLFLAG_RD,
202 &me.gs_writes_in_flight, 0, "Write Requests in flight");
204 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, in_flight, CTLFLAG_RD,
205 &me.gs_in_flight, 0, "Requests in flight");
207 SYSCTL_ULONG(_kern_geom_sched, OID_AUTO, done, CTLFLAG_RD,
208 &me.gs_done, 0, "Total done");
210 SYSCTL_ULONG(_kern_geom_sched, OID_AUTO, requests, CTLFLAG_RD,
211 &me.gs_requests, 0, "Total requests");
213 SYSCTL_STRING(_kern_geom_sched, OID_AUTO, algorithms, CTLFLAG_RD,
214 &me.gs_names, 0, "Algorithm names");
216 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, alg_count, CTLFLAG_RD,
217 &me.gs_sched_count, 0, "Number of algorithms");
219 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, debug, CTLFLAG_RW,
220 &me.gs_debug, 0, "Debug level");
222 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, expire_secs, CTLFLAG_RW,
223 &me.gs_expire_secs, 0, "Expire time in seconds");
226 * g_sched calls the scheduler algorithms with this lock held.
227 * The locking functions are exposed so the scheduler algorithms can also
228 * protect themselves e.g. when running a callout handler.
231 g_sched_lock(struct g_geom *gp)
233 struct g_sched_softc *sc = gp->softc;
235 mtx_lock(&sc->sc_mtx);
239 g_sched_unlock(struct g_geom *gp)
241 struct g_sched_softc *sc = gp->softc;
243 mtx_unlock(&sc->sc_mtx);
247 * Support functions to handle references to the module,
248 * which are coming from devices using this scheduler.
251 g_gsched_ref(struct g_gsched *gsp)
254 atomic_add_int(&gsp->gs_refs, 1);
258 g_gsched_unref(struct g_gsched *gsp)
261 atomic_add_int(&gsp->gs_refs, -1);
265 * Update the stats when this request is done.
268 g_sched_update_stats(struct bio *bio)
273 me.gs_bytes_in_flight -= bio->bio_length;
274 if (bio->bio_cmd == BIO_WRITE) {
275 me.gs_writes_in_flight--;
276 me.gs_write_bytes_in_flight -= bio->bio_length;
281 * Dispatch any pending request.
284 g_sched_forced_dispatch(struct g_geom *gp)
286 struct g_sched_softc *sc = gp->softc;
287 struct g_gsched *gsp = sc->sc_gsched;
290 KASSERT(mtx_owned(&sc->sc_mtx),
291 ("sc_mtx not owned during forced dispatch"));
293 while ((bp = gsp->gs_next(sc->sc_data, 1)) != NULL)
294 g_io_request(bp, LIST_FIRST(&gp->consumer));
298 * The main dispatch loop, called either here after the start
299 * routine, or by scheduling algorithms when they receive a timeout
300 * or a 'done' notification. Does not share code with the forced
301 * dispatch path, since the gs_done() callback can call us.
304 g_sched_dispatch(struct g_geom *gp)
306 struct g_sched_softc *sc = gp->softc;
307 struct g_gsched *gsp = sc->sc_gsched;
310 KASSERT(mtx_owned(&sc->sc_mtx), ("sc_mtx not owned during dispatch"));
312 if ((sc->sc_flags & G_SCHED_FLUSHING))
315 while ((bp = gsp->gs_next(sc->sc_data, 0)) != NULL)
316 g_io_request(bp, LIST_FIRST(&gp->consumer));
320 * Recent (8.0 and above) versions of FreeBSD have support to
321 * register classifiers of disk requests. The classifier is
322 * invoked by g_io_request(), and stores the information into
323 * bp->bio_classifier1.
325 * Support for older versions, which is left here only for
326 * documentation purposes, relies on two hacks:
327 * 1. classification info is written into the bio_caller1
328 * field of the topmost node in the bio chain. This field
329 * is rarely used, but this module is incompatible with
330 * those that use bio_caller1 for other purposes,
331 * such as ZFS and gjournal;
332 * 2. g_io_request() is patched in-memory when the module is
333 * loaded, so that the function calls a classifier as its
334 * first thing. g_io_request() is restored when the module
335 * is unloaded. This functionality is only supported for
336 * x86 and amd64, other architectures need source code changes.
340 * Lookup the identity of the issuer of the original request.
341 * In the current implementation we use the curthread of the
342 * issuer, but different mechanisms may be implemented later
343 * so we do not make assumptions on the return value which for
344 * us is just an opaque identifier.
348 g_sched_classify(struct bio *bp)
351 /* we have classifier fields in the struct bio */
352 return ((u_long)bp->bio_classifier1);
355 /* Return the hash chain for the given key. */
356 static inline struct g_hash *
357 g_sched_hash(struct g_sched_softc *sc, u_long key)
360 return (&sc->sc_hash[key & sc->sc_mask]);
364 * Helper function for the children classes, which takes
365 * a geom and a bio and returns the private descriptor
366 * associated to the request. This involves fetching
367 * the classification field and [al]locating the
368 * corresponding entry in the hash table.
371 g_sched_get_class(struct g_geom *gp, struct bio *bp)
373 struct g_sched_softc *sc;
374 struct g_sched_class *gsc;
375 struct g_gsched *gsp;
376 struct g_hash *bucket;
380 key = g_sched_classify(bp);
381 bucket = g_sched_hash(sc, key);
382 LIST_FOREACH(gsc, bucket, gsc_clist) {
383 if (key == gsc->gsc_key) {
385 return (gsc->gsc_priv);
390 gsc = malloc(sizeof(*gsc) + gsp->gs_priv_size,
391 M_GEOM_SCHED, M_NOWAIT | M_ZERO);
395 if (gsp->gs_init_class(sc->sc_data, gsc->gsc_priv)) {
396 free(gsc, M_GEOM_SCHED);
400 gsc->gsc_refs = 2; /* 1 for the hash table, 1 for the caller. */
402 LIST_INSERT_HEAD(bucket, gsc, gsc_clist);
404 gsc->gsc_expire = ticks + me.gs_expire_secs * hz;
406 return (gsc->gsc_priv);
410 * Release a reference to the per-client descriptor,
413 g_sched_put_class(struct g_geom *gp, void *priv)
415 struct g_sched_class *gsc;
416 struct g_sched_softc *sc;
418 gsc = g_sched_priv2class(priv);
419 gsc->gsc_expire = ticks + me.gs_expire_secs * hz;
421 if (--gsc->gsc_refs > 0)
425 sc->sc_gsched->gs_fini_class(sc->sc_data, priv);
427 LIST_REMOVE(gsc, gsc_clist);
428 free(gsc, M_GEOM_SCHED);
432 g_sched_hash_fini(struct g_geom *gp, struct g_hash *hp, u_long mask,
433 struct g_gsched *gsp, void *data)
435 struct g_sched_class *cp, *cp2;
441 if (data && gsp->gs_hash_unref)
442 gsp->gs_hash_unref(data);
444 for (i = 0; i < G_SCHED_HASH_SIZE; i++) {
445 LIST_FOREACH_SAFE(cp, &hp[i], gsc_clist, cp2)
446 g_sched_put_class(gp, cp->gsc_priv);
449 hashdestroy(hp, M_GEOM_SCHED, mask);
452 static struct g_hash *
453 g_sched_hash_init(struct g_gsched *gsp, u_long *mask, int flags)
457 if (gsp->gs_priv_size == 0)
460 hash = hashinit_flags(G_SCHED_HASH_SIZE, M_GEOM_SCHED, mask, flags);
466 g_sched_flush_classes(struct g_geom *gp)
468 struct g_sched_softc *sc;
469 struct g_sched_class *cp, *cp2;
474 if (!sc->sc_hash || ticks - sc->sc_flush_ticks <= 0)
477 for (i = 0; i < G_SCHED_HASH_SIZE; i++) {
478 LIST_FOREACH_SAFE(cp, &sc->sc_hash[i], gsc_clist, cp2) {
479 if (cp->gsc_refs == 1 && ticks - cp->gsc_expire > 0)
480 g_sched_put_class(gp, cp->gsc_priv);
484 sc->sc_flush_ticks = ticks + me.gs_expire_secs * hz;
488 * Wait for the completion of any outstanding request. To ensure
489 * that this does not take forever the caller has to make sure that
490 * no new request enter the scehduler before calling us.
492 * Must be called with the gp mutex held and topology locked.
495 g_sched_wait_pending(struct g_geom *gp)
497 struct g_sched_softc *sc = gp->softc;
498 int endticks = ticks + hz;
502 while (sc->sc_pending && endticks - ticks >= 0)
503 msleep(gp, &sc->sc_mtx, 0, "sched_wait_pending", hz / 4);
505 return (sc->sc_pending ? ETIMEDOUT : 0);
509 g_sched_remove_locked(struct g_geom *gp, struct g_gsched *gsp)
511 struct g_sched_softc *sc = gp->softc;
514 /* Set the flushing flag: new bios will not enter the scheduler. */
515 sc->sc_flags |= G_SCHED_FLUSHING;
517 g_sched_forced_dispatch(gp);
518 error = g_sched_wait_pending(gp);
522 /* No more requests pending or in flight from the old gsp. */
524 g_sched_hash_fini(gp, sc->sc_hash, sc->sc_mask, gsp, sc->sc_data);
528 * Avoid deadlock here by releasing the gp mutex and reacquiring
529 * it once done. It should be safe, since no reconfiguration or
530 * destruction can take place due to the geom topology lock; no
531 * new request can use the current sc_data since we flagged the
532 * geom as being flushed.
535 gsp->gs_fini(sc->sc_data);
538 sc->sc_gsched = NULL;
543 sc->sc_flags &= ~G_SCHED_FLUSHING;
549 g_sched_remove(struct g_geom *gp, struct g_gsched *gsp)
554 error = g_sched_remove_locked(gp, gsp); /* gsp is surely non-null */
561 * Support function for create/taste -- locate the desired
562 * algorithm and grab a reference to it.
564 static struct g_gsched *
565 g_gsched_find(const char *name)
567 struct g_gsched *gsp = NULL;
569 mtx_lock(&me.gs_mtx);
570 LIST_FOREACH(gsp, &me.gs_scheds, glist) {
571 if (strcmp(name, gsp->gs_name) == 0) {
576 mtx_unlock(&me.gs_mtx);
582 * Rebuild the list of scheduler names.
583 * To be called with me.gs_mtx lock held.
586 g_gsched_build_names(struct g_gsched *gsp)
589 struct g_gsched *cur;
592 LIST_FOREACH(cur, &me.gs_scheds, glist) {
593 l = strlen(cur->gs_name);
594 if (l + pos + 1 + 1 < sizeof(me.gs_names)) {
596 me.gs_names[pos++] = ' ';
597 strcpy(me.gs_names + pos, cur->gs_name);
601 me.gs_names[pos] = '\0';
605 * Register or unregister individual scheduling algorithms.
608 g_gsched_register(struct g_gsched *gsp)
610 struct g_gsched *cur;
613 mtx_lock(&me.gs_mtx);
614 LIST_FOREACH(cur, &me.gs_scheds, glist) {
615 if (strcmp(gsp->gs_name, cur->gs_name) == 0)
619 G_SCHED_DEBUG(0, "A scheduler named %s already"
620 "exists.", gsp->gs_name);
623 LIST_INSERT_HEAD(&me.gs_scheds, gsp, glist);
626 g_gsched_build_names(gsp);
628 mtx_unlock(&me.gs_mtx);
633 struct g_gsched_unregparm {
634 struct g_gsched *gup_gsp;
639 g_gsched_unregister(void *arg, int flag)
641 struct g_gsched_unregparm *parm = arg;
642 struct g_gsched *gsp = parm->gup_gsp, *cur, *tmp;
643 struct g_sched_softc *sc;
644 struct g_geom *gp, *gp_tmp;
651 if (flag == EV_CANCEL)
654 mtx_lock(&me.gs_mtx);
656 LIST_FOREACH_SAFE(gp, &g_sched_class.geom, geom, gp_tmp) {
657 if (gp->class != &g_sched_class)
658 continue; /* Should not happen. */
661 if (sc->sc_gsched == gsp) {
662 error = g_sched_remove(gp, gsp);
668 LIST_FOREACH_SAFE(cur, &me.gs_scheds, glist, tmp) {
672 if (gsp->gs_refs != 1) {
673 G_SCHED_DEBUG(0, "%s still in use.",
675 parm->gup_error = EBUSY;
677 LIST_REMOVE(gsp, glist);
679 g_gsched_build_names(gsp);
685 G_SCHED_DEBUG(0, "%s not registered.", gsp->gs_name);
686 parm->gup_error = ENOENT;
690 mtx_unlock(&me.gs_mtx);
694 g_gsched_global_init(void)
697 if (!me.gs_initialized) {
698 G_SCHED_DEBUG(0, "Initializing global data.");
699 mtx_init(&me.gs_mtx, "gsched", NULL, MTX_DEF);
700 LIST_INIT(&me.gs_scheds);
701 bioq_init(&me.gs_pending);
702 me.gs_initialized = 1;
707 * Module event called when a scheduling algorithm module is loaded or
711 g_gsched_modevent(module_t mod, int cmd, void *arg)
713 struct g_gsched *gsp = arg;
714 struct g_gsched_unregparm parm;
717 G_SCHED_DEBUG(0, "Modevent %d.", cmd);
720 * If the module is loaded at boot, the geom thread that calls
721 * g_sched_init() might actually run after g_gsched_modevent(),
722 * so make sure that the module is properly initialized.
724 g_gsched_global_init();
729 error = g_gsched_register(gsp);
730 G_SCHED_DEBUG(0, "Loaded module %s error %d.",
731 gsp->gs_name, error);
733 g_retaste(&g_sched_class);
740 error = g_waitfor_event(g_gsched_unregister,
741 &parm, M_WAITOK, NULL);
743 error = parm.gup_error;
744 G_SCHED_DEBUG(0, "Unloaded module %s error %d.",
745 gsp->gs_name, error);
753 #define TRC_BIO_EVENT(e, bp) g_sched_trace_bio_ ## e (bp)
756 g_sched_type(struct bio *bp)
759 if (bp->bio_cmd == BIO_READ)
761 else if (bp->bio_cmd == BIO_WRITE)
767 g_sched_trace_bio_START(struct bio *bp)
770 CTR5(KTR_GSCHED, "S %lu %c %lu/%lu %lu", g_sched_classify(bp),
771 g_sched_type(bp), bp->bio_offset / ULONG_MAX,
772 bp->bio_offset, bp->bio_length);
776 g_sched_trace_bio_DONE(struct bio *bp)
779 CTR5(KTR_GSCHED, "D %lu %c %lu/%lu %lu", g_sched_classify(bp),
780 g_sched_type(bp), bp->bio_offset / ULONG_MAX,
781 bp->bio_offset, bp->bio_length);
784 #define TRC_BIO_EVENT(e, bp)
788 * g_sched_done() and g_sched_start() dispatch the geom requests to
789 * the scheduling algorithm in use.
792 g_sched_done(struct bio *bio)
794 struct g_geom *gp = bio->bio_caller2;
795 struct g_sched_softc *sc = gp->softc;
797 TRC_BIO_EVENT(DONE, bio);
799 KASSERT(bio->bio_caller1, ("null bio_caller1 in g_sched_done"));
803 g_sched_update_stats(bio);
804 sc->sc_gsched->gs_done(sc->sc_data, bio);
805 if (!--sc->sc_pending)
808 g_sched_flush_classes(gp);
815 g_sched_start(struct bio *bp)
817 struct g_geom *gp = bp->bio_to->geom;
818 struct g_sched_softc *sc = gp->softc;
821 TRC_BIO_EVENT(START, bp);
822 G_SCHED_LOGREQ(bp, "Request received.");
824 cbp = g_clone_bio(bp);
826 g_io_deliver(bp, ENOMEM);
829 cbp->bio_done = g_sched_done;
830 cbp->bio_to = LIST_FIRST(&gp->provider);
831 KASSERT(cbp->bio_to != NULL, ("NULL provider"));
833 /* We only schedule reads and writes. */
834 if (bp->bio_cmd != BIO_READ && bp->bio_cmd != BIO_WRITE)
837 G_SCHED_LOGREQ(cbp, "Sending request.");
841 * Call the algorithm's gs_start to queue the request in the
842 * scheduler. If gs_start fails then pass the request down,
843 * otherwise call g_sched_dispatch() which tries to push
844 * one or more requests down.
846 if (!sc->sc_gsched || (sc->sc_flags & G_SCHED_FLUSHING) ||
847 sc->sc_gsched->gs_start(sc->sc_data, cbp)) {
852 * We use bio_caller1 to mark requests that are scheduled
853 * so make sure it is not NULL.
855 if (cbp->bio_caller1 == NULL)
856 cbp->bio_caller1 = &me; /* anything not NULL */
858 cbp->bio_caller2 = gp;
861 /* Update general stats. */
864 me.gs_bytes_in_flight += bp->bio_length;
865 if (bp->bio_cmd == BIO_WRITE) {
866 me.gs_writes_in_flight++;
867 me.gs_write_bytes_in_flight += bp->bio_length;
869 g_sched_dispatch(gp);
874 cbp->bio_done = g_std_done;
875 cbp->bio_caller1 = NULL; /* not scheduled */
876 g_io_request(cbp, LIST_FIRST(&gp->consumer));
880 * The next few functions are the geom glue.
883 g_sched_orphan(struct g_consumer *cp)
887 g_sched_destroy(cp->geom, 1);
891 g_sched_access(struct g_provider *pp, int dr, int dw, int de)
894 struct g_consumer *cp;
898 cp = LIST_FIRST(&gp->consumer);
899 error = g_access(cp, dr, dw, de);
905 g_sched_temporary_start(struct bio *bio)
908 mtx_lock(&me.gs_mtx);
910 bioq_disksort(&me.gs_pending, bio);
911 mtx_unlock(&me.gs_mtx);
915 g_sched_flush_pending(g_start_t *start)
919 while ((bp = bioq_takefirst(&me.gs_pending)))
924 g_insert_proxy(struct g_geom *gp, struct g_provider *newpp,
925 struct g_geom *dstgp, struct g_provider *pp, struct g_consumer *cp)
927 struct g_sched_softc *sc = gp->softc;
928 g_start_t *saved_start, *flush = g_sched_start;
929 int error = 0, endticks = ticks + hz;
931 g_cancel_event(newpp); /* prevent taste() */
932 /* copy private fields */
933 newpp->private = pp->private;
934 newpp->index = pp->index;
936 /* Queue all the early requests coming for us. */
938 saved_start = pp->geom->start;
939 dstgp->start = g_sched_temporary_start;
941 while (pp->nstart - pp->nend != me.gs_npending &&
942 endticks - ticks >= 0)
943 tsleep(pp, PRIBIO, "-", hz/10);
945 if (pp->nstart - pp->nend != me.gs_npending) {
951 /* link pp to this geom */
952 LIST_REMOVE(pp, provider);
954 LIST_INSERT_HEAD(&gp->provider, pp, provider);
957 * replicate the counts from the parent in the
958 * new provider and consumer nodes
960 cp->acr = newpp->acr = pp->acr;
961 cp->acw = newpp->acw = pp->acw;
962 cp->ace = newpp->ace = pp->ace;
963 sc->sc_flags |= G_SCHED_PROXYING;
966 dstgp->start = saved_start;
968 g_sched_flush_pending(flush);
974 * Create a geom node for the device passed as *pp.
975 * If successful, add a reference to this gsp.
978 g_sched_create(struct gctl_req *req, struct g_class *mp,
979 struct g_provider *pp, struct g_gsched *gsp, int proxy)
981 struct g_sched_softc *sc = NULL;
982 struct g_geom *gp, *dstgp;
983 struct g_provider *newpp = NULL;
984 struct g_consumer *cp = NULL;
990 snprintf(name, sizeof(name), "%s%s", pp->name, G_SCHED_SUFFIX);
991 LIST_FOREACH(gp, &mp->geom, geom) {
992 if (strcmp(gp->name, name) == 0) {
993 gctl_error(req, "Geom %s already exists.",
999 gp = g_new_geomf(mp, "%s", name);
1000 dstgp = proxy ? pp->geom : gp; /* where do we link the provider */
1002 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
1003 sc->sc_gsched = gsp;
1004 sc->sc_data = gsp->gs_init(gp);
1005 if (sc->sc_data == NULL) {
1010 sc->sc_hash = g_sched_hash_init(gsp, &sc->sc_mask, HASH_WAITOK);
1013 * Do not initialize the flush mechanism, will be initialized
1014 * on the first insertion on the hash table.
1017 mtx_init(&sc->sc_mtx, "g_sched_mtx", NULL, MTX_DEF);
1020 gp->start = g_sched_start;
1021 gp->orphan = g_sched_orphan;
1022 gp->access = g_sched_access;
1023 gp->dumpconf = g_sched_dumpconf;
1025 newpp = g_new_providerf(dstgp, "%s", gp->name);
1026 newpp->mediasize = pp->mediasize;
1027 newpp->sectorsize = pp->sectorsize;
1029 cp = g_new_consumer(gp);
1030 error = g_attach(cp, proxy ? newpp : pp);
1032 gctl_error(req, "Cannot attach to provider %s.",
1037 g_error_provider(newpp, 0);
1039 error = g_insert_proxy(gp, newpp, dstgp, pp, cp);
1043 G_SCHED_DEBUG(0, "Device %s created.", gp->name);
1051 if (cp->provider != NULL)
1053 g_destroy_consumer(cp);
1056 g_destroy_provider(newpp);
1058 g_sched_hash_fini(gp, sc->sc_hash, sc->sc_mask,
1061 gsp->gs_fini(sc->sc_data);
1069 * Support for dynamic switching of scheduling algorithms.
1070 * First initialize the data structures for the new algorithm,
1071 * then call g_sched_remove_locked() to flush all references
1072 * to the old one, finally link the new algorithm.
1075 g_sched_change_algo(struct gctl_req *req, struct g_class *mp,
1076 struct g_provider *pp, struct g_gsched *gsp)
1078 struct g_sched_softc *sc;
1080 struct g_hash *newh;
1088 data = gsp->gs_init(gp);
1092 newh = g_sched_hash_init(gsp, &mask, HASH_WAITOK);
1093 if (gsp->gs_priv_size && !newh) {
1099 if (sc->sc_gsched) { /* can be NULL in some cases */
1100 error = g_sched_remove_locked(gp, sc->sc_gsched);
1106 sc->sc_gsched = gsp;
1117 g_sched_hash_fini(gp, newh, mask, gsp, data);
1128 * Stop the request flow directed to the proxy, redirecting the new
1129 * requests to the me.gs_pending queue.
1131 static struct g_provider *
1132 g_detach_proxy(struct g_geom *gp)
1134 struct g_consumer *cp;
1135 struct g_provider *pp, *newpp;
1138 pp = LIST_FIRST(&gp->provider);
1141 cp = LIST_FIRST(&gp->consumer);
1144 newpp = cp->provider;
1149 pp->geom->start = g_sched_temporary_start;
1153 printf("%s error detaching proxy %s\n", __FUNCTION__, gp->name);
1159 g_sched_blackhole(struct bio *bp)
1162 g_io_deliver(bp, ENXIO);
1166 g_reparent_provider(struct g_provider *pp, struct g_geom *gp,
1167 struct g_provider *newpp)
1170 LIST_REMOVE(pp, provider);
1172 pp->private = newpp->private;
1173 pp->index = newpp->index;
1176 LIST_INSERT_HEAD(&gp->provider, pp, provider);
1180 g_unproxy_provider(struct g_provider *oldpp, struct g_provider *newpp)
1182 struct g_geom *gp = oldpp->geom;
1184 g_reparent_provider(oldpp, newpp->geom, newpp);
1187 * Hackish: let the system destroy the old provider for us, just
1188 * in case someone attached a consumer to it, in which case a
1189 * direct call to g_destroy_provider() would not work.
1191 g_reparent_provider(newpp, gp, NULL);
1195 * Complete the proxy destruction, linking the old provider to its
1196 * original geom, and destroying the proxy provider. Also take care
1197 * of issuing the pending requests collected in me.gs_pending (if any).
1200 g_destroy_proxy(struct g_geom *gp, struct g_provider *oldpp)
1202 struct g_consumer *cp;
1203 struct g_provider *newpp;
1206 cp = LIST_FIRST(&gp->consumer);
1209 newpp = cp->provider;
1213 /* Relink the provider to its original geom. */
1214 g_unproxy_provider(oldpp, newpp);
1216 /* Detach consumer from provider, and destroy provider. */
1217 cp->acr = newpp->acr = 0;
1218 cp->acw = newpp->acw = 0;
1219 cp->ace = newpp->ace = 0;
1222 /* Send the pending bios through the right start function. */
1223 g_sched_flush_pending(oldpp->geom->start);
1227 printf("%s error destroying proxy %s\n", __FUNCTION__, gp->name);
1229 /* We cannot send the pending bios anywhere... */
1230 g_sched_flush_pending(g_sched_blackhole);
1236 g_sched_destroy(struct g_geom *gp, boolean_t force)
1238 struct g_provider *pp, *oldpp = NULL;
1239 struct g_sched_softc *sc;
1240 struct g_gsched *gsp;
1243 g_topology_assert();
1247 if (!(sc->sc_flags & G_SCHED_PROXYING)) {
1248 pp = LIST_FIRST(&gp->provider);
1249 if (pp && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
1250 const char *msg = force ?
1251 "but we force removal" : "cannot remove";
1253 G_SCHED_DEBUG(!force,
1254 "Device %s is still open (r%dw%de%d), %s.",
1255 pp->name, pp->acr, pp->acw, pp->ace, msg);
1259 G_SCHED_DEBUG(0, "Device %s removed.", gp->name);
1262 oldpp = g_detach_proxy(gp);
1264 gsp = sc->sc_gsched;
1267 * XXX bad hack here: force a dispatch to release
1268 * any reference to the hash table still held by
1273 * We are dying here, no new requests should enter
1274 * the scheduler. This is granted by the topolgy,
1275 * either in case we were proxying (new bios are
1276 * being redirected) or not (see the access check
1279 g_sched_forced_dispatch(gp);
1280 error = g_sched_wait_pending(gp);
1284 * Not all the requests came home: this might happen
1285 * under heavy load, or if we were waiting for any
1286 * bio which is served in the event path (see
1287 * geom_slice.c for an example of how this can
1288 * happen). Try to restore a working configuration
1291 if ((sc->sc_flags & G_SCHED_PROXYING) && oldpp) {
1292 g_sched_flush_pending(force ?
1293 g_sched_blackhole : g_sched_start);
1297 * In the forced destroy case there is not so much
1298 * we can do, we have pending bios that will call
1299 * g_sched_done() somehow, and we don't want them
1300 * to crash the system using freed memory. We tell
1301 * the user that something went wrong, and leak some
1303 * Note: the callers using force = 1 ignore the
1307 G_SCHED_DEBUG(0, "Pending requests while "
1308 " destroying geom, some memory leaked.");
1315 g_sched_hash_fini(gp, sc->sc_hash, sc->sc_mask,
1318 gsp->gs_fini(sc->sc_data);
1319 g_gsched_unref(gsp);
1320 sc->sc_gsched = NULL;
1324 if ((sc->sc_flags & G_SCHED_PROXYING) && oldpp) {
1325 error = g_destroy_proxy(gp, oldpp);
1329 G_SCHED_DEBUG(0, "Unrecoverable error while "
1330 "destroying a proxy geom, leaking some "
1338 mtx_destroy(&sc->sc_mtx);
1342 g_wither_geom(gp, ENXIO);
1348 g_sched_destroy_geom(struct gctl_req *req, struct g_class *mp,
1352 return (g_sched_destroy(gp, 0));
1356 * Functions related to the classification of requests.
1358 * On recent FreeBSD versions (8.0 and above), we store a reference
1359 * to the issuer of a request in bp->bio_classifier1 as soon
1360 * as the bio is posted to the geom queue (and not later, because
1361 * requests are managed by the g_down thread afterwards).
1365 * Classifier support for recent FreeBSD versions: we use
1366 * a very simple classifier, only use curthread to tag a request.
1367 * The classifier is registered at module load, and unregistered
1371 g_sched_tag(void *arg, struct bio *bp)
1374 bp->bio_classifier1 = curthread;
1378 static struct g_classifier_hook g_sched_classifier = {
1379 .func = g_sched_tag,
1383 g_classifier_ini(void)
1386 g_register_classifier(&g_sched_classifier);
1390 g_classifier_fini(void)
1393 g_unregister_classifier(&g_sched_classifier);
1397 g_sched_init(struct g_class *mp)
1400 g_gsched_global_init();
1402 G_SCHED_DEBUG(0, "Loading: mp = %p, g_sched_class = %p.",
1403 mp, &g_sched_class);
1405 /* Patch g_io_request to store classification info in the bio. */
1410 g_sched_fini(struct g_class *mp)
1413 g_classifier_fini();
1415 G_SCHED_DEBUG(0, "Unloading...");
1417 KASSERT(LIST_EMPTY(&me.gs_scheds), ("still registered schedulers"));
1418 mtx_destroy(&me.gs_mtx);
1422 g_sched_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag,
1425 struct g_consumer *cp;
1428 cp = LIST_FIRST(&pp->geom->consumer);
1431 gp = cp->provider->geom;
1432 if (gp->ioctl == NULL)
1434 return (gp->ioctl(cp->provider, cmd, data, fflag, td));
1438 * Read the i-th argument for a request, skipping the /dev/
1439 * prefix if present.
1442 g_sched_argi(struct gctl_req *req, int i)
1444 static const char *dev_prefix = "/dev/";
1447 int l = strlen(dev_prefix);
1449 snprintf(param, sizeof(param), "arg%d", i);
1450 name = gctl_get_asciiparam(req, param);
1452 gctl_error(req, "No 'arg%d' argument", i);
1453 else if (strncmp(name, dev_prefix, l) == 0)
1459 * Fetch nargs and do appropriate checks.
1462 g_sched_get_nargs(struct gctl_req *req)
1466 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1467 if (nargs == NULL) {
1468 gctl_error(req, "No 'nargs' argument");
1472 gctl_error(req, "Missing device(s).");
1477 * Check whether we should add the class on certain volumes when
1478 * this geom is created. Right now this is under control of a kenv
1479 * variable containing the names of all devices that we care about.
1480 * Probably we should only support transparent insertion as the
1481 * preferred mode of operation.
1483 static struct g_geom *
1484 g_sched_taste(struct g_class *mp, struct g_provider *pp,
1487 struct g_gsched *gsp = NULL; /* the . algorithm we want */
1488 const char *s; /* generic string pointer */
1489 const char *taste_names; /* devices we like */
1492 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__,
1493 mp->name, pp->name);
1494 g_topology_assert();
1496 G_SCHED_DEBUG(2, "Tasting %s.", pp->name);
1499 /* do not taste on ourselves */
1500 if (pp->geom->class == mp)
1503 taste_names = kern_getenv("geom.sched.taste");
1504 if (taste_names == NULL)
1507 l = strlen(pp->name);
1508 for (s = taste_names; *s &&
1509 (s = strstr(s, pp->name)); s++) {
1510 /* further checks for an exact match */
1511 if ( (s == taste_names || s[-1] == ' ') &&
1512 (s[l] == '\0' || s[l] == ' ') )
1517 G_SCHED_DEBUG(0, "Attach device %s match [%s]\n",
1520 /* look up the provider name in the list */
1521 s = kern_getenv("geom.sched.algo");
1525 gsp = g_gsched_find(s); /* also get a reference */
1527 G_SCHED_DEBUG(0, "Bad '%s' algorithm.", s);
1531 /* XXX create with 1 as last argument ? */
1532 g_sched_create(NULL, mp, pp, gsp, 0);
1533 g_gsched_unref(gsp);
1539 g_sched_ctl_create(struct gctl_req *req, struct g_class *mp, int proxy)
1541 struct g_provider *pp;
1542 struct g_gsched *gsp;
1546 g_topology_assert();
1548 name = gctl_get_asciiparam(req, "algo");
1550 gctl_error(req, "No '%s' argument", "algo");
1554 gsp = g_gsched_find(name); /* also get a reference */
1556 gctl_error(req, "Bad algorithm '%s'", name);
1560 nargs = g_sched_get_nargs(req);
1563 * Run on the arguments, and break on any error.
1564 * We look for a device name, but skip the /dev/ prefix if any.
1566 for (i = 0; i < nargs; i++) {
1567 name = g_sched_argi(req, i);
1570 pp = g_provider_by_name(name);
1572 G_SCHED_DEBUG(1, "Provider %s is invalid.", name);
1573 gctl_error(req, "Provider %s is invalid.", name);
1576 if (g_sched_create(req, mp, pp, gsp, proxy) != 0)
1580 g_gsched_unref(gsp);
1584 g_sched_ctl_configure(struct gctl_req *req, struct g_class *mp)
1586 struct g_provider *pp;
1587 struct g_gsched *gsp;
1591 g_topology_assert();
1593 name = gctl_get_asciiparam(req, "algo");
1595 gctl_error(req, "No '%s' argument", "algo");
1599 gsp = g_gsched_find(name); /* also get a reference */
1601 gctl_error(req, "Bad algorithm '%s'", name);
1605 nargs = g_sched_get_nargs(req);
1608 * Run on the arguments, and break on any error.
1609 * We look for a device name, but skip the /dev/ prefix if any.
1611 for (i = 0; i < nargs; i++) {
1612 name = g_sched_argi(req, i);
1615 pp = g_provider_by_name(name);
1616 if (pp == NULL || pp->geom->class != mp) {
1617 G_SCHED_DEBUG(1, "Provider %s is invalid.", name);
1618 gctl_error(req, "Provider %s is invalid.", name);
1621 if (g_sched_change_algo(req, mp, pp, gsp) != 0)
1625 g_gsched_unref(gsp);
1628 static struct g_geom *
1629 g_sched_find_geom(struct g_class *mp, const char *name)
1633 LIST_FOREACH(gp, &mp->geom, geom) {
1634 if (strcmp(gp->name, name) == 0)
1641 g_sched_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1643 int nargs, *force, error, i;
1647 g_topology_assert();
1649 nargs = g_sched_get_nargs(req);
1651 force = gctl_get_paraml(req, "force", sizeof(*force));
1652 if (force == NULL) {
1653 gctl_error(req, "No 'force' argument");
1657 for (i = 0; i < nargs; i++) {
1658 name = g_sched_argi(req, i);
1662 gp = g_sched_find_geom(mp, name);
1664 G_SCHED_DEBUG(1, "Device %s is invalid.", name);
1665 gctl_error(req, "Device %s is invalid.", name);
1669 error = g_sched_destroy(gp, *force);
1671 gctl_error(req, "Cannot destroy device %s (error=%d).",
1679 g_sched_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1683 g_topology_assert();
1685 version = gctl_get_paraml(req, "version", sizeof(*version));
1686 if (version == NULL) {
1687 gctl_error(req, "No '%s' argument.", "version");
1691 if (*version != G_SCHED_VERSION) {
1692 gctl_error(req, "Userland and kernel parts are "
1697 if (strcmp(verb, "create") == 0) {
1698 g_sched_ctl_create(req, mp, 0);
1700 } else if (strcmp(verb, "insert") == 0) {
1701 g_sched_ctl_create(req, mp, 1);
1703 } else if (strcmp(verb, "configure") == 0) {
1704 g_sched_ctl_configure(req, mp);
1706 } else if (strcmp(verb, "destroy") == 0) {
1707 g_sched_ctl_destroy(req, mp);
1711 gctl_error(req, "Unknown verb.");
1715 g_sched_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1716 struct g_consumer *cp, struct g_provider *pp)
1718 struct g_sched_softc *sc = gp->softc;
1719 struct g_gsched *gsp = sc->sc_gsched;
1720 if (indent == NULL) { /* plaintext */
1721 sbuf_printf(sb, " algo %s", gsp ? gsp->gs_name : "--");
1723 if (gsp != NULL && gsp->gs_dumpconf)
1724 gsp->gs_dumpconf(sb, indent, gp, cp, pp);
1727 DECLARE_GEOM_CLASS(g_sched_class, g_sched);
1728 MODULE_VERSION(geom_sched, 0);