xref: /freebsd/sys/geom/notes (revision 801bb689ca8be76700b0c16c159683b5fa89472d)
1801bb689SPoul-Henning Kamp$FreeBSD$
2801bb689SPoul-Henning Kamp
3801bb689SPoul-Henning KampFor the lack of a better place to put them, this file will contain
4801bb689SPoul-Henning Kampnotes on some of the more intricate details of geom.
5801bb689SPoul-Henning Kamp
6801bb689SPoul-Henning Kamp-----------------------------------------------------------------------
7801bb689SPoul-Henning KampLocking of bio_children and bio_inbed
8801bb689SPoul-Henning Kamp
9801bb689SPoul-Henning Kampbio_children is used by g_std_done() and g_clone_bio() to keep track
10801bb689SPoul-Henning Kampof children cloned off a request.  g_clone_bio will increment the
11801bb689SPoul-Henning Kampbio_children counter for each time it is called and g_std_done will
12801bb689SPoul-Henning Kampincrement bio_inbed for every call, and if the two counters are
13801bb689SPoul-Henning Kampequal, call g_io_deliver() on the parent bio.
14801bb689SPoul-Henning Kamp
15801bb689SPoul-Henning KampThe general assumption is that g_clone_bio() is called only in
16801bb689SPoul-Henning Kampthe g_down thread, and g_std_done() only in the g_up thread and
17801bb689SPoul-Henning Kamptherefore the two fields do not generally need locking.  These
18801bb689SPoul-Henning Kamprestrictions are not enforced by the code, but only with great
19801bb689SPoul-Henning Kampcare should they be violated.
20801bb689SPoul-Henning Kamp
21801bb689SPoul-Henning KampIt is the responsibility of the class implementation to avoid the
22801bb689SPoul-Henning Kampfollowing race condition:  A class intend to split a bio in two
23801bb689SPoul-Henning Kampchildren.  It clones the bio, and requests I/O on the child.
24801bb689SPoul-Henning KampThis I/O operation completes before the second child is cloned
25801bb689SPoul-Henning Kampand g_std_done() sees the counters both equal 1 and finishes off
26801bb689SPoul-Henning Kampthe bio.
27801bb689SPoul-Henning Kamp
28801bb689SPoul-Henning KampThere is no race present in the common case where the bio is split
29801bb689SPoul-Henning Kampin multiple parts in the class start method and the I/O is requested
30801bb689SPoul-Henning Kampon another GEOM class below:  There is only one g_down thread and
31801bb689SPoul-Henning Kampthe class below will not get its start method run until we return
32801bb689SPoul-Henning Kampfrom our start method, and consequently the I/O cannot complete
33801bb689SPoul-Henning Kampprematurely.
34801bb689SPoul-Henning Kamp
35801bb689SPoul-Henning KampIn all other cases, this race needs to be mitigated, for instance
36801bb689SPoul-Henning Kampby cloning all children before I/O is request on any of them.
37801bb689SPoul-Henning Kamp
38801bb689SPoul-Henning KampNotice that cloning an "extra" child and calling g_std_done() on
39801bb689SPoul-Henning Kampit directly opens another race since the assumption is that
40801bb689SPoul-Henning Kampg_std_done() only is called in the g_up thread.
41