grace.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. locks_in_grace(struct net *net)
  61. {
  62. struct list_head *grace_list = net_generic(net, grace_net_id);
  63. return !list_empty(grace_list);
  64. }
  65. EXPORT_SYMBOL_GPL(locks_in_grace);
  66. static int __net_init
  67. grace_init_net(struct net *net)
  68. {
  69. struct list_head *grace_list = net_generic(net, grace_net_id);
  70. INIT_LIST_HEAD(grace_list);
  71. return 0;
  72. }
  73. static void __net_exit
  74. grace_exit_net(struct net *net)
  75. {
  76. struct list_head *grace_list = net_generic(net, grace_net_id);
  77. BUG_ON(!list_empty(grace_list));
  78. }
  79. static struct pernet_operations grace_net_ops = {
  80. .init = grace_init_net,
  81. .exit = grace_exit_net,
  82. .id = &grace_net_id,
  83. .size = sizeof(struct list_head),
  84. };
  85. static int __init
  86. init_grace(void)
  87. {
  88. return register_pernet_subsys(&grace_net_ops);
  89. }
  90. static void __exit
  91. exit_grace(void)
  92. {
  93. unregister_pernet_subsys(&grace_net_ops);
  94. }
  95. MODULE_AUTHOR("Jeff Layton <jlayton@primarydata.com>");
  96. MODULE_LICENSE("GPL");
  97. module_init(init_grace)
  98. module_exit(exit_grace)