2007-05-31 05:09:45 +04:00
#!/bin/sh
2013-04-03 07:44:08 +04:00
# This must run as root as CTDB tool commands need to access CTDB socket
2016-07-06 10:31:51 +03:00
[ "$(id -u)" -eq 0 ] || exec sudo "$0" "$@"
2013-04-03 07:44:08 +04:00
2007-06-01 14:54:26 +04:00
# this script needs to be installed so that statd points to it with the -H
2007-06-02 13:45:06 +04:00
# command line argument. The easiest way to do that is to put something like this in
# /etc/sysconfig/nfs:
# STATD_HOSTNAME="myhostname -H /etc/ctdb/statd-callout"
2013-01-03 08:33:10 +04:00
[ -n "$CTDB_BASE" ] || \
2016-11-03 08:03:24 +03:00
CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; echo "$PWD")
2007-09-14 08:14:03 +04:00
2016-06-29 10:36:05 +03:00
. "${CTDB_BASE}/functions"
2013-11-08 09:41:11 +04:00
# Overwrite this so we get some logging
die ()
{
script_log "statd-callout" "$@"
exit 1
}
2009-11-09 17:06:59 +03:00
loadconfig ctdb
2007-06-03 16:07:07 +04:00
loadconfig nfs
2007-05-31 05:09:45 +04:00
2013-11-08 09:41:11 +04:00
[ -n "$NFS_HOSTNAME" ] || \
die "NFS_HOSTNAME is not configured. statd-callout failed"
2017-03-02 08:43:51 +03:00
############################################################
2007-09-07 02:52:56 +04:00
2018-03-07 03:12:29 +03:00
ctdb_setup_state_dir "service" "nfs"
2015-02-13 12:55:43 +03:00
2018-03-07 03:12:29 +03:00
# script_state_dir set by ctdb_setup_state_dir()
# shellcheck disable=SC2154
d="${script_state_dir}/statd-callout"
mkdir -p "$d" || die "Failed to create directory \"${d}\""
cd "$d" || die "Failed to change directory to \"${d}\""
2015-02-13 12:55:43 +03:00
2016-07-06 10:41:55 +03:00
pnn=$(ctdb_get_pnn)
2017-03-02 08:43:51 +03:00
############################################################
send_notifies ()
{
_smnotify="${CTDB_HELPER_BINDIR}/smnotify"
# State must monotonically increase, across the entire
# cluster. Use seconds since epoch and hope the time is in
# sync across nodes. Even numbers mean service is shut down,
# odd numbers mean service is started.
# Intentionally round to an even number
# shellcheck disable=SC2017
_state_even=$(( $(date '+%s') / 2 * 2))
_prev=""
while read _sip _cip ; do
# NOTE: Consider optimising smnotify to read all the
# data from stdin and then run it in the background.
# Reset stateval for each serverip
if [ "$_sip" != "$_prev" ] ; then
_stateval="$_state_even"
fi
# Send notifies for server shutdown
"$_smnotify" --client="$_cip" --ip="$_sip" \
--server="$_sip" --stateval="$_stateval"
"$_smnotify" --client="$_cip" --ip="$_sip" \
--server="$NFS_HOSTNAME" --stateval="$_stateval"
# Send notifies for server startup
_stateval=$((_stateval + 1))
"$_smnotify" --client="$_cip" --ip="$_sip" \
--server="$_sip" --stateval="$_stateval"
"$_smnotify" --client="$_cip" --ip="$_sip" \
--server="$NFS_HOSTNAME" --stateval="$_stateval"
done
}
delete_records ()
{
while read _sip _cip ; do
_key="statd-state@${_sip}@${_cip}"
echo "\"${_key}\" \"\""
done | $CTDB ptrans "ctdb.tdb"
}
############################################################
2007-05-31 05:09:45 +04:00
case "$1" in
2015-02-13 12:55:43 +03:00
# Keep a single file to keep track of the last "add-client" or
# "del-client'. These get pushed to ctdb.tdb during "update",
# which will generally be run once each "monitor" cycle. In this
# way we avoid scalability problems with flood of persistent
# transactions after a "notify" when all the clients re-take their
# locks.
2013-11-08 09:41:11 +04:00
add-client)
# statd does not tell us to which IP the client connected so
# we must add it to all the IPs that we serve
cip="$2"
date=$(date '+%s')
2016-07-06 10:16:44 +03:00
# x is intentionally ignored
# shellcheck disable=SC2034
2016-06-08 13:28:56 +03:00
$CTDB ip -X |
2015-02-13 12:55:43 +03:00
tail -n +2 |
while IFS="|" read x sip node x ; do
[ "$node" = "$pnn" ] || continue # not us
key="statd-state@${sip}@${cip}"
echo "\"${key}\" \"${date}\"" >"$key"
done
2007-05-31 05:09:45 +04:00
;;
2015-02-13 12:55:43 +03:00
del-client)
2013-11-08 09:41:11 +04:00
# statd does not tell us from which IP the client disconnected
# so we must add it to all the IPs that we serve
cip="$2"
2016-07-06 10:16:44 +03:00
# x is intentionally ignored
# shellcheck disable=SC2034
2016-06-08 13:28:56 +03:00
$CTDB ip -X |
2015-02-13 12:55:43 +03:00
tail -n +2 |
while IFS="|" read x sip node x ; do
[ "$node" = "$pnn" ] || continue # not us
key="statd-state@${sip}@${cip}"
echo "\"${key}\" \"\"" >"$key"
done
2007-05-31 05:09:45 +04:00
;;
2015-02-13 12:55:43 +03:00
update)
files=$(echo statd-state@*)
if [ "$files" = "statd-state@*" ] ; then
# No files!
exit 0
fi
# Filter out lines for any IP addresses that are not currently
# hosted public IP addresses.
2016-06-08 13:28:56 +03:00
ctdb_ips=$($CTDB ip | tail -n +2)
2015-02-13 12:55:43 +03:00
sed_expr=$(echo "$ctdb_ips" |
2016-07-06 13:09:07 +03:00
awk -v pnn="$pnn" 'pnn == $2 {
ip = $1; gsub(/\./, "\\.", ip);
2015-02-13 12:55:43 +03:00
printf "/statd-state@%s@/p\n", ip }')
2016-07-06 10:31:51 +03:00
# Intentional multi-word expansion for multiple files
# shellcheck disable=SC2086
2017-01-04 01:53:54 +03:00
items=$(sed -n "$sed_expr" $files)
if [ -n "$items" ] ; then
if echo "$items" | $CTDB ptrans "ctdb.tdb" ; then
# shellcheck disable=SC2086
rm $files
fi
2015-02-13 12:55:43 +03:00
fi
2017-01-04 01:53:54 +03:00
;;
2015-02-13 12:55:43 +03:00
2013-11-08 09:41:11 +04:00
notify)
2007-09-07 02:52:56 +04:00
# we must restart the lockmanager (on all nodes) so that we get
2015-07-27 00:02:57 +03:00
# a clusterwide grace period (so other clients don't take out
2007-09-07 02:52:56 +04:00
# conflicting locks through other nodes before all locks have been
# reclaimed)
# we need these settings to make sure that no tcp connections survive
# across a very fast failover/failback
2007-10-22 04:18:38 +04:00
#echo 10 > /proc/sys/net/ipv4/tcp_fin_timeout
2007-10-21 00:42:33 +04:00
#echo 0 > /proc/sys/net/ipv4/tcp_max_tw_buckets
#echo 0 > /proc/sys/net/ipv4/tcp_max_orphans
2007-09-07 02:52:56 +04:00
2015-07-27 00:02:57 +03:00
# Delete the notification list for statd, we don't want it to
2010-08-30 12:13:28 +04:00
# ping any clients
2007-09-07 02:52:56 +04:00
rm -f /var/lib/nfs/statd/sm/*
rm -f /var/lib/nfs/statd/sm.bak/*
2007-09-07 06:14:53 +04:00
2015-06-24 14:36:14 +03:00
# We must also let some time pass between stopping and
# restarting the lock manager. Otherwise there is a window
# where the lock manager will respond "strangely" immediately
# after restarting it, which causes clients to fail to reclaim
# their locks.
2017-02-14 01:04:41 +03:00
nfs_callout_init
2015-07-01 11:32:35 +03:00
"$CTDB_NFS_CALLOUT" "stop" "nlockmgr" >/dev/null 2>&1
sleep 2
"$CTDB_NFS_CALLOUT" "start" "nlockmgr" >/dev/null 2>&1
2007-09-07 02:52:56 +04:00
# we now need to send out additional statd notifications to ensure
# that clients understand that the lockmanager has restarted.
# we have three cases:
# 1, clients that ignore the ip address the stat notification came from
# and ONLY care about the 'name' in the notify packet.
# these clients ONLY work with lock failover IFF that name
# can be resolved into an ipaddress that matches the one used
# to mount the share. (==linux clients)
# This is handled when starting lockmanager above, but those
# packets are sent from the "wrong" ip address, something linux
# clients are ok with, buth other clients will barf at.
# 2, Some clients only accept statd packets IFF they come from the
# 'correct' ip address.
# 2a,Send out the notification using the 'correct' ip address and also
# specify the 'correct' hostname in the statd packet.
# Some clients require both the correct source address and also the
# correct name. (these clients also ONLY work if the ip addresses
# used to map the share can be resolved into the name returned in
# the notify packet.)
# 2b,Other clients require that the source ip address of the notify
# packet matches the ip address used to take out the lock.
# I.e. that the correct source address is used.
# These clients also require that the statd notify packet contains
# the name as the ip address used when the lock was taken out.
#
# Both 2a and 2b are commonly used in lockmanagers since they maximize
# probability that the client will accept the statd notify packet and
# not just ignore it.
2010-08-30 12:13:28 +04:00
# For all IPs we serve, collect info and push to the config database
2013-11-08 09:41:11 +04:00
# Construct a sed expression to take catdb output and produce pairs of:
# server-IP client-IP
# but only for the server-IPs that are hosted on this node.
2016-06-08 13:28:56 +03:00
ctdb_all_ips=$($CTDB ip all | tail -n +2)
2015-02-13 12:55:43 +03:00
sed_expr=$(echo "$ctdb_all_ips" |
2016-07-06 13:09:07 +03:00
awk -v pnn="$pnn" 'pnn == $2 {
ip = $1; gsub(/\./, "\\.", ip);
2014-12-19 06:19:32 +03:00
printf "s/^key.*=.*statd-state@\\(%s\\)@\\([^\"]*\\).*/\\1 \\2/p\n", ip }')
2013-11-08 09:41:11 +04:00
2016-06-08 13:28:56 +03:00
statd_state=$($CTDB catdb ctdb.tdb | sed -n "$sed_expr" | sort)
2013-11-19 08:40:08 +04:00
[ -n "$statd_state" ] || exit 0
2013-11-08 09:41:11 +04:00
2017-03-02 08:43:51 +03:00
echo "$statd_state" | send_notifies
echo "$statd_state" | delete_records
2015-02-13 12:55:43 +03:00
# Remove any stale touch files (i.e. for IPs not currently
# hosted on this node and created since the last "update").
# There's nothing else we can do with them at this stage.
echo "$ctdb_all_ips" |
2016-06-29 11:11:44 +03:00
awk -v pnn="$pnn" 'pnn != $2 { print $1 }' |
2015-02-13 12:55:43 +03:00
while read sip ; do
rm -f "statd-state@${sip}@"*
done
2007-05-31 05:09:45 +04:00
;;
esac