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