directory-locking 5.0 KB

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