2afe77e63b45c5fb6d2ab954608e3749d87d9b3d
[zfs.git] / config / kernel.m4
1 dnl #
2 dnl # Default ZFS kernel configuration 
3 dnl #
4 AC_DEFUN([ZFS_AC_CONFIG_KERNEL], [
5         ZFS_AC_KERNEL
6         ZFS_AC_SPL
7         ZFS_AC_KERNEL_CONFIG
8         ZFS_AC_KERNEL_BDEV_BLOCK_DEVICE_OPERATIONS
9         ZFS_AC_KERNEL_TYPE_FMODE_T
10         ZFS_AC_KERNEL_KOBJ_NAME_LEN
11         ZFS_AC_KERNEL_BLKDEV_GET_BY_PATH
12         ZFS_AC_KERNEL_OPEN_BDEV_EXCLUSIVE
13         ZFS_AC_KERNEL_INVALIDATE_BDEV_ARGS
14         ZFS_AC_KERNEL_BDEV_LOGICAL_BLOCK_SIZE
15         ZFS_AC_KERNEL_BIO_EMPTY_BARRIER
16         ZFS_AC_KERNEL_BIO_FAILFAST
17         ZFS_AC_KERNEL_BIO_FAILFAST_DTD
18         ZFS_AC_KERNEL_REQ_FAILFAST_MASK
19         ZFS_AC_KERNEL_BIO_END_IO_T_ARGS
20         ZFS_AC_KERNEL_BIO_RW_SYNC
21         ZFS_AC_KERNEL_BIO_RW_SYNCIO
22         ZFS_AC_KERNEL_REQ_SYNC
23         ZFS_AC_KERNEL_BLK_END_REQUEST
24         ZFS_AC_KERNEL_BLK_FETCH_REQUEST
25         ZFS_AC_KERNEL_BLK_REQUEUE_REQUEST
26         ZFS_AC_KERNEL_BLK_RQ_BYTES
27         ZFS_AC_KERNEL_BLK_RQ_POS
28         ZFS_AC_KERNEL_BLK_RQ_SECTORS
29         ZFS_AC_KERNEL_GET_DISK_RO
30         ZFS_AC_KERNEL_RQ_IS_SYNC
31         ZFS_AC_KERNEL_RQ_FOR_EACH_SEGMENT
32         ZFS_AC_KERNEL_CONST_XATTR_HANDLER
33         ZFS_AC_KERNEL_XATTR_HANDLER_GET
34         ZFS_AC_KERNEL_XATTR_HANDLER_SET
35         ZFS_AC_KERNEL_FSYNC
36         ZFS_AC_KERNEL_EVICT_INODE
37         ZFS_AC_KERNEL_NR_CACHED_OBJECTS
38         ZFS_AC_KERNEL_FREE_CACHED_OBJECTS
39         ZFS_AC_KERNEL_INSERT_INODE_LOCKED
40         ZFS_AC_KERNEL_D_OBTAIN_ALIAS
41         ZFS_AC_KERNEL_CHECK_DISK_SIZE_CHANGE
42         ZFS_AC_KERNEL_TRUNCATE_SETSIZE
43         ZFS_AC_KERNEL_6ARGS_SECURITY_INODE_INIT_SECURITY
44         ZFS_AC_KERNEL_MOUNT_NODEV
45         ZFS_AC_KERNEL_SHRINK
46         ZFS_AC_KERNEL_BDI
47         ZFS_AC_KERNEL_BDI_SETUP_AND_REGISTER
48         ZFS_AC_KERNEL_SET_NLINK
49
50         AS_IF([test "$LINUX_OBJ" != "$LINUX"], [
51                 KERNELMAKE_PARAMS="$KERNELMAKE_PARAMS O=$LINUX_OBJ"
52         ])
53         AC_SUBST(KERNELMAKE_PARAMS)
54
55
56         dnl # -Wall -fno-strict-aliasing -Wstrict-prototypes and other
57         dnl # compiler options are added by the kernel build system.
58         KERNELCPPFLAGS="$KERNELCPPFLAGS $NO_UNUSED_BUT_SET_VARIABLE"
59         KERNELCPPFLAGS="$KERNELCPPFLAGS -DHAVE_SPL -D_KERNEL"
60         KERNELCPPFLAGS="$KERNELCPPFLAGS -DTEXT_DOMAIN=\\\"zfs-linux-kernel\\\""
61
62         AC_SUBST(KERNELCPPFLAGS)
63 ])
64
65 dnl #
66 dnl # Detect name used for Module.symvers file in kernel
67 dnl #
68 AC_DEFUN([ZFS_AC_MODULE_SYMVERS], [
69         modpost=$LINUX/scripts/Makefile.modpost
70         AC_MSG_CHECKING([kernel file name for module symbols])
71         AS_IF([test -f "$modpost"], [
72                 AS_IF([grep -q Modules.symvers $modpost], [
73                         LINUX_SYMBOLS=Modules.symvers
74                 ], [
75                         LINUX_SYMBOLS=Module.symvers
76                 ])
77
78                 AS_IF([test ! -f "$LINUX_OBJ/$LINUX_SYMBOLS"], [
79                         AC_MSG_ERROR([
80         *** Please make sure the kernel devel package for your distribution
81         *** is installed.  If your building with a custom kernel make sure the
82         *** kernel is configured, built, and the '--with-linux=PATH' configure
83         *** option refers to the location of the kernel source.])
84                 ])
85         ], [
86                 LINUX_SYMBOLS=NONE
87         ])
88         AC_MSG_RESULT($LINUX_SYMBOLS)
89         AC_SUBST(LINUX_SYMBOLS)
90 ])
91
92 dnl #
93 dnl # Detect the kernel to be built against
94 dnl #
95 AC_DEFUN([ZFS_AC_KERNEL], [
96         AC_ARG_WITH([linux],
97                 AS_HELP_STRING([--with-linux=PATH],
98                 [Path to kernel source]),
99                 [kernelsrc="$withval"])
100
101         AC_ARG_WITH(linux-obj,
102                 AS_HELP_STRING([--with-linux-obj=PATH],
103                 [Path to kernel build objects]),
104                 [kernelbuild="$withval"])
105
106         AC_MSG_CHECKING([kernel source directory])
107         AS_IF([test -z "$kernelsrc"], [
108                 AS_IF([test -e "/lib/modules/$(uname -r)/source"], [
109                         headersdir="/lib/modules/$(uname -r)/source"
110                         sourcelink=$(readlink -f "$headersdir")
111                 ], [test -e "/lib/modules/$(uname -r)/build"], [
112                         headersdir="/lib/modules/$(uname -r)/build"
113                         sourcelink=$(readlink -f "$headersdir")
114                 ], [
115                         sourcelink=$(ls -1d /usr/src/kernels/* \
116                                      /usr/src/linux-* \
117                                      2>/dev/null | grep -v obj | tail -1)
118                 ])
119
120                 AS_IF([test -n "$sourcelink" && test -e ${sourcelink}], [
121                         kernelsrc=`readlink -f ${sourcelink}`
122                 ], [
123                         AC_MSG_RESULT([Not found])
124                         AC_MSG_ERROR([
125         *** Please make sure the kernel devel package for your distribution
126         *** is installed then try again.  If that fails you can specify the
127         *** location of the kernel source with the '--with-linux=PATH' option.])
128                 ])
129         ], [
130                 AS_IF([test "$kernelsrc" = "NONE"], [
131                         kernsrcver=NONE
132                 ])
133         ])
134
135         AC_MSG_RESULT([$kernelsrc])
136         AC_MSG_CHECKING([kernel build directory])
137         AS_IF([test -z "$kernelbuild"], [
138                 AS_IF([test -e "/lib/modules/$(uname -r)/build"], [
139                         kernelbuild=`readlink -f /lib/modules/$(uname -r)/build`
140                 ], [test -d ${kernelsrc}-obj/${target_cpu}/${target_cpu}], [
141                         kernelbuild=${kernelsrc}-obj/${target_cpu}/${target_cpu}
142                 ], [test -d ${kernelsrc}-obj/${target_cpu}/default], [
143                         kernelbuild=${kernelsrc}-obj/${target_cpu}/default
144                 ], [test -d `dirname ${kernelsrc}`/build-${target_cpu}], [
145                         kernelbuild=`dirname ${kernelsrc}`/build-${target_cpu}
146                 ], [
147                         kernelbuild=${kernelsrc}
148                 ])
149         ])
150         AC_MSG_RESULT([$kernelbuild])
151
152         AC_MSG_CHECKING([kernel source version])
153         utsrelease1=$kernelbuild/include/linux/version.h
154         utsrelease2=$kernelbuild/include/linux/utsrelease.h
155         utsrelease3=$kernelbuild/include/generated/utsrelease.h
156         AS_IF([test -r $utsrelease1 && fgrep -q UTS_RELEASE $utsrelease1], [
157                 utsrelease=linux/version.h
158         ], [test -r $utsrelease2 && fgrep -q UTS_RELEASE $utsrelease2], [
159                 utsrelease=linux/utsrelease.h
160         ], [test -r $utsrelease3 && fgrep -q UTS_RELEASE $utsrelease3], [
161                 utsrelease=generated/utsrelease.h
162         ])
163
164         AS_IF([test "$utsrelease"], [
165                 kernsrcver=`(echo "#include <$utsrelease>";
166                              echo "kernsrcver=UTS_RELEASE") |
167                              cpp -I $kernelbuild/include |
168                              grep "^kernsrcver=" | cut -d \" -f 2`
169
170                 AS_IF([test -z "$kernsrcver"], [
171                         AC_MSG_RESULT([Not found])
172                         AC_MSG_ERROR([*** Cannot determine kernel version.])
173                 ])
174         ], [
175                 AC_MSG_RESULT([Not found])
176                 AC_MSG_ERROR([*** Cannot find UTS_RELEASE definition.])
177         ])
178
179         AC_MSG_RESULT([$kernsrcver])
180
181         LINUX=${kernelsrc}
182         LINUX_OBJ=${kernelbuild}
183         LINUX_VERSION=${kernsrcver}
184
185         AC_SUBST(LINUX)
186         AC_SUBST(LINUX_OBJ)
187         AC_SUBST(LINUX_VERSION)
188
189         ZFS_AC_MODULE_SYMVERS
190 ])
191
192 dnl #
193 dnl # Detect name used for the additional SPL Module.symvers file.  If one
194 dnl # does not exist this is likely because the SPL has been configured
195 dnl # but not built.  To allow recursive builds a good guess is made as to
196 dnl # what this file will be named based on what it is named in the kernel
197 dnl # build products.  This file will first be used at link time so if
198 dnl # the guess is wrong the build will fail then.  This unfortunately
199 dnl # means the ZFS package does not contain a reliable mechanism to
200 dnl # detect symbols exported by the SPL at configure time.
201 dnl #
202 AC_DEFUN([ZFS_AC_SPL_MODULE_SYMVERS], [
203         AC_MSG_CHECKING([spl file name for module symbols])
204         AS_IF([test -r $SPL_OBJ/Module.symvers], [
205                 SPL_SYMBOLS=Module.symvers
206         ], [test -r $SPL_OBJ/Modules.symvers], [
207                 SPL_SYMBOLS=Modules.symvers
208         ], [test -r $SPL_OBJ/module/Module.symvers], [
209                 SPL_SYMBOLS=Module.symvers
210         ], [test -r $SPL_OBJ/module/Modules.symvers], [
211                 SPL_SYMBOLS=Modules.symvers
212         ], [
213                 SPL_SYMBOLS=$LINUX_SYMBOLS
214         ])
215
216         AC_MSG_RESULT([$SPL_SYMBOLS])
217         AC_SUBST(SPL_SYMBOLS)
218 ])
219
220 dnl #
221 dnl # Detect the SPL module to be built against
222 dnl #
223 AC_DEFUN([ZFS_AC_SPL], [
224         AC_ARG_WITH([spl],
225                 AS_HELP_STRING([--with-spl=PATH],
226                 [Path to spl source]),
227                 [splsrc="$withval"])
228
229         AC_ARG_WITH([spl-obj],
230                 AS_HELP_STRING([--with-spl-obj=PATH],
231                 [Path to spl build objects]),
232                 [splbuild="$withval"])
233
234
235         AC_MSG_CHECKING([spl source directory])
236         AS_IF([test -z "$splsrc"], [
237                 sourcelink=`ls -1d /usr/src/spl-*/${LINUX_VERSION} \
238                             2>/dev/null | tail -1`
239
240                 AS_IF([test -z "$sourcelink" || test ! -e $sourcelink], [
241                         sourcelink=../spl
242                 ])
243
244                 AS_IF([test -e $sourcelink], [
245                         splsrc=`readlink -f ${sourcelink}`
246                 ], [
247                         AC_MSG_RESULT([Not found])
248                         AC_MSG_ERROR([
249         *** Please make sure the spl devel package for your distribution
250         *** is installed then try again.  If that fails you can specify the
251         *** location of the spl source with the '--with-spl=PATH' option.])
252                 ])
253         ], [
254                 AS_IF([test "$splsrc" = "NONE"], [
255                         splbuild=NONE
256                         splsrcver=NONE
257                 ])
258         ])
259
260         AC_MSG_RESULT([$splsrc])
261         AC_MSG_CHECKING([spl build directory])
262         AS_IF([test -z "$splbuild"], [
263                 splbuild=${splsrc}
264         ])
265         AC_MSG_RESULT([$splbuild])
266
267         AC_MSG_CHECKING([spl source version])
268         AS_IF([test -r $splbuild/spl_config.h &&
269                 fgrep -q SPL_META_VERSION $splbuild/spl_config.h], [
270
271                 splsrcver=`(echo "#include <spl_config.h>";
272                             echo "splsrcver=SPL_META_VERSION") |
273                             cpp -I $splbuild |
274                             grep "^splsrcver=" | cut -d \" -f 2`
275         ])
276
277         AS_IF([test -z "$splsrcver"], [
278                 AC_MSG_RESULT([Not found])
279                 AC_MSG_ERROR([
280         *** Cannot determine the version of the spl source.
281         *** Please prepare the spl source before running this script])
282         ])
283
284         AC_MSG_RESULT([$splsrcver])
285
286         SPL=${splsrc}
287         SPL_OBJ=${splbuild}
288         SPL_VERSION=${splsrcver}
289
290         AC_SUBST(SPL)
291         AC_SUBST(SPL_OBJ)
292         AC_SUBST(SPL_VERSION)
293
294         ZFS_AC_SPL_MODULE_SYMVERS
295 ])
296
297 dnl #
298 dnl # Certain kernel build options are not supported.  These must be
299 dnl # detected at configure time and cause a build failure.  Otherwise
300 dnl # modules may be successfully built that behave incorrectly.
301 dnl #
302 dnl # CONFIG_PREEMPT - Preempt kernels require special handling.
303 dnl #
304 dnl # There are certain kernel build options which when enabled are
305 dnl # completely incompatible with non GPL kernel modules.  It is best
306 dnl # to detect these at configure time and fail with a clear error
307 dnl # rather than build everything and fail during linking.
308 dnl #
309 dnl # CONFIG_DEBUG_LOCK_ALLOC - Maps mutex_lock() to mutex_lock_nested()
310 dnl #
311 AC_DEFUN([ZFS_AC_KERNEL_CONFIG], [
312
313         ZFS_LINUX_CONFIG([PREEMPT],
314                 AC_MSG_ERROR([
315         *** Kernel built with CONFIG_PREEMPT which is not supported.
316         *** You must rebuild your kernel without this option.]), [])
317
318         AS_IF([test "$ZFS_META_LICENSE" = CDDL], [
319                 ZFS_LINUX_CONFIG([DEBUG_LOCK_ALLOC],
320                 AC_MSG_ERROR([
321         *** Kernel built with CONFIG_DEBUG_LOCK_ALLOC which is
322         *** incompatible with the CDDL license.  You must rebuild
323         *** your kernel without this option.]), [])
324         ])
325
326         AS_IF([test "$ZFS_META_LICENSE" = GPL], [
327                 AC_DEFINE([HAVE_GPL_ONLY_SYMBOLS], [1],
328                         [Define to 1 if licensed under the GPL])
329         ])
330 ])
331
332 dnl #
333 dnl # ZFS_LINUX_CONFTEST
334 dnl #
335 AC_DEFUN([ZFS_LINUX_CONFTEST], [
336 cat confdefs.h - <<_ACEOF >conftest.c
337 $1
338 _ACEOF
339 ])
340
341 dnl #
342 dnl # ZFS_LANG_PROGRAM(C)([PROLOGUE], [BODY])
343 dnl #
344 m4_define([ZFS_LANG_PROGRAM], [
345 $1
346 int
347 main (void)
348 {
349 dnl Do *not* indent the following line: there may be CPP directives.
350 dnl Don't move the `;' right after for the same reason.
351 $2
352   ;
353   return 0;
354 }
355 ])
356
357 dnl #
358 dnl # ZFS_LINUX_COMPILE_IFELSE / like AC_COMPILE_IFELSE
359 dnl #
360 AC_DEFUN([ZFS_LINUX_COMPILE_IFELSE], [
361         m4_ifvaln([$1], [ZFS_LINUX_CONFTEST([$1])])
362         rm -Rf build && mkdir -p build
363         echo "obj-m := conftest.o" >build/Makefile
364         AS_IF(
365                 [AC_TRY_COMMAND(cp conftest.c build && make [$2] -C $LINUX_OBJ EXTRA_CFLAGS="-Werror-implicit-function-declaration $EXTRA_KCFLAGS" $ARCH_UM M=$PWD/build) >/dev/null && AC_TRY_COMMAND([$3])],
366                 [$4],
367                 [_AC_MSG_LOG_CONFTEST m4_ifvaln([$5],[$5])]
368         )
369         rm -Rf build
370 ])
371
372 dnl #
373 dnl # ZFS_LINUX_TRY_COMPILE like AC_TRY_COMPILE
374 dnl #
375 AC_DEFUN([ZFS_LINUX_TRY_COMPILE],
376         [ZFS_LINUX_COMPILE_IFELSE(
377         [AC_LANG_SOURCE([ZFS_LANG_PROGRAM([[$1]], [[$2]])])],
378         [modules],
379         [test -s build/conftest.o],
380         [$3], [$4])
381 ])
382
383 dnl #
384 dnl # ZFS_LINUX_CONFIG
385 dnl #
386 AC_DEFUN([ZFS_LINUX_CONFIG],
387         [AC_MSG_CHECKING([whether Linux was built with CONFIG_$1])
388         ZFS_LINUX_TRY_COMPILE([
389                 #include <linux/module.h>
390         ],[
391                 #ifndef CONFIG_$1
392                 #error CONFIG_$1 not #defined
393                 #endif
394         ],[
395                 AC_MSG_RESULT([yes])
396                 $2
397         ],[
398                 AC_MSG_RESULT([no])
399                 $3
400         ])
401 ])
402
403 dnl #
404 dnl # ZFS_CHECK_SYMBOL_EXPORT
405 dnl # check symbol exported or not
406 dnl #
407 AC_DEFUN([ZFS_CHECK_SYMBOL_EXPORT],
408         [AC_MSG_CHECKING([whether symbol $1 is exported])
409         grep -q -E '[[[:space:]]]$1[[[:space:]]]' \
410                 $LINUX_OBJ/$LINUX_SYMBOLS 2>/dev/null
411         rc=$?
412         AS_IF([test $rc -ne 0], [
413                 export=0
414                 for file in $2; do
415                         grep -q -E "EXPORT_SYMBOL.*($1)" "$LINUX/$file" 2>/dev/null
416                         rc=$?
417                         AS_IF([test $rc -eq 0], [
418                                 export=1
419                                 break;
420                         ])
421                 done
422                 AS_IF([test $export -eq 0], [
423                         AC_MSG_RESULT([no])
424                         $4
425                 ], [
426                         AC_MSG_RESULT([yes])
427                         $3
428                 ])
429         ], [
430                 AC_MSG_RESULT([yes])
431                 $3
432         ])
433 ])