GNU Linux-libre 6.1.91-gnu
[releases.git] / Documentation / filesystems / directory-locking.rst
1 =================
2 Directory Locking
3 =================
4
5
6 Locking scheme used for directory operations is based on two
7 kinds of locks - per-inode (->i_rwsem) and per-filesystem
8 (->s_vfs_rename_mutex).
9
10 When taking the i_rwsem on multiple non-directory objects, we
11 always acquire the locks in order by increasing address.  We'll call
12 that "inode pointer" order in the following.
13
14 For our purposes all operations fall in 5 classes:
15
16 1) read access.  Locking rules: caller locks directory we are accessing.
17 The lock is taken shared.
18
19 2) object creation.  Locking rules: same as above, but the lock is taken
20 exclusive.
21
22 3) object removal.  Locking rules: caller locks parent, finds victim,
23 locks victim and calls the method.  Locks are exclusive.
24
25 4) rename() that is _not_ cross-directory.  Locking rules: caller locks
26 the parent and finds source and target.  Then we decide which of the
27 source and target need to be locked.  Source needs to be locked if it's a
28 non-directory; target - if it's a non-directory or about to be removed.
29 Take the locks that need to be taken, in inode pointer order if need
30 to take both (that can happen only when both source and target are
31 non-directories - the source because it wouldn't be locked otherwise
32 and the target because mixing directory and non-directory is allowed
33 only with RENAME_EXCHANGE, and that won't be removing the target).
34 After the locks had been taken, call the method.  All locks are exclusive.
35
36 5) link creation.  Locking rules:
37
38         * lock parent
39         * check that source is not a directory
40         * lock source
41         * call the method.
42
43 All locks are exclusive.
44
45 6) cross-directory rename.  The trickiest in the whole bunch.  Locking
46 rules:
47
48         * lock the filesystem
49         * lock parents in "ancestors first" order. If one is not ancestor of
50           the other, lock the parent of source first.
51         * find source and target.
52         * if old parent is equal to or is a descendent of target
53           fail with -ENOTEMPTY
54         * if new parent is equal to or is a descendent of source
55           fail with -ELOOP
56         * Lock subdirectories involved (source before target).
57         * Lock non-directories involved, in inode pointer order.
58         * call the method.
59
60 All ->i_rwsem are taken exclusive.
61
62 The rules above obviously guarantee that all directories that are going to be
63 read, modified or removed by method will be locked by caller.
64
65
66 If no directory is its own ancestor, the scheme above is deadlock-free.
67
68 Proof:
69
70 [XXX: will be updated once we are done massaging the lock_rename()]
71         First of all, at any moment we have a linear ordering of the
72         objects - A < B iff (A is an ancestor of B) or (B is not an ancestor
73         of A and ptr(A) < ptr(B)).
74
75         That ordering can change.  However, the following is true:
76
77 (1) if object removal or non-cross-directory rename holds lock on A and
78     attempts to acquire lock on B, A will remain the parent of B until we
79     acquire the lock on B.  (Proof: only cross-directory rename can change
80     the parent of object and it would have to lock the parent).
81
82 (2) if cross-directory rename holds the lock on filesystem, order will not
83     change until rename acquires all locks.  (Proof: other cross-directory
84     renames will be blocked on filesystem lock and we don't start changing
85     the order until we had acquired all locks).
86
87 (3) locks on non-directory objects are acquired only after locks on
88     directory objects, and are acquired in inode pointer order.
89     (Proof: all operations but renames take lock on at most one
90     non-directory object, except renames, which take locks on source and
91     target in inode pointer order in the case they are not directories.)
92
93 Now consider the minimal deadlock.  Each process is blocked on
94 attempt to acquire some lock and already holds at least one lock.  Let's
95 consider the set of contended locks.  First of all, filesystem lock is
96 not contended, since any process blocked on it is not holding any locks.
97 Thus all processes are blocked on ->i_rwsem.
98
99 By (3), any process holding a non-directory lock can only be
100 waiting on another non-directory lock with a larger address.  Therefore
101 the process holding the "largest" such lock can always make progress, and
102 non-directory objects are not included in the set of contended locks.
103
104 Thus link creation can't be a part of deadlock - it can't be
105 blocked on source and it means that it doesn't hold any locks.
106
107 Any contended object is either held by cross-directory rename or
108 has a child that is also contended.  Indeed, suppose that it is held by
109 operation other than cross-directory rename.  Then the lock this operation
110 is blocked on belongs to child of that object due to (1).
111
112 It means that one of the operations is cross-directory rename.
113 Otherwise the set of contended objects would be infinite - each of them
114 would have a contended child and we had assumed that no object is its
115 own descendent.  Moreover, there is exactly one cross-directory rename
116 (see above).
117
118 Consider the object blocking the cross-directory rename.  One
119 of its descendents is locked by cross-directory rename (otherwise we
120 would again have an infinite set of contended objects).  But that
121 means that cross-directory rename is taking locks out of order.  Due
122 to (2) the order hadn't changed since we had acquired filesystem lock.
123 But locking rules for cross-directory rename guarantee that we do not
124 try to acquire lock on descendent before the lock on ancestor.
125 Contradiction.  I.e.  deadlock is impossible.  Q.E.D.
126
127
128 These operations are guaranteed to avoid loop creation.  Indeed,
129 the only operation that could introduce loops is cross-directory rename.
130 Since the only new (parent, child) pair added by rename() is (new parent,
131 source), such loop would have to contain these objects and the rest of it
132 would have to exist before rename().  I.e. at the moment of loop creation
133 rename() responsible for that would be holding filesystem lock and new parent
134 would have to be equal to or a descendent of source.  But that means that
135 new parent had been equal to or a descendent of source since the moment when
136 we had acquired filesystem lock and rename() would fail with -ELOOP in that
137 case.
138
139 While this locking scheme works for arbitrary DAGs, it relies on
140 ability to check that directory is a descendent of another object.  Current
141 implementation assumes that directory graph is a tree.  This assumption is
142 also preserved by all operations (cross-directory rename on a tree that would
143 not introduce a cycle will leave it a tree and link() fails for directories).
144
145 Notice that "directory" in the above == "anything that might have
146 children", so if we are going to introduce hybrid objects we will need
147 either to make sure that link(2) doesn't work for them or to make changes
148 in is_subdir() that would make it work even in presence of such beasts.