1- Locking scheme used for directory operations is based on two
1+ =================
2+ Directory Locking
3+ =================
4+
5+
6+ Locking scheme used for directory operations is based on two
27kinds of locks - per-inode (->i_rwsem) and per-filesystem
38(->s_vfs_rename_mutex).
49
5- When taking the i_rwsem on multiple non-directory objects, we
10+ When taking the i_rwsem on multiple non-directory objects, we
611always acquire the locks in order by increasing address. We'll call
712that "inode pointer" order in the following.
813
9- For our purposes all operations fall in 5 classes:
14+ For our purposes all operations fall in 5 classes:
1015
11161) read access. Locking rules: caller locks directory we are accessing.
1217The lock is taken shared.
@@ -27,25 +32,29 @@ NB: we might get away with locking the the source (and target in exchange
2732case) shared.
2833
29345) link creation. Locking rules:
35+
3036 * lock parent
3137 * check that source is not a directory
3238 * lock source
3339 * call the method.
40+
3441All locks are exclusive.
3542
36436) cross-directory rename. The trickiest in the whole bunch. Locking
3744rules:
45+
3846 * lock the filesystem
3947 * lock parents in "ancestors first" order.
4048 * find source and target.
4149 * if old parent is equal to or is a descendent of target
42- fail with -ENOTEMPTY
50+ fail with -ENOTEMPTY
4351 * if new parent is equal to or is a descendent of source
44- fail with -ELOOP
52+ fail with -ELOOP
4553 * If it's an exchange, lock both the source and the target.
4654 * If the target exists, lock it. If the source is a non-directory,
4755 lock it. If we need to lock both, do so in inode pointer order.
4856 * call the method.
57+
4958All ->i_rwsem are taken exclusive. Again, we might get away with locking
5059the the source (and target in exchange case) shared.
5160
@@ -54,10 +63,11 @@ read, modified or removed by method will be locked by caller.
5463
5564
5665If no directory is its own ancestor, the scheme above is deadlock-free.
66+
5767Proof:
5868
5969 First of all, at any moment we have a partial ordering of the
60- objects - A < B iff A is an ancestor of B.
70+ objects - A < B iff A is an ancestor of B.
6171
6272 That ordering can change. However, the following is true:
6373
@@ -77,32 +87,32 @@ objects - A < B iff A is an ancestor of B.
7787 non-directory object, except renames, which take locks on source and
7888 target in inode pointer order in the case they are not directories.)
7989
80- Now consider the minimal deadlock. Each process is blocked on
90+ Now consider the minimal deadlock. Each process is blocked on
8191attempt to acquire some lock and already holds at least one lock. Let's
8292consider the set of contended locks. First of all, filesystem lock is
8393not contended, since any process blocked on it is not holding any locks.
8494Thus all processes are blocked on ->i_rwsem.
8595
86- By (3), any process holding a non-directory lock can only be
96+ By (3), any process holding a non-directory lock can only be
8797waiting on another non-directory lock with a larger address. Therefore
8898the process holding the "largest" such lock can always make progress, and
8999non-directory objects are not included in the set of contended locks.
90100
91- Thus link creation can't be a part of deadlock - it can't be
101+ Thus link creation can't be a part of deadlock - it can't be
92102blocked on source and it means that it doesn't hold any locks.
93103
94- Any contended object is either held by cross-directory rename or
104+ Any contended object is either held by cross-directory rename or
95105has a child that is also contended. Indeed, suppose that it is held by
96106operation other than cross-directory rename. Then the lock this operation
97107is blocked on belongs to child of that object due to (1).
98108
99- It means that one of the operations is cross-directory rename.
109+ It means that one of the operations is cross-directory rename.
100110Otherwise the set of contended objects would be infinite - each of them
101111would have a contended child and we had assumed that no object is its
102112own descendent. Moreover, there is exactly one cross-directory rename
103113(see above).
104114
105- Consider the object blocking the cross-directory rename. One
115+ Consider the object blocking the cross-directory rename. One
106116of its descendents is locked by cross-directory rename (otherwise we
107117would again have an infinite set of contended objects). But that
108118means that cross-directory rename is taking locks out of order. Due
@@ -112,7 +122,7 @@ try to acquire lock on descendent before the lock on ancestor.
112122Contradiction. I.e. deadlock is impossible. Q.E.D.
113123
114124
115- These operations are guaranteed to avoid loop creation. Indeed,
125+ These operations are guaranteed to avoid loop creation. Indeed,
116126the only operation that could introduce loops is cross-directory rename.
117127Since the only new (parent, child) pair added by rename() is (new parent,
118128source), such loop would have to contain these objects and the rest of it
@@ -123,13 +133,13 @@ new parent had been equal to or a descendent of source since the moment when
123133we had acquired filesystem lock and rename() would fail with -ELOOP in that
124134case.
125135
126- While this locking scheme works for arbitrary DAGs, it relies on
136+ While this locking scheme works for arbitrary DAGs, it relies on
127137ability to check that directory is a descendent of another object. Current
128138implementation assumes that directory graph is a tree. This assumption is
129139also preserved by all operations (cross-directory rename on a tree that would
130140not introduce a cycle will leave it a tree and link() fails for directories).
131141
132- Notice that "directory" in the above == "anything that might have
142+ Notice that "directory" in the above == "anything that might have
133143children", so if we are going to introduce hybrid objects we will need
134144either to make sure that link(2) doesn't work for them or to make changes
135145in is_subdir() that would make it work even in presence of such beasts.
0 commit comments