grace.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Common code for control of lockd and nfsv4 grace periods.
  4. *
  5. * Transplanted from lockd code
  6. */
  7. #include <linux/module.h>
  8. #include <net/net_namespace.h>
  9. #include <net/netns/generic.h>
  10. #include <linux/fs.h>
  11. static unsigned int grace_net_id;
  12. static DEFINE_SPINLOCK(grace_lock);
  13. /**
  14. * locks_start_grace
  15. * @net: net namespace that this lock manager belongs to
  16. * @lm: who this grace period is for
  17. *
  18. * A grace period is a period during which locks should not be given
  19. * out. Currently grace periods are only enforced by the two lock
  20. * managers (lockd and nfsd), using the locks_in_grace() function to
  21. * check when they are in a grace period.
  22. *
  23. * This function is called to start a grace period.
  24. */
  25. void
  26. locks_start_grace(struct net *net, struct lock_manager *lm)
  27. {
  28. struct list_head *grace_list = net_generic(net, grace_net_id);
  29. spin_lock(&grace_lock);
  30. if (list_empty(&lm->list))
  31. list_add(&lm->list, grace_list);
  32. else
  33. WARN(1, "double list_add attempt detected in net %x %s\n",
  34. net->ns.inum, (net == &init_net) ? "(init_net)" : "");
  35. spin_unlock(&grace_lock);
  36. }
  37. EXPORT_SYMBOL_GPL(locks_start_grace);
  38. /**
  39. * locks_end_grace
  40. * @net: net namespace that this lock manager belongs to
  41. * @lm: who this grace period is for
  42. *
  43. * Call this function to state that the given lock manager is ready to
  44. * resume regular locking. The grace period will not end until all lock
  45. * managers that called locks_start_grace() also call locks_end_grace().
  46. * Note that callers count on it being safe to call this more than once,
  47. * and the second call should be a no-op.
  48. */
  49. void
  50. locks_end_grace(struct lock_manager *lm)
  51. {
  52. spin_lock(&grace_lock);
  53. list_del_init(&lm->list);
  54. spin_unlock(&grace_lock);
  55. }
  56. EXPORT_SYMBOL_GPL(locks_end_grace);
  57. static bool
  58. __state_in_grace(struct net *net, bool open)
  59. {
  60. struct list_head *grace_list = net_generic(net, grace_net_id);
  61. struct lock_manager *lm;
  62. if (!open)
  63. return !list_empty(grace_list);
  64. spin_lock(&grace_lock);
  65. list_for_each_entry(lm, grace_list, list) {
  66. if (lm->block_opens) {
  67. spin_unlock(&grace_lock);
  68. return true;
  69. }
  70. }
  71. spin_unlock(&grace_lock);
  72. return false;
  73. }
  74. /**
  75. * locks_in_grace
  76. *
  77. * Lock managers call this function to determine when it is OK for them
  78. * to answer ordinary lock requests, and when they should accept only
  79. * lock reclaims.
  80. */
  81. bool locks_in_grace(struct net *net)
  82. {
  83. return __state_in_grace(net, false);
  84. }
  85. EXPORT_SYMBOL_GPL(locks_in_grace);
  86. bool opens_in_grace(struct net *net)
  87. {
  88. return __state_in_grace(net, true);
  89. }
  90. EXPORT_SYMBOL_GPL(opens_in_grace);
  91. static int __net_init
  92. grace_init_net(struct net *net)
  93. {
  94. struct list_head *grace_list = net_generic(net, grace_net_id);
  95. INIT_LIST_HEAD(grace_list);
  96. return 0;
  97. }
  98. static void __net_exit
  99. grace_exit_net(struct net *net)
  100. {
  101. struct list_head *grace_list = net_generic(net, grace_net_id);
  102. WARN_ONCE(!list_empty(grace_list),
  103. "net %x %s: grace_list is not empty\n",
  104. net->ns.inum, __func__);
  105. }
  106. static struct pernet_operations grace_net_ops = {
  107. .init = grace_init_net,
  108. .exit = grace_exit_net,
  109. .id = &grace_net_id,
  110. .size = sizeof(struct list_head),
  111. };
  112. static int __init
  113. init_grace(void)
  114. {
  115. return register_pernet_subsys(&grace_net_ops);
  116. }
  117. static void __exit
  118. exit_grace(void)
  119. {
  120. unregister_pernet_subsys(&grace_net_ops);
  121. }
  122. MODULE_AUTHOR("Jeff Layton <jlayton@primarydata.com>");
  123. MODULE_LICENSE("GPL");
  124. module_init(init_grace)
  125. module_exit(exit_grace)