1ec23eb54SMauro Carvalho Chehab================= 2ec23eb54SMauro Carvalho ChehabDirectory Locking 3ec23eb54SMauro Carvalho Chehab================= 4ec23eb54SMauro Carvalho Chehab 5ec23eb54SMauro Carvalho Chehab 6ec23eb54SMauro Carvalho ChehabLocking scheme used for directory operations is based on two 7ec23eb54SMauro Carvalho Chehabkinds of locks - per-inode (->i_rwsem) and per-filesystem 8ec23eb54SMauro Carvalho Chehab(->s_vfs_rename_mutex). 9ec23eb54SMauro Carvalho Chehab 10ec23eb54SMauro Carvalho ChehabWhen taking the i_rwsem on multiple non-directory objects, we 11ec23eb54SMauro Carvalho Chehabalways acquire the locks in order by increasing address. We'll call 12ec23eb54SMauro Carvalho Chehabthat "inode pointer" order in the following. 13ec23eb54SMauro Carvalho Chehab 14ec23eb54SMauro Carvalho ChehabFor our purposes all operations fall in 5 classes: 15ec23eb54SMauro Carvalho Chehab 16ec23eb54SMauro Carvalho Chehab1) read access. Locking rules: caller locks directory we are accessing. 17ec23eb54SMauro Carvalho ChehabThe lock is taken shared. 18ec23eb54SMauro Carvalho Chehab 19ec23eb54SMauro Carvalho Chehab2) object creation. Locking rules: same as above, but the lock is taken 20ec23eb54SMauro Carvalho Chehabexclusive. 21ec23eb54SMauro Carvalho Chehab 22ec23eb54SMauro Carvalho Chehab3) object removal. Locking rules: caller locks parent, finds victim, 23ec23eb54SMauro Carvalho Chehablocks victim and calls the method. Locks are exclusive. 24ec23eb54SMauro Carvalho Chehab 25ec23eb54SMauro Carvalho Chehab4) rename() that is _not_ cross-directory. Locking rules: caller locks 26ec23eb54SMauro Carvalho Chehabthe parent and finds source and target. In case of exchange (with 27ec23eb54SMauro Carvalho ChehabRENAME_EXCHANGE in flags argument) lock both. In any case, 28ec23eb54SMauro Carvalho Chehabif the target already exists, lock it. If the source is a non-directory, 29ec23eb54SMauro Carvalho Chehablock it. If we need to lock both, lock them in inode pointer order. 30ec23eb54SMauro Carvalho ChehabThen call the method. All locks are exclusive. 31ec23eb54SMauro Carvalho ChehabNB: we might get away with locking the the source (and target in exchange 32ec23eb54SMauro Carvalho Chehabcase) shared. 33ec23eb54SMauro Carvalho Chehab 34ec23eb54SMauro Carvalho Chehab5) link creation. Locking rules: 35ec23eb54SMauro Carvalho Chehab 36ec23eb54SMauro Carvalho Chehab * lock parent 37ec23eb54SMauro Carvalho Chehab * check that source is not a directory 38ec23eb54SMauro Carvalho Chehab * lock source 39ec23eb54SMauro Carvalho Chehab * call the method. 40ec23eb54SMauro Carvalho Chehab 41ec23eb54SMauro Carvalho ChehabAll locks are exclusive. 42ec23eb54SMauro Carvalho Chehab 43ec23eb54SMauro Carvalho Chehab6) cross-directory rename. The trickiest in the whole bunch. Locking 44ec23eb54SMauro Carvalho Chehabrules: 45ec23eb54SMauro Carvalho Chehab 46ec23eb54SMauro Carvalho Chehab * lock the filesystem 47ec23eb54SMauro Carvalho Chehab * lock parents in "ancestors first" order. 48ec23eb54SMauro Carvalho Chehab * find source and target. 49ec23eb54SMauro Carvalho Chehab * if old parent is equal to or is a descendent of target 50ec23eb54SMauro Carvalho Chehab fail with -ENOTEMPTY 51ec23eb54SMauro Carvalho Chehab * if new parent is equal to or is a descendent of source 52ec23eb54SMauro Carvalho Chehab fail with -ELOOP 53ec23eb54SMauro Carvalho Chehab * If it's an exchange, lock both the source and the target. 54ec23eb54SMauro Carvalho Chehab * If the target exists, lock it. If the source is a non-directory, 55ec23eb54SMauro Carvalho Chehab lock it. If we need to lock both, do so in inode pointer order. 56ec23eb54SMauro Carvalho Chehab * call the method. 57ec23eb54SMauro Carvalho Chehab 58ec23eb54SMauro Carvalho ChehabAll ->i_rwsem are taken exclusive. Again, we might get away with locking 59ec23eb54SMauro Carvalho Chehabthe the source (and target in exchange case) shared. 60ec23eb54SMauro Carvalho Chehab 61ec23eb54SMauro Carvalho ChehabThe rules above obviously guarantee that all directories that are going to be 62ec23eb54SMauro Carvalho Chehabread, modified or removed by method will be locked by caller. 63ec23eb54SMauro Carvalho Chehab 64ec23eb54SMauro Carvalho Chehab 65ec23eb54SMauro Carvalho ChehabIf no directory is its own ancestor, the scheme above is deadlock-free. 66ec23eb54SMauro Carvalho Chehab 67ec23eb54SMauro Carvalho ChehabProof: 68ec23eb54SMauro Carvalho Chehab 69ec23eb54SMauro Carvalho Chehab First of all, at any moment we have a partial ordering of the 70ec23eb54SMauro Carvalho Chehab objects - A < B iff A is an ancestor of B. 71ec23eb54SMauro Carvalho Chehab 72ec23eb54SMauro Carvalho Chehab That ordering can change. However, the following is true: 73ec23eb54SMauro Carvalho Chehab 74ec23eb54SMauro Carvalho Chehab(1) if object removal or non-cross-directory rename holds lock on A and 75ec23eb54SMauro Carvalho Chehab attempts to acquire lock on B, A will remain the parent of B until we 76ec23eb54SMauro Carvalho Chehab acquire the lock on B. (Proof: only cross-directory rename can change 77ec23eb54SMauro Carvalho Chehab the parent of object and it would have to lock the parent). 78ec23eb54SMauro Carvalho Chehab 79ec23eb54SMauro Carvalho Chehab(2) if cross-directory rename holds the lock on filesystem, order will not 80ec23eb54SMauro Carvalho Chehab change until rename acquires all locks. (Proof: other cross-directory 81ec23eb54SMauro Carvalho Chehab renames will be blocked on filesystem lock and we don't start changing 82ec23eb54SMauro Carvalho Chehab the order until we had acquired all locks). 83ec23eb54SMauro Carvalho Chehab 84ec23eb54SMauro Carvalho Chehab(3) locks on non-directory objects are acquired only after locks on 85ec23eb54SMauro Carvalho Chehab directory objects, and are acquired in inode pointer order. 86ec23eb54SMauro Carvalho Chehab (Proof: all operations but renames take lock on at most one 87ec23eb54SMauro Carvalho Chehab non-directory object, except renames, which take locks on source and 88ec23eb54SMauro Carvalho Chehab target in inode pointer order in the case they are not directories.) 89ec23eb54SMauro Carvalho Chehab 90ec23eb54SMauro Carvalho ChehabNow consider the minimal deadlock. Each process is blocked on 91ec23eb54SMauro Carvalho Chehabattempt to acquire some lock and already holds at least one lock. Let's 92ec23eb54SMauro Carvalho Chehabconsider the set of contended locks. First of all, filesystem lock is 93ec23eb54SMauro Carvalho Chehabnot contended, since any process blocked on it is not holding any locks. 94ec23eb54SMauro Carvalho ChehabThus all processes are blocked on ->i_rwsem. 95ec23eb54SMauro Carvalho Chehab 96ec23eb54SMauro Carvalho ChehabBy (3), any process holding a non-directory lock can only be 97ec23eb54SMauro Carvalho Chehabwaiting on another non-directory lock with a larger address. Therefore 98ec23eb54SMauro Carvalho Chehabthe process holding the "largest" such lock can always make progress, and 99ec23eb54SMauro Carvalho Chehabnon-directory objects are not included in the set of contended locks. 100ec23eb54SMauro Carvalho Chehab 101ec23eb54SMauro Carvalho ChehabThus link creation can't be a part of deadlock - it can't be 102ec23eb54SMauro Carvalho Chehabblocked on source and it means that it doesn't hold any locks. 103ec23eb54SMauro Carvalho Chehab 104ec23eb54SMauro Carvalho ChehabAny contended object is either held by cross-directory rename or 105ec23eb54SMauro Carvalho Chehabhas a child that is also contended. Indeed, suppose that it is held by 106ec23eb54SMauro Carvalho Chehaboperation other than cross-directory rename. Then the lock this operation 107ec23eb54SMauro Carvalho Chehabis blocked on belongs to child of that object due to (1). 108ec23eb54SMauro Carvalho Chehab 109ec23eb54SMauro Carvalho ChehabIt means that one of the operations is cross-directory rename. 110ec23eb54SMauro Carvalho ChehabOtherwise the set of contended objects would be infinite - each of them 111ec23eb54SMauro Carvalho Chehabwould have a contended child and we had assumed that no object is its 112ec23eb54SMauro Carvalho Chehabown descendent. Moreover, there is exactly one cross-directory rename 113ec23eb54SMauro Carvalho Chehab(see above). 114ec23eb54SMauro Carvalho Chehab 115ec23eb54SMauro Carvalho ChehabConsider the object blocking the cross-directory rename. One 116ec23eb54SMauro Carvalho Chehabof its descendents is locked by cross-directory rename (otherwise we 117ec23eb54SMauro Carvalho Chehabwould again have an infinite set of contended objects). But that 118ec23eb54SMauro Carvalho Chehabmeans that cross-directory rename is taking locks out of order. Due 119ec23eb54SMauro Carvalho Chehabto (2) the order hadn't changed since we had acquired filesystem lock. 120ec23eb54SMauro Carvalho ChehabBut locking rules for cross-directory rename guarantee that we do not 121ec23eb54SMauro Carvalho Chehabtry to acquire lock on descendent before the lock on ancestor. 122ec23eb54SMauro Carvalho ChehabContradiction. I.e. deadlock is impossible. Q.E.D. 123ec23eb54SMauro Carvalho Chehab 124ec23eb54SMauro Carvalho Chehab 125ec23eb54SMauro Carvalho ChehabThese operations are guaranteed to avoid loop creation. Indeed, 126ec23eb54SMauro Carvalho Chehabthe only operation that could introduce loops is cross-directory rename. 127ec23eb54SMauro Carvalho ChehabSince the only new (parent, child) pair added by rename() is (new parent, 128ec23eb54SMauro Carvalho Chehabsource), such loop would have to contain these objects and the rest of it 129ec23eb54SMauro Carvalho Chehabwould have to exist before rename(). I.e. at the moment of loop creation 130ec23eb54SMauro Carvalho Chehabrename() responsible for that would be holding filesystem lock and new parent 131ec23eb54SMauro Carvalho Chehabwould have to be equal to or a descendent of source. But that means that 132ec23eb54SMauro Carvalho Chehabnew parent had been equal to or a descendent of source since the moment when 133ec23eb54SMauro Carvalho Chehabwe had acquired filesystem lock and rename() would fail with -ELOOP in that 134ec23eb54SMauro Carvalho Chehabcase. 135ec23eb54SMauro Carvalho Chehab 136ec23eb54SMauro Carvalho ChehabWhile this locking scheme works for arbitrary DAGs, it relies on 137ec23eb54SMauro Carvalho Chehabability to check that directory is a descendent of another object. Current 138ec23eb54SMauro Carvalho Chehabimplementation assumes that directory graph is a tree. This assumption is 139ec23eb54SMauro Carvalho Chehabalso preserved by all operations (cross-directory rename on a tree that would 140ec23eb54SMauro Carvalho Chehabnot introduce a cycle will leave it a tree and link() fails for directories). 141ec23eb54SMauro Carvalho Chehab 142ec23eb54SMauro Carvalho ChehabNotice that "directory" in the above == "anything that might have 143ec23eb54SMauro Carvalho Chehabchildren", so if we are going to introduce hybrid objects we will need 144ec23eb54SMauro Carvalho Chehabeither to make sure that link(2) doesn't work for them or to make changes 145ec23eb54SMauro Carvalho Chehabin is_subdir() that would make it work even in presence of such beasts. 146