linux/Documentation/filesystems/directory-locking
<<
>>
Prefs
   1        Locking scheme used for directory operations is based on two
   2kinds of locks - per-inode (->i_mutex) and per-filesystem
   3(->s_vfs_rename_mutex).
   4
   5        When taking the i_mutex on multiple non-directory objects, we
   6always acquire the locks in order by increasing address.  We'll call
   7that "inode pointer" order in the following.
   8
   9        For our purposes all operations fall in 5 classes:
  10
  111) read access.  Locking rules: caller locks directory we are accessing.
  12
  132) object creation.  Locking rules: same as above.
  14
  153) object removal.  Locking rules: caller locks parent, finds victim,
  16locks victim and calls the method.
  17
  184) rename() that is _not_ cross-directory.  Locking rules: caller locks
  19the parent and finds source and target.  If target already exists, lock
  20it.  If source is a non-directory, lock it.  If that means we need to
  21lock both, lock them in inode pointer order.
  22
  235) link creation.  Locking rules:
  24        * lock parent
  25        * check that source is not a directory
  26        * lock source
  27        * call the method.
  28
  296) cross-directory rename.  The trickiest in the whole bunch.  Locking
  30rules:
  31        * lock the filesystem
  32        * lock parents in "ancestors first" order.
  33        * find source and target.
  34        * if old parent is equal to or is a descendent of target
  35                fail with -ENOTEMPTY
  36        * if new parent is equal to or is a descendent of source
  37                fail with -ELOOP
  38        * If target exists, lock it.  If source is a non-directory, lock
  39          it.  In case that means we need to lock both source and target,
  40          do so in inode pointer order.
  41        * call the method.
  42
  43
  44The rules above obviously guarantee that all directories that are going to be
  45read, modified or removed by method will be locked by caller.
  46
  47
  48If no directory is its own ancestor, the scheme above is deadlock-free.
  49Proof:
  50
  51        First of all, at any moment we have a partial ordering of the
  52objects - A < B iff A is an ancestor of B.
  53
  54        That ordering can change.  However, the following is true:
  55
  56(1) if object removal or non-cross-directory rename holds lock on A and
  57    attempts to acquire lock on B, A will remain the parent of B until we
  58    acquire the lock on B.  (Proof: only cross-directory rename can change
  59    the parent of object and it would have to lock the parent).
  60
  61(2) if cross-directory rename holds the lock on filesystem, order will not
  62    change until rename acquires all locks.  (Proof: other cross-directory
  63    renames will be blocked on filesystem lock and we don't start changing
  64    the order until we had acquired all locks).
  65
  66(3) locks on non-directory objects are acquired only after locks on
  67    directory objects, and are acquired in inode pointer order.
  68    (Proof: all operations but renames take lock on at most one
  69    non-directory object, except renames, which take locks on source and
  70    target in inode pointer order in the case they are not directories.)
  71
  72        Now consider the minimal deadlock.  Each process is blocked on
  73attempt to acquire some lock and already holds at least one lock.  Let's
  74consider the set of contended locks.  First of all, filesystem lock is
  75not contended, since any process blocked on it is not holding any locks.
  76Thus all processes are blocked on ->i_mutex.
  77
  78        By (3), any process holding a non-directory lock can only be
  79waiting on another non-directory lock with a larger address.  Therefore
  80the process holding the "largest" such lock can always make progress, and
  81non-directory objects are not included in the set of contended locks.
  82
  83        Thus link creation can't be a part of deadlock - it can't be
  84blocked on source and it means that it doesn't hold any locks.
  85
  86        Any contended object is either held by cross-directory rename or
  87has a child that is also contended.  Indeed, suppose that it is held by
  88operation other than cross-directory rename.  Then the lock this operation
  89is blocked on belongs to child of that object due to (1).
  90
  91        It means that one of the operations is cross-directory rename.
  92Otherwise the set of contended objects would be infinite - each of them
  93would have a contended child and we had assumed that no object is its
  94own descendent.  Moreover, there is exactly one cross-directory rename
  95(see above).
  96
  97        Consider the object blocking the cross-directory rename.  One
  98of its descendents is locked by cross-directory rename (otherwise we
  99would again have an infinite set of contended objects).  But that
 100means that cross-directory rename is taking locks out of order.  Due
 101to (2) the order hadn't changed since we had acquired filesystem lock.
 102But locking rules for cross-directory rename guarantee that we do not
 103try to acquire lock on descendent before the lock on ancestor.
 104Contradiction.  I.e.  deadlock is impossible.  Q.E.D.
 105
 106
 107        These operations are guaranteed to avoid loop creation.  Indeed,
 108the only operation that could introduce loops is cross-directory rename.
 109Since the only new (parent, child) pair added by rename() is (new parent,
 110source), such loop would have to contain these objects and the rest of it
 111would have to exist before rename().  I.e. at the moment of loop creation
 112rename() responsible for that would be holding filesystem lock and new parent
 113would have to be equal to or a descendent of source.  But that means that
 114new parent had been equal to or a descendent of source since the moment when
 115we had acquired filesystem lock and rename() would fail with -ELOOP in that
 116case.
 117
 118        While this locking scheme works for arbitrary DAGs, it relies on
 119ability to check that directory is a descendent of another object.  Current
 120implementation assumes that directory graph is a tree.  This assumption is
 121also preserved by all operations (cross-directory rename on a tree that would
 122not introduce a cycle will leave it a tree and link() fails for directories).
 123
 124        Notice that "directory" in the above == "anything that might have
 125children", so if we are going to introduce hybrid objects we will need
 126either to make sure that link(2) doesn't work for them or to make changes
 127in is_subdir() that would make it work even in presence of such beasts.
 128