1 // SPDX-License-Identifier: GPL-2.0-only
3 * Common code for control of lockd and nfsv4 grace periods.
5 * Transplanted from lockd code
8 #include <linux/module.h>
9 #include <net/net_namespace.h>
10 #include <net/netns/generic.h>
13 static unsigned int grace_net_id;
14 static DEFINE_SPINLOCK(grace_lock);
18 * @net: net namespace that this lock manager belongs to
19 * @lm: who this grace period is for
21 * A grace period is a period during which locks should not be given
22 * out. Currently grace periods are only enforced by the two lock
23 * managers (lockd and nfsd), using the locks_in_grace() function to
24 * check when they are in a grace period.
26 * This function is called to start a grace period.
29 locks_start_grace(struct net *net, struct lock_manager *lm)
31 struct list_head *grace_list = net_generic(net, grace_net_id);
33 spin_lock(&grace_lock);
34 if (list_empty(&lm->list))
35 list_add(&lm->list, grace_list);
37 WARN(1, "double list_add attempt detected in net %x %s\n",
38 net->ns.inum, (net == &init_net) ? "(init_net)" : "");
39 spin_unlock(&grace_lock);
41 EXPORT_SYMBOL_GPL(locks_start_grace);
45 * @lm: who this grace period is for
47 * Call this function to state that the given lock manager is ready to
48 * resume regular locking. The grace period will not end until all lock
49 * managers that called locks_start_grace() also call locks_end_grace().
50 * Note that callers count on it being safe to call this more than once,
51 * and the second call should be a no-op.
54 locks_end_grace(struct lock_manager *lm)
56 spin_lock(&grace_lock);
57 list_del_init(&lm->list);
58 spin_unlock(&grace_lock);
60 EXPORT_SYMBOL_GPL(locks_end_grace);
63 __state_in_grace(struct net *net, bool open)
65 struct list_head *grace_list = net_generic(net, grace_net_id);
66 struct lock_manager *lm;
69 return !list_empty(grace_list);
71 spin_lock(&grace_lock);
72 list_for_each_entry(lm, grace_list, list) {
73 if (lm->block_opens) {
74 spin_unlock(&grace_lock);
78 spin_unlock(&grace_lock);
84 * @net: network namespace
86 * Lock managers call this function to determine when it is OK for them
87 * to answer ordinary lock requests, and when they should accept only
90 bool locks_in_grace(struct net *net)
92 return __state_in_grace(net, false);
94 EXPORT_SYMBOL_GPL(locks_in_grace);
96 bool opens_in_grace(struct net *net)
98 return __state_in_grace(net, true);
100 EXPORT_SYMBOL_GPL(opens_in_grace);
102 static int __net_init
103 grace_init_net(struct net *net)
105 struct list_head *grace_list = net_generic(net, grace_net_id);
107 INIT_LIST_HEAD(grace_list);
111 static void __net_exit
112 grace_exit_net(struct net *net)
114 struct list_head *grace_list = net_generic(net, grace_net_id);
116 WARN_ONCE(!list_empty(grace_list),
117 "net %x %s: grace_list is not empty\n",
118 net->ns.inum, __func__);
121 static struct pernet_operations grace_net_ops = {
122 .init = grace_init_net,
123 .exit = grace_exit_net,
125 .size = sizeof(struct list_head),
131 return register_pernet_subsys(&grace_net_ops);
137 unregister_pernet_subsys(&grace_net_ops);
140 MODULE_AUTHOR("Jeff Layton <jlayton@primarydata.com>");
141 MODULE_LICENSE("GPL");
142 module_init(init_grace)
143 module_exit(exit_grace)