2007-05-31 05:09:45 +04:00
#!/bin/sh
2024-11-26 03:25:09 +03:00
# statd must be configured to use statd_callout, CTDB's binary
# counterpart to this script, as its availability call-out.
2020-07-13 03:16:33 +03:00
#
2023-06-13 03:39:37 +03:00
# Modern NFS utils versions use /etc/nfs.conf:
#
# [statd]
# name = mycluster
2024-05-10 04:42:26 +03:00
# ha-callout = /usr/local/libexec/ctdb/statd_callout
2023-06-13 03:39:37 +03:00
#
# Older Linux versions may use something like the following...
2020-07-13 03:16:33 +03:00
#
# /etc/sysconfig/nfs (Red Hat) or /etc/default/nfs-common (Debian):
2024-05-01 03:22:05 +03:00
# STATD_HOSTNAME="mycluster -H /usr/local/libexec/ctdb/statd_callout"
2020-07-13 03:16:33 +03:00
#
2017-03-03 07:44:08 +03:00
# If using Linux kernel NFS then the following should also be set in
# /etc/nfs.conf:
#
# [sm-notify]
# lift-grace = n
#
# See sm-notify(8) for details. This doesn't matter when using
# NFS-Ganesha because sm-notify's attempt to lift grace will fail
# silently if /proc/fs/lockd/nlm_end_grace is not found.
#
2007-06-02 13:45:06 +04:00
2024-11-26 03:25:09 +03:00
if [ -z "$CTDB_BASE" ]; then
2024-05-10 04:42:26 +03:00
export CTDB_BASE="/usr/local/etc/ctdb"
fi
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
2023-06-16 04:09:02 +03:00
die()
2013-11-08 09:41:11 +04:00
{
2024-05-10 04:42:26 +03:00
script_log "statd_callout_helper" "$@"
2023-06-16 04:09:02 +03:00
exit 1
2013-11-08 09:41:11 +04:00
}
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
2023-06-19 05:17:44 +03:00
find_statd_sm_dir()
{
if [ -n "$CTDB_TEST_MODE" ]; then
_f="${CTDB_TEST_TMP_DIR}/sm"
mkdir -p "$_f" "${_f}.bak"
echo "$_f"
return
fi
for _sm_dir in /var/lib/nfs/statd/sm /var/lib/nfs/sm; do
if [ -d "$_sm_dir" ]; then
echo "$_sm_dir"
break
fi
done
}
# Ensure the state directory exists and can be written when called as
# a non-root user. Assume the user to run as is the owner of the
# system statd sm directory, since both rpc.statd and sm-notify run as
# this directory's owner, so it can read and modify the directory.
create_add_del_client_dir()
{
_dir="$1"
if [ ! -d "$_dir" ]; then
mkdir -p "$_dir" || die "Failed to create directory \"${_dir}\""
ref=$(find_statd_sm_dir)
[ -n "$ref" ] || die "Failed to find statd sm directory"
chown --reference="$ref" "$_dir"
fi
}
2018-03-07 03:12:29 +03:00
# script_state_dir set by ctdb_setup_state_dir()
# shellcheck disable=SC2154
2024-05-10 04:42:26 +03:00
statd_callout_state_dir="${script_state_dir}/statd_callout"
2018-03-07 03:12:29 +03:00
2023-06-29 06:11:46 +03:00
statd_callout_db="ctdb.tdb"
2024-05-08 07:44:13 +03:00
statd_callout_queue_dir="${statd_callout_state_dir}/queue"
2023-06-29 06:11:46 +03:00
2017-03-02 08:43:51 +03:00
############################################################
2017-03-03 07:44:08 +03:00
# Read pairs of:
# server-IP client-IP
# from stdin and send associated SM_NOTIFY packets.
2023-06-16 04:09:02 +03:00
send_notifies()
2017-03-02 08:43:51 +03:00
{
# State must monotonically increase, across the entire
2017-03-03 07:44:08 +03:00
# cluster. Use seconds since epoch and assume the time is in
2017-03-02 08:43:51 +03:00
# sync across nodes. Even numbers mean service is shut down,
2017-03-03 07:44:08 +03:00
# odd numbers mean service is up. However, sm-notify always
# reads the state and converts it to odd (if necessary, by
# adding 1 when it is even) because it only sends "up"
# notifications. Note that there is a 2038 issue here but we
# will get to that later.
_state=$(date '+%s')
_helper="${CTDB_HELPER_BINDIR}/ctdb_smnotify_helper"
2017-03-02 08:43:51 +03:00
2017-03-03 07:44:08 +03:00
_notify_dir="${statd_callout_state_dir}/sm-notify"
mkdir -p "$_notify_dir"
2017-03-02 08:43:51 +03:00
2023-06-19 02:43:33 +03:00
while read -r _sip _cip; do
2017-03-03 07:44:08 +03:00
# Create a directory per server IP containing a file
# for each client IP
mkdir -p \
"${_notify_dir}/${_sip}/sm" \
"${_notify_dir}/${_sip}/sm.bak"
_out="${_notify_dir}/${_sip}/sm/${_cip}"
"$_helper" "monitor" "$_cip" "$_sip" >"$_out"
done
2017-03-02 08:43:51 +03:00
2017-03-03 07:44:08 +03:00
# Send notifications for server startup
_ref=$(find_statd_sm_dir)
for _sip_dir in "$_notify_dir"/*; do
if [ "$_sip_dir" = "${_notify_dir}/*" ]; then
break
2017-03-02 08:43:51 +03:00
fi
2017-03-03 07:44:08 +03:00
_sip="${_sip_dir##*/}" # basename
# Write the state as a host order 32-bit integer. See
# note at top of function about state.
_out="${_sip_dir}/state"
"$_helper" "state" "$_state" >"$_out"
# The ownership of the directory and contents should
# match the system's statd sm directory, so that
# sm-notify drops privileges and switches to run as
# the directory owner.
chown -R --reference="$_ref" "$_sip_dir"
timeout 10 sm-notify -d -f -m 0 -n -P "$_sip_dir" -v "$_sip"
rm -rf "$_sip_dir"
2017-03-02 08:43:51 +03:00
done
}
2023-06-16 04:09:02 +03:00
delete_records()
2017-03-02 08:43:51 +03:00
{
2023-06-19 02:43:33 +03:00
while read -r _sip _cip; do
2017-03-02 08:43:51 +03:00
_key="statd-state@${_sip}@${_cip}"
echo "\"${_key}\" \"\""
2023-06-29 06:11:46 +03:00
done | $CTDB ptrans "$statd_callout_db"
2017-03-02 08:43:51 +03:00
}
############################################################
2023-06-29 06:11:46 +03:00
# Keep a file per server-IP/client-IP pair, to keep track of the last
# "add-client" or "del-client'. These get pushed to a database during
# "update", which will generally be run once each "monitor" cycle. In
# this way we avoid scalability problems with flood of persistent
2023-06-16 04:09:02 +03:00
# transactions after a "notify" when all the clients re-take their
# locks.
2023-06-29 06:11:46 +03:00
startup()
{
2023-06-19 05:17:44 +03:00
create_add_del_client_dir "$statd_callout_queue_dir"
2023-06-29 06:25:03 +03:00
2023-06-29 06:11:46 +03:00
$CTDB attach "$statd_callout_db" persistent
2024-05-10 04:42:26 +03:00
_default="${CTDB_SCRIPT_VARDIR}/statd_callout.conf"
_config_file="${CTDB_STATD_CALLOUT_CONFIG_FILE:-"${_default}"}"
cat >"$_config_file" <<EOF
persistent_db
${statd_callout_queue_dir}
${CTDB_MY_PUBLIC_IPS_CACHE}
EOF
2023-06-29 06:11:46 +03:00
}
############################################################
case "$1" in
startup)
startup
;;
2023-06-16 04:09:02 +03:00
update)
2024-05-08 07:44:13 +03:00
cd "$statd_callout_queue_dir" ||
die "Failed to change directory to \"${statd_callout_queue_dir}\""
2023-06-16 04:09:02 +03:00
files=$(echo statd-state@*)
if [ "$files" = "statd-state@*" ]; then
# No files!
exit 0
2015-02-13 12:55:43 +03:00
fi
2023-06-29 03:12:44 +03:00
sed_expr=$(awk '{
ip = $1; gsub(/\./, "\\.", ip);
printf "/statd-state@%s@/p\n", ip }' "$CTDB_MY_PUBLIC_IPS_CACHE")
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)
2023-06-16 04:09:02 +03:00
if [ -n "$items" ]; then
2023-06-29 06:11:46 +03:00
if echo "$items" | $CTDB ptrans "$statd_callout_db"; then
2017-01-04 01:53:54 +03:00
# 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
2023-06-16 04:09:02 +03: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
2023-06-16 04:09:02 +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
2023-08-02 06:37:03 +03:00
dir=$(find_statd_sm_dir)
rm -f "${dir}/"* "${dir}.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
2023-06-16 04:09:02 +03:00
sleep 2
2015-07-01 11:32:35 +03:00
"$CTDB_NFS_CALLOUT" "start" "nlockmgr" >/dev/null 2>&1
2007-09-07 02:52:56 +04:00
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.
2023-06-29 03:12:44 +03:00
sed_expr=$(awk '{
ip = $1; gsub(/\./, "\\.", ip);
printf "s/^key.*=.*statd-state@\\(%s\\)@\\([^\"]*\\).*/\\1 \\2/p\n", ip }' \
"$CTDB_MY_PUBLIC_IPS_CACHE")
2013-11-08 09:41:11 +04:00
2023-06-29 06:11:46 +03:00
statd_state=$($CTDB catdb "$statd_callout_db" |
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.
2023-06-29 03:12:44 +03:00
pnn=$(ctdb_get_pnn)
$CTDB ip all |
tail -n +2 |
2023-06-16 04:09:02 +03:00
awk -v pnn="$pnn" 'pnn != $2 { print $1 }' |
2023-06-19 02:43:33 +03:00
while read -r sip; do
2024-05-08 07:44:13 +03:00
rm -f "${statd_callout_queue_dir}/statd-state@${sip}@"*
2023-06-16 04:09:02 +03:00
done
2007-05-31 05:09:45 +04:00
;;
esac