grace.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. list_add(&lm->list, grace_list);
  30. spin_unlock(&grace_lock);
  31. }
  32. EXPORT_SYMBOL_GPL(locks_start_grace);
  33. /**
  34. * locks_end_grace
  35. * @net: net namespace that this lock manager belongs to
  36. * @lm: who this grace period is for
  37. *
  38. * Call this function to state that the given lock manager is ready to
  39. * resume regular locking. The grace period will not end until all lock
  40. * managers that called locks_start_grace() also call locks_end_grace().
  41. * Note that callers count on it being safe to call this more than once,
  42. * and the second call should be a no-op.
  43. */
  44. void
  45. locks_end_grace(struct lock_manager *lm)
  46. {
  47. spin_lock(&grace_lock);
  48. list_del_init(&lm->list);
  49. spin_unlock(&grace_lock);
  50. }
  51. EXPORT_SYMBOL_GPL(locks_end_grace);
  52. /**
  53. * locks_in_grace
  54. *
  55. * Lock managers call this function to determine when it is OK for them
  56. * to answer ordinary lock requests, and when they should accept only
  57. * lock reclaims.
  58. */
  59. int
  60. __state_in_grace(struct net *net, bool open)
  61. {
  62. struct list_head *grace_list = net_generic(net, grace_net_id);
  63. struct lock_manager *lm;
  64. if (!open)
  65. return !list_empty(grace_list);
  66. list_for_each_entry(lm, grace_list, list) {
  67. if (lm->block_opens)
  68. return true;
  69. }
  70. return false;
  71. }
  72. int locks_in_grace(struct net *net)
  73. {
  74. return __state_in_grace(net, 0);
  75. }
  76. EXPORT_SYMBOL_GPL(locks_in_grace);
  77. int opens_in_grace(struct net *net)
  78. {
  79. return __state_in_grace(net, 1);
  80. }
  81. EXPORT_SYMBOL_GPL(opens_in_grace);
  82. static int __net_init
  83. grace_init_net(struct net *net)
  84. {
  85. struct list_head *grace_list = net_generic(net, grace_net_id);
  86. INIT_LIST_HEAD(grace_list);
  87. return 0;
  88. }
  89. static void __net_exit
  90. grace_exit_net(struct net *net)
  91. {
  92. struct list_head *grace_list = net_generic(net, grace_net_id);
  93. BUG_ON(!list_empty(grace_list));
  94. }
  95. static struct pernet_operations grace_net_ops = {
  96. .init = grace_init_net,
  97. .exit = grace_exit_net,
  98. .id = &grace_net_id,
  99. .size = sizeof(struct list_head),
  100. };
  101. static int __init
  102. init_grace(void)
  103. {
  104. return register_pernet_subsys(&grace_net_ops);
  105. }
  106. static void __exit
  107. exit_grace(void)
  108. {
  109. unregister_pernet_subsys(&grace_net_ops);
  110. }
  111. MODULE_AUTHOR("Jeff Layton <jlayton@primarydata.com>");
  112. MODULE_LICENSE("GPL");
  113. module_init(init_grace)
  114. module_exit(exit_grace)