# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2023 Veeam Software Group GmbH

#
# Configuration relevant only for the bdevfilter
#
ccflags-y += $(shell 							\
	grep -qw "struct ftrace_regs" $(srctree)/include/linux/ftrace.h	&&	\
		echo -D HAVE_FTRACE_REGS)
ccflags-y += $(shell 							\
	grep -qw "ftrace_regs_set_instruction_pointer" $(srctree)/include/linux/ftrace.h && \
		echo -D HAVE_FTRACE_REGS_SET_INSTRUCTION_POINTER)
ccflags-y += $(shell 							\
	grep -qw "void submit_bio_noacct" $(srctree)/include/linux/blkdev.h &&	\
		echo -D HAVE_VOID_SUBMIT_BIO_NOACCT)
ccflags-y += $(shell 							\
	grep -qw "TSK_TRACE_FL_TRACE_BIT" $(srctree)/include/linux/ftrace.h &&	\
		echo -D HAVE_NOT_FTRACE_FREE_FILTER)
#
# [6.6 : latest]
# include/linux/blkdev.h
# void bdev_mark_dead(struct block_device *bdev, bool surprise);
#
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep -qw "void bdev_mark_dead" $(srctree)/include/linux/blkdev.h &&	\
		echo -D HAVE_BDEV_MARK_DEAD)
#
# [5.18 : 6.5]
# include/linux/blkdev.h
# void del_gendisk(struct gendisk *gp);
#
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep -qw "void del_gendisk" $(srctree)/include/linux/blkdev.h && \
		echo -D HAVE_DEL_GENDISK)
#
# [5.10 : 5.17]
# include/linux/genhd.h
# void del_gendisk(struct gendisk *gp);
#
ccflags-y += $(shell test -f $(srctree)/include/linux/genhd.h &&		\
	grep -qw "void del_gendisk" $(srctree)/include/linux/genhd.h && \
		echo -D HAVE_DEL_GENDISK)

#
# [5.10 : 5.12]
# include/linux/genhd.h
# struct block_device *disk_part_iter_next(struct disk_part_iter *piter);
#
ccflags-y += $(shell test -f $(srctree)/include/linux/genhd.h &&		\
	grep -qw "struct disk_part_iter" $(srctree)/include/linux/genhd.h &&	\
		echo -D HAVE_DISK_PART_ITER)

#
# Configuration relevant for the blksnap and bdevfilter
#
ccflags-y += $(shell 							\
	grep -qw "blk_qc_t submit_bio_noacct" $(srctree)/include/linux/blkdev.h	&& \
		echo -D HAVE_QC_SUBMIT_BIO_NOACCT)

ccflags-y += $(shell 							\
	grep -qw "struct super_block \*freeze_bdev"				\
		$(srctree)/include/linux/blkdev.h &&				\
		echo -D HAVE_SUPER_BLOCK_FREEZE)
ccflags-y += $(shell 							\
	grep -qw "int bdev_freeze"						\
		$(srctree)/include/linux/blkdev.h &&				\
		echo -D HAVE_BDEV_FREEZE)
ccflags-y += $(shell 							\
	grep -qw "\*bi_disk;" $(srctree)/include/linux/blk_types.h &&		\
		echo -D HAVE_BI_BDISK)
ccflags-y += $(shell test -f $(srctree)/include/linux/blk_types.h &&		\
	grep -qw "\*bd_queue;" $(srctree)/include/linux/blk_types.h &&		\
		echo -D HAVE_BD_QUEUE)
ccflags-y += $(shell test -f $(srctree)/include/linux/genhd.h &&		\
	grep -qw "sector_t bdev_nr_sectors" $(srctree)/include/linux/genhd.h &&	\
		echo -D HAVE_BDEV_NR_SECTORS)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep -qw "sector_t bdev_nr_sectors" $(srctree)/include/linux/blkdev.h && \
		echo -D HAVE_BDEV_NR_SECTORS)
ccflags-y += $(shell test -f $(srctree)/include/linux/genhd.h &&		\
	grep -qw "int add_disk" $(srctree)/include/linux/genhd.h &&		\
		echo -D HAVE_ADD_DISK_RESULT)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep -qw "int add_disk" $(srctree)/include/linux/blkdev.h &&		\
		echo -D HAVE_ADD_DISK_RESULT)
ccflags-y += $(shell test -f $(srctree)/include/linux/genhd.h &&		\
	grep -qw "int __must_check add_disk" $(srctree)/include/linux/genhd.h && \
		echo -D HAVE_ADD_DISK_RESULT)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep -qw "int __must_check add_disk" $(srctree)/include/linux/blkdev.h && \
		echo -D HAVE_ADD_DISK_RESULT)
ccflags-y += $(shell test -f $(srctree)/include/linux/genhd.h &&		\
	grep -qw "void blk_cleanup_disk" $(srctree)/include/linux/genhd.h &&	\
		echo -D HAVE_BLK_CLEANUP_DISK)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep -qw "void blk_cleanup_disk" $(srctree)/include/linux/blkdev.h &&	\
		echo -D HAVE_BLK_CLEANUP_DISK)
ccflags-y += $(shell test -f $(srctree)/include/linux/genhd.h &&		\
		echo -D HAVE_GENHD_H)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep -qw "struct blk_holder_ops" $(srctree)/include/linux/blkdev.h &&	\
		echo -D HAVE_BLK_HOLDER_OPS)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep -qw "struct bdev_handle" $(srctree)/include/linux/blkdev.h &&	\
		echo -D HAVE_BDEV_HANDLE)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep -qw "bdev_file_open_by_dev" $(srctree)/include/linux/blkdev.h &&	\
		echo -D HAVE_BDEV_FILE_OPEN)
ccflags-y += $(shell test -f $(srctree)/include/linux/bvec.h &&		\
	grep -qw "inline void bvec_set_page" $(srctree)/include/linux/bvec.h &&	\
		echo -D HAVE_BVEC_SET_PAGE)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h  &&		\
	grep -qw "define BLK_OPEN_READ" $(srctree)/include/linux/blkdev.h &&	\
		echo -D HAVE_BLK_OPEN_MODE)
ccflags-y += $(shell test -f $(srctree)/include/linux/genhd.h  &&		\
	grep -qw "define blk_alloc_disk" $(srctree)/include/linux/genhd.h &&	\
		echo -D HAVE_BLK_ALLOC_DISK)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h  &&		\
	grep -qw "define blk_alloc_disk" $(srctree)/include/linux/blkdev.h &&	\
		echo -D HAVE_BLK_ALLOC_DISK)
ccflags-y += $(shell test -f $(srctree)/include/linux/genhd.h  &&		\
	grep -qw "struct mutex open_mutex" $(srctree)/include/linux/genhd.h &&	\
		echo -D HAVE_GENDISK_OPEN_MUTEX)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h  &&		\
	grep -qw "struct mutex open_mutex" $(srctree)/include/linux/blkdev.h &&	\
		echo -D HAVE_GENDISK_OPEN_MUTEX)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h  &&		\
	grep -qw "static inline bool disk_live" $(srctree)/include/linux/blkdev.h && \
		echo -D HAVE_DISK_LIVE)
ccflags-y += $(shell test -f $(srctree)/include/linux/highmem.h  &&		\
	grep -qw "static inline void memcpy_page" $(srctree)/include/linux/highmem.h && \
		echo -D HAVE_MEMCPY_PAGE)
ccflags-y += $(shell test -f $(srctree)/include/linux/bio.h  &&		\
	grep -qw "struct bio \*bio_alloc_clone" $(srctree)/include/linux/bio.h && \
		echo -D HAVE_BIO_ALLOC_CLONE)
ccflags-y += $(shell test -f $(srctree)/include/linux/bio.h  &&		\
	grep -w "bio_alloc_bioset" $(srctree)/include/linux/bio.h | grep -qw "struct block_device"&& \
		echo -D HAVE_BIO_ALLOC_BIOSET_BDEV)
ccflags-y += $(shell test -f $(srctree)/include/linux/fs.h  &&		\
	grep -w "\*ki_complete" $(srctree)/include/linux/fs.h | grep -qw "long ret2" && \
		echo -D HAVE_KI_COMPLETE_RET2)
ccflags-y += $(shell test -f $(srctree)/include/linux/blk-crypto-profile.h  &&		\
	grep -qw "struct blk_crypto_profile" $(srctree)/include/linux/blk-crypto-profile.h &&	\
		echo -D HAVE_BLK_CRYPTO_PROFILE)
ccflags-y += $(shell test -f $(srctree)/include/linux/blkdev.h &&		\
	grep "__blk_alloc_disk" $(srctree)/include/linux/blkdev.h |		\
	grep -qw "struct queue_limits" &&					\
		echo -D HAVE_BDEV_QUEUE_LIMITS)
ccflags-y += $(shell test -f $(srctree)/include/linux/fs.h  &&		\
	grep -qw "kiocb_start_write" $(srctree)/include/linux/fs.h &&		\
		echo -D HAVE_KIOCB_START_WRITE)
