ctdb-scripts: Quote some variable expansions
[vlendec/samba-autobuild/.git] / ctdb / config / statd-callout
index 0e52cbade36010921d1ddb4a26d4c376a3e345f3..509ed182c06bc452b7eadb2f3cf53f32b9ca62fb 100755 (executable)
 #!/bin/sh
 
+# This must run as root as CTDB tool commands need to access CTDB socket
+[ $(id -u) -eq 0 ] || exec sudo "$0" "$@"
+
 # this script needs to be installed so that statd points to it with the -H 
 # 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"
 
-[ -z "$CTDB_BASE" ] && {
-    export CTDB_BASE="/etc/ctdb"
+[ -n "$CTDB_BASE" ] || \
+    CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; dirname "$PWD")
+
+. "${CTDB_BASE}/functions"
+
+# Overwrite this so we get some logging
+die ()
+{
+    script_log "statd-callout" "$@"
+    exit 1
 }
 
-. $CTDB_BASE/functions
 loadconfig ctdb
 loadconfig nfs
 
-[ -z $NFS_HOSTNAME ] && {
-       echo NFS_HOSTNAME is not configured. statd-callout failed.
-       exit 0
-}
+[ -n "$NFS_HOSTNAME" ] || \
+    die "NFS_HOSTNAME is not configured. statd-callout failed"
+
+# A handy newline
+nl="
+"
+
+ctdb_setup_service_state_dir "statd-callout"
+
+cd "$service_state_dir" || \
+    die "Failed to change directory to \"${service_state_dir}\""
 
 case "$1" in
-  add-client)
-       # the callout does not tell us to which ip the client connected
-       # so we must add it to all the ips that we serve
-       PNN=`ctdb xpnn | sed -e "s/.*://"`
-       ctdb ip -Y | while read LINE; do
-               NODE=`echo $LINE | cut -f3 -d:`
-               [ "$NODE" = "$PNN" ] || {
-                       # not us
-                       continue
-               } 
-               IP=`echo $LINE | cut -f2 -d:`
-               mkdir -p $CTDB_VARDIR/state/statd/ip/$IP
-               touch $CTDB_VARDIR/state/statd/ip/$IP/$2
-       done
-       ;;
-  del-client)
-       # the callout does not tell us to which ip the client disconnected
-       # so we must remove it from all the ips that we serve
-       PNN=`ctdb xpnn | sed -e "s/.*://"`
-       ctdb ip -Y | while read LINE; do
-               NODE=`echo $LINE | cut -f3 -d:`
-               [ "$NODE" = "$PNN" ] || {
-                       # not us
-                       continue
-               } 
-               IP=`echo $LINE | cut -f2 -d:`
-               mkdir -p $CTDB_VARDIR/state/statd/ip/$IP
-               rm -f $CTDB_VARDIR/state/statd/ip/$IP/$2
-       done
-       ;;
-  updatelocal)
-       # For all IPs we serve, collect info and push to the config database
-       PNN=`ctdb xpnn | sed -e "s/.*://"`
-       ctdb ip -Y | tail -n +2 | while read LINE; do
-               NODE=`echo $LINE | cut -f3 -d:`
-               [ "$NODE" = "$PNN" ] || {
-                       continue
-               } 
-               IP=`echo $LINE | cut -f2 -d:`
-
-               mkdir -p $CTDB_VARDIR/state/statd/ip/$IP
-
-               rm -f $CTDB_VARDIR/state/statd/ip/$IP.tar
-               tar cfP $CTDB_VARDIR/state/statd/ip/$IP.tar $CTDB_VARDIR/state/statd/ip/$IP
-
-               rm -f $CTDB_VARDIR/state/statd/ip/$IP.rec
-               ctdb pfetch ctdb.tdb statd-state:$IP $CTDB_VARDIR/state/statd/ip/$IP.rec 2>/dev/null
-               [ "$?" = "0" ] || {
-                       # something went wrong,  try storing this data
-                       echo No record. Store STATD state data for $IP
-                       ctdb pstore ctdb.tdb statd-state:$IP $CTDB_VARDIR/state/statd/ip/$IP.tar 2>/dev/null
-                       continue
-               }
-
-               cmp $CTDB_VARDIR/state/statd/ip/$IP.tar $CTDB_VARDIR/state/statd/ip/$IP.rec >/dev/null 2>/dev/null
-               [ "$?" = "0" ] || {
-                       # something went wrong,  try storing this data
-                       echo Updated record. Store STATD state data for $IP
-                       ctdb pstore ctdb.tdb statd-state:$IP $CTDB_VARDIR/state/statd/ip/$IP.tar 2>/dev/null
-                       continue
-               }
+    # 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.
+
+    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"
+       ctdb_get_pnn
+       date=$(date '+%s')
+       $CTDB ip -X |
+       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
        ;;
 
