Back to home page

OSCL-LXR

 
 

    


0001 =================
0002 Directory Locking
0003 =================
0004 
0005 
0006 Locking scheme used for directory operations is based on two
0007 kinds of locks - per-inode (->i_rwsem) and per-filesystem
0008 (->s_vfs_rename_mutex).
0009 
0010 When taking the i_rwsem on multiple non-directory objects, we
0011 always acquire the locks in order by increasing address.  We'll call
0012 that "inode pointer" order in the following.
0013 
0014 For our purposes all operations fall in 5 classes:
0015 
0016 1) read access.  Locking rules: caller locks directory we are accessing.
0017 The lock is taken shared.
0018 
0019 2) object creation.  Locking rules: same as above, but the lock is taken
0020 exclusive.
0021 
0022 3) object removal.  Locking rules: caller locks parent, finds victim,
0023 locks victim and calls the method.  Locks are exclusive.
0024 
0025 4) rename() that is _not_ cross-directory.  Locking rules: caller locks
0026 the parent and finds source and target.  In case of exchange (with
0027 RENAME_EXCHANGE in flags argument) lock both.  In any case,
0028 if the target already exists, lock it.  If the source is a non-directory,
0029 lock it.  If we need to lock both, lock them in inode pointer order.
0030 Then call the method.  All locks are exclusive.
0031 NB: we might get away with locking the source (and target in exchange
0032 case) shared.
0033 
0034 5) link creation.  Locking rules:
0035 
0036         * lock parent
0037         * check that source is not a directory
0038         * lock source
0039         * call the method.
0040 
0041 All locks are exclusive.
0042 
0043 6) cross-directory rename.  The trickiest in the whole bunch.  Locking
0044 rules:
0045 
0046         * lock the filesystem
0047         * lock parents in "ancestors first" order.
0048         * find source and target.
0049         * if old parent is equal to or is a descendent of target
0050           fail with -ENOTEMPTY
0051         * if new parent is equal to or is a descendent of source
0052           fail with -ELOOP
0053         * If it's an exchange, lock both the source and the target.
0054         * If the target exists, lock it.  If the source is a non-directory,
0055           lock it.  If we need to lock both, do so in inode pointer order.
0056         * call the method.
0057 
0058 All ->i_rwsem are taken exclusive.  Again, we might get away with locking
0059 the source (and target in exchange case) shared.
0060 
0061 The rules above obviously guarantee that all directories that are going to be
0062 read, modified or removed by method will be locked by caller.
0063 
0064 
0065 If no directory is its own ancestor, the scheme above is deadlock-free.
0066 
0067 Proof:
0068 
0069         First of all, at any moment we have a partial ordering of the
0070         objects - A < B iff A is an ancestor of B.
0071 
0072         That ordering can change.  However, the following is true:
0073 
0074 (1) if object removal or non-cross-directory rename holds lock on A and
0075     attempts to acquire lock on B, A will remain the parent of B until we
0076     acquire the lock on B.  (Proof: only cross-directory rename can change
0077     the parent of object and it would have to lock the parent).
0078 
0079 (2) if cross-directory rename holds the lock on filesystem, order will not
0080     change until rename acquires all locks.  (Proof: other cross-directory
0081     renames will be blocked on filesystem lock and we don't start changing
0082     the order until we had acquired all locks).
0083 
0084 (3) locks on non-directory objects are acquired only after locks on
0085     directory objects, and are acquired in inode pointer order.
0086     (Proof: all operations but renames take lock on at most one
0087     non-directory object, except renames, which take locks on source and
0088     target in inode pointer order in the case they are not directories.)
0089 
0090 Now consider the minimal deadlock.  Each process is blocked on
0091 attempt to acquire some lock and already holds at least one lock.  Let's
0092 consider the set of contended locks.  First of all, filesystem lock is
0093 not contended, since any process blocked on it is not holding any locks.
0094 Thus all processes are blocked on ->i_rwsem.
0095 
0096 By (3), any process holding a non-directory lock can only be
0097 waiting on another non-directory lock with a larger address.  Therefore
0098 the process holding the "largest" such lock can always make progress, and
0099 non-directory objects are not included in the set of contended locks.
0100 
0101 Thus link creation can't be a part of deadlock - it can't be
0102 blocked on source and it means that it doesn't hold any locks.
0103 
0104 Any contended object is either held by cross-directory rename or
0105 has a child that is also contended.  Indeed, suppose that it is held by
0106 operation other than cross-directory rename.  Then the lock this operation
0107 is blocked on belongs to child of that object due to (1).
0108 
0109 It means that one of the operations is cross-directory rename.
0110 Otherwise the set of contended objects would be infinite - each of them
0111 would have a contended child and we had assumed that no object is its
0112 own descendent.  Moreover, there is exactly one cross-directory rename
0113 (see above).
0114 
0115 Consider the object blocking the cross-directory rename.  One
0116 of its descendents is locked by cross-directory rename (otherwise we
0117 would again have an infinite set of contended objects).  But that
0118 means that cross-directory rename is taking locks out of order.  Due
0119 to (2) the order hadn't changed since we had acquired filesystem lock.
0120 But locking rules for cross-directory rename guarantee that we do not
0121 try to acquire lock on descendent before the lock on ancestor.
0122 Contradiction.  I.e.  deadlock is impossible.  Q.E.D.
0123 
0124 
0125 These operations are guaranteed to avoid loop creation.  Indeed,
0126 the only operation that could introduce loops is cross-directory rename.
0127 Since the only new (parent, child) pair added by rename() is (new parent,
0128 source), such loop would have to contain these objects and the rest of it
0129 would have to exist before rename().  I.e. at the moment of loop creation
0130 rename() responsible for that would be holding filesystem lock and new parent
0131 would have to be equal to or a descendent of source.  But that means that
0132 new parent had been equal to or a descendent of source since the moment when
0133 we had acquired filesystem lock and rename() would fail with -ELOOP in that
0134 case.
0135 
0136 While this locking scheme works for arbitrary DAGs, it relies on
0137 ability to check that directory is a descendent of another object.  Current
0138 implementation assumes that directory graph is a tree.  This assumption is
0139 also preserved by all operations (cross-directory rename on a tree that would
0140 not introduce a cycle will leave it a tree and link() fails for directories).
0141 
0142 Notice that "directory" in the above == "anything that might have
0143 children", so if we are going to introduce hybrid objects we will need
0144 either to make sure that link(2) doesn't work for them or to make changes
0145 in is_subdir() that would make it work even in presence of such beasts.