notes revision 110523
1$FreeBSD: head/sys/geom/notes 110523 2003-02-07 23:08:24Z phk $
2
3For the lack of a better place to put them, this file will contain
4notes on some of the more intricate details of geom.
5
6-----------------------------------------------------------------------
7Locking of bio_children and bio_inbed
8
9bio_children is used by g_std_done() and g_clone_bio() to keep track
10of children cloned off a request.  g_clone_bio will increment the
11bio_children counter for each time it is called and g_std_done will
12increment bio_inbed for every call, and if the two counters are
13equal, call g_io_deliver() on the parent bio.
14
15The general assumption is that g_clone_bio() is called only in
16the g_down thread, and g_std_done() only in the g_up thread and
17therefore the two fields do not generally need locking.  These
18restrictions are not enforced by the code, but only with great
19care should they be violated.
20
21It is the responsibility of the class implementation to avoid the
22following race condition:  A class intend to split a bio in two
23children.  It clones the bio, and requests I/O on the child. 
24This I/O operation completes before the second child is cloned
25and g_std_done() sees the counters both equal 1 and finishes off
26the bio.
27
28There is no race present in the common case where the bio is split
29in multiple parts in the class start method and the I/O is requested
30on another GEOM class below:  There is only one g_down thread and
31the class below will not get its start method run until we return
32from our start method, and consequently the I/O cannot complete
33prematurely.
34
35In all other cases, this race needs to be mitigated, for instance
36by cloning all children before I/O is request on any of them.
37
38Notice that cloning an "extra" child and calling g_std_done() on
39it directly opens another race since the assumption is that
40g_std_done() only is called in the g_up thread.
41