blob: 504ba940c36c177071f8c509fdeaf4f1cc368273 [file] [log] [blame]
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -03001=================
2Directory Locking
3=================
4
5
6Locking scheme used for directory operations is based on two
Al Virod42b3862016-05-26 00:04:18 -04007kinds of locks - per-inode (->i_rwsem) and per-filesystem
Josef 'Jeff' Sipekc2b38982007-05-24 12:21:43 -04008(->s_vfs_rename_mutex).
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030010When taking the i_rwsem on multiple non-directory objects, we
J. Bruce Fields6cedba82012-03-05 11:40:41 -050011always acquire the locks in order by increasing address. We'll call
12that "inode pointer" order in the following.
13
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030014For our purposes all operations fall in 5 classes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
161) read access. Locking rules: caller locks directory we are accessing.
Al Virod42b3862016-05-26 00:04:18 -040017The lock is taken shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Al Virod42b3862016-05-26 00:04:18 -0400192) object creation. Locking rules: same as above, but the lock is taken
20exclusive.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
223) object removal. Locking rules: caller locks parent, finds victim,
Al Virod42b3862016-05-26 00:04:18 -040023locks victim and calls the method. Locks are exclusive.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
254) rename() that is _not_ cross-directory. Locking rules: caller locks
Al Virod42b3862016-05-26 00:04:18 -040026the parent and finds source and target. In case of exchange (with
Miklos Szeredi18fc84d2016-09-27 11:03:58 +020027RENAME_EXCHANGE in flags argument) lock both. In any case,
Al Virod42b3862016-05-26 00:04:18 -040028if the target already exists, lock it. If the source is a non-directory,
29lock it. If we need to lock both, lock them in inode pointer order.
30Then call the method. All locks are exclusive.
Randy Dunlap2f322952020-07-03 14:43:19 -070031NB: we might get away with locking the source (and target in exchange
Al Virod42b3862016-05-26 00:04:18 -040032case) shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
345) link creation. Locking rules:
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * lock parent
37 * check that source is not a directory
38 * lock source
39 * call the method.
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030040
Al Virod42b3862016-05-26 00:04:18 -040041All locks are exclusive.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
436) cross-directory rename. The trickiest in the whole bunch. Locking
44rules:
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * lock the filesystem
47 * lock parents in "ancestors first" order.
48 * find source and target.
49 * if old parent is equal to or is a descendent of target
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030050 fail with -ENOTEMPTY
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * if new parent is equal to or is a descendent of source
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030052 fail with -ELOOP
Al Virod42b3862016-05-26 00:04:18 -040053 * If it's an exchange, lock both the source and the target.
54 * If the target exists, lock it. If the source is a non-directory,
55 lock it. If we need to lock both, do so in inode pointer order.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * call the method.
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030057
Al Virod42b3862016-05-26 00:04:18 -040058All ->i_rwsem are taken exclusive. Again, we might get away with locking
Randy Dunlap2f322952020-07-03 14:43:19 -070059the source (and target in exchange case) shared.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61The rules above obviously guarantee that all directories that are going to be
62read, modified or removed by method will be locked by caller.
63
64
65If no directory is its own ancestor, the scheme above is deadlock-free.
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067Proof:
68
69 First of all, at any moment we have a partial ordering of the
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030070 objects - A < B iff A is an ancestor of B.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 That ordering can change. However, the following is true:
73
74(1) if object removal or non-cross-directory rename holds lock on A and
75 attempts to acquire lock on B, A will remain the parent of B until we
76 acquire the lock on B. (Proof: only cross-directory rename can change
77 the parent of object and it would have to lock the parent).
78
79(2) if cross-directory rename holds the lock on filesystem, order will not
80 change until rename acquires all locks. (Proof: other cross-directory
81 renames will be blocked on filesystem lock and we don't start changing
82 the order until we had acquired all locks).
83
J. Bruce Fields6cedba82012-03-05 11:40:41 -050084(3) locks on non-directory objects are acquired only after locks on
85 directory objects, and are acquired in inode pointer order.
86 (Proof: all operations but renames take lock on at most one
87 non-directory object, except renames, which take locks on source and
88 target in inode pointer order in the case they are not directories.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030090Now consider the minimal deadlock. Each process is blocked on
Linus Torvalds1da177e2005-04-16 15:20:36 -070091attempt to acquire some lock and already holds at least one lock. Let's
92consider the set of contended locks. First of all, filesystem lock is
93not contended, since any process blocked on it is not holding any locks.
Al Virod42b3862016-05-26 00:04:18 -040094Thus all processes are blocked on ->i_rwsem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -030096By (3), any process holding a non-directory lock can only be
J. Bruce Fields6cedba82012-03-05 11:40:41 -050097waiting on another non-directory lock with a larger address. Therefore
98the process holding the "largest" such lock can always make progress, and
99non-directory objects are not included in the set of contended locks.
100
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -0300101Thus link creation can't be a part of deadlock - it can't be
J. Bruce Fields6cedba82012-03-05 11:40:41 -0500102blocked on source and it means that it doesn't hold any locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -0300104Any contended object is either held by cross-directory rename or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105has a child that is also contended. Indeed, suppose that it is held by
106operation other than cross-directory rename. Then the lock this operation
107is blocked on belongs to child of that object due to (1).
108
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -0300109It means that one of the operations is cross-directory rename.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110Otherwise the set of contended objects would be infinite - each of them
111would have a contended child and we had assumed that no object is its
112own descendent. Moreover, there is exactly one cross-directory rename
113(see above).
114
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -0300115Consider the object blocking the cross-directory rename. One
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116of its descendents is locked by cross-directory rename (otherwise we
Paolo Ornati670e9f32006-10-03 22:57:56 +0200117would again have an infinite set of contended objects). But that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118means that cross-directory rename is taking locks out of order. Due
119to (2) the order hadn't changed since we had acquired filesystem lock.
120But locking rules for cross-directory rename guarantee that we do not
121try to acquire lock on descendent before the lock on ancestor.
122Contradiction. I.e. deadlock is impossible. Q.E.D.
123
124
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -0300125These operations are guaranteed to avoid loop creation. Indeed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126the only operation that could introduce loops is cross-directory rename.
127Since the only new (parent, child) pair added by rename() is (new parent,
128source), such loop would have to contain these objects and the rest of it
129would have to exist before rename(). I.e. at the moment of loop creation
130rename() responsible for that would be holding filesystem lock and new parent
131would have to be equal to or a descendent of source. But that means that
132new parent had been equal to or a descendent of source since the moment when
133we had acquired filesystem lock and rename() would fail with -ELOOP in that
134case.
135
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -0300136While this locking scheme works for arbitrary DAGs, it relies on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137ability to check that directory is a descendent of another object. Current
138implementation assumes that directory graph is a tree. This assumption is
139also preserved by all operations (cross-directory rename on a tree that would
140not introduce a cycle will leave it a tree and link() fails for directories).
141
Mauro Carvalho Chehabec23eb52019-07-26 09:51:27 -0300142Notice that "directory" in the above == "anything that might have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143children", so if we are going to introduce hybrid objects we will need
144either to make sure that link(2) doesn't work for them or to make changes
145in is_subdir() that would make it work even in presence of such beasts.