2 * Copyright (c) 2009-2010 Fabio Checconi
3 * Copyright (c) 2009-2010 Luigi Rizzo, Universita` di Pisa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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 AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Main control module for geom-based disk schedulers ('sched').
35 * A 'sched' node is typically inserted transparently between
36 * an existing provider pp and its original geom gp
40 * using the command "geom sched insert <provider>" and
41 * resulting in the following topology
43 * [pp --> sched_gp --> cp] [new_pp --> gp ... ]
45 * Deletion "geom sched destroy <provider>.sched." restores the
46 * original chain. The normal "geom sched create <provide>"
50 * Internally, the 'sched' uses the following data structures
52 * geom{} g_sched_softc{} g_gsched{}
53 * +----------+ +---------------+ +-------------+
54 * | softc *-|--->| sc_gsched *-|-->| gs_init |
55 * | ... | | | | gs_fini |
56 * | | | [ hash table] | | gs_start |
57 * +----------+ | | | ... |
63 * +---------------+ | algorithm- |
67 * A g_sched_softc{} is created with a "geom sched insert" call.
68 * In turn this instantiates a specific scheduling algorithm,
69 * which sets sc_gsched to point to the algorithm callbacks,
70 * and calls gs_init() to create the g_*_softc{} .
71 * The other callbacks (gs_start, gs_next, ...) are invoked
74 * g_sched_softc{} is defined in g_sched.h and mostly used here;
75 * g_gsched{}, and the gs_callbacks, are documented in gs_scheduler.h;
76 * g_*_softc{} is defined/implemented by each algorithm (gs_*.c)
79 * When a bio is received on the provider, it goes to the
80 * g_sched_start() which calls gs_start() to initially queue it;
81 * then we call g_sched_dispatch() that loops around gs_next()
82 * to select zero or more bio's to be sent downstream.
84 * g_sched_dispatch() can also be called as a result of a timeout,
85 * e.g. when doing anticipation or pacing requests.
87 * When a bio comes back, it goes to g_sched_done() which in turn
88 * calls gs_done(). The latter does any necessary housekeeping in
89 * the scheduling algorithm, and may decide to call g_sched_dispatch()
90 * to send more bio's downstream.
92 * If an algorithm needs per-flow queues, these are created
93 * calling gs_init_class() and destroyed with gs_fini_class(),
94 * and they are also inserted in the hash table implemented in
97 * If an algorithm is replaced, or a transparently-inserted node is
98 * removed with "geom sched destroy", we need to remove all references
99 * to the g_*_softc{} and g_sched_softc from the bio's still in
100 * the scheduler. g_sched_forced_dispatch() helps doing this.
101 * XXX need to explain better.
104 #include <sys/cdefs.h>
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/kernel.h>
108 #include <sys/module.h>
109 #include <sys/lock.h>
110 #include <sys/mutex.h>
112 #include <sys/limits.h>
113 #include <sys/hash.h>
114 #include <sys/sbuf.h>
115 #include <sys/sysctl.h>
116 #include <sys/malloc.h>
117 #include <sys/proc.h> /* we access curthread */
118 #include <geom/geom.h>
119 #include "gs_scheduler.h"
120 #include "g_sched.h" /* geom hooks */
123 * Size of the per-geom hash table storing traffic classes.
124 * We may decide to change it at a later time, it has no ABI
125 * implications as it is only used for run-time allocations.
127 #define G_SCHED_HASH_SIZE 32
129 static int g_sched_destroy(struct g_geom *gp, boolean_t force);
130 static int g_sched_destroy_geom(struct gctl_req *req,
131 struct g_class *mp, struct g_geom *gp);
132 static void g_sched_config(struct gctl_req *req, struct g_class *mp,
134 static struct g_geom *g_sched_taste(struct g_class *mp,
135 struct g_provider *pp, int flags __unused);
136 static void g_sched_dumpconf(struct sbuf *sb, const char *indent,
137 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
138 static void g_sched_init(struct g_class *mp);
139 static void g_sched_fini(struct g_class *mp);
140 static int g_sched_ioctl(struct g_provider *pp, u_long cmd, void *data,
141 int fflag, struct thread *td);
143 struct g_class g_sched_class = {
144 .name = G_SCHED_CLASS_NAME,
145 .version = G_VERSION,
146 .ctlreq = g_sched_config,
147 .taste = g_sched_taste,
148 .destroy_geom = g_sched_destroy_geom,
149 .init = g_sched_init,
150 .ioctl = g_sched_ioctl,
154 MALLOC_DEFINE(M_GEOM_SCHED, "GEOM_SCHED", "Geom schedulers data structures");
157 * Global variables describing the state of the geom_sched module.
158 * There is only one static instance of this structure.
160 LIST_HEAD(gs_list, g_gsched); /* type, link field */
161 struct geom_sched_vars {
163 struct gs_list gs_scheds; /* list of algorithms */
165 u_int gs_sched_count; /* how many algorithms ? */
166 u_int gs_patched; /* g_io_request was patched */
168 u_int gs_initialized;
169 u_int gs_expire_secs; /* expiration of hash entries */
171 struct bio_queue_head gs_pending;
174 /* The following are for stats, usually protected by gs_mtx. */
175 u_long gs_requests; /* total requests */
176 u_long gs_done; /* total done */
177 u_int gs_in_flight; /* requests in flight */
178 u_int gs_writes_in_flight;
179 u_int gs_bytes_in_flight;
180 u_int gs_write_bytes_in_flight;
182 char gs_names[256]; /* names of schedulers */
185 static struct geom_sched_vars me = {
186 .gs_expire_secs = 10,
189 SYSCTL_DECL(_kern_geom);
190 SYSCTL_NODE(_kern_geom, OID_AUTO, sched, CTLFLAG_RW, 0,
193 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, in_flight_wb, CTLFLAG_RD,
194 &me.gs_write_bytes_in_flight, 0, "Write bytes in flight");
196 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, in_flight_b, CTLFLAG_RD,
197 &me.gs_bytes_in_flight, 0, "Bytes in flight");
199 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, in_flight_w, CTLFLAG_RD,
200 &me.gs_writes_in_flight, 0, "Write Requests in flight");
202 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, in_flight, CTLFLAG_RD,
203 &me.gs_in_flight, 0, "Requests in flight");
205 SYSCTL_ULONG(_kern_geom_sched, OID_AUTO, done, CTLFLAG_RD,
206 &me.gs_done, 0, "Total done");
208 SYSCTL_ULONG(_kern_geom_sched, OID_AUTO, requests, CTLFLAG_RD,
209 &me.gs_requests, 0, "Total requests");
211 SYSCTL_STRING(_kern_geom_sched, OID_AUTO, algorithms, CTLFLAG_RD,
212 &me.gs_names, 0, "Algorithm names");
214 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, alg_count, CTLFLAG_RD,
215 &me.gs_sched_count, 0, "Number of algorithms");
217 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, debug, CTLFLAG_RW,
218 &me.gs_debug, 0, "Debug level");
220 SYSCTL_UINT(_kern_geom_sched, OID_AUTO, expire_secs, CTLFLAG_RW,
221 &me.gs_expire_secs, 0, "Expire time in seconds");
224 * g_sched calls the scheduler algorithms with this lock held.
225 * The locking functions are exposed so the scheduler algorithms can also
226 * protect themselves e.g. when running a callout handler.
229 g_sched_lock(struct g_geom *gp)
231 struct g_sched_softc *sc = gp->softc;
233 mtx_lock(&sc->sc_mtx);
237 g_sched_unlock(struct g_geom *gp)
239 struct g_sched_softc *sc = gp->softc;
241 mtx_unlock(&sc->sc_mtx);
245 * Support functions to handle references to the module,
246 * which are coming from devices using this scheduler.
249 g_gsched_ref(struct g_gsched *gsp)
252 atomic_add_int(&gsp->gs_refs, 1);
256 g_gsched_unref(struct g_gsched *gsp)
259 atomic_add_int(&gsp->gs_refs, -1);
263 * Update the stats when this request is done.
266 g_sched_update_stats(struct bio *bio)
271 me.gs_bytes_in_flight -= bio->bio_length;
272 if (bio->bio_cmd == BIO_WRITE) {
273 me.gs_writes_in_flight--;
274 me.gs_write_bytes_in_flight -= bio->bio_length;
279 * Dispatch any pending request.
282 g_sched_forced_dispatch(struct g_geom *gp)
284 struct g_sched_softc *sc = gp->softc;
285 struct g_gsched *gsp = sc->sc_gsched;
288 KASSERT(mtx_owned(&sc->sc_mtx),
289 ("sc_mtx not owned during forced dispatch"));
291 while ((bp = gsp->gs_next(sc->sc_data, 1)) != NULL)
292 g_io_request(bp, LIST_FIRST(&gp->consumer));
296 * The main dispatch loop, called either here after the start
297 * routine, or by scheduling algorithms when they receive a timeout
298 * or a 'done' notification. Does not share code with the forced
299 * dispatch path, since the gs_done() callback can call us.
302 g_sched_dispatch(struct g_geom *gp)
304 struct g_sched_softc *sc = gp->softc;
305 struct g_gsched *gsp = sc->sc_gsched;
308 KASSERT(mtx_owned(&sc->sc_mtx), ("sc_mtx not owned during dispatch"));
310 if ((sc->sc_flags & G_SCHED_FLUSHING))
313 while ((bp = gsp->gs_next(sc->sc_data, 0)) != NULL)
314 g_io_request(bp, LIST_FIRST(&gp->consumer));
318 * Recent (8.0 and above) versions of FreeBSD have support to
319 * register classifiers of disk requests. The classifier is
320 * invoked by g_io_request(), and stores the information into
321 * bp->bio_classifier1.
323 * Support for older versions, which is left here only for
324 * documentation purposes, relies on two hacks:
325 * 1. classification info is written into the bio_caller1
326 * field of the topmost node in the bio chain. This field
327 * is rarely used, but this module is incompatible with
328 * those that use bio_caller1 for other purposes,
329 * such as ZFS and gjournal;
330 * 2. g_io_request() is patched in-memory when the module is
331 * loaded, so that the function calls a classifier as its
332 * first thing. g_io_request() is restored when the module
333 * is unloaded. This functionality is only supported for
334 * x86 and amd64, other architectures need source code changes.
338 * Lookup the identity of the issuer of the original request.
339 * In the current implementation we use the curthread of the
340 * issuer, but different mechanisms may be implemented later
341 * so we do not make assumptions on the return value which for
342 * us is just an opaque identifier.
346 g_sched_classify(struct bio *bp)
349 /* we have classifier fields in the struct bio */
350 return ((u_long)bp->bio_classifier1);
353 /* Return the hash chain for the given key. */
354 static inline struct g_hash *
355 g_sched_hash(struct g_sched_softc *sc, u_long key)
358 return (&sc->sc_hash[key & sc->sc_mask]);
362 * Helper function for the children classes, which takes
363 * a geom and a bio and returns the private descriptor
364 * associated to the request. This involves fetching
365 * the classification field and [al]locating the
366 * corresponding entry in the hash table.
369 g_sched_get_class(struct g_geom *gp, struct bio *bp)
371 struct g_sched_softc *sc;
372 struct g_sched_class *gsc;
373 struct g_gsched *gsp;
374 struct g_hash *bucket;
378 key = g_sched_classify(bp);
379 bucket = g_sched_hash(sc, key);
380 LIST_FOREACH(gsc, bucket, gsc_clist) {
381 if (key == gsc->gsc_key) {
383 return (gsc->gsc_priv);
388 gsc = malloc(sizeof(*gsc) + gsp->gs_priv_size,
389 M_GEOM_SCHED, M_NOWAIT | M_ZERO);
393 if (gsp->gs_init_class(sc->sc_data, gsc->gsc_priv)) {
394 free(gsc, M_GEOM_SCHED);
398 gsc->gsc_refs = 2; /* 1 for the hash table, 1 for the caller. */
400 LIST_INSERT_HEAD(bucket, gsc, gsc_clist);
402 gsc->gsc_expire = ticks + me.gs_expire_secs * hz;
404 return (gsc->gsc_priv);
408 * Release a reference to the per-client descriptor,
411 g_sched_put_class(struct g_geom *gp, void *priv)
413 struct g_sched_class *gsc;
414 struct g_sched_softc *sc;
416 gsc = g_sched_priv2class(priv);
417 gsc->gsc_expire = ticks + me.gs_expire_secs * hz;
419 if (--gsc->gsc_refs > 0)
423 sc->sc_gsched->gs_fini_class(sc->sc_data, priv);
425 LIST_REMOVE(gsc, gsc_clist);
426 free(gsc, M_GEOM_SCHED);
430 g_sched_hash_fini(struct g_geom *gp, struct g_hash *hp, u_long mask,
431 struct g_gsched *gsp, void *data)
433 struct g_sched_class *cp, *cp2;
439 if (data && gsp->gs_hash_unref)
440 gsp->gs_hash_unref(data);
442 for (i = 0; i < G_SCHED_HASH_SIZE; i++) {
443 LIST_FOREACH_SAFE(cp, &hp[i], gsc_clist, cp2)
444 g_sched_put_class(gp, cp->gsc_priv);
447 hashdestroy(hp, M_GEOM_SCHED, mask);
450 static struct g_hash *
451 g_sched_hash_init(struct g_gsched *gsp, u_long *mask, int flags)
455 if (gsp->gs_priv_size == 0)
458 hash = hashinit_flags(G_SCHED_HASH_SIZE, M_GEOM_SCHED, mask, flags);
464 g_sched_flush_classes(struct g_geom *gp)
466 struct g_sched_softc *sc;
467 struct g_sched_class *cp, *cp2;
472 if (!sc->sc_hash || ticks - sc->sc_flush_ticks <= 0)
475 for (i = 0; i < G_SCHED_HASH_SIZE; i++) {
476 LIST_FOREACH_SAFE(cp, &sc->sc_hash[i], gsc_clist, cp2) {
477 if (cp->gsc_refs == 1 && ticks - cp->gsc_expire > 0)
478 g_sched_put_class(gp, cp->gsc_priv);
482 sc->sc_flush_ticks = ticks + me.gs_expire_secs * hz;
486 * Wait for the completion of any outstanding request. To ensure
487 * that this does not take forever the caller has to make sure that
488 * no new request enter the scehduler before calling us.
490 * Must be called with the gp mutex held and topology locked.
493 g_sched_wait_pending(struct g_geom *gp)
495 struct g_sched_softc *sc = gp->softc;
496 int endticks = ticks + hz;
500 while (sc->sc_pending && endticks - ticks >= 0)
501 msleep(gp, &sc->sc_mtx, 0, "sched_wait_pending", hz / 4);
503 return (sc->sc_pending ? ETIMEDOUT : 0);
507 g_sched_remove_locked(struct g_geom *gp, struct g_gsched *gsp)
509 struct g_sched_softc *sc = gp->softc;
512 /* Set the flushing flag: new bios will not enter the scheduler. */
513 sc->sc_flags |= G_SCHED_FLUSHING;
515 g_sched_forced_dispatch(gp);
516 error = g_sched_wait_pending(gp);
520 /* No more requests pending or in flight from the old gsp. */
522 g_sched_hash_fini(gp, sc->sc_hash, sc->sc_mask, gsp, sc->sc_data);
526 * Avoid deadlock here by releasing the gp mutex and reacquiring
527 * it once done. It should be safe, since no reconfiguration or
528 * destruction can take place due to the geom topology lock; no
529 * new request can use the current sc_data since we flagged the
530 * geom as being flushed.
533 gsp->gs_fini(sc->sc_data);
536 sc->sc_gsched = NULL;
541 sc->sc_flags &= ~G_SCHED_FLUSHING;
547 g_sched_remove(struct g_geom *gp, struct g_gsched *gsp)
552 error = g_sched_remove_locked(gp, gsp); /* gsp is surely non-null */
559 * Support function for create/taste -- locate the desired
560 * algorithm and grab a reference to it.
562 static struct g_gsched *
563 g_gsched_find(const char *name)
565 struct g_gsched *gsp = NULL;
567 mtx_lock(&me.gs_mtx);
568 LIST_FOREACH(gsp, &me.gs_scheds, glist) {
569 if (strcmp(name, gsp->gs_name) == 0) {
574 mtx_unlock(&me.gs_mtx);
580 * Rebuild the list of scheduler names.
581 * To be called with me.gs_mtx lock held.
584 g_gsched_build_names(struct g_gsched *gsp)
587 struct g_gsched *cur;
590 LIST_FOREACH(cur, &me.gs_scheds, glist) {
591 l = strlen(cur->gs_name);
592 if (l + pos + 1 + 1 < sizeof(me.gs_names)) {
594 me.gs_names[pos++] = ' ';
595 strcpy(me.gs_names + pos, cur->gs_name);
599 me.gs_names[pos] = '\0';
603 * Register or unregister individual scheduling algorithms.
606 g_gsched_register(struct g_gsched *gsp)
608 struct g_gsched *cur;
611 mtx_lock(&me.gs_mtx);
612 LIST_FOREACH(cur, &me.gs_scheds, glist) {
613 if (strcmp(gsp->gs_name, cur->gs_name) == 0)
617 G_SCHED_DEBUG(0, "A scheduler named %s already"
618 "exists.", gsp->gs_name);
621 LIST_INSERT_HEAD(&me.gs_scheds, gsp, glist);
624 g_gsched_build_names(gsp);
626 mtx_unlock(&me.gs_mtx);
631 struct g_gsched_unregparm {
632 struct g_gsched *gup_gsp;
637 g_gsched_unregister(void *arg, int flag)
639 struct g_gsched_unregparm *parm = arg;
640 struct g_gsched *gsp = parm->gup_gsp, *cur, *tmp;
641 struct g_sched_softc *sc;
642 struct g_geom *gp, *gp_tmp;
649 if (flag == EV_CANCEL)
652 mtx_lock(&me.gs_mtx);
654 LIST_FOREACH_SAFE(gp, &g_sched_class.geom, geom, gp_tmp) {
655 if (gp->class != &g_sched_class)
656 continue; /* Should not happen. */
659 if (sc->sc_gsched == gsp) {
660 error = g_sched_remove(gp, gsp);
666 LIST_FOREACH_SAFE(cur, &me.gs_scheds, glist, tmp) {
670 if (gsp->gs_refs != 1) {
671 G_SCHED_DEBUG(0, "%s still in use.",
673 parm->gup_error = EBUSY;
675 LIST_REMOVE(gsp, glist);
677 g_gsched_build_names(gsp);
683 G_SCHED_DEBUG(0, "%s not registered.", gsp->gs_name);
684 parm->gup_error = ENOENT;
688 mtx_unlock(&me.gs_mtx);
692 g_gsched_global_init(void)
695 if (!me.gs_initialized) {
696 G_SCHED_DEBUG(0, "Initializing global data.");
697 mtx_init(&me.gs_mtx, "gsched", NULL, MTX_DEF);
698 LIST_INIT(&me.gs_scheds);
699 bioq_init(&me.gs_pending);
700 me.gs_initialized = 1;
705 * Module event called when a scheduling algorithm module is loaded or
709 g_gsched_modevent(module_t mod, int cmd, void *arg)
711 struct g_gsched *gsp = arg;
712 struct g_gsched_unregparm parm;
715 G_SCHED_DEBUG(0, "Modevent %d.", cmd);
718 * If the module is loaded at boot, the geom thread that calls
719 * g_sched_init() might actually run after g_gsched_modevent(),
720 * so make sure that the module is properly initialized.
722 g_gsched_global_init();
727 error = g_gsched_register(gsp);
728 G_SCHED_DEBUG(0, "Loaded module %s error %d.",
729 gsp->gs_name, error);
731 g_retaste(&g_sched_class);
738 error = g_waitfor_event(g_gsched_unregister,
739 &parm, M_WAITOK, NULL);
741 error = parm.gup_error;
742 G_SCHED_DEBUG(0, "Unloaded module %s error %d.",
743 gsp->gs_name, error);
751 #define TRC_BIO_EVENT(e, bp) g_sched_trace_bio_ ## e (bp)
754 g_sched_type(struct bio *bp)
757 if (bp->bio_cmd == BIO_READ)
759 else if (bp->bio_cmd == BIO_WRITE)
765 g_sched_trace_bio_START(struct bio *bp)
768 CTR5(KTR_GSCHED, "S %lu %c %lu/%lu %lu", g_sched_classify(bp),
769 g_sched_type(bp), bp->bio_offset / ULONG_MAX,
770 bp->bio_offset, bp->bio_length);
774 g_sched_trace_bio_DONE(struct bio *bp)
777 CTR5(KTR_GSCHED, "D %lu %c %lu/%lu %lu", g_sched_classify(bp),
778 g_sched_type(bp), bp->bio_offset / ULONG_MAX,
779 bp->bio_offset, bp->bio_length);
782 #define TRC_BIO_EVENT(e, bp)
786 * g_sched_done() and g_sched_start() dispatch the geom requests to
787 * the scheduling algorithm in use.
790 g_sched_done(struct bio *bio)
792 struct g_geom *gp = bio->bio_caller2;
793 struct g_sched_softc *sc = gp->softc;
795 TRC_BIO_EVENT(DONE, bio);
797 KASSERT(bio->bio_caller1, ("null bio_caller1 in g_sched_done"));
801 g_sched_update_stats(bio);
802 sc->sc_gsched->gs_done(sc->sc_data, bio);
803 if (!--sc->sc_pending)
806 g_sched_flush_classes(gp);
813 g_sched_start(struct bio *bp)
815 struct g_geom *gp = bp->bio_to->geom;
816 struct g_sched_softc *sc = gp->softc;
819 TRC_BIO_EVENT(START, bp);
820 G_SCHED_LOGREQ(bp, "Request received.");
822 cbp = g_clone_bio(bp);
824 g_io_deliver(bp, ENOMEM);
827 cbp->bio_done = g_sched_done;
828 cbp->bio_to = LIST_FIRST(&gp->provider);
829 KASSERT(cbp->bio_to != NULL, ("NULL provider"));
831 /* We only schedule reads and writes. */
832 if (bp->bio_cmd != BIO_READ && bp->bio_cmd != BIO_WRITE)
835 G_SCHED_LOGREQ(cbp, "Sending request.");
839 * Call the algorithm's gs_start to queue the request in the
840 * scheduler. If gs_start fails then pass the request down,
841 * otherwise call g_sched_dispatch() which tries to push
842 * one or more requests down.
844 if (!sc->sc_gsched || (sc->sc_flags & G_SCHED_FLUSHING) ||
845 sc->sc_gsched->gs_start(sc->sc_data, cbp)) {
850 * We use bio_caller1 to mark requests that are scheduled
851 * so make sure it is not NULL.
853 if (cbp->bio_caller1 == NULL)
854 cbp->bio_caller1 = &me; /* anything not NULL */
856 cbp->bio_caller2 = gp;
859 /* Update general stats. */
862 me.gs_bytes_in_flight += bp->bio_length;
863 if (bp->bio_cmd == BIO_WRITE) {
864 me.gs_writes_in_flight++;
865 me.gs_write_bytes_in_flight += bp->bio_length;
867 g_sched_dispatch(gp);
872 cbp->bio_done = g_std_done;
873 cbp->bio_caller1 = NULL; /* not scheduled */
874 g_io_request(cbp, LIST_FIRST(&gp->consumer));
878 * The next few functions are the geom glue.
881 g_sched_orphan(struct g_consumer *cp)
885 g_sched_destroy(cp->geom, 1);
889 g_sched_access(struct g_provider *pp, int dr, int dw, int de)
892 struct g_consumer *cp;
896 cp = LIST_FIRST(&gp->consumer);
897 error = g_access(cp, dr, dw, de);
903 g_sched_temporary_start(struct bio *bio)
906 mtx_lock(&me.gs_mtx);
908 bioq_disksort(&me.gs_pending, bio);
909 mtx_unlock(&me.gs_mtx);
913 g_sched_flush_pending(g_start_t *start)
917 while ((bp = bioq_takefirst(&me.gs_pending)))
922 g_insert_proxy(struct g_geom *gp, struct g_provider *newpp,
923 struct g_geom *dstgp, struct g_provider *pp, struct g_consumer *cp)
925 struct g_sched_softc *sc = gp->softc;
926 g_start_t *saved_start, *flush = g_sched_start;
927 int error = 0, endticks = ticks + hz;
929 g_cancel_event(newpp); /* prevent taste() */
930 /* copy private fields */
931 newpp->private = pp->private;
932 newpp->index = pp->index;
934 /* Queue all the early requests coming for us. */
936 saved_start = pp->geom->start;
937 dstgp->start = g_sched_temporary_start;
939 while (pp->nstart - pp->nend != me.gs_npending &&
940 endticks - ticks >= 0)
941 tsleep(pp, PRIBIO, "-", hz/10);
943 if (pp->nstart - pp->nend != me.gs_npending) {
949 /* link pp to this geom */
950 LIST_REMOVE(pp, provider);
952 LIST_INSERT_HEAD(&gp->provider, pp, provider);
955 * replicate the counts from the parent in the
956 * new provider and consumer nodes
958 cp->acr = newpp->acr = pp->acr;
959 cp->acw = newpp->acw = pp->acw;
960 cp->ace = newpp->ace = pp->ace;
961 sc->sc_flags |= G_SCHED_PROXYING;
964 dstgp->start = saved_start;
966 g_sched_flush_pending(flush);
972 * Create a geom node for the device passed as *pp.
973 * If successful, add a reference to this gsp.
976 g_sched_create(struct gctl_req *req, struct g_class *mp,
977 struct g_provider *pp, struct g_gsched *gsp, int proxy)
979 struct g_sched_softc *sc = NULL;
980 struct g_geom *gp, *dstgp;
981 struct g_provider *newpp = NULL;
982 struct g_consumer *cp = NULL;
988 snprintf(name, sizeof(name), "%s%s", pp->name, G_SCHED_SUFFIX);
989 LIST_FOREACH(gp, &mp->geom, geom) {
990 if (strcmp(gp->name, name) == 0) {
991 gctl_error(req, "Geom %s already exists.",
997 gp = g_new_geomf(mp, "%s", name);
998 dstgp = proxy ? pp->geom : gp; /* where do we link the provider */
1000 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
1001 sc->sc_gsched = gsp;
1002 sc->sc_data = gsp->gs_init(gp);
1003 if (sc->sc_data == NULL) {
1008 sc->sc_hash = g_sched_hash_init(gsp, &sc->sc_mask, HASH_WAITOK);
1011 * Do not initialize the flush mechanism, will be initialized
1012 * on the first insertion on the hash table.
1015 mtx_init(&sc->sc_mtx, "g_sched_mtx", NULL, MTX_DEF);
1018 gp->start = g_sched_start;
1019 gp->orphan = g_sched_orphan;
1020 gp->access = g_sched_access;
1021 gp->dumpconf = g_sched_dumpconf;
1023 newpp = g_new_providerf(dstgp, "%s", gp->name);
1024 newpp->mediasize = pp->mediasize;
1025 newpp->sectorsize = pp->sectorsize;
1027 cp = g_new_consumer(gp);
1028 error = g_attach(cp, proxy ? newpp : pp);
1030 gctl_error(req, "Cannot attach to provider %s.",
1035 g_error_provider(newpp, 0);
1037 error = g_insert_proxy(gp, newpp, dstgp, pp, cp);
1041 G_SCHED_DEBUG(0, "Device %s created.", gp->name);
1049 if (cp->provider != NULL)
1051 g_destroy_consumer(cp);
1054 g_destroy_provider(newpp);
1056 g_sched_hash_fini(gp, sc->sc_hash, sc->sc_mask,
1059 gsp->gs_fini(sc->sc_data);
1067 * Support for dynamic switching of scheduling algorithms.
1068 * First initialize the data structures for the new algorithm,
1069 * then call g_sched_remove_locked() to flush all references
1070 * to the old one, finally link the new algorithm.
1073 g_sched_change_algo(struct gctl_req *req, struct g_class *mp,
1074 struct g_provider *pp, struct g_gsched *gsp)
1076 struct g_sched_softc *sc;
1078 struct g_hash *newh;
1086 data = gsp->gs_init(gp);
1090 newh = g_sched_hash_init(gsp, &mask, HASH_WAITOK);
1091 if (gsp->gs_priv_size && !newh) {
1097 if (sc->sc_gsched) { /* can be NULL in some cases */
1098 error = g_sched_remove_locked(gp, sc->sc_gsched);
1104 sc->sc_gsched = gsp;
1115 g_sched_hash_fini(gp, newh, mask, gsp, data);
1126 * Stop the request flow directed to the proxy, redirecting the new
1127 * requests to the me.gs_pending queue.
1129 static struct g_provider *
1130 g_detach_proxy(struct g_geom *gp)
1132 struct g_consumer *cp;
1133 struct g_provider *pp, *newpp;
1136 pp = LIST_FIRST(&gp->provider);
1139 cp = LIST_FIRST(&gp->consumer);
1142 newpp = cp->provider;
1147 pp->geom->start = g_sched_temporary_start;
1151 printf("%s error detaching proxy %s\n", __FUNCTION__, gp->name);
1157 g_sched_blackhole(struct bio *bp)
1160 g_io_deliver(bp, ENXIO);
1164 g_reparent_provider(struct g_provider *pp, struct g_geom *gp,
1165 struct g_provider *newpp)
1168 LIST_REMOVE(pp, provider);
1170 pp->private = newpp->private;
1171 pp->index = newpp->index;
1174 LIST_INSERT_HEAD(&gp->provider, pp, provider);
1178 g_unproxy_provider(struct g_provider *oldpp, struct g_provider *newpp)
1180 struct g_geom *gp = oldpp->geom;
1182 g_reparent_provider(oldpp, newpp->geom, newpp);
1185 * Hackish: let the system destroy the old provider for us, just
1186 * in case someone attached a consumer to it, in which case a
1187 * direct call to g_destroy_provider() would not work.
1189 g_reparent_provider(newpp, gp, NULL);
1193 * Complete the proxy destruction, linking the old provider to its
1194 * original geom, and destroying the proxy provider. Also take care
1195 * of issuing the pending requests collected in me.gs_pending (if any).
1198 g_destroy_proxy(struct g_geom *gp, struct g_provider *oldpp)
1200 struct g_consumer *cp;
1201 struct g_provider *newpp;
1204 cp = LIST_FIRST(&gp->consumer);
1207 newpp = cp->provider;
1211 /* Relink the provider to its original geom. */
1212 g_unproxy_provider(oldpp, newpp);
1214 /* Detach consumer from provider, and destroy provider. */
1215 cp->acr = newpp->acr = 0;
1216 cp->acw = newpp->acw = 0;
1217 cp->ace = newpp->ace = 0;
1220 /* Send the pending bios through the right start function. */
1221 g_sched_flush_pending(oldpp->geom->start);
1225 printf("%s error destroying proxy %s\n", __FUNCTION__, gp->name);
1227 /* We cannot send the pending bios anywhere... */
1228 g_sched_flush_pending(g_sched_blackhole);
1234 g_sched_destroy(struct g_geom *gp, boolean_t force)
1236 struct g_provider *pp, *oldpp = NULL;
1237 struct g_sched_softc *sc;
1238 struct g_gsched *gsp;
1241 g_topology_assert();
1245 if (!(sc->sc_flags & G_SCHED_PROXYING)) {
1246 pp = LIST_FIRST(&gp->provider);
1247 if (pp && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
1248 const char *msg = force ?
1249 "but we force removal" : "cannot remove";
1251 G_SCHED_DEBUG(!force,
1252 "Device %s is still open (r%dw%de%d), %s.",
1253 pp->name, pp->acr, pp->acw, pp->ace, msg);
1257 G_SCHED_DEBUG(0, "Device %s removed.", gp->name);
1260 oldpp = g_detach_proxy(gp);
1262 gsp = sc->sc_gsched;
1265 * XXX bad hack here: force a dispatch to release
1266 * any reference to the hash table still held by
1271 * We are dying here, no new requests should enter
1272 * the scheduler. This is granted by the topolgy,
1273 * either in case we were proxying (new bios are
1274 * being redirected) or not (see the access check
1277 g_sched_forced_dispatch(gp);
1278 error = g_sched_wait_pending(gp);
1282 * Not all the requests came home: this might happen
1283 * under heavy load, or if we were waiting for any
1284 * bio which is served in the event path (see
1285 * geom_slice.c for an example of how this can
1286 * happen). Try to restore a working configuration
1289 if ((sc->sc_flags & G_SCHED_PROXYING) && oldpp) {
1290 g_sched_flush_pending(force ?
1291 g_sched_blackhole : g_sched_start);
1295 * In the forced destroy case there is not so much
1296 * we can do, we have pending bios that will call
1297 * g_sched_done() somehow, and we don't want them
1298 * to crash the system using freed memory. We tell
1299 * the user that something went wrong, and leak some
1301 * Note: the callers using force = 1 ignore the
1305 G_SCHED_DEBUG(0, "Pending requests while "
1306 " destroying geom, some memory leaked.");
1313 g_sched_hash_fini(gp, sc->sc_hash, sc->sc_mask,
1316 gsp->gs_fini(sc->sc_data);
1317 g_gsched_unref(gsp);
1318 sc->sc_gsched = NULL;
1322 if ((sc->sc_flags & G_SCHED_PROXYING) && oldpp) {
1323 error = g_destroy_proxy(gp, oldpp);
1327 G_SCHED_DEBUG(0, "Unrecoverable error while "
1328 "destroying a proxy geom, leaking some "
1336 mtx_destroy(&sc->sc_mtx);
1340 g_wither_geom(gp, ENXIO);
1346 g_sched_destroy_geom(struct gctl_req *req, struct g_class *mp,
1350 return (g_sched_destroy(gp, 0));
1354 * Functions related to the classification of requests.
1356 * On recent FreeBSD versions (8.0 and above), we store a reference
1357 * to the issuer of a request in bp->bio_classifier1 as soon
1358 * as the bio is posted to the geom queue (and not later, because
1359 * requests are managed by the g_down thread afterwards).
1363 * Classifier support for recent FreeBSD versions: we use
1364 * a very simple classifier, only use curthread to tag a request.
1365 * The classifier is registered at module load, and unregistered
1369 g_sched_tag(void *arg, struct bio *bp)
1372 bp->bio_classifier1 = curthread;
1376 static struct g_classifier_hook g_sched_classifier = {
1377 .func = g_sched_tag,
1381 g_classifier_ini(void)
1384 g_register_classifier(&g_sched_classifier);
1388 g_classifier_fini(void)
1391 g_unregister_classifier(&g_sched_classifier);
1395 g_sched_init(struct g_class *mp)
1398 g_gsched_global_init();
1400 G_SCHED_DEBUG(0, "Loading: mp = %p, g_sched_class = %p.",
1401 mp, &g_sched_class);
1403 /* Patch g_io_request to store classification info in the bio. */
1408 g_sched_fini(struct g_class *mp)
1411 g_classifier_fini();
1413 G_SCHED_DEBUG(0, "Unloading...");
1415 KASSERT(LIST_EMPTY(&me.gs_scheds), ("still registered schedulers"));
1416 mtx_destroy(&me.gs_mtx);
1420 g_sched_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag,
1423 struct g_consumer *cp;
1426 cp = LIST_FIRST(&pp->geom->consumer);
1429 gp = cp->provider->geom;
1430 if (gp->ioctl == NULL)
1432 return (gp->ioctl(cp->provider, cmd, data, fflag, td));
1436 * Read the i-th argument for a request, skipping the /dev/
1437 * prefix if present.
1440 g_sched_argi(struct gctl_req *req, int i)
1442 static const char *dev_prefix = "/dev/";
1445 int l = strlen(dev_prefix);
1447 snprintf(param, sizeof(param), "arg%d", i);
1448 name = gctl_get_asciiparam(req, param);
1450 gctl_error(req, "No 'arg%d' argument", i);
1451 else if (strncmp(name, dev_prefix, l) == 0)
1457 * Fetch nargs and do appropriate checks.
1460 g_sched_get_nargs(struct gctl_req *req)
1464 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1465 if (nargs == NULL) {
1466 gctl_error(req, "No 'nargs' argument");
1470 gctl_error(req, "Missing device(s).");
1475 * Check whether we should add the class on certain volumes when
1476 * this geom is created. Right now this is under control of a kenv
1477 * variable containing the names of all devices that we care about.
1478 * Probably we should only support transparent insertion as the
1479 * preferred mode of operation.
1481 static struct g_geom *
1482 g_sched_taste(struct g_class *mp, struct g_provider *pp,
1485 struct g_gsched *gsp = NULL; /* the . algorithm we want */
1486 const char *s; /* generic string pointer */
1487 const char *taste_names; /* devices we like */
1490 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__,
1491 mp->name, pp->name);
1492 g_topology_assert();
1494 G_SCHED_DEBUG(2, "Tasting %s.", pp->name);
1497 /* do not taste on ourselves */
1498 if (pp->geom->class == mp)
1501 taste_names = kern_getenv("geom.sched.taste");
1502 if (taste_names == NULL)
1505 l = strlen(pp->name);
1506 for (s = taste_names; *s &&
1507 (s = strstr(s, pp->name)); s++) {
1508 /* further checks for an exact match */
1509 if ( (s == taste_names || s[-1] == ' ') &&
1510 (s[l] == '\0' || s[l] == ' ') )
1515 G_SCHED_DEBUG(0, "Attach device %s match [%s]\n",
1518 /* look up the provider name in the list */
1519 s = kern_getenv("geom.sched.algo");
1523 gsp = g_gsched_find(s); /* also get a reference */
1525 G_SCHED_DEBUG(0, "Bad '%s' algorithm.", s);
1529 /* XXX create with 1 as last argument ? */
1530 g_sched_create(NULL, mp, pp, gsp, 0);
1531 g_gsched_unref(gsp);
1537 g_sched_ctl_create(struct gctl_req *req, struct g_class *mp, int proxy)
1539 struct g_provider *pp;
1540 struct g_gsched *gsp;
1544 g_topology_assert();
1546 name = gctl_get_asciiparam(req, "algo");
1548 gctl_error(req, "No '%s' argument", "algo");
1552 gsp = g_gsched_find(name); /* also get a reference */
1554 gctl_error(req, "Bad algorithm '%s'", name);
1558 nargs = g_sched_get_nargs(req);
1561 * Run on the arguments, and break on any error.
1562 * We look for a device name, but skip the /dev/ prefix if any.
1564 for (i = 0; i < nargs; i++) {
1565 name = g_sched_argi(req, i);
1568 pp = g_provider_by_name(name);
1570 G_SCHED_DEBUG(1, "Provider %s is invalid.", name);
1571 gctl_error(req, "Provider %s is invalid.", name);
1574 if (g_sched_create(req, mp, pp, gsp, proxy) != 0)
1578 g_gsched_unref(gsp);
1582 g_sched_ctl_configure(struct gctl_req *req, struct g_class *mp)
1584 struct g_provider *pp;
1585 struct g_gsched *gsp;
1589 g_topology_assert();
1591 name = gctl_get_asciiparam(req, "algo");
1593 gctl_error(req, "No '%s' argument", "algo");
1597 gsp = g_gsched_find(name); /* also get a reference */
1599 gctl_error(req, "Bad algorithm '%s'", name);
1603 nargs = g_sched_get_nargs(req);
1606 * Run on the arguments, and break on any error.
1607 * We look for a device name, but skip the /dev/ prefix if any.
1609 for (i = 0; i < nargs; i++) {
1610 name = g_sched_argi(req, i);
1613 pp = g_provider_by_name(name);
1614 if (pp == NULL || pp->geom->class != mp) {
1615 G_SCHED_DEBUG(1, "Provider %s is invalid.", name);
1616 gctl_error(req, "Provider %s is invalid.", name);
1619 if (g_sched_change_algo(req, mp, pp, gsp) != 0)
1623 g_gsched_unref(gsp);
1626 static struct g_geom *
1627 g_sched_find_geom(struct g_class *mp, const char *name)
1631 LIST_FOREACH(gp, &mp->geom, geom) {
1632 if (strcmp(gp->name, name) == 0)
1639 g_sched_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1641 int nargs, *force, error, i;
1645 g_topology_assert();
1647 nargs = g_sched_get_nargs(req);
1649 force = gctl_get_paraml(req, "force", sizeof(*force));
1650 if (force == NULL) {
1651 gctl_error(req, "No 'force' argument");
1655 for (i = 0; i < nargs; i++) {
1656 name = g_sched_argi(req, i);
1660 gp = g_sched_find_geom(mp, name);
1662 G_SCHED_DEBUG(1, "Device %s is invalid.", name);
1663 gctl_error(req, "Device %s is invalid.", name);
1667 error = g_sched_destroy(gp, *force);
1669 gctl_error(req, "Cannot destroy device %s (error=%d).",
1677 g_sched_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1681 g_topology_assert();
1683 version = gctl_get_paraml(req, "version", sizeof(*version));
1684 if (version == NULL) {
1685 gctl_error(req, "No '%s' argument.", "version");
1689 if (*version != G_SCHED_VERSION) {
1690 gctl_error(req, "Userland and kernel parts are "
1695 if (strcmp(verb, "create") == 0) {
1696 g_sched_ctl_create(req, mp, 0);
1698 } else if (strcmp(verb, "insert") == 0) {
1699 g_sched_ctl_create(req, mp, 1);
1701 } else if (strcmp(verb, "configure") == 0) {
1702 g_sched_ctl_configure(req, mp);
1704 } else if (strcmp(verb, "destroy") == 0) {
1705 g_sched_ctl_destroy(req, mp);
1709 gctl_error(req, "Unknown verb.");
1713 g_sched_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1714 struct g_consumer *cp, struct g_provider *pp)
1716 struct g_sched_softc *sc = gp->softc;
1717 struct g_gsched *gsp = sc->sc_gsched;
1718 if (indent == NULL) { /* plaintext */
1719 sbuf_printf(sb, " algo %s", gsp ? gsp->gs_name : "--");
1721 if (gsp != NULL && gsp->gs_dumpconf)
1722 gsp->gs_dumpconf(sb, indent, gp, cp, pp);
1725 DECLARE_GEOM_CLASS(g_sched_class, g_sched);
1726 MODULE_VERSION(geom_sched, 0);