fix the case of an outdated bind mount
authorIan Kelling <ian@iankelling.org>
Wed, 2 Dec 2020 01:14:48 +0000 (20:14 -0500)
committerIan Kelling <ian@iankelling.org>
Wed, 2 Dec 2020 01:14:48 +0000 (20:14 -0500)
mount-latest-subvol

index 77ade005af8620cb7e6a5999aed80de0affeed5c..048c9522e5f73e7d68605d2fca314de33d21924e 100644 (file)
@@ -103,6 +103,26 @@ kill-dir() {
   done
   return 1
 }
+umount-kill() {
+  dir=$1
+  if mountpoint -q $dir; then
+    if m umount -R $dir; then
+      unmounted+=($dir)
+    else
+      if ! kill-dir TERM TERM TERM INT INT HUP HUP TERM TERM TERM INT INT HUP HUP; then
+        if $force; then  kill-dir KILL; fi
+      fi
+
+      if m umount -R $dir; then
+        unmounted+=($dir)
+      else
+        echo "$0: failed to umount $dir"
+        umount_ret=false
+        ret=1
+      fi
+    fi
+  fi
+}
 
 ##### begin command line parsing ########
 
@@ -211,7 +231,7 @@ for vol in q a o i; do
 
   ##### begin building up list of bind mounts  ######
   binds=() # list of bind mounts
-  roots=($d) # list of bind mounts, plus the original mount
+  roots=($d)
   while true; do
     new_roots=()
     for r in ${roots[@]}; do
@@ -222,6 +242,7 @@ for vol in q a o i; do
     done
     (( ${#new_roots} )) || break
     binds+=(${new_roots[@]})
+    # roots is used to recursively find binds of binds if they exist.
     roots=( ${new_roots[@]} )
   done
   ##### end building up list of bind mounts  ######
@@ -232,7 +253,14 @@ for vol in q a o i; do
   # populated by check-subvol-stale if stale
   if ! fresh_snap=$(cat /nocow/btrfs-stale/$vol 2>/dev/null); then
     mnt $d
+    did=$(stat -c%d $d)
     for b in ${binds[@]}; do
+      if mountpoint -q $b; then
+        bid=$(stat -c%d $b)
+        if [[ $did != $bid ]]; then
+          umount-kill $b
+        fi
+      fi
       mnt $b
     done
     continue
@@ -245,24 +273,7 @@ for vol in q a o i; do
   umount_ret=true
   unmounted=()
   for dir in $(echo $d ${binds[*]}\ |tac -s\ ); do
-    if mountpoint -q $dir; then
-      if m umount -R $dir; then
-        unmounted+=($dir)
-      else
-        if ! kill-dir TERM TERM TERM INT INT HUP HUP TERM TERM TERM INT INT HUP HUP; then
-          if $force; then  kill-dir KILL; fi
-        fi
-
-        if m umount -R $dir; then
-          unmounted+=($dir)
-        else
-          echo "$0: failed to umount $dir"
-          umount_ret=false
-          ret=1
-          continue
-        fi
-      fi
-    fi
+    umount-kill $dir
   done
 
   # if we unmounted some but not all, restore them and move on