PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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