-  updateremote)
-       # For all IPs we dont serve, pull the state from the database
-       PNN=`ctdb xpnn | sed -e "s/.*://"`
-       ctdb ip -Y | tail -n +2 | while read LINE; do
-               NODE=`echo $LINE | cut -f3 -d:`
-               [ "$NODE" = "$PNN" ] && {
-                       continue
-               } 
-               IP=`echo $LINE | cut -f2 -d:`
-
-               mkdir -p $CTDB_VARDIR/state/statd/ip/$IP
-
-               rm -f $CTDB_VARDIR/state/statd/ip/$IP.rec
-               ctdb pfetch ctdb.tdb statd-state:$IP $CTDB_VARDIR/state/statd/ip/$IP.rec 2>/dev/null
-               [ "$?" = "0" ] || {
-                       continue
-               }
-
-               rm -f $CTDB_VARDIR/state/statd/ip/$IP/*
-               tar xfP $CTDB_VARDIR/state/statd/ip/$IP.rec
+    del-client)
+       # 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"
+       ctdb_get_pnn
+       $CTDB ip -X |
+       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
        ;;
 
-  notify)
+    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.
+       ctdb_get_pnn
+       ctdb_ips=$($CTDB ip | tail -n +2)
+       sed_expr=$(echo "$ctdb_ips" |
+           awk -v pnn="$pnn" 'pnn == $2 { \
+                ip = $1; gsub(/\./, "\\.", ip); \
+                printf "/statd-state@%s@/p\n", ip }')
+        if cat $files | sed -n "$sed_expr" | $CTDB ptrans "ctdb.tdb" ; then
+            rm $files
+       fi
+        ;;
+
+    notify)
        # we must restart the lockmanager (on all nodes) so that we get
-       # a clusterwide grace period (so other clients dont take out
+       # a clusterwide grace period (so other clients don't take out
        # conflicting locks through other nodes before all locks have been
        # reclaimed)
 
@@ -118,7 +103,7 @@ case "$1" in
        #echo 0 > /proc/sys/net/ipv4/tcp_max_tw_buckets
        #echo 0 > /proc/sys/net/ipv4/tcp_max_orphans
 
-       # Delete the notification list for statd, we dont want it to 
+       # Delete the notification list for statd, we don't want it to 
        # ping any clients
        rm -f /var/lib/nfs/statd/sm/*
        rm -f /var/lib/nfs/statd/sm.bak/*
@@ -129,23 +114,16 @@ case "$1" in
        # We use epoch and hope the nodes are close enough in clock.
        # Even numbers mean service is shut down, odd numbers mean
        # service is started.
-       STATE=$(( $(date '+%s') / 2 * 2))
-
-
-       # we must also let some time pass between stopping and restarting the
-       # lockmanager since othervise there is a window where the lockmanager
-       # will respond "strangely" immediately after restarting it, which
-       # causes clients to fail to reclaim the locks.
-       # 
-       if [ "$NFS_SERVER_MODE" = "ganesha" ] ; then
-            startstop_ganesha stop >/dev/null 2>&1
-            sleep 2
-            startstop_ganesha start >/dev/null 2>&1
-       else
-            startstop_nfslock stop >/dev/null 2>&1
-            sleep 2
-            startstop_nfslock start >/dev/null 2>&1
-       fi
+       state_even=$(( $(date '+%s') / 2 * 2))
+
+       # 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.
+       "$CTDB_NFS_CALLOUT" "stop" "nlockmgr" >/dev/null 2>&1
+        sleep 2
+       "$CTDB_NFS_CALLOUT" "start" "nlockmgr" >/dev/null 2>&1
 
        # we now need to send out additional statd notifications to ensure
        # that clients understand that the lockmanager has restarted.
@@ -176,22 +154,61 @@ case "$1" in
        # probability that the client will accept the statd notify packet and
        # not just ignore it.
        # For all IPs we serve, collect info and push to the config database
-       PNN=`ctdb xpnn | sed -e "s/.*://"`
-       ctdb ip -Y | tail -n +2 | while read LINE; do
-               NODE=`echo $LINE | cut -f3 -d:`
-               [ "$NODE" = "$PNN" ] || {
-                       continue
-               } 
-               IP=`echo $LINE | cut -f2 -d:`
-
-               ls $CTDB_VARDIR/state/statd/ip/$IP | while read CLIENT; do
-                       rm $CTDB_VARDIR/state/statd/ip/$IP/$CLIENT
-                       smnotify --client=$CLIENT --ip=$IP --server=$ip --stateval=$STATE
-                       smnotify --client=$CLIENT --ip=$IP --server=$NFS_HOSTNAME --stateval=$STATE
-                       STATE=$(($STATE + 1))
-                       smnotify --client=$CLIENT --ip=$IP --server=$ip --stateval=$STATE
-                       smnotify --client=$CLIENT --ip=$IP --server=$NFS_HOSTNAME --stateval=$STATE
-               done
-       done
+       ctdb_get_pnn
+
+       # 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.
+       ctdb_all_ips=$($CTDB ip all | tail -n +2)
+       sed_expr=$(echo "$ctdb_all_ips" |
+           awk -v pnn="$pnn" 'pnn == $2 { \
+                ip = $1; gsub(/\./, "\\.", ip); \
+                printf "s/^key.*=.*statd-state@\\(%s\\)@\\([^\"]*\\).*/\\1 \\2/p\n", ip }')
+
+       statd_state=$($CTDB catdb ctdb.tdb | sed -n "$sed_expr" | sort)
+       [ -n "$statd_state" ] || exit 0
+
+       smnotify="${CTDB_HELPER_BINDIR}/smnotify"
+       prev=""
+       echo "$statd_state" | {
+           # This all needs to be in the same command group at the
+           # end of the pipe so it doesn't get lost when the loop
+           # completes.
+           items=""
+           while read sip cip ; do
+               # Collect item to delete from the DB
+               key="statd-state@${sip}@${cip}"
+               item="\"${key}\" \"\""
+               items="${items}${items:+${nl}}${item}"
+
+               # NOTE: Consider optimising smnotify to read all the
+               # data from stdin and then run it in the background.
+
+               # Reset stateval for each serverip
+               [ "$sip" = "$prev" ] || stateval="$state_even"
+               # 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
+
+           echo "$items" | $CTDB ptrans "ctdb.tdb"
+       }
+
+       # 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" |
+           awk -v pnn="$pnn" 'pnn != $2 { print $1 }' |
+           while read sip ; do
+               rm -f "statd-state@${sip}@"*
+           done
        ;;
 esac