WHATSNEW: Add release notes for Samba 4.12.13.
[Samba.git] / ctdb / config / statd-callout
blob67ed2a5bc62154490fdb05acf36e95c39aeee40d
1 #!/bin/sh
3 # This must run as root as CTDB tool commands need to access CTDB socket
4 [ "$(id -u)" -eq 0 ] || exec sudo "$0" "$@"
6 # statd must be configured to use this script as its high availability call-out.
8 # In most Linux versions this can be done using something like the following...
10 # /etc/sysconfig/nfs (Red Hat) or /etc/default/nfs-common (Debian):
11 # NFS_HOSTNAME=myhostname
12 # STATD_HOSTNAME="${NFS_HOSTNAME} -H /etc/ctdb/statd-callout"
14 # Newer Red Hat Linux variants instead use /etc/nfs.conf:
15 # [statd]
16 # name = myhostname
17 # ha-callout = /etc/ctdb/statd-callout
19 [ -n "$CTDB_BASE" ] || \
20 CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; echo "$PWD")
22 . "${CTDB_BASE}/functions"
24 # Overwrite this so we get some logging
25 die ()
27 script_log "statd-callout" "$@"
28 exit 1
31 # Try different variables to find config file for NFS_HOSTNAME
32 load_system_config "nfs" "nfs-common"
34 # If NFS_HOSTNAME not set then try to pull it out of /etc/nfs.conf
35 if [ -z "$NFS_HOSTNAME" ] && type nfsconf >/dev/null 2>&1 ; then
36 NFS_HOSTNAME=$(nfsconf --get statd name)
39 [ -n "$NFS_HOSTNAME" ] || \
40 die "NFS_HOSTNAME is not configured. statd-callout failed"
42 ############################################################
44 ctdb_setup_state_dir "service" "nfs"
46 # script_state_dir set by ctdb_setup_state_dir()
47 # shellcheck disable=SC2154
48 d="${script_state_dir}/statd-callout"
50 mkdir -p "$d" || die "Failed to create directory \"${d}\""
51 cd "$d" || die "Failed to change directory to \"${d}\""
53 pnn=$(ctdb_get_pnn)
55 ############################################################
57 send_notifies ()
59 _smnotify="${CTDB_HELPER_BINDIR}/smnotify"
61 # State must monotonically increase, across the entire
62 # cluster. Use seconds since epoch and hope the time is in
63 # sync across nodes. Even numbers mean service is shut down,
64 # odd numbers mean service is started.
66 # Intentionally round to an even number
67 # shellcheck disable=SC2017
68 _state_even=$(( $(date '+%s') / 2 * 2))
70 _prev=""
71 while read _sip _cip ; do
72 # NOTE: Consider optimising smnotify to read all the
73 # data from stdin and then run it in the background.
75 # Reset stateval for each serverip
76 if [ "$_sip" != "$_prev" ] ; then
77 _stateval="$_state_even"
80 # Send notifies for server shutdown
81 "$_smnotify" --client="$_cip" --ip="$_sip" \
82 --server="$_sip" --stateval="$_stateval"
83 "$_smnotify" --client="$_cip" --ip="$_sip" \
84 --server="$NFS_HOSTNAME" --stateval="$_stateval"
86 # Send notifies for server startup
87 _stateval=$((_stateval + 1))
88 "$_smnotify" --client="$_cip" --ip="$_sip" \
89 --server="$_sip" --stateval="$_stateval"
90 "$_smnotify" --client="$_cip" --ip="$_sip" \
91 --server="$NFS_HOSTNAME" --stateval="$_stateval"
92 done
95 delete_records ()
97 while read _sip _cip ; do
98 _key="statd-state@${_sip}@${_cip}"
99 echo "\"${_key}\" \"\""
100 done | $CTDB ptrans "ctdb.tdb"
103 ############################################################
105 case "$1" in
106 # Keep a single file to keep track of the last "add-client" or
107 # "del-client'. These get pushed to ctdb.tdb during "update",
108 # which will generally be run once each "monitor" cycle. In this
109 # way we avoid scalability problems with flood of persistent
110 # transactions after a "notify" when all the clients re-take their
111 # locks.
113 add-client)
114 # statd does not tell us to which IP the client connected so
115 # we must add it to all the IPs that we serve
116 cip="$2"
117 date=$(date '+%s')
118 # x is intentionally ignored
119 # shellcheck disable=SC2034
120 $CTDB ip -X |
121 tail -n +2 |
122 while IFS="|" read x sip node x ; do
123 [ "$node" = "$pnn" ] || continue # not us
124 key="statd-state@${sip}@${cip}"
125 echo "\"${key}\" \"${date}\"" >"$key"
126 done
129 del-client)
130 # statd does not tell us from which IP the client disconnected
131 # so we must add it to all the IPs that we serve
132 cip="$2"
133 # x is intentionally ignored
134 # shellcheck disable=SC2034
135 $CTDB ip -X |
136 tail -n +2 |
137 while IFS="|" read x sip node x ; do
138 [ "$node" = "$pnn" ] || continue # not us
139 key="statd-state@${sip}@${cip}"
140 echo "\"${key}\" \"\"" >"$key"
141 done
144 update)
145 files=$(echo statd-state@*)
146 if [ "$files" = "statd-state@*" ] ; then
147 # No files!
148 exit 0
150 # Filter out lines for any IP addresses that are not currently
151 # hosted public IP addresses.
152 ctdb_ips=$($CTDB ip | tail -n +2)
153 sed_expr=$(echo "$ctdb_ips" |
154 awk -v pnn="$pnn" 'pnn == $2 {
155 ip = $1; gsub(/\./, "\\.", ip);
156 printf "/statd-state@%s@/p\n", ip }')
157 # Intentional multi-word expansion for multiple files
158 # shellcheck disable=SC2086
159 items=$(sed -n "$sed_expr" $files)
160 if [ -n "$items" ] ; then
161 if echo "$items" | $CTDB ptrans "ctdb.tdb" ; then
162 # shellcheck disable=SC2086
163 rm $files
168 notify)
169 # we must restart the lockmanager (on all nodes) so that we get
170 # a clusterwide grace period (so other clients don't take out
171 # conflicting locks through other nodes before all locks have been
172 # reclaimed)
174 # we need these settings to make sure that no tcp connections survive
175 # across a very fast failover/failback
176 #echo 10 > /proc/sys/net/ipv4/tcp_fin_timeout
177 #echo 0 > /proc/sys/net/ipv4/tcp_max_tw_buckets
178 #echo 0 > /proc/sys/net/ipv4/tcp_max_orphans
180 # Delete the notification list for statd, we don't want it to
181 # ping any clients
182 rm -f /var/lib/nfs/statd/sm/*
183 rm -f /var/lib/nfs/statd/sm.bak/*
185 # We must also let some time pass between stopping and
186 # restarting the lock manager. Otherwise there is a window
187 # where the lock manager will respond "strangely" immediately
188 # after restarting it, which causes clients to fail to reclaim
189 # their locks.
190 nfs_callout_init
191 "$CTDB_NFS_CALLOUT" "stop" "nlockmgr" >/dev/null 2>&1
192 sleep 2
193 "$CTDB_NFS_CALLOUT" "start" "nlockmgr" >/dev/null 2>&1
195 # we now need to send out additional statd notifications to ensure
196 # that clients understand that the lockmanager has restarted.
197 # we have three cases:
198 # 1, clients that ignore the ip address the stat notification came from
199 # and ONLY care about the 'name' in the notify packet.
200 # these clients ONLY work with lock failover IFF that name
201 # can be resolved into an ipaddress that matches the one used
202 # to mount the share. (==linux clients)
203 # This is handled when starting lockmanager above, but those
204 # packets are sent from the "wrong" ip address, something linux
205 # clients are ok with, buth other clients will barf at.
206 # 2, Some clients only accept statd packets IFF they come from the
207 # 'correct' ip address.
208 # 2a,Send out the notification using the 'correct' ip address and also
209 # specify the 'correct' hostname in the statd packet.
210 # Some clients require both the correct source address and also the
211 # correct name. (these clients also ONLY work if the ip addresses
212 # used to map the share can be resolved into the name returned in
213 # the notify packet.)
214 # 2b,Other clients require that the source ip address of the notify
215 # packet matches the ip address used to take out the lock.
216 # I.e. that the correct source address is used.
217 # These clients also require that the statd notify packet contains
218 # the name as the ip address used when the lock was taken out.
220 # Both 2a and 2b are commonly used in lockmanagers since they maximize
221 # probability that the client will accept the statd notify packet and
222 # not just ignore it.
223 # For all IPs we serve, collect info and push to the config database
225 # Construct a sed expression to take catdb output and produce pairs of:
226 # server-IP client-IP
227 # but only for the server-IPs that are hosted on this node.
228 ctdb_all_ips=$($CTDB ip all | tail -n +2)
229 sed_expr=$(echo "$ctdb_all_ips" |
230 awk -v pnn="$pnn" 'pnn == $2 {
231 ip = $1; gsub(/\./, "\\.", ip);
232 printf "s/^key.*=.*statd-state@\\(%s\\)@\\([^\"]*\\).*/\\1 \\2/p\n", ip }')
234 statd_state=$($CTDB catdb ctdb.tdb | sed -n "$sed_expr" | sort)
235 [ -n "$statd_state" ] || exit 0
237 echo "$statd_state" | send_notifies
238 echo "$statd_state" | delete_records
240 # Remove any stale touch files (i.e. for IPs not currently
241 # hosted on this node and created since the last "update").
242 # There's nothing else we can do with them at this stage.
243 echo "$ctdb_all_ips" |
244 awk -v pnn="$pnn" 'pnn != $2 { print $1 }' |
245 while read sip ; do
246 rm -f "statd-state@${sip}@"*
247 done
249 esac