directory-locking.rst 6.3 KB

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