grace.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <linux/filelock.h>
  12. static unsigned int grace_net_id;
  13. static DEFINE_SPINLOCK(grace_lock);
  14. /**
  15. * locks_start_grace
  16. * @net: net namespace that this lock manager belongs to
  17. * @lm: who this grace period is for
  18. *
  19. * A grace period is a period during which locks should not be given
  20. * out. Currently grace periods are only enforced by the two lock
  21. * managers (lockd and nfsd), using the locks_in_grace() function to
  22. * check when they are in a grace period.
  23. *
  24. * This function is called to start a grace period.
  25. */
  26. void
  27. locks_start_grace(struct net *net, struct lock_manager *lm)
  28. {
  29. struct list_head *grace_list = net_generic(net, grace_net_id);
  30. spin_lock(&grace_lock);
  31. if (list_empty(&lm->list))
  32. list_add(&lm->list, grace_list);
  33. else
  34. WARN(1, "double list_add attempt detected in net %x %s\n",
  35. net->ns.inum, (net == &init_net) ? "(init_net)" : "");
  36. spin_unlock(&grace_lock);
  37. }
  38. EXPORT_SYMBOL_GPL(locks_start_grace);
  39. /**
  40. * locks_end_grace
  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. * @net: network namespace
  77. *
  78. * Lock managers call this function to determine when it is OK for them
  79. * to answer ordinary lock requests, and when they should accept only
  80. * lock reclaims.
  81. */
  82. bool locks_in_grace(struct net *net)
  83. {
  84. return __state_in_grace(net, false);
  85. }
  86. EXPORT_SYMBOL_GPL(locks_in_grace);
  87. bool opens_in_grace(struct net *net)
  88. {
  89. return __state_in_grace(net, true);
  90. }
  91. EXPORT_SYMBOL_GPL(opens_in_grace);
  92. static int __net_init
  93. grace_init_net(struct net *net)
  94. {
  95. struct list_head *grace_list = net_generic(net, grace_net_id);
  96. INIT_LIST_HEAD(grace_list);
  97. return 0;
  98. }
  99. static void __net_exit
  100. grace_exit_net(struct net *net)
  101. {
  102. struct list_head *grace_list = net_generic(net, grace_net_id);
  103. WARN_ONCE(!list_empty(grace_list),
  104. "net %x %s: grace_list is not empty\n",
  105. net->ns.inum, __func__);
  106. }
  107. static struct pernet_operations grace_net_ops = {
  108. .init = grace_init_net,
  109. .exit = grace_exit_net,
  110. .id = &grace_net_id,
  111. .size = sizeof(struct list_head),
  112. };
  113. static int __init
  114. init_grace(void)
  115. {
  116. return register_pernet_subsys(&grace_net_ops);
  117. }
  118. static void __exit
  119. exit_grace(void)
  120. {
  121. unregister_pernet_subsys(&grace_net_ops);
  122. }
  123. MODULE_AUTHOR("Jeff Layton <jlayton@primarydata.com>");
  124. MODULE_DESCRIPTION("NFS client and server infrastructure");
  125. MODULE_LICENSE("GPL");
  126. module_init(init_grace)
  127. module_exit(exit_grace)