xref: /freebsd/sys/contrib/openzfs/config/kernel.m4 (revision cf83038cec6874aafacf56a1cac531d079af016b)
1dnl #
2dnl # Default ZFS kernel configuration
3dnl #
4AC_DEFUN([ZFS_AC_CONFIG_KERNEL], [
5	AM_COND_IF([BUILD_LINUX], [
6		dnl # Setup the kernel build environment.
7		ZFS_AC_KERNEL
8		ZFS_AC_QAT
9
10		dnl # Sanity checks for module building and CONFIG_* defines
11		ZFS_AC_KERNEL_CONFIG_DEFINED
12		ZFS_AC_MODULE_SYMVERS
13
14		dnl # Sequential ZFS_LINUX_TRY_COMPILE tests
15		ZFS_AC_KERNEL_FPU_HEADER
16		ZFS_AC_KERNEL_OBJTOOL_HEADER
17		ZFS_AC_KERNEL_WAIT_QUEUE_ENTRY_T
18		ZFS_AC_KERNEL_MISC_MINOR
19		ZFS_AC_KERNEL_DECLARE_EVENT_CLASS
20
21		dnl # Parallel ZFS_LINUX_TEST_SRC / ZFS_LINUX_TEST_RESULT tests
22		ZFS_AC_KERNEL_TEST_SRC
23		ZFS_AC_KERNEL_TEST_RESULT
24
25		AS_IF([test "$LINUX_OBJ" != "$LINUX"], [
26			KERNEL_MAKE="$KERNEL_MAKE O=$LINUX_OBJ"
27		])
28
29		AC_SUBST(KERNEL_MAKE)
30	])
31])
32
33dnl #
34dnl # Generate and compile all of the kernel API test cases to determine
35dnl # which interfaces are available.  By invoking the kernel build system
36dnl # only once the compilation can be done in parallel significantly
37dnl # speeding up the process.
38dnl #
39AC_DEFUN([ZFS_AC_KERNEL_TEST_SRC], [
40	ZFS_AC_KERNEL_SRC_OBJTOOL
41	ZFS_AC_KERNEL_SRC_GLOBAL_PAGE_STATE
42	ZFS_AC_KERNEL_SRC_ACCESS_OK_TYPE
43	ZFS_AC_KERNEL_SRC_PDE_DATA
44	ZFS_AC_KERNEL_SRC_FALLOCATE
45	ZFS_AC_KERNEL_SRC_2ARGS_ZLIB_DEFLATE_WORKSPACESIZE
46	ZFS_AC_KERNEL_SRC_RWSEM
47	ZFS_AC_KERNEL_SRC_SCHED
48	ZFS_AC_KERNEL_SRC_USLEEP_RANGE
49	ZFS_AC_KERNEL_SRC_KMEM_CACHE
50	ZFS_AC_KERNEL_SRC_KVMALLOC
51	ZFS_AC_KERNEL_SRC_VMALLOC_PAGE_KERNEL
52	ZFS_AC_KERNEL_SRC_WAIT
53	ZFS_AC_KERNEL_SRC_INODE_TIMES
54	ZFS_AC_KERNEL_SRC_INODE_LOCK
55	ZFS_AC_KERNEL_SRC_GROUP_INFO_GID
56	ZFS_AC_KERNEL_SRC_RW
57	ZFS_AC_KERNEL_SRC_TIMER_SETUP
58	ZFS_AC_KERNEL_SRC_SUPER_USER_NS
59	ZFS_AC_KERNEL_SRC_PROC_OPERATIONS
60	ZFS_AC_KERNEL_SRC_BLOCK_DEVICE_OPERATIONS
61	ZFS_AC_KERNEL_SRC_BIO
62	ZFS_AC_KERNEL_SRC_BLKDEV
63	ZFS_AC_KERNEL_SRC_BLK_QUEUE
64	ZFS_AC_KERNEL_SRC_GENHD_FLAGS
65	ZFS_AC_KERNEL_SRC_REVALIDATE_DISK
66	ZFS_AC_KERNEL_SRC_GET_DISK_RO
67	ZFS_AC_KERNEL_SRC_GENERIC_READLINK_GLOBAL
68	ZFS_AC_KERNEL_SRC_DISCARD_GRANULARITY
69	ZFS_AC_KERNEL_SRC_INODE_OWNER_OR_CAPABLE
70	ZFS_AC_KERNEL_SRC_XATTR
71	ZFS_AC_KERNEL_SRC_ACL
72	ZFS_AC_KERNEL_SRC_INODE_GETATTR
73	ZFS_AC_KERNEL_SRC_INODE_SET_FLAGS
74	ZFS_AC_KERNEL_SRC_INODE_SET_IVERSION
75	ZFS_AC_KERNEL_SRC_SHOW_OPTIONS
76	ZFS_AC_KERNEL_SRC_FILE_INODE
77	ZFS_AC_KERNEL_SRC_FILE_DENTRY
78	ZFS_AC_KERNEL_SRC_FSYNC
79	ZFS_AC_KERNEL_SRC_AIO_FSYNC
80	ZFS_AC_KERNEL_SRC_EVICT_INODE
81	ZFS_AC_KERNEL_SRC_DIRTY_INODE
82	ZFS_AC_KERNEL_SRC_SHRINKER
83	ZFS_AC_KERNEL_SRC_MKDIR
84	ZFS_AC_KERNEL_SRC_LOOKUP_FLAGS
85	ZFS_AC_KERNEL_SRC_CREATE
86	ZFS_AC_KERNEL_SRC_PERMISSION
87	ZFS_AC_KERNEL_SRC_GET_LINK
88	ZFS_AC_KERNEL_SRC_PUT_LINK
89	ZFS_AC_KERNEL_SRC_TMPFILE
90	ZFS_AC_KERNEL_SRC_AUTOMOUNT
91	ZFS_AC_KERNEL_SRC_ENCODE_FH_WITH_INODE
92	ZFS_AC_KERNEL_SRC_COMMIT_METADATA
93	ZFS_AC_KERNEL_SRC_CLEAR_INODE
94	ZFS_AC_KERNEL_SRC_SETATTR_PREPARE
95	ZFS_AC_KERNEL_SRC_INSERT_INODE_LOCKED
96	ZFS_AC_KERNEL_SRC_DENTRY
97	ZFS_AC_KERNEL_SRC_TRUNCATE_SETSIZE
98	ZFS_AC_KERNEL_SRC_SECURITY_INODE
99	ZFS_AC_KERNEL_SRC_FST_MOUNT
100	ZFS_AC_KERNEL_SRC_BDI
101	ZFS_AC_KERNEL_SRC_SET_NLINK
102	ZFS_AC_KERNEL_SRC_SGET
103	ZFS_AC_KERNEL_SRC_LSEEK_EXECUTE
104	ZFS_AC_KERNEL_SRC_VFS_FILEMAP_DIRTY_FOLIO
105	ZFS_AC_KERNEL_SRC_VFS_READ_FOLIO
106	ZFS_AC_KERNEL_SRC_VFS_GETATTR
107	ZFS_AC_KERNEL_SRC_VFS_FSYNC_2ARGS
108	ZFS_AC_KERNEL_SRC_VFS_ITERATE
109	ZFS_AC_KERNEL_SRC_VFS_DIRECT_IO
110	ZFS_AC_KERNEL_SRC_VFS_READPAGES
111	ZFS_AC_KERNEL_SRC_VFS_SET_PAGE_DIRTY_NOBUFFERS
112	ZFS_AC_KERNEL_SRC_VFS_RW_ITERATE
113	ZFS_AC_KERNEL_SRC_VFS_GENERIC_WRITE_CHECKS
114	ZFS_AC_KERNEL_SRC_VFS_IOV_ITER
115	ZFS_AC_KERNEL_SRC_KMAP_ATOMIC_ARGS
116	ZFS_AC_KERNEL_SRC_FOLLOW_DOWN_ONE
117	ZFS_AC_KERNEL_SRC_MAKE_REQUEST_FN
118	ZFS_AC_KERNEL_SRC_GENERIC_IO_ACCT
119	ZFS_AC_KERNEL_SRC_FPU
120	ZFS_AC_KERNEL_SRC_FMODE_T
121	ZFS_AC_KERNEL_SRC_KUIDGID_T
122	ZFS_AC_KERNEL_SRC_KUID_HELPERS
123	ZFS_AC_KERNEL_SRC_MODULE_PARAM_CALL_CONST
124	ZFS_AC_KERNEL_SRC_RENAME
125	ZFS_AC_KERNEL_SRC_CURRENT_TIME
126	ZFS_AC_KERNEL_SRC_USERNS_CAPABILITIES
127	ZFS_AC_KERNEL_SRC_IN_COMPAT_SYSCALL
128	ZFS_AC_KERNEL_SRC_KTIME
129	ZFS_AC_KERNEL_SRC_TOTALRAM_PAGES_FUNC
130	ZFS_AC_KERNEL_SRC_TOTALHIGH_PAGES
131	ZFS_AC_KERNEL_SRC_KSTRTOUL
132	ZFS_AC_KERNEL_SRC_PERCPU
133	ZFS_AC_KERNEL_SRC_CPU_HOTPLUG
134	ZFS_AC_KERNEL_SRC_GENERIC_FILLATTR_USERNS
135	ZFS_AC_KERNEL_SRC_MKNOD
136	ZFS_AC_KERNEL_SRC_SYMLINK
137	ZFS_AC_KERNEL_SRC_BIO_MAX_SEGS
138	ZFS_AC_KERNEL_SRC_SIGNAL_STOP
139	ZFS_AC_KERNEL_SRC_SIGINFO
140	ZFS_AC_KERNEL_SRC_SYSFS
141	ZFS_AC_KERNEL_SRC_SET_SPECIAL_STATE
142	ZFS_AC_KERNEL_SRC_STANDALONE_LINUX_STDARG
143	ZFS_AC_KERNEL_SRC_PAGEMAP_FOLIO_WAIT_BIT
144	ZFS_AC_KERNEL_SRC_ADD_DISK
145	ZFS_AC_KERNEL_SRC_KTHREAD
146	ZFS_AC_KERNEL_SRC_ZERO_PAGE
147	ZFS_AC_KERNEL_SRC___COPY_FROM_USER_INATOMIC
148
149	AC_MSG_CHECKING([for available kernel interfaces])
150	ZFS_LINUX_TEST_COMPILE_ALL([kabi])
151	AC_MSG_RESULT([done])
152])
153
154dnl #
155dnl # Check results of kernel interface tests.
156dnl #
157AC_DEFUN([ZFS_AC_KERNEL_TEST_RESULT], [
158	ZFS_AC_KERNEL_ACCESS_OK_TYPE
159	ZFS_AC_KERNEL_GLOBAL_PAGE_STATE
160	ZFS_AC_KERNEL_OBJTOOL
161	ZFS_AC_KERNEL_PDE_DATA
162	ZFS_AC_KERNEL_FALLOCATE
163	ZFS_AC_KERNEL_2ARGS_ZLIB_DEFLATE_WORKSPACESIZE
164	ZFS_AC_KERNEL_RWSEM
165	ZFS_AC_KERNEL_SCHED
166	ZFS_AC_KERNEL_USLEEP_RANGE
167	ZFS_AC_KERNEL_KMEM_CACHE
168	ZFS_AC_KERNEL_KVMALLOC
169	ZFS_AC_KERNEL_VMALLOC_PAGE_KERNEL
170	ZFS_AC_KERNEL_WAIT
171	ZFS_AC_KERNEL_INODE_TIMES
172	ZFS_AC_KERNEL_INODE_LOCK
173	ZFS_AC_KERNEL_GROUP_INFO_GID
174	ZFS_AC_KERNEL_RW
175	ZFS_AC_KERNEL_TIMER_SETUP
176	ZFS_AC_KERNEL_SUPER_USER_NS
177	ZFS_AC_KERNEL_PROC_OPERATIONS
178	ZFS_AC_KERNEL_BLOCK_DEVICE_OPERATIONS
179	ZFS_AC_KERNEL_BIO
180	ZFS_AC_KERNEL_BLKDEV
181	ZFS_AC_KERNEL_BLK_QUEUE
182	ZFS_AC_KERNEL_GENHD_FLAGS
183	ZFS_AC_KERNEL_REVALIDATE_DISK
184	ZFS_AC_KERNEL_GET_DISK_RO
185	ZFS_AC_KERNEL_GENERIC_READLINK_GLOBAL
186	ZFS_AC_KERNEL_DISCARD_GRANULARITY
187	ZFS_AC_KERNEL_INODE_OWNER_OR_CAPABLE
188	ZFS_AC_KERNEL_XATTR
189	ZFS_AC_KERNEL_ACL
190	ZFS_AC_KERNEL_INODE_GETATTR
191	ZFS_AC_KERNEL_INODE_SET_FLAGS
192	ZFS_AC_KERNEL_INODE_SET_IVERSION
193	ZFS_AC_KERNEL_SHOW_OPTIONS
194	ZFS_AC_KERNEL_FILE_INODE
195	ZFS_AC_KERNEL_FILE_DENTRY
196	ZFS_AC_KERNEL_FSYNC
197	ZFS_AC_KERNEL_AIO_FSYNC
198	ZFS_AC_KERNEL_EVICT_INODE
199	ZFS_AC_KERNEL_DIRTY_INODE
200	ZFS_AC_KERNEL_SHRINKER
201	ZFS_AC_KERNEL_MKDIR
202	ZFS_AC_KERNEL_LOOKUP_FLAGS
203	ZFS_AC_KERNEL_CREATE
204	ZFS_AC_KERNEL_PERMISSION
205	ZFS_AC_KERNEL_GET_LINK
206	ZFS_AC_KERNEL_PUT_LINK
207	ZFS_AC_KERNEL_TMPFILE
208	ZFS_AC_KERNEL_AUTOMOUNT
209	ZFS_AC_KERNEL_ENCODE_FH_WITH_INODE
210	ZFS_AC_KERNEL_COMMIT_METADATA
211	ZFS_AC_KERNEL_CLEAR_INODE
212	ZFS_AC_KERNEL_SETATTR_PREPARE
213	ZFS_AC_KERNEL_INSERT_INODE_LOCKED
214	ZFS_AC_KERNEL_DENTRY
215	ZFS_AC_KERNEL_TRUNCATE_SETSIZE
216	ZFS_AC_KERNEL_SECURITY_INODE
217	ZFS_AC_KERNEL_FST_MOUNT
218	ZFS_AC_KERNEL_BDI
219	ZFS_AC_KERNEL_SET_NLINK
220	ZFS_AC_KERNEL_SGET
221	ZFS_AC_KERNEL_LSEEK_EXECUTE
222	ZFS_AC_KERNEL_VFS_FILEMAP_DIRTY_FOLIO
223	ZFS_AC_KERNEL_VFS_READ_FOLIO
224	ZFS_AC_KERNEL_VFS_GETATTR
225	ZFS_AC_KERNEL_VFS_FSYNC_2ARGS
226	ZFS_AC_KERNEL_VFS_ITERATE
227	ZFS_AC_KERNEL_VFS_DIRECT_IO
228	ZFS_AC_KERNEL_VFS_READPAGES
229	ZFS_AC_KERNEL_VFS_SET_PAGE_DIRTY_NOBUFFERS
230	ZFS_AC_KERNEL_VFS_RW_ITERATE
231	ZFS_AC_KERNEL_VFS_GENERIC_WRITE_CHECKS
232	ZFS_AC_KERNEL_VFS_IOV_ITER
233	ZFS_AC_KERNEL_KMAP_ATOMIC_ARGS
234	ZFS_AC_KERNEL_FOLLOW_DOWN_ONE
235	ZFS_AC_KERNEL_MAKE_REQUEST_FN
236	ZFS_AC_KERNEL_GENERIC_IO_ACCT
237	ZFS_AC_KERNEL_FPU
238	ZFS_AC_KERNEL_FMODE_T
239	ZFS_AC_KERNEL_KUIDGID_T
240	ZFS_AC_KERNEL_KUID_HELPERS
241	ZFS_AC_KERNEL_MODULE_PARAM_CALL_CONST
242	ZFS_AC_KERNEL_RENAME
243	ZFS_AC_KERNEL_CURRENT_TIME
244	ZFS_AC_KERNEL_USERNS_CAPABILITIES
245	ZFS_AC_KERNEL_IN_COMPAT_SYSCALL
246	ZFS_AC_KERNEL_KTIME
247	ZFS_AC_KERNEL_TOTALRAM_PAGES_FUNC
248	ZFS_AC_KERNEL_TOTALHIGH_PAGES
249	ZFS_AC_KERNEL_KSTRTOUL
250	ZFS_AC_KERNEL_PERCPU
251	ZFS_AC_KERNEL_CPU_HOTPLUG
252	ZFS_AC_KERNEL_GENERIC_FILLATTR_USERNS
253	ZFS_AC_KERNEL_MKNOD
254	ZFS_AC_KERNEL_SYMLINK
255	ZFS_AC_KERNEL_BIO_MAX_SEGS
256	ZFS_AC_KERNEL_SIGNAL_STOP
257	ZFS_AC_KERNEL_SIGINFO
258	ZFS_AC_KERNEL_SYSFS
259	ZFS_AC_KERNEL_SET_SPECIAL_STATE
260	ZFS_AC_KERNEL_STANDALONE_LINUX_STDARG
261	ZFS_AC_KERNEL_PAGEMAP_FOLIO_WAIT_BIT
262	ZFS_AC_KERNEL_ADD_DISK
263	ZFS_AC_KERNEL_KTHREAD
264	ZFS_AC_KERNEL_ZERO_PAGE
265	ZFS_AC_KERNEL___COPY_FROM_USER_INATOMIC
266])
267
268dnl #
269dnl # Detect name used for Module.symvers file in kernel
270dnl #
271AC_DEFUN([ZFS_AC_MODULE_SYMVERS], [
272	modpost=$LINUX/scripts/Makefile.modpost
273	AC_MSG_CHECKING([kernel file name for module symbols])
274	AS_IF([test "x$enable_linux_builtin" != xyes -a -f "$modpost"], [
275		AS_IF([grep -q Modules.symvers $modpost], [
276			LINUX_SYMBOLS=Modules.symvers
277		], [
278			LINUX_SYMBOLS=Module.symvers
279		])
280
281		AS_IF([test ! -f "$LINUX_OBJ/$LINUX_SYMBOLS"], [
282			AC_MSG_ERROR([
283	*** Please make sure the kernel devel package for your distribution
284	*** is installed.  If you are building with a custom kernel, make sure
285	*** the kernel is configured, built, and the '--with-linux=PATH'
286	*** configure option refers to the location of the kernel source.
287			])
288		])
289	], [
290		LINUX_SYMBOLS=NONE
291	])
292	AC_MSG_RESULT($LINUX_SYMBOLS)
293	AC_SUBST(LINUX_SYMBOLS)
294])
295
296dnl #
297dnl # Detect the kernel to be built against
298dnl #
299dnl # Most modern Linux distributions have separate locations for bare
300dnl # source (source) and prebuilt (build) files. Additionally, there are
301dnl # `source` and `build` symlinks in `/lib/modules/$(KERNEL_VERSION)`
302dnl # pointing to them. The directory search order is now:
303dnl #
304dnl # - `configure` command line values if both `--with-linux` and
305dnl #   `--with-linux-obj` were defined
306dnl #
307dnl # - If only `--with-linux` was defined, `--with-linux-obj` is assumed
308dnl #   to have the same value as `--with-linux`
309dnl #
310dnl # - If neither `--with-linux` nor `--with-linux-obj` were defined
311dnl #   autodetection is used:
312dnl #
313dnl #   - `/lib/modules/$(uname -r)/{source,build}` respectively, if exist.
314dnl #
315dnl #   - If only `/lib/modules/$(uname -r)/build` exists, it is assumed
316dnl #     to be both source and build directory.
317dnl #
318dnl #   - The first directory in `/lib/modules` with the highest version
319dnl #     number according to `sort -V` which contains both `source` and
320dnl #     `build` symlinks/directories. If module directory contains only
321dnl #     `build` component, it is assumed to be both source and build
322dnl #     directory.
323dnl #
324dnl #   - Last resort: the first directory matching `/usr/src/kernels/*`
325dnl #     and `/usr/src/linux-*` with the highest version number according
326dnl #     to `sort -V` is assumed to be both source and build directory.
327dnl #
328AC_DEFUN([ZFS_AC_KERNEL], [
329	AC_ARG_WITH([linux],
330		AS_HELP_STRING([--with-linux=PATH],
331		[Path to kernel source]),
332		[kernelsrc="$withval"])
333
334	AC_ARG_WITH(linux-obj,
335		AS_HELP_STRING([--with-linux-obj=PATH],
336		[Path to kernel build objects]),
337		[kernelbuild="$withval"])
338
339	AC_MSG_CHECKING([kernel source and build directories])
340	AS_IF([test -n "$kernelsrc" && test -z "$kernelbuild"], [
341		kernelbuild="$kernelsrc"
342	], [test -z "$kernelsrc"], [
343		AS_IF([test -e "/lib/modules/$(uname -r)/source" && \
344		       test -e "/lib/modules/$(uname -r)/build"], [
345			src="/lib/modules/$(uname -r)/source"
346			build="/lib/modules/$(uname -r)/build"
347		], [test -e "/lib/modules/$(uname -r)/build"], [
348			build="/lib/modules/$(uname -r)/build"
349			src="$build"
350		], [
351			src=
352
353			for d in $(ls -1d /lib/modules/* 2>/dev/null | sort -Vr); do
354				if test -e "$d/source" && test -e "$d/build"; then
355					src="$d/source"
356					build="$d/build"
357					break
358				fi
359
360				if test -e "$d/build"; then
361					src="$d/build"
362					build="$d/build"
363					break
364				fi
365			done
366
367			# the least reliable method
368			if test -z "$src"; then
369				src=$(ls -1d /usr/src/kernels/* /usr/src/linux-* \
370				      2>/dev/null | grep -v obj | sort -Vr | head -1)
371				build="$src"
372			fi
373		])
374
375		AS_IF([test -n "$src" && test -e "$src"], [
376			kernelsrc=$(readlink -e "$src")
377		], [
378			kernelsrc="[Not found]"
379		])
380		AS_IF([test -n "$build" && test -e "$build"], [
381			kernelbuild=$(readlink -e "$build")
382		], [
383			kernelbuild="[Not found]"
384		])
385	], [
386		AS_IF([test "$kernelsrc" = "NONE"], [
387			kernsrcver=NONE
388		])
389		withlinux=yes
390	])
391
392	AC_MSG_RESULT([done])
393	AC_MSG_CHECKING([kernel source directory])
394	AC_MSG_RESULT([$kernelsrc])
395	AC_MSG_CHECKING([kernel build directory])
396	AC_MSG_RESULT([$kernelbuild])
397	AS_IF([test ! -d "$kernelsrc" || test ! -d "$kernelbuild"], [
398		AC_MSG_ERROR([
399	*** Please make sure the kernel devel package for your distribution
400	*** is installed and then try again.  If that fails, you can specify the
401	*** location of the kernel source and build with the '--with-linux=PATH' and
402	*** '--with-linux-obj=PATH' options respectively.])
403	])
404
405	AC_MSG_CHECKING([kernel source version])
406	utsrelease1=$kernelbuild/include/linux/version.h
407	utsrelease2=$kernelbuild/include/linux/utsrelease.h
408	utsrelease3=$kernelbuild/include/generated/utsrelease.h
409	AS_IF([test -r $utsrelease1 && grep -qF UTS_RELEASE $utsrelease1], [
410		utsrelease=$utsrelease1
411	], [test -r $utsrelease2 && grep -qF UTS_RELEASE $utsrelease2], [
412		utsrelease=$utsrelease2
413	], [test -r $utsrelease3 && grep -qF UTS_RELEASE $utsrelease3], [
414		utsrelease=$utsrelease3
415	])
416
417	AS_IF([test -n "$utsrelease"], [
418		kernsrcver=$($AWK '/UTS_RELEASE/ { gsub(/"/, "", $[3]); print $[3] }' $utsrelease)
419		AS_IF([test -z "$kernsrcver"], [
420			AC_MSG_RESULT([Not found])
421			AC_MSG_ERROR([
422	*** Cannot determine kernel version.
423			])
424		])
425	], [
426		AC_MSG_RESULT([Not found])
427		if test "x$enable_linux_builtin" != xyes; then
428			AC_MSG_ERROR([
429	*** Cannot find UTS_RELEASE definition.
430			])
431		else
432			AC_MSG_ERROR([
433	*** Cannot find UTS_RELEASE definition.
434	*** Please run 'make prepare' inside the kernel source tree.])
435		fi
436	])
437
438	AC_MSG_RESULT([$kernsrcver])
439
440	AS_VERSION_COMPARE([$kernsrcver], [$ZFS_META_KVER_MIN], [
441		 AC_MSG_ERROR([
442	*** Cannot build against kernel version $kernsrcver.
443	*** The minimum supported kernel version is $ZFS_META_KVER_MIN.
444		])
445	])
446
447	LINUX=${kernelsrc}
448	LINUX_OBJ=${kernelbuild}
449	LINUX_VERSION=${kernsrcver}
450
451	AC_SUBST(LINUX)
452	AC_SUBST(LINUX_OBJ)
453	AC_SUBST(LINUX_VERSION)
454])
455
456dnl #
457dnl # Detect the QAT module to be built against, QAT provides hardware
458dnl # acceleration for data compression:
459dnl #
460dnl # https://01.org/intel-quickassist-technology
461dnl #
462dnl # 1) Download and install QAT driver from the above link
463dnl # 2) Start QAT driver in your system:
464dnl # 	 service qat_service start
465dnl # 3) Enable QAT in ZFS, e.g.:
466dnl # 	 ./configure --with-qat=<qat-driver-path>/QAT1.6
467dnl # 	 make
468dnl # 4) Set GZIP compression in ZFS dataset:
469dnl # 	 zfs set compression = gzip <dataset>
470dnl #
471dnl # Then the data written to this ZFS pool is compressed by QAT accelerator
472dnl # automatically, and de-compressed by QAT when read from the pool.
473dnl #
474dnl # 1) Get QAT hardware statistics with:
475dnl #	 cat /proc/icp_dh895xcc_dev/qat
476dnl # 2) To disable QAT:
477dnl # 	 insmod zfs.ko zfs_qat_disable=1
478dnl #
479AC_DEFUN([ZFS_AC_QAT], [
480	AC_ARG_WITH([qat],
481		AS_HELP_STRING([--with-qat=PATH],
482		[Path to qat source]),
483		AS_IF([test "$withval" = "yes"],
484			AC_MSG_ERROR([--with-qat=PATH requires a PATH]),
485			[qatsrc="$withval"]))
486
487	AC_ARG_WITH([qat-obj],
488		AS_HELP_STRING([--with-qat-obj=PATH],
489		[Path to qat build objects]),
490		[qatbuild="$withval"])
491
492	AS_IF([test ! -z "${qatsrc}"], [
493		AC_MSG_CHECKING([qat source directory])
494		AC_MSG_RESULT([$qatsrc])
495		QAT_SRC="${qatsrc}/quickassist"
496		AS_IF([ test ! -e "$QAT_SRC/include/cpa.h"], [
497			AC_MSG_ERROR([
498	*** Please make sure the qat driver package is installed
499	*** and specify the location of the qat source with the
500	*** '--with-qat=PATH' option then try again. Failed to
501	*** find cpa.h in:
502	${QAT_SRC}/include])
503		])
504	])
505
506	AS_IF([test ! -z "${qatsrc}"], [
507		AC_MSG_CHECKING([qat build directory])
508		AS_IF([test -z "$qatbuild"], [
509			qatbuild="${qatsrc}/build"
510		])
511
512		AC_MSG_RESULT([$qatbuild])
513		QAT_OBJ=${qatbuild}
514		AS_IF([ ! test -e "$QAT_OBJ/icp_qa_al.ko" && ! test -e "$QAT_OBJ/qat_api.ko"], [
515			AC_MSG_ERROR([
516	*** Please make sure the qat driver is installed then try again.
517	*** Failed to find icp_qa_al.ko or qat_api.ko in:
518	$QAT_OBJ])
519		])
520
521		AC_SUBST(QAT_SRC)
522		AC_SUBST(QAT_OBJ)
523
524		AC_DEFINE(HAVE_QAT, 1,
525		[qat is enabled and existed])
526	])
527
528	dnl #
529	dnl # Detect the name used for the QAT Module.symvers file.
530	dnl #
531	AS_IF([test ! -z "${qatsrc}"], [
532		AC_MSG_CHECKING([qat file for module symbols])
533		QAT_SYMBOLS=$QAT_SRC/lookaside/access_layer/src/Module.symvers
534
535		AS_IF([test -r $QAT_SYMBOLS], [
536			AC_MSG_RESULT([$QAT_SYMBOLS])
537			AC_SUBST(QAT_SYMBOLS)
538		],[
539			AC_MSG_ERROR([
540	*** Please make sure the qat driver is installed then try again.
541	*** Failed to find Module.symvers in:
542	$QAT_SYMBOLS
543			])
544		])
545	])
546])
547
548dnl #
549dnl # ZFS_LINUX_CONFTEST_H
550dnl #
551AC_DEFUN([ZFS_LINUX_CONFTEST_H], [
552test -d build/$2 || mkdir -p build/$2
553cat - <<_ACEOF >build/$2/$2.h
554$1
555_ACEOF
556])
557
558dnl #
559dnl # ZFS_LINUX_CONFTEST_C
560dnl #
561AC_DEFUN([ZFS_LINUX_CONFTEST_C], [
562test -d build/$2 || mkdir -p build/$2
563cat confdefs.h - <<_ACEOF >build/$2/$2.c
564$1
565_ACEOF
566])
567
568dnl #
569dnl # ZFS_LINUX_CONFTEST_MAKEFILE
570dnl #
571dnl # $1 - test case name
572dnl # $2 - add to top-level Makefile
573dnl # $3 - additional build flags
574dnl #
575AC_DEFUN([ZFS_LINUX_CONFTEST_MAKEFILE], [
576	test -d build || mkdir -p build
577	test -d build/$1 || mkdir -p build/$1
578
579	file=build/$1/Makefile
580
581	dnl # Example command line to manually build source.
582	cat - <<_ACEOF >$file
583# Example command line to manually build source
584# make modules -C $LINUX_OBJ $ARCH_UM M=$PWD/build/$1
585
586ccflags-y := -Werror $FRAME_LARGER_THAN
587_ACEOF
588
589	dnl # Additional custom CFLAGS as requested.
590	m4_ifval($3, [echo "ccflags-y += $3" >>$file], [])
591
592	dnl # Test case source
593	echo "obj-m := $1.o" >>$file
594
595	AS_IF([test "x$2" = "xyes"], [echo "obj-m += $1/" >>build/Makefile], [])
596])
597
598dnl #
599dnl # ZFS_LINUX_TEST_PROGRAM(C)([PROLOGUE], [BODY])
600dnl #
601m4_define([ZFS_LINUX_TEST_PROGRAM], [
602#include <linux/module.h>
603$1
604
605int
606main (void)
607{
608$2
609	;
610	return 0;
611}
612
613MODULE_DESCRIPTION("conftest");
614MODULE_AUTHOR(ZFS_META_AUTHOR);
615MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
616MODULE_LICENSE($3);
617])
618
619dnl #
620dnl # ZFS_LINUX_TEST_REMOVE
621dnl #
622dnl # Removes the specified test source and results.
623dnl #
624AC_DEFUN([ZFS_LINUX_TEST_REMOVE], [
625	test -d build/$1 && rm -Rf build/$1
626	test -f build/Makefile && sed '/$1/d' build/Makefile
627])
628
629dnl #
630dnl # ZFS_LINUX_COMPILE
631dnl #
632dnl # $1 - build dir
633dnl # $2 - test command
634dnl # $3 - pass command
635dnl # $4 - fail command
636dnl # $5 - set KBUILD_MODPOST_NOFINAL='yes'
637dnl # $6 - set KBUILD_MODPOST_WARN='yes'
638dnl #
639dnl # Used internally by ZFS_LINUX_TEST_{COMPILE,MODPOST}
640dnl #
641AC_DEFUN([ZFS_LINUX_COMPILE], [
642	AC_ARG_VAR([KERNEL_CC], [C compiler for
643		building kernel modules])
644	AC_ARG_VAR([KERNEL_LD], [Linker for
645		building kernel modules])
646	AC_ARG_VAR([KERNEL_LLVM], [Binary option to
647		build kernel modules with LLVM/CLANG toolchain])
648	AC_TRY_COMMAND([
649	    KBUILD_MODPOST_NOFINAL="$5" KBUILD_MODPOST_WARN="$6"
650	    make modules -k -j$TEST_JOBS ${KERNEL_CC:+CC=$KERNEL_CC}
651	    ${KERNEL_LD:+LD=$KERNEL_LD} ${KERNEL_LLVM:+LLVM=$KERNEL_LLVM}
652	    CONFIG_MODULES=y CFLAGS_MODULE=-DCONFIG_MODULES
653	    -C $LINUX_OBJ $ARCH_UM M=$PWD/$1 >$1/build.log 2>&1])
654	AS_IF([AC_TRY_COMMAND([$2])], [$3], [$4])
655])
656
657dnl #
658dnl # ZFS_LINUX_TEST_COMPILE
659dnl #
660dnl # Perform a full compile excluding the final modpost phase.
661dnl #
662AC_DEFUN([ZFS_LINUX_TEST_COMPILE], [
663	ZFS_LINUX_COMPILE([$2], [test -f $2/build.log], [
664		mv $2/Makefile $2/Makefile.compile.$1
665		mv $2/build.log $2/build.log.$1
666	],[
667	        AC_MSG_ERROR([
668        *** Unable to compile test source to determine kernel interfaces.])
669	], [yes], [])
670])
671
672dnl #
673dnl # ZFS_LINUX_TEST_MODPOST
674dnl #
675dnl # Perform a full compile including the modpost phase.  This may
676dnl # be an incremental build if the objects have already been built.
677dnl #
678AC_DEFUN([ZFS_LINUX_TEST_MODPOST], [
679	ZFS_LINUX_COMPILE([$2], [test -f $2/build.log], [
680		mv $2/Makefile $2/Makefile.modpost.$1
681		cat $2/build.log >>build/build.log.$1
682	],[
683	        AC_MSG_ERROR([
684        *** Unable to modpost test source to determine kernel interfaces.])
685	], [], [yes])
686])
687
688dnl #
689dnl # Perform the compilation of the test cases in two phases.
690dnl #
691dnl # Phase 1) attempt to build the object files for all of the tests
692dnl #          defined by the ZFS_LINUX_TEST_SRC macro.  But do not
693dnl #          perform the final modpost stage.
694dnl #
695dnl # Phase 2) disable all tests which failed the initial compilation,
696dnl #          then invoke the final modpost step for the remaining tests.
697dnl #
698dnl # This allows us efficiently build the test cases in parallel while
699dnl # remaining resilient to build failures which are expected when
700dnl # detecting the available kernel interfaces.
701dnl #
702dnl # The maximum allowed parallelism can be controlled by setting the
703dnl # TEST_JOBS environment variable.  Otherwise, it default to $(nproc).
704dnl #
705AC_DEFUN([ZFS_LINUX_TEST_COMPILE_ALL], [
706	dnl # Phase 1 - Compilation only, final linking is skipped.
707	ZFS_LINUX_TEST_COMPILE([$1], [build])
708
709	dnl #
710	dnl # Phase 2 - When building external modules disable test cases
711	dnl # which failed to compile and invoke modpost to verify the
712	dnl # final linking.
713	dnl #
714	dnl # Test names suffixed with '_license' call modpost independently
715	dnl # to ensure that a single incompatibility does not result in the
716	dnl # modpost phase exiting early.  This check is not performed on
717	dnl # every symbol since the majority are compatible and doing so
718	dnl # would significantly slow down this phase.
719	dnl #
720	dnl # When configuring for builtin (--enable-linux-builtin)
721	dnl # fake the linking step artificially create the expected .ko
722	dnl # files for tests which did compile.  This is required for
723	dnl # kernels which do not have loadable module support or have
724	dnl # not yet been built.
725	dnl #
726	AS_IF([test "x$enable_linux_builtin" = "xno"], [
727		for dir in $(awk '/^obj-m/ { print [$]3 }' \
728		    build/Makefile.compile.$1); do
729			name=${dir%/}
730			AS_IF([test -f build/$name/$name.o], [
731				AS_IF([test "${name##*_}" = "license"], [
732					ZFS_LINUX_TEST_MODPOST([$1],
733					    [build/$name])
734					echo "obj-n += $dir" >>build/Makefile
735				], [
736					echo "obj-m += $dir" >>build/Makefile
737				])
738			], [
739				echo "obj-n += $dir" >>build/Makefile
740			])
741		done
742
743		ZFS_LINUX_TEST_MODPOST([$1], [build])
744	], [
745		for dir in $(awk '/^obj-m/ { print [$]3 }' \
746		    build/Makefile.compile.$1); do
747			name=${dir%/}
748			AS_IF([test -f build/$name/$name.o], [
749				touch build/$name/$name.ko
750			])
751		done
752	])
753])
754
755dnl #
756dnl # ZFS_LINUX_TEST_SRC
757dnl #
758dnl # $1 - name
759dnl # $2 - global
760dnl # $3 - source
761dnl # $4 - extra cflags
762dnl # $5 - check license-compatibility
763dnl #
764dnl # Check if the test source is buildable at all and then if it is
765dnl # license compatible.
766dnl #
767dnl # N.B because all of the test cases are compiled in parallel they
768dnl # must never depend on the results of previous tests.  Each test
769dnl # needs to be entirely independent.
770dnl #
771AC_DEFUN([ZFS_LINUX_TEST_SRC], [
772	ZFS_LINUX_CONFTEST_C([ZFS_LINUX_TEST_PROGRAM([[$2]], [[$3]],
773	    [["Dual BSD/GPL"]])], [$1])
774	ZFS_LINUX_CONFTEST_MAKEFILE([$1], [yes], [$4])
775
776	AS_IF([ test -n "$5" ], [
777		ZFS_LINUX_CONFTEST_C([ZFS_LINUX_TEST_PROGRAM(
778		    [[$2]], [[$3]], [[$5]])], [$1_license])
779		ZFS_LINUX_CONFTEST_MAKEFILE([$1_license], [yes], [$4])
780	])
781])
782
783dnl #
784dnl # ZFS_LINUX_TEST_RESULT
785dnl #
786dnl # $1 - name of a test source (ZFS_LINUX_TEST_SRC)
787dnl # $2 - run on success (valid .ko generated)
788dnl # $3 - run on failure (unable to compile)
789dnl #
790AC_DEFUN([ZFS_LINUX_TEST_RESULT], [
791	AS_IF([test -d build/$1], [
792		AS_IF([test -f build/$1/$1.ko], [$2], [$3])
793	], [
794		AC_MSG_ERROR([
795	*** No matching source for the "$1" test, check that
796	*** both the test source and result macros refer to the same name.
797		])
798	])
799])
800
801dnl #
802dnl # ZFS_LINUX_TEST_ERROR
803dnl #
804dnl # Generic error message which can be used when none of the expected
805dnl # kernel interfaces were detected.
806dnl #
807AC_DEFUN([ZFS_LINUX_TEST_ERROR], [
808	AC_MSG_ERROR([
809	*** None of the expected "$1" interfaces were detected.
810	*** This may be because your kernel version is newer than what is
811	*** supported, or you are using a patched custom kernel with
812	*** incompatible modifications.
813	***
814	*** ZFS Version: $ZFS_META_ALIAS
815	*** Compatible Kernels: $ZFS_META_KVER_MIN - $ZFS_META_KVER_MAX
816	])
817])
818
819dnl #
820dnl # ZFS_LINUX_TEST_RESULT_SYMBOL
821dnl #
822dnl # Like ZFS_LINUX_TEST_RESULT except ZFS_CHECK_SYMBOL_EXPORT is called to
823dnl # verify symbol exports, unless --enable-linux-builtin was provided to
824dnl # configure.
825dnl #
826AC_DEFUN([ZFS_LINUX_TEST_RESULT_SYMBOL], [
827	AS_IF([ ! test -f build/$1/$1.ko], [
828		$5
829	], [
830		AS_IF([test "x$enable_linux_builtin" != "xyes"], [
831			ZFS_CHECK_SYMBOL_EXPORT([$2], [$3], [$4], [$5])
832		], [
833			$4
834		])
835	])
836])
837
838dnl #
839dnl # ZFS_LINUX_COMPILE_IFELSE
840dnl #
841AC_DEFUN([ZFS_LINUX_COMPILE_IFELSE], [
842	ZFS_LINUX_TEST_REMOVE([conftest])
843
844	m4_ifvaln([$1], [ZFS_LINUX_CONFTEST_C([$1], [conftest])])
845	m4_ifvaln([$5], [ZFS_LINUX_CONFTEST_H([$5], [conftest])],
846	    [ZFS_LINUX_CONFTEST_H([], [conftest])])
847
848	ZFS_LINUX_CONFTEST_MAKEFILE([conftest], [no],
849	    [m4_ifvaln([$5], [-I$PWD/build/conftest], [])])
850	ZFS_LINUX_COMPILE([build/conftest], [$2], [$3], [$4], [], [])
851])
852
853dnl #
854dnl # ZFS_LINUX_TRY_COMPILE
855dnl #
856dnl # $1 - global
857dnl # $2 - source
858dnl # $3 - run on success (valid .ko generated)
859dnl # $4 - run on failure (unable to compile)
860dnl #
861dnl # When configuring as builtin (--enable-linux-builtin) for kernels
862dnl # without loadable module support (CONFIG_MODULES=n) only the object
863dnl # file is created.  See ZFS_LINUX_TEST_COMPILE_ALL for details.
864dnl #
865AC_DEFUN([ZFS_LINUX_TRY_COMPILE], [
866	AS_IF([test "x$enable_linux_builtin" = "xyes"], [
867		ZFS_LINUX_COMPILE_IFELSE(
868		    [ZFS_LINUX_TEST_PROGRAM([[$1]], [[$2]],
869		    [[ZFS_META_LICENSE]])],
870		    [test -f build/conftest/conftest.o], [$3], [$4])
871	], [
872		ZFS_LINUX_COMPILE_IFELSE(
873		    [ZFS_LINUX_TEST_PROGRAM([[$1]], [[$2]],
874		    [[ZFS_META_LICENSE]])],
875		    [test -f build/conftest/conftest.ko], [$3], [$4])
876	])
877])
878
879dnl #
880dnl # ZFS_CHECK_SYMBOL_EXPORT
881dnl #
882dnl # Check if a symbol is exported on not by consulting the symbols
883dnl # file, or optionally the source code.
884dnl #
885AC_DEFUN([ZFS_CHECK_SYMBOL_EXPORT], [
886	grep -q -E '[[[:space:]]]$1[[[:space:]]]' \
887		$LINUX_OBJ/$LINUX_SYMBOLS 2>/dev/null
888	rc=$?
889	if test $rc -ne 0; then
890		export=0
891		for file in $2; do
892			grep -q -E "EXPORT_SYMBOL.*($1)" \
893				"$LINUX/$file" 2>/dev/null
894			rc=$?
895			if test $rc -eq 0; then
896				export=1
897				break;
898			fi
899		done
900		if test $export -eq 0; then :
901			$4
902		else :
903			$3
904		fi
905	else :
906		$3
907	fi
908])
909
910dnl #
911dnl # ZFS_LINUX_TRY_COMPILE_SYMBOL
912dnl #
913dnl # Like ZFS_LINUX_TRY_COMPILER except ZFS_CHECK_SYMBOL_EXPORT is called
914dnl # to verify symbol exports, unless --enable-linux-builtin was provided
915dnl # to configure.
916dnl #
917AC_DEFUN([ZFS_LINUX_TRY_COMPILE_SYMBOL], [
918	ZFS_LINUX_TRY_COMPILE([$1], [$2], [rc=0], [rc=1])
919	if test $rc -ne 0; then :
920		$6
921	else
922		if test "x$enable_linux_builtin" != xyes; then
923			ZFS_CHECK_SYMBOL_EXPORT([$3], [$4], [rc=0], [rc=1])
924		fi
925		if test $rc -ne 0; then :
926			$6
927		else :
928			$5
929		fi
930	fi
931])
932
933dnl #
934dnl # ZFS_LINUX_TRY_COMPILE_HEADER
935dnl # like ZFS_LINUX_TRY_COMPILE, except the contents conftest.h are
936dnl # provided via the fifth parameter
937dnl #
938AC_DEFUN([ZFS_LINUX_TRY_COMPILE_HEADER], [
939	ZFS_LINUX_COMPILE_IFELSE(
940	    [ZFS_LINUX_TEST_PROGRAM([[$1]], [[$2]], [[ZFS_META_LICENSE]])],
941	    [test -f build/conftest/conftest.ko],
942	    [$3], [$4], [$5])
943])
944