dccd61c7c5c3bef2f12506dd3de986e894c8edda
[platform/kernel/linux-rpi.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 the
26 parent and finds source and target.  We lock both (provided they exist).  If we
27 need to lock two inodes of different type (dir vs non-dir), we lock directory
28 first.  If we need to lock two inodes of the same type, lock them in inode
29 pointer order.  Then call the method.  All locks are exclusive.
30 NB: we might get away with locking the source (and target in exchange
31 case) shared.
32
33 5) link creation.  Locking rules:
34
35         * lock parent
36         * check that source is not a directory
37         * lock source
38         * call the method.
39
40 All locks are exclusive.
41
42 6) cross-directory rename.  The trickiest in the whole bunch.  Locking
43 rules:
44
45         * lock the filesystem
46         * lock parents in "ancestors first" order. If one is not ancestor of
47           the other, lock them in inode pointer order.
48         * find source and target.
49         * if old parent is equal to or is a descendent of target
50           fail with -ENOTEMPTY
51         * if new parent is equal to or is a descendent of source
52           fail with -ELOOP
53         * Lock both the source and the target provided they exist. If we
54           need to lock two inodes of different type (dir vs non-dir), we lock
55           the directory first. If we need to lock two inodes of the same type,
56           lock them in inode pointer order.
57         * call the method.
58
59 All ->i_rwsem are taken exclusive.  Again, we might get away with locking
60 the source (and target in exchange case) shared.
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         First of all, at any moment we have a linear ordering of the
71         objects - A < B iff (A is an ancestor of B) or (B is not an ancestor
72         of A and ptr(A) < ptr(B)).
73
74         That ordering can change.  However, the following is true:
75
76 (1) if object removal or non-cross-directory rename holds lock on A and
77     attempts to acquire lock on B, A will remain the parent of B until we
78     acquire the lock on B.  (Proof: only cross-directory rename can change
79     the parent of object and it would have to lock the parent).
80
81 (2) if cross-directory rename holds the lock on filesystem, order will not
82     change until rename acquires all locks.  (Proof: other cross-directory
83     renames will be blocked on filesystem lock and we don't start changing
84     the order until we had acquired all locks).
85
86 (3) locks on non-directory objects are acquired only after locks on
87     directory objects, and are acquired in inode pointer order.
88     (Proof: all operations but renames take lock on at most one
89     non-directory object, except renames, which take locks on source and
90     target in inode pointer order in the case they are not directories.)
91
92 Now consider the minimal deadlock.  Each process is blocked on
93 attempt to acquire some lock and already holds at least one lock.  Let's
94 consider the set of contended locks.  First of all, filesystem lock is
95 not contended, since any process blocked on it is not holding any locks.
96 Thus all processes are blocked on ->i_rwsem.
97
98 By (3), any process holding a non-directory lock can only be
99 waiting on another non-directory lock with a larger address.  Therefore
100 the process holding the "largest" such lock can always make progress, and
101 non-directory objects are not included in the set of contended locks.
102
103 Thus link creation can't be a part of deadlock - it can't be
104 blocked on source and it means that it doesn't hold any locks.
105
106 Any contended object is either held by cross-directory rename or
107 has a child that is also contended.  Indeed, suppose that it is held by
108 operation other than cross-directory rename.  Then the lock this operation
109 is blocked on belongs to child of that object due to (1).
110
111 It means that one of the operations is cross-directory rename.
112 Otherwise the set of contended objects would be infinite - each of them
113 would have a contended child and we had assumed that no object is its
114 own descendent.  Moreover, there is exactly one cross-directory rename
115 (see above).
116
117 Consider the object blocking the cross-directory rename.  One
118 of its descendents is locked by cross-directory rename (otherwise we
119 would again have an infinite set of contended objects).  But that
120 means that cross-directory rename is taking locks out of order.  Due
121 to (2) the order hadn't changed since we had acquired filesystem lock.
122 But locking rules for cross-directory rename guarantee that we do not
123 try to acquire lock on descendent before the lock on ancestor.
124 Contradiction.  I.e.  deadlock is impossible.  Q.E.D.
125
126
127 These operations are guaranteed to avoid loop creation.  Indeed,
128 the only operation that could introduce loops is cross-directory rename.
129 Since the only new (parent, child) pair added by rename() is (new parent,
130 source), such loop would have to contain these objects and the rest of it
131 would have to exist before rename().  I.e. at the moment of loop creation
132 rename() responsible for that would be holding filesystem lock and new parent
133 would have to be equal to or a descendent of source.  But that means that
134 new parent had been equal to or a descendent of source since the moment when
135 we had acquired filesystem lock and rename() would fail with -ELOOP in that
136 case.
137
138 While this locking scheme works for arbitrary DAGs, it relies on
139 ability to check that directory is a descendent of another object.  Current
140 implementation assumes that directory graph is a tree.  This assumption is
141 also preserved by all operations (cross-directory rename on a tree that would
142 not introduce a cycle will leave it a tree and link() fails for directories).
143
144 Notice that "directory" in the above == "anything that might have
145 children", so if we are going to introduce hybrid objects we will need
146 either to make sure that link(2) doesn't work for them or to make changes
147 in is_subdir() that would make it work even in presence of such beasts.