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