From 91f8ff5cf5559381f6295eb84374c34de9dbe53c Mon Sep 17 00:00:00 2001 From: Lukas Nykryn Date: Mon, 18 Jan 2016 13:32:54 +0100 Subject: netfs: tweak nfs umount We want to run one round of regular umount before we will look for open descriptors. In the case that there are some stale mounts, without any open file descriptors on them we will avoid blocking stat call later. Resolves: #1296900 --- rc.d/init.d/functions | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'rc.d/init.d/functions') diff --git a/rc.d/init.d/functions b/rc.d/init.d/functions index 45c71f93..7a4fde04 100644 --- a/rc.d/init.d/functions +++ b/rc.d/init.d/functions @@ -111,6 +111,76 @@ __umount_loop() { done } +# Similar to __umount loop above, without calling fuser +__umount_loop_2() { + local remaining= + local count + + #call regular umount + remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r) + action "$3" fstab-decode umount $remaining + + count=4 + remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r) + while [ "$count" -gt 0 ]; do + [ -z "$remaining" ] && break + count=$(($count-1)) + usleep 500000 + remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r) + done + [ -z "$remaining" ] && return 0 + + devs=$(stat -c "%d" $remaining) + action "$4" fstab-decode umount "-l" $remaining + + # find fds that don't start with /, are not sockets or pipes or other. + # these are potentially detached fds + detached_fds=$(find /proc/ -regex '/proc/[0-9]+/fd/.*' -printf "%p %l\n" 2>/dev/null |\ + grep -Ev '/proc/[0-9]+/fd/[0-9]+ (/.*|inotify|\[.+\]|(socket|pipe):\[[0-9]+\])') + + # check each detached fd to see if it has the same device + # as one of our lazy umounted filesystems + kill_list= + [ -n "$detached_fds" ] && while read fdline; do + fd=${fdline%% *} + pid=$(echo $fdline | sed -r 's/\/proc\/([0-9]+).+/\1/') + fd_dev=$(stat -L -c "%d" $fd) + for dev in $devs ; do + [ "$dev" = "$fd_dev" ] && kill_list+="$pid " + done + done <<< "$detached_fds" + + [ -n "$kill_list" ] && kill $kill_list + + # run a little wait/check loop for procs to exit + count=4 + while [ "$count" -gt 0 ] ; do + [ -z "$kill_list" ] && break + count=$(($count-1)) + usleep 500000 + remaining= + for pid in $kill_list ; do + [ -d "/proc/$pid" ] && remaining+="$pid " + done + kill_list=$remaining + done + + # try to finish the job: + if [ -n "$kill_list" ] ; then + kill -9 $kill_list + usleep 500000 + # last check + remaining= + for pid in $kill_list ; do + [ -d "/proc/$pid" ] && remaining+="$pid " + done + kill_list=$remaining + fi + STRING=$"Killing processes with open filedescriptors on the unmounted disk:" + [ -z "$kill_list" ] && success "$STRING" || failure "$STRING" + echo +} + # Similar to __umount loop above, specialized for loopback devices __umount_loopback_loop() { local remaining devremaining sig= -- cgit v1.2.1