- if ! btrfs dev stats -c $mnt >$tmp; then
- if diff -q $mnt/btrfs-dev-stats $tmp; then
- diff -u $mnt/btrfs-dev-stats $tmp | mail -s "$HOSTNAME: error: btrfs dev stats -c $mnt" root@localhost
- cat $tmp >$mnt/btrfs-dev-stats
+ #### begin look for diff in stats, eg: increasing error count ####
+ if $stats; then
+ tmp=$(mktemp)
+ # ${mnt%/} so that if mnt is / we avoid making a buggy looking path
+ stats_path=${mnt%/}/btrfs-dev-stats
+ if [[ ! -e $stats_path ]]; then
+ btrfs dev stats -c $mnt >$stats_path ||: # populate initial reading
+ elif ! btrfs dev stats -c $mnt >$tmp; then
+ if ! diff -q $stats_path $tmp; then
+ mv $stats_path $stats_path.1
+ cat $tmp >$stats_path
+ diff=$(diff -u $stats_path $tmp 2>&1 ||:)
+ printf "diff of: btrfs dev stats -c %s\n%s\n" "$mnt" "$diff"
+ exim -odf -i root <<EOF
+From: root@$(hostname -f)
+To: root@$(hostname -f)
+Subject: btrfsmaint: device stats changed for $mnt
+
+diff of: btrfs dev stats -c $mnt
+$diff
+EOF
+ fi