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