xref: /linux/fs/nfs_common/grace.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f7790029SJeff Layton /*
3f7790029SJeff Layton  * Common code for control of lockd and nfsv4 grace periods.
4f7790029SJeff Layton  *
5f7790029SJeff Layton  * Transplanted from lockd code
6f7790029SJeff Layton  */
7f7790029SJeff Layton 
8f7790029SJeff Layton #include <linux/module.h>
9f7790029SJeff Layton #include <net/net_namespace.h>
10f7790029SJeff Layton #include <net/netns/generic.h>
11f7790029SJeff Layton #include <linux/fs.h>
125970e15dSJeff Layton #include <linux/filelock.h>
13f7790029SJeff Layton 
14c7d03a00SAlexey Dobriyan static unsigned int grace_net_id;
15f7790029SJeff Layton static DEFINE_SPINLOCK(grace_lock);
16f7790029SJeff Layton 
17f7790029SJeff Layton /**
18f7790029SJeff Layton  * locks_start_grace
19f7790029SJeff Layton  * @net: net namespace that this lock manager belongs to
20f7790029SJeff Layton  * @lm: who this grace period is for
21f7790029SJeff Layton  *
22f7790029SJeff Layton  * A grace period is a period during which locks should not be given
23f7790029SJeff Layton  * out.  Currently grace periods are only enforced by the two lock
24f7790029SJeff Layton  * managers (lockd and nfsd), using the locks_in_grace() function to
25f7790029SJeff Layton  * check when they are in a grace period.
26f7790029SJeff Layton  *
27f7790029SJeff Layton  * This function is called to start a grace period.
28f7790029SJeff Layton  */
29f7790029SJeff Layton void
locks_start_grace(struct net * net,struct lock_manager * lm)30f7790029SJeff Layton locks_start_grace(struct net *net, struct lock_manager *lm)
31f7790029SJeff Layton {
32f7790029SJeff Layton 	struct list_head *grace_list = net_generic(net, grace_net_id);
33f7790029SJeff Layton 
34f7790029SJeff Layton 	spin_lock(&grace_lock);
3581833de1SVasily Averin 	if (list_empty(&lm->list))
36f7790029SJeff Layton 		list_add(&lm->list, grace_list);
3781833de1SVasily Averin 	else
3881833de1SVasily Averin 		WARN(1, "double list_add attempt detected in net %x %s\n",
3981833de1SVasily Averin 		     net->ns.inum, (net == &init_net) ? "(init_net)" : "");
40f7790029SJeff Layton 	spin_unlock(&grace_lock);
41f7790029SJeff Layton }
42f7790029SJeff Layton EXPORT_SYMBOL_GPL(locks_start_grace);
43f7790029SJeff Layton 
44f7790029SJeff Layton /**
45f7790029SJeff Layton  * locks_end_grace
46f7790029SJeff Layton  * @lm: who this grace period is for
47f7790029SJeff Layton  *
48f7790029SJeff Layton  * Call this function to state that the given lock manager is ready to
49f7790029SJeff Layton  * resume regular locking.  The grace period will not end until all lock
50f7790029SJeff Layton  * managers that called locks_start_grace() also call locks_end_grace().
51f7790029SJeff Layton  * Note that callers count on it being safe to call this more than once,
52f7790029SJeff Layton  * and the second call should be a no-op.
53f7790029SJeff Layton  */
54f7790029SJeff Layton void
locks_end_grace(struct lock_manager * lm)55f7790029SJeff Layton locks_end_grace(struct lock_manager *lm)
56f7790029SJeff Layton {
57f7790029SJeff Layton 	spin_lock(&grace_lock);
58f7790029SJeff Layton 	list_del_init(&lm->list);
59f7790029SJeff Layton 	spin_unlock(&grace_lock);
60f7790029SJeff Layton }
61f7790029SJeff Layton EXPORT_SYMBOL_GPL(locks_end_grace);
62f7790029SJeff Layton 
63003278e4SCorentin Labbe static bool
__state_in_grace(struct net * net,bool open)64c87fb4a3SJ. Bruce Fields __state_in_grace(struct net *net, bool open)
65f7790029SJeff Layton {
66f7790029SJeff Layton 	struct list_head *grace_list = net_generic(net, grace_net_id);
67c87fb4a3SJ. Bruce Fields 	struct lock_manager *lm;
68f7790029SJeff Layton 
69c87fb4a3SJ. Bruce Fields 	if (!open)
70f7790029SJeff Layton 		return !list_empty(grace_list);
71c87fb4a3SJ. Bruce Fields 
724a9d81caSCheng Lin 	spin_lock(&grace_lock);
73c87fb4a3SJ. Bruce Fields 	list_for_each_entry(lm, grace_list, list) {
744a9d81caSCheng Lin 		if (lm->block_opens) {
754a9d81caSCheng Lin 			spin_unlock(&grace_lock);
76c87fb4a3SJ. Bruce Fields 			return true;
77c87fb4a3SJ. Bruce Fields 		}
784a9d81caSCheng Lin 	}
794a9d81caSCheng Lin 	spin_unlock(&grace_lock);
80c87fb4a3SJ. Bruce Fields 	return false;
81c87fb4a3SJ. Bruce Fields }
82c87fb4a3SJ. Bruce Fields 
83809d4fcfSCorentin Labbe /**
84809d4fcfSCorentin Labbe  * locks_in_grace
855823e400SChenXiaoSong  * @net: network namespace
86809d4fcfSCorentin Labbe  *
87809d4fcfSCorentin Labbe  * Lock managers call this function to determine when it is OK for them
88809d4fcfSCorentin Labbe  * to answer ordinary lock requests, and when they should accept only
89809d4fcfSCorentin Labbe  * lock reclaims.
90809d4fcfSCorentin Labbe  */
locks_in_grace(struct net * net)91003278e4SCorentin Labbe bool locks_in_grace(struct net *net)
92c87fb4a3SJ. Bruce Fields {
93003278e4SCorentin Labbe 	return __state_in_grace(net, false);
94f7790029SJeff Layton }
95f7790029SJeff Layton EXPORT_SYMBOL_GPL(locks_in_grace);
96f7790029SJeff Layton 
opens_in_grace(struct net * net)97003278e4SCorentin Labbe bool opens_in_grace(struct net *net)
98c87fb4a3SJ. Bruce Fields {
99003278e4SCorentin Labbe 	return __state_in_grace(net, true);
100c87fb4a3SJ. Bruce Fields }
101c87fb4a3SJ. Bruce Fields EXPORT_SYMBOL_GPL(opens_in_grace);
102c87fb4a3SJ. Bruce Fields 
103f7790029SJeff Layton static int __net_init
grace_init_net(struct net * net)104f7790029SJeff Layton grace_init_net(struct net *net)
105f7790029SJeff Layton {
106f7790029SJeff Layton 	struct list_head *grace_list = net_generic(net, grace_net_id);
107f7790029SJeff Layton 
108f7790029SJeff Layton 	INIT_LIST_HEAD(grace_list);
109f7790029SJeff Layton 	return 0;
110f7790029SJeff Layton }
111f7790029SJeff Layton 
112f7790029SJeff Layton static void __net_exit
grace_exit_net(struct net * net)113f7790029SJeff Layton grace_exit_net(struct net *net)
114f7790029SJeff Layton {
115f7790029SJeff Layton 	struct list_head *grace_list = net_generic(net, grace_net_id);
116f7790029SJeff Layton 
117b8722857SVasily Averin 	WARN_ONCE(!list_empty(grace_list),
118b8722857SVasily Averin 		  "net %x %s: grace_list is not empty\n",
119b8722857SVasily Averin 		  net->ns.inum, __func__);
120f7790029SJeff Layton }
121f7790029SJeff Layton 
122f7790029SJeff Layton static struct pernet_operations grace_net_ops = {
123f7790029SJeff Layton 	.init = grace_init_net,
124f7790029SJeff Layton 	.exit = grace_exit_net,
125f7790029SJeff Layton 	.id   = &grace_net_id,
126f7790029SJeff Layton 	.size = sizeof(struct list_head),
127f7790029SJeff Layton };
128f7790029SJeff Layton 
129f7790029SJeff Layton static int __init
init_grace(void)130f7790029SJeff Layton init_grace(void)
131f7790029SJeff Layton {
132f7790029SJeff Layton 	return register_pernet_subsys(&grace_net_ops);
133f7790029SJeff Layton }
134f7790029SJeff Layton 
135f7790029SJeff Layton static void __exit
exit_grace(void)136f7790029SJeff Layton exit_grace(void)
137f7790029SJeff Layton {
138f7790029SJeff Layton 	unregister_pernet_subsys(&grace_net_ops);
139f7790029SJeff Layton }
140f7790029SJeff Layton 
141f7790029SJeff Layton MODULE_AUTHOR("Jeff Layton <jlayton@primarydata.com>");
142*d3318990SJeff Johnson MODULE_DESCRIPTION("NFS client and server infrastructure");
143f7790029SJeff Layton MODULE_LICENSE("GPL");
144f7790029SJeff Layton module_init(init_grace)
145f7790029SJeff Layton module_exit(exit_grace)
146