#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only
# Copyright 2020 Google LLC

# Require f2fs compression support on the scratch filesystem.
# Optionally, check for support for a specific compression algorithm.
_require_scratch_f2fs_compression()
{
	local algorithm=$1

	_require_scratch

	if [ ! -e /sys/fs/f2fs/features/compression ]; then
		_notrun "Kernel doesn't support f2fs compression"
	fi
	# Note: '-O compression' is only accepted when used in
	# combination with extra_attr.
	if ! _scratch_mkfs -O compression,extra_attr >> $seqres.full; then
		_notrun "f2fs-tools doesn't support compression"
	fi
	if [ -n "$algorithm" ]; then
		if ! _scratch_mount "-o compress_algorithm=$algorithm"; then
			_notrun "Kernel doesn't support $algorithm compression algorithm for f2fs"
		fi
		_scratch_unmount
	fi
}

_check_f2fs_filesystem()
{
    local device=$1

    # If type is set, we're mounted
    local type=`_fs_type $device`
    local ok=1

    if [ "$type" = "f2fs" ]
    then
        # mounted ...
        local mountpoint=`_umount_or_remount_ro $device`
    fi

    $F2FS_FSCK_PROG --dry-run $device >$tmp.fsck.f2fs 2>&1
    if [ $? -ne 0 ];then
        _log_err "_check_f2fs_filesystem: filesystem on $device is inconsistent"
        echo "*** fsck.f2fs output ***" >>$seqres.full
        cat $tmp.fsck.f2fs              >>$seqres.full
        echo "*** end fsck.f2fs output" >>$seqres.full

    ok=0
    fi
    rm -f $tmp.fsck.f2fs

    if [ $ok -eq 0 ]
    then
        echo "*** mount output ***"		>>$seqres.full
        _mount					>>$seqres.full
        echo "*** end mount output"		>>$seqres.full
    elif [ "$type" = "f2fs" ]
    then
	# was mounted ...
	_mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
	ok=$?
    fi

    if [ $ok -eq 0 ]; then
	return 1
    fi

    return 0
}

# check that inject.f2fs supports to inject specific field in specific meta area
_require_inject_f2fs_command()
{
	_require_command "$F2FS_INJECT_PROG" inject.f2fs

	if [ $# -ne 2 ]; then
		echo "Usage: _require_inject_f2fs_command metaarea member" 1>&2
		_exit 1
	fi
	metaarea=$1
	member=$2

	case $metaarea in
	sb|cp|nat|sit)
		val=0
		;;
	ssa|node|dent)
		;;
	*)
		_notrun "unsupport metaarea: $metaarea"
		;;
	esac

	$F2FS_INJECT_PROG "--$metaarea" "$val" "-h" | grep "$member:" > /dev/null || \
		_notrun "--$metaarea --mb $member support is missing"
}
