cope with non-standard install dirs in event scripts
[vlendec/samba-autobuild/.git] / ctdb / config / statd-callout
1 #!/bin/sh
2
3 # this script needs to be installed so that statd points to it with the -H 
4 # command line argument. The easiest way to do that is to put something like this in 
5 # /etc/sysconfig/nfs:
6 #   STATD_HOSTNAME="myhostname -H /etc/ctdb/statd-callout"
7
8 [ -z "$CTDB_BASE" ] && {
9     export CTDB_BASE="/etc/ctdb"
10 }
11
12 . $CTDB_BASE/functions
13 loadconfig nfs
14
15 [ -z "$STATD_SHARED_DIRECTORY" ] && {
16         echo STATD_SHARED_DIRECTORY not configured. statd-callout failed.
17         exit 0
18 }
19
20 [ -d $STATD_SHARED_DIRECTORY ] || exit 0
21
22 [ -z $NFS_HOSTNAME ] && {
23         echo NFS_HOSTNAME is not configured. statd-callout failed.
24         exit 0
25 }
26
27 case "$1" in
28   add-client)
29         # the callout does not tell us to which ip the client connected
30         # so we must add it to all the ips that we serve
31         for f in `/bin/ls $CTDB_BASE/state/statd/ip/*`; do
32             ip=`/bin/basename $f`
33             [ -d $STATD_SHARED_DIRECTORY/$ip ] || /bin/mkdir $STATD_SHARED_DIRECTORY/$ip
34             /bin/touch $STATD_SHARED_DIRECTORY/$ip/$2
35         done
36         ;;
37   del-client)
38         # the callout does not tell us to which ip the client connected
39         # so we must add it to all the ips that we serve
40         for f in `/bin/ls $CTDB_BASE/state/statd/ip/*`; do
41             ip=`/bin/basename $f`
42             /bin/rm -f $STATD_SHARED_DIRECTORY/$ip/$2
43         done
44         ;;
45   notify)
46         # we must restart the lockmanager (on all nodes) so that we get
47         # a clusterwide grace period (so other clients dont take out
48         # conflicting locks through other nodes before all locks have been
49         # reclaimed)
50
51         # we need these settings to make sure that no tcp connections survive
52         # across a very fast failover/failback
53         echo 0 > /proc/sys/net/ipv4/tcp_max_tw_buckets
54         echo 0 > /proc/sys/net/ipv4/tcp_fin_timeout
55         echo 0 > /proc/sys/net/ipv4/tcp_max_orphans
56
57         # rebuild the state directory for the local statd to use the correct
58         # state value and to initally send notifications to all clients
59         rm -f /var/lib/nfs/statd/sm/*
60         rm -f /var/lib/nfs/statd/sm.bak/*
61         cat $STATD_SHARED_DIRECTORY/state >/var/lib/nfs/statd/state
62
63
64         # we must keep a monotonically increasing state variable for the entire
65         # cluster  so state always increases when ip addresses fail from one
66         # node to another
67         [ ! -f $STATD_SHARED_DIRECTORY/state ] && {
68                 echo 1 | awk '{printf("%c%c%c%c", $0, $0/256, $0/256/256, $0/256/256/256);}' >$STATD_SHARED_DIRECTORY/state
69         }
70         # read current state
71         STATE=`od -t d4 $STATD_SHARED_DIRECTORY/state | head -1 | sed -e "s/^[0-9]*[^0-9]*//"`
72         # write current state+2 back to the state file
73         # the /2 *2 are to ensure that state is odd. state must be odd.
74         STATE=`expr $STATE "/" 2 "*" 2 "+" 3`
75         echo $STATE | awk '{printf("%c%c%c%c", $0, $0/256, $0/256/256, $0/256/256/256);}' >$STATD_SHARED_DIRECTORY/state
76         
77
78
79         # we must also let some time pass between stopping and restarting the
80         # lockmanager since othervise there is a window where the lockmanager
81         # will respond "strangely" immediately after restarting it, which
82         # causes clients to fail to reclaim the locks.
83         # 
84         service nfslock stop > /dev/null 2>&1
85         sleep 2
86
87         # copy all monitored clients on this node to the local lockmanager
88         for f in `/bin/ls $CTDB_BASE/state/statd/ip/* 2>/dev/null`; do
89             ip=`/bin/basename $f`
90             [ -d $STATD_SHARED_DIRECTORY/$ip ] && [ -x /usr/bin/smnotify ] && {
91                 for g in `/bin/ls $STATD_SHARED_DIRECTORY/$ip/* 2>/dev/null`; do
92                         client=`/bin/basename $g`
93                         touch /var/lib/nfs/statd/sm/$client
94                 done
95             }
96         done
97
98         # now start lockmanager again with the new state directory.
99         service nfslock start > /dev/null 2>&1
100
101         # we now need to send out additional statd notifications to ensure
102         # that clients understand that the lockmanager has restarted.
103         # we have three cases:
104         # 1, clients that ignore the ip address the stat notification came from
105         #    and ONLY care about the 'name' in the notify packet.
106         #    these clients ONLY work with lock failover IFF that name
107         #    can be resolved into an ipaddress that matches the one used
108         #    to mount the share.  (==linux clients)
109         #    This is handled when starting lockmanager above,  but those
110         #    packets are sent from the "wrong" ip address, something linux
111         #    clients are ok with, buth other clients will barf at.
112         # 2, Some clients only accept statd packets IFF they come from the
113         #    'correct' ip address.
114         # 2a,Send out the notification using the 'correct' ip address and also
115         #    specify the 'correct' hostname in the statd packet.
116         #    Some clients require both the correct source address and also the
117         #    correct name. (these clients also ONLY work if the ip addresses
118         #    used to map the share can be resolved into the name returned in
119         #    the notify packet.)
120         # 2b,Other clients require that the source ip address of the notify
121         #    packet matches the ip address used to take out the lock.
122         #    I.e. that the correct source address is used.
123         #    These clients also require that the statd notify packet contains
124         #    the name as the ip address used when the lock was taken out.
125         #
126         # Both 2a and 2b are commonly used in lockmanagers since they maximize
127         # probability that the client will accept the statd notify packet and
128         # not just ignore it.
129         for f in `/bin/ls $CTDB_BASE/state/statd/ip/* 2>/dev/null`; do
130             ip=`/bin/basename $f`
131             [ -d $STATD_SHARED_DIRECTORY/$ip ] && [ -x /usr/bin/smnotify ] && {
132                 for g in `/bin/ls $STATD_SHARED_DIRECTORY/$ip/* 2>/dev/null`; do
133                         client=`/bin/basename $g`
134 #                       /bin/rm -f $g
135                         # send out notifications from the "correct" address
136                         # (the same addresse as where the lock was taken out
137                         # on)   some clients require that the source address
138                         # matches where the lock was taken out.
139                         # also send it both as a name that the client
140                         # hopefully can resolve into the server ip and
141                         # and also by specifying the raw ip address as name.
142                         /usr/bin/smnotify --client=$client --ip=$ip --server=$ip --stateval=$STATE
143                         /usr/bin/smnotify --client=$client --ip=$ip --server=$NFS_HOSTNAME --stateval=$STATE
144                 done
145             }
146         done
147         ;;
148 esac