grace.c 3.2 KB

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