directory-locking 5.8 KB

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