#!/bin/bash
#
# this file contains common code for the btrfs maintenance scripts
#

# function: expand_auto_mountpoint
# parameter: path list from config variable or 'auto'
#
# if the parameter is 'auto', this function prints path list of all btrfs
# mountpoints, otherwise prints the parameter unchanged
expand_auto_mountpoint() {
	local MNTLIST="$1"

	if [ "$MNTLIST" = "auto" ]; then
		local BTRFS_DEVICES
		local DEVICE
		local MNT

		# find all mounted btrfs filesystems, print their device nodes, sort them
		# and remove identical entries
		BTRFS_DEVICES=$(findmnt --types btrfs --output "SOURCE" --nofsroot --noheading | sort | uniq)
		# find one (and only one) corresponding mountpoint for each btrfs device node
		MNTLIST=""
		for DEVICE in $BTRFS_DEVICES; do
			MNT=$(findmnt --types btrfs --first-only --noheadings --output "TARGET" --source "$DEVICE")
			if [ -n "$MNTLIST" ]; then
				MNTLIST="$MNTLIST:$MNT"
			else
				MNTLIST="$MNT"
			fi
		done
	fi
	echo -n "$MNTLIST"
}

# function: detect_mixed_bg
# parameter: path to a mounted filesystem
#
# check if the filesystem contains mixed block groups,
detect_mixed_bg() {
	# simple test is to read 'btrfs fi df',
	# (we could look for /sys/sfs/btrfs/UUID/allocation/mixed if we know
	# the UUID)

	btrfs filesystem df "$1" | grep -q "Data+Metadata"
}

# function: check_scrub_running
# parameter: path to a mounted filesystem
#
# check if scrub is in progress on a given filesystem, return 0 if it is so
check_scrub_running() {
	btrfs scrub status "$1" | grep -q "scrub.*running for"
}

# function: check_balance_running
# parameter: path to a mounted filesystem
#
# check if balance is in progress on a given filesystem, return 0 if it is so
check_balance_running() {
	# 0: not in progress
	# 1: in progress
	# 2: other error (EPERM)
	if btrfs balance status "$1" >& /dev/null; then
		return 1
	fi
	return 0
}

# function: is_btrfs
# parameter: path to a mounted filesystem
#
# check if filesystem is a btrfs
is_btrfs() {
	local FS=$(stat -f --format=%T "$1")

	[ "$FS" = "btrfs" ] && return 0
	return 1
}

# function: is_raid56
# parameter: path to a mounted filesystem
#
# check if filesystem is on a RAID-5 or RAID-6
is_raid56() {
	btrfs filesystem usage "$1" | grep Data,RAID5 && return 0
	btrfs filesystem usage "$1" | grep Data,RAID6 && return 0
	return 1
}

# function: btrfs_fsid
# parameter: path to a mounted filesystem
#
# return filesystem UUID on a given path
btrfs_fsid() {
	btrfs filesystem show "$1" | sed -n -e '/uuid:/ {s/^.*uuid: //;p }'
}

# function: run_task
# parameter: command to run, expecting the mountpoint to be the last argument
#
# run the given command with concurrency protection unless allowed by the
# config, use for tasks that should not run at the same time due to heavy IO
run_task() {
	local MNT="${@:$#}"
	local UUID=$(btrfs_fsid "$MNT")
	local verbose

	if test "$BTRFS_ALLOW_CONCURRENCY" = "true"; then
		"$@"
	else
		# Flock older than 2.27 does not support --verbose option, check
		# if it's available as we'd like to log the information
		if /usr/bin/flock --help 2>&1 | grep -q -- --verbose; then
			verbose="--verbose"
		fi

		/usr/bin/flock $verbose /run/btrfs-maintenance-running."$UUID" "$@"
	fi
}
