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 25*1db06b3dSAl Viro4) rename() that is _not_ cross-directory. Locking rules: caller locks 26*1db06b3dSAl Virothe parent and finds source and target. Then we decide which of the 27*1db06b3dSAl Virosource and target need to be locked. Source needs to be locked if it's a 28*1db06b3dSAl Vironon-directory; target - if it's a non-directory or about to be removed. 29*1db06b3dSAl ViroTake the locks that need to be taken, in inode pointer order if need 30*1db06b3dSAl Viroto take both (that can happen only when both source and target are 31*1db06b3dSAl Vironon-directories - the source because it wouldn't be locked otherwise 32*1db06b3dSAl Viroand the target because mixing directory and non-directory is allowed 33*1db06b3dSAl Viroonly with RENAME_EXCHANGE, and that won't be removing the target). 34*1db06b3dSAl ViroAfter the locks had been taken, call the method. All locks are exclusive. 35ec23eb54SMauro Carvalho Chehab 36ec23eb54SMauro Carvalho Chehab5) link creation. Locking rules: 37ec23eb54SMauro Carvalho Chehab 38ec23eb54SMauro Carvalho Chehab * lock parent 39ec23eb54SMauro Carvalho Chehab * check that source is not a directory 40ec23eb54SMauro Carvalho Chehab * lock source 41ec23eb54SMauro Carvalho Chehab * call the method. 42ec23eb54SMauro Carvalho Chehab 43ec23eb54SMauro Carvalho ChehabAll locks are exclusive. 44ec23eb54SMauro Carvalho Chehab 45ec23eb54SMauro Carvalho Chehab6) cross-directory rename. The trickiest in the whole bunch. Locking 46ec23eb54SMauro Carvalho Chehabrules: 47ec23eb54SMauro Carvalho Chehab 48ec23eb54SMauro Carvalho Chehab * lock the filesystem 4928eceedaSJan Kara * lock parents in "ancestors first" order. If one is not ancestor of 50*1db06b3dSAl Viro the other, lock the parent of source first. 51ec23eb54SMauro Carvalho Chehab * find source and target. 52ec23eb54SMauro Carvalho Chehab * if old parent is equal to or is a descendent of target 53ec23eb54SMauro Carvalho Chehab fail with -ENOTEMPTY 54ec23eb54SMauro Carvalho Chehab * if new parent is equal to or is a descendent of source 55ec23eb54SMauro Carvalho Chehab fail with -ELOOP 56*1db06b3dSAl Viro * Lock subdirectories involved (source before target). 57*1db06b3dSAl Viro * Lock non-directories involved, in inode pointer order. 58ec23eb54SMauro Carvalho Chehab * call the method. 59ec23eb54SMauro Carvalho Chehab 60*1db06b3dSAl ViroAll ->i_rwsem are taken exclusive. 61ec23eb54SMauro Carvalho Chehab 62ec23eb54SMauro Carvalho ChehabThe rules above obviously guarantee that all directories that are going to be 63ec23eb54SMauro Carvalho Chehabread, modified or removed by method will be locked by caller. 64ec23eb54SMauro Carvalho Chehab 65ec23eb54SMauro Carvalho Chehab 66ec23eb54SMauro Carvalho ChehabIf no directory is its own ancestor, the scheme above is deadlock-free. 67ec23eb54SMauro Carvalho Chehab 68ec23eb54SMauro Carvalho ChehabProof: 69ec23eb54SMauro Carvalho Chehab 70*1db06b3dSAl Viro[XXX: will be updated once we are done massaging the lock_rename()] 7128eceedaSJan Kara First of all, at any moment we have a linear ordering of the 7228eceedaSJan Kara objects - A < B iff (A is an ancestor of B) or (B is not an ancestor 7328eceedaSJan Kara of A and ptr(A) < ptr(B)). 74ec23eb54SMauro Carvalho Chehab 75ec23eb54SMauro Carvalho Chehab That ordering can change. However, the following is true: 76ec23eb54SMauro Carvalho Chehab 77ec23eb54SMauro Carvalho Chehab(1) if object removal or non-cross-directory rename holds lock on A and 78ec23eb54SMauro Carvalho Chehab attempts to acquire lock on B, A will remain the parent of B until we 79ec23eb54SMauro Carvalho Chehab acquire the lock on B. (Proof: only cross-directory rename can change 80ec23eb54SMauro Carvalho Chehab the parent of object and it would have to lock the parent). 81ec23eb54SMauro Carvalho Chehab 82ec23eb54SMauro Carvalho Chehab(2) if cross-directory rename holds the lock on filesystem, order will not 83ec23eb54SMauro Carvalho Chehab change until rename acquires all locks. (Proof: other cross-directory 84ec23eb54SMauro Carvalho Chehab renames will be blocked on filesystem lock and we don't start changing 85ec23eb54SMauro Carvalho Chehab the order until we had acquired all locks). 86ec23eb54SMauro Carvalho Chehab 87ec23eb54SMauro Carvalho Chehab(3) locks on non-directory objects are acquired only after locks on 88ec23eb54SMauro Carvalho Chehab directory objects, and are acquired in inode pointer order. 89ec23eb54SMauro Carvalho Chehab (Proof: all operations but renames take lock on at most one 90ec23eb54SMauro Carvalho Chehab non-directory object, except renames, which take locks on source and 91ec23eb54SMauro Carvalho Chehab target in inode pointer order in the case they are not directories.) 92ec23eb54SMauro Carvalho Chehab 93ec23eb54SMauro Carvalho ChehabNow consider the minimal deadlock. Each process is blocked on 94ec23eb54SMauro Carvalho Chehabattempt to acquire some lock and already holds at least one lock. Let's 95ec23eb54SMauro Carvalho Chehabconsider the set of contended locks. First of all, filesystem lock is 96ec23eb54SMauro Carvalho Chehabnot contended, since any process blocked on it is not holding any locks. 97ec23eb54SMauro Carvalho ChehabThus all processes are blocked on ->i_rwsem. 98ec23eb54SMauro Carvalho Chehab 99ec23eb54SMauro Carvalho ChehabBy (3), any process holding a non-directory lock can only be 100ec23eb54SMauro Carvalho Chehabwaiting on another non-directory lock with a larger address. Therefore 101ec23eb54SMauro Carvalho Chehabthe process holding the "largest" such lock can always make progress, and 102ec23eb54SMauro Carvalho Chehabnon-directory objects are not included in the set of contended locks. 103ec23eb54SMauro Carvalho Chehab 104ec23eb54SMauro Carvalho ChehabThus link creation can't be a part of deadlock - it can't be 105ec23eb54SMauro Carvalho Chehabblocked on source and it means that it doesn't hold any locks. 106ec23eb54SMauro Carvalho Chehab 107ec23eb54SMauro Carvalho ChehabAny contended object is either held by cross-directory rename or 108ec23eb54SMauro Carvalho Chehabhas a child that is also contended. Indeed, suppose that it is held by 109ec23eb54SMauro Carvalho Chehaboperation other than cross-directory rename. Then the lock this operation 110ec23eb54SMauro Carvalho Chehabis blocked on belongs to child of that object due to (1). 111ec23eb54SMauro Carvalho Chehab 112ec23eb54SMauro Carvalho ChehabIt means that one of the operations is cross-directory rename. 113ec23eb54SMauro Carvalho ChehabOtherwise the set of contended objects would be infinite - each of them 114ec23eb54SMauro Carvalho Chehabwould have a contended child and we had assumed that no object is its 115ec23eb54SMauro Carvalho Chehabown descendent. Moreover, there is exactly one cross-directory rename 116ec23eb54SMauro Carvalho Chehab(see above). 117ec23eb54SMauro Carvalho Chehab 118ec23eb54SMauro Carvalho ChehabConsider the object blocking the cross-directory rename. One 119ec23eb54SMauro Carvalho Chehabof its descendents is locked by cross-directory rename (otherwise we 120ec23eb54SMauro Carvalho Chehabwould again have an infinite set of contended objects). But that 121ec23eb54SMauro Carvalho Chehabmeans that cross-directory rename is taking locks out of order. Due 122ec23eb54SMauro Carvalho Chehabto (2) the order hadn't changed since we had acquired filesystem lock. 123ec23eb54SMauro Carvalho ChehabBut locking rules for cross-directory rename guarantee that we do not 124ec23eb54SMauro Carvalho Chehabtry to acquire lock on descendent before the lock on ancestor. 125ec23eb54SMauro Carvalho ChehabContradiction. I.e. deadlock is impossible. Q.E.D. 126ec23eb54SMauro Carvalho Chehab 127ec23eb54SMauro Carvalho Chehab 128ec23eb54SMauro Carvalho ChehabThese operations are guaranteed to avoid loop creation. Indeed, 129ec23eb54SMauro Carvalho Chehabthe only operation that could introduce loops is cross-directory rename. 130ec23eb54SMauro Carvalho ChehabSince the only new (parent, child) pair added by rename() is (new parent, 131ec23eb54SMauro Carvalho Chehabsource), such loop would have to contain these objects and the rest of it 132ec23eb54SMauro Carvalho Chehabwould have to exist before rename(). I.e. at the moment of loop creation 133ec23eb54SMauro Carvalho Chehabrename() responsible for that would be holding filesystem lock and new parent 134ec23eb54SMauro Carvalho Chehabwould have to be equal to or a descendent of source. But that means that 135ec23eb54SMauro Carvalho Chehabnew parent had been equal to or a descendent of source since the moment when 136ec23eb54SMauro Carvalho Chehabwe had acquired filesystem lock and rename() would fail with -ELOOP in that 137ec23eb54SMauro Carvalho Chehabcase. 138ec23eb54SMauro Carvalho Chehab 139ec23eb54SMauro Carvalho ChehabWhile this locking scheme works for arbitrary DAGs, it relies on 140ec23eb54SMauro Carvalho Chehabability to check that directory is a descendent of another object. Current 141ec23eb54SMauro Carvalho Chehabimplementation assumes that directory graph is a tree. This assumption is 142ec23eb54SMauro Carvalho Chehabalso preserved by all operations (cross-directory rename on a tree that would 143ec23eb54SMauro Carvalho Chehabnot introduce a cycle will leave it a tree and link() fails for directories). 144ec23eb54SMauro Carvalho Chehab 145ec23eb54SMauro Carvalho ChehabNotice that "directory" in the above == "anything that might have 146ec23eb54SMauro Carvalho Chehabchildren", so if we are going to introduce hybrid objects we will need 147ec23eb54SMauro Carvalho Chehabeither to make sure that link(2) doesn't work for them or to make changes 148ec23eb54SMauro Carvalho Chehabin is_subdir() that would make it work even in presence of such beasts. 149