Extend CONFIG_DEBUG_LOCK_ALLOC check
[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_QUEUE_FLUSH
25         ZFS_AC_KERNEL_BLK_QUEUE_MAX_HW_SECTORS
26         ZFS_AC_KERNEL_BLK_QUEUE_MAX_SEGMENTS
27         ZFS_AC_KERNEL_BLK_QUEUE_PHYSICAL_BLOCK_SIZE
28         ZFS_AC_KERNEL_BLK_QUEUE_IO_OPT
29         ZFS_AC_KERNEL_BLK_QUEUE_NONROT
30         ZFS_AC_KERNEL_BLK_QUEUE_DISCARD
31         ZFS_AC_KERNEL_BLK_FETCH_REQUEST
32         ZFS_AC_KERNEL_BLK_REQUEUE_REQUEST
33         ZFS_AC_KERNEL_BLK_RQ_BYTES
34         ZFS_AC_KERNEL_BLK_RQ_POS
35         ZFS_AC_KERNEL_BLK_RQ_SECTORS
36         ZFS_AC_KERNEL_GET_DISK_RO
37         ZFS_AC_KERNEL_RQ_IS_SYNC
38         ZFS_AC_KERNEL_RQ_FOR_EACH_SEGMENT
39         ZFS_AC_KERNEL_CONST_XATTR_HANDLER
40         ZFS_AC_KERNEL_XATTR_HANDLER_GET
41         ZFS_AC_KERNEL_XATTR_HANDLER_SET
42         ZFS_AC_KERNEL_SHOW_OPTIONS
43         ZFS_AC_KERNEL_FSYNC
44         ZFS_AC_KERNEL_EVICT_INODE
45         ZFS_AC_KERNEL_NR_CACHED_OBJECTS
46         ZFS_AC_KERNEL_FREE_CACHED_OBJECTS
47         ZFS_AC_KERNEL_FALLOCATE
48         ZFS_AC_KERNEL_CREATE_UMODE_T
49         ZFS_AC_KERNEL_AUTOMOUNT
50         ZFS_AC_KERNEL_INSERT_INODE_LOCKED
51         ZFS_AC_KERNEL_D_OBTAIN_ALIAS
52         ZFS_AC_KERNEL_CHECK_DISK_SIZE_CHANGE
53         ZFS_AC_KERNEL_TRUNCATE_SETSIZE
54         ZFS_AC_KERNEL_6ARGS_SECURITY_INODE_INIT_SECURITY
55         ZFS_AC_KERNEL_CALLBACK_SECURITY_INODE_INIT_SECURITY
56         ZFS_AC_KERNEL_MOUNT_NODEV
57         ZFS_AC_KERNEL_SHRINK
58         ZFS_AC_KERNEL_BDI
59         ZFS_AC_KERNEL_BDI_SETUP_AND_REGISTER
60         ZFS_AC_KERNEL_SET_NLINK
61
62         AS_IF([test "$LINUX_OBJ" != "$LINUX"], [
63                 KERNELMAKE_PARAMS="$KERNELMAKE_PARAMS O=$LINUX_OBJ"
64         ])
65         AC_SUBST(KERNELMAKE_PARAMS)
66
67
68         dnl # -Wall -fno-strict-aliasing -Wstrict-prototypes and other
69         dnl # compiler options are added by the kernel build system.
70         KERNELCPPFLAGS="$KERNELCPPFLAGS $NO_UNUSED_BUT_SET_VARIABLE"
71         KERNELCPPFLAGS="$KERNELCPPFLAGS -DHAVE_SPL -D_KERNEL"
72         KERNELCPPFLAGS="$KERNELCPPFLAGS -DTEXT_DOMAIN=\\\"zfs-linux-kernel\\\""
73
74         AC_SUBST(KERNELCPPFLAGS)
75 ])
76
77 dnl #
78 dnl # Detect name used for Module.symvers file in kernel
79 dnl #
80 AC_DEFUN([ZFS_AC_MODULE_SYMVERS], [
81         modpost=$LINUX/scripts/Makefile.modpost
82         AC_MSG_CHECKING([kernel file name for module symbols])
83         AS_IF([test -f "$modpost"], [
84                 AS_IF([grep -q Modules.symvers $modpost], [
85                         LINUX_SYMBOLS=Modules.symvers
86                 ], [
87                         LINUX_SYMBOLS=Module.symvers
88                 ])
89
90                 AS_IF([test ! -f "$LINUX_OBJ/$LINUX_SYMBOLS"], [
91                         AC_MSG_ERROR([
92         *** Please make sure the kernel devel package for your distribution
93         *** is installed.  If your building with a custom kernel make sure the
94         *** kernel is configured, built, and the '--with-linux=PATH' configure
95         *** option refers to the location of the kernel source.])
96                 ])
97         ], [
98                 LINUX_SYMBOLS=NONE
99         ])
100         AC_MSG_RESULT($LINUX_SYMBOLS)
101         AC_SUBST(LINUX_SYMBOLS)
102 ])
103
104 dnl #
105 dnl # Detect the kernel to be built against
106 dnl #
107 AC_DEFUN([ZFS_AC_KERNEL], [
108         AC_ARG_WITH([linux],
109                 AS_HELP_STRING([--with-linux=PATH],
110                 [Path to kernel source]),
111                 [kernelsrc="$withval"])
112
113         AC_ARG_WITH(linux-obj,
114                 AS_HELP_STRING([--with-linux-obj=PATH],
115                 [Path to kernel build objects]),
116                 [kernelbuild="$withval"])
117
118         AC_MSG_CHECKING([kernel source directory])
119         AS_IF([test -z "$kernelsrc"], [
120                 AS_IF([test -e "/lib/modules/$(uname -r)/source"], [
121                         headersdir="/lib/modules/$(uname -r)/source"
122                         sourcelink=$(readlink -f "$headersdir")
123                 ], [test -e "/lib/modules/$(uname -r)/build"], [
124                         headersdir="/lib/modules/$(uname -r)/build"
125                         sourcelink=$(readlink -f "$headersdir")
126                 ], [
127                         sourcelink=$(ls -1d /usr/src/kernels/* \
128                                      /usr/src/linux-* \
129                                      2>/dev/null | grep -v obj | tail -1)
130                 ])
131
132                 AS_IF([test -n "$sourcelink" && test -e ${sourcelink}], [
133                         kernelsrc=`readlink -f ${sourcelink}`
134                 ], [
135                         AC_MSG_RESULT([Not found])
136                         AC_MSG_ERROR([
137         *** Please make sure the kernel devel package for your distribution
138         *** is installed then try again.  If that fails you can specify the
139         *** location of the kernel source with the '--with-linux=PATH' option.])
140                 ])
141         ], [
142                 AS_IF([test "$kernelsrc" = "NONE"], [
143                         kernsrcver=NONE
144                 ])
145         ])
146
147         AC_MSG_RESULT([$kernelsrc])
148         AC_MSG_CHECKING([kernel build directory])
149         AS_IF([test -z "$kernelbuild"], [
150                 AS_IF([test -e "/lib/modules/$(uname -r)/build"], [
151                         kernelbuild=`readlink -f /lib/modules/$(uname -r)/build`
152                 ], [test -d ${kernelsrc}-obj/${target_cpu}/${target_cpu}], [
153                         kernelbuild=${kernelsrc}-obj/${target_cpu}/${target_cpu}
154                 ], [test -d ${kernelsrc}-obj/${target_cpu}/default], [
155                         kernelbuild=${kernelsrc}-obj/${target_cpu}/default
156                 ], [test -d `dirname ${kernelsrc}`/build-${target_cpu}], [
157                         kernelbuild=`dirname ${kernelsrc}`/build-${target_cpu}
158                 ], [
159                         kernelbuild=${kernelsrc}
160                 ])
161         ])
162         AC_MSG_RESULT([$kernelbuild])
163
164         AC_MSG_CHECKING([kernel source version])
165         utsrelease1=$kernelbuild/include/linux/version.h
166         utsrelease2=$kernelbuild/include/linux/utsrelease.h
167         utsrelease3=$kernelbuild/include/generated/utsrelease.h
168         AS_IF([test -r $utsrelease1 && fgrep -q UTS_RELEASE $utsrelease1], [
169                 utsrelease=linux/version.h
170         ], [test -r $utsrelease2 && fgrep -q UTS_RELEASE $utsrelease2], [
171                 utsrelease=linux/utsrelease.h
172         ], [test -r $utsrelease3 && fgrep -q UTS_RELEASE $utsrelease3], [
173                 utsrelease=generated/utsrelease.h
174         ])
175
176         AS_IF([test "$utsrelease"], [
177                 kernsrcver=`(echo "#include <$utsrelease>";
178                              echo "kernsrcver=UTS_RELEASE") |
179                              cpp -I $kernelbuild/include |
180                              grep "^kernsrcver=" | cut -d \" -f 2`
181
182                 AS_IF([test -z "$kernsrcver"], [
183                         AC_MSG_RESULT([Not found])
184                         AC_MSG_ERROR([*** Cannot determine kernel version.])
185                 ])
186         ], [
187                 AC_MSG_RESULT([Not found])
188                 AC_MSG_ERROR([*** Cannot find UTS_RELEASE definition.])
189         ])
190
191         AC_MSG_RESULT([$kernsrcver])
192
193         LINUX=${kernelsrc}
194         LINUX_OBJ=${kernelbuild}
195         LINUX_VERSION=${kernsrcver}
196
197         AC_SUBST(LINUX)
198         AC_SUBST(LINUX_OBJ)
199         AC_SUBST(LINUX_VERSION)
200
201         ZFS_AC_MODULE_SYMVERS
202 ])
203
204 dnl #
205 dnl # Detect name used for the additional SPL Module.symvers file.  If one
206 dnl # does not exist this is likely because the SPL has been configured
207 dnl # but not built.  To allow recursive builds a good guess is made as to
208 dnl # what this file will be named based on what it is named in the kernel
209 dnl # build products.  This file will first be used at link time so if
210 dnl # the guess is wrong the build will fail then.  This unfortunately
211 dnl # means the ZFS package does not contain a reliable mechanism to
212 dnl # detect symbols exported by the SPL at configure time.
213 dnl #
214 AC_DEFUN([ZFS_AC_SPL_MODULE_SYMVERS], [
215         AC_MSG_CHECKING([spl file name for module symbols])
216         AS_IF([test -r $SPL_OBJ/Module.symvers], [
217                 SPL_SYMBOLS=Module.symvers
218         ], [test -r $SPL_OBJ/Modules.symvers], [
219                 SPL_SYMBOLS=Modules.symvers
220         ], [test -r $SPL_OBJ/module/Module.symvers], [
221                 SPL_SYMBOLS=Module.symvers
222         ], [test -r $SPL_OBJ/module/Modules.symvers], [
223                 SPL_SYMBOLS=Modules.symvers
224         ], [
225                 SPL_SYMBOLS=$LINUX_SYMBOLS
226         ])
227
228         AC_MSG_RESULT([$SPL_SYMBOLS])
229         AC_SUBST(SPL_SYMBOLS)
230 ])
231
232 dnl #
233 dnl # Detect the SPL module to be built against
234 dnl #
235 AC_DEFUN([ZFS_AC_SPL], [
236         AC_ARG_WITH([spl],
237                 AS_HELP_STRING([--with-spl=PATH],
238                 [Path to spl source]),
239                 [splsrc="$withval"])
240
241         AC_ARG_WITH([spl-obj],
242                 AS_HELP_STRING([--with-spl-obj=PATH],
243                 [Path to spl build objects]),
244                 [splbuild="$withval"])
245
246
247         AC_MSG_CHECKING([spl source directory])
248         AS_IF([test -z "$splsrc"], [
249                 sourcelink=`ls -1d /usr/src/spl-*/${LINUX_VERSION} \
250                             2>/dev/null | tail -1`
251
252                 AS_IF([test -z "$sourcelink" || test ! -e $sourcelink], [
253                         sourcelink=../spl
254                 ])
255
256                 AS_IF([test -e $sourcelink], [
257                         splsrc=`readlink -f ${sourcelink}`
258                 ], [
259                         AC_MSG_RESULT([Not found])
260                         AC_MSG_ERROR([
261         *** Please make sure the spl devel package for your distribution
262         *** is installed then try again.  If that fails you can specify the
263         *** location of the spl source with the '--with-spl=PATH' option.])
264                 ])
265         ], [
266                 AS_IF([test "$splsrc" = "NONE"], [
267                         splbuild=NONE
268                         splsrcver=NONE
269                 ])
270         ])
271
272         AC_MSG_RESULT([$splsrc])
273         AC_MSG_CHECKING([spl build directory])
274         AS_IF([test -z "$splbuild"], [
275                 splbuild=${splsrc}
276         ])
277         AC_MSG_RESULT([$splbuild])
278
279         AC_MSG_CHECKING([spl source version])
280         AS_IF([test -r $splbuild/spl_config.h &&
281                 fgrep -q SPL_META_VERSION $splbuild/spl_config.h], [
282
283                 splsrcver=`(echo "#include <spl_config.h>";
284                             echo "splsrcver=SPL_META_VERSION-SPL_META_RELEASE") |
285                             cpp -I $splbuild |
286                             grep "^splsrcver=" | tr -d \" | cut -d= -f2`
287         ])
288
289         AS_IF([test -z "$splsrcver"], [
290                 AC_MSG_RESULT([Not found])
291                 AC_MSG_ERROR([
292         *** Cannot determine the version of the spl source.
293         *** Please prepare the spl source before running this script])
294         ])
295
296         AC_MSG_RESULT([$splsrcver])
297
298         SPL=${splsrc}
299         SPL_OBJ=${splbuild}
300         SPL_VERSION=${splsrcver}
301
302         AC_SUBST(SPL)
303         AC_SUBST(SPL_OBJ)
304         AC_SUBST(SPL_VERSION)
305
306         ZFS_AC_SPL_MODULE_SYMVERS
307 ])
308
309 dnl #
310 dnl # Certain kernel build options are not supported.  These must be
311 dnl # detected at configure time and cause a build failure.  Otherwise
312 dnl # modules may be successfully built that behave incorrectly.
313 dnl #
314 AC_DEFUN([ZFS_AC_KERNEL_CONFIG], [
315
316         AS_IF([test "$ZFS_META_LICENSE" = GPL], [
317                 AC_DEFINE([HAVE_GPL_ONLY_SYMBOLS], [1],
318                         [Define to 1 if licensed under the GPL])
319         ])
320
321         ZFS_AC_KERNEL_CONFIG_PREEMPT
322         ZFS_AC_KERNEL_CONFIG_DEBUG_LOCK_ALLOC
323 ])
324
325 dnl #
326 dnl # Check CONFIG_PREEMPT
327 dnl #
328 dnl # Premptible kernels will be supported in the future.  But at the
329 dnl # moment there are a few places in the code which need to be updated
330 dnl # to accomidate them.  Until that work occurs we should detect this
331 dnl # at configure time and fail with a sensible message.  Otherwise,
332 dnl # people will be able to build successfully, however they will have
333 dnl # stability problems.  See https://github.com/zfsonlinux/zfs/issues/83
334 dnl #
335 AC_DEFUN([ZFS_AC_KERNEL_CONFIG_PREEMPT], [
336
337         ZFS_LINUX_CONFIG([PREEMPT],
338                 AC_MSG_ERROR([
339         *** Kernel built with CONFIG_PREEMPT which is not supported.
340         *** You must rebuild your kernel without this option.]), [])
341 ])
342
343 dnl #
344 dnl # Check CONFIG_DEBUG_LOCK_ALLOC
345 dnl #
346 dnl # This is typically only set for debug kernels because it comes with
347 dnl # a performance penalty.  However, when it is set it maps the non-GPL
348 dnl # symbol mutex_lock() to the GPL-only mutex_lock_nested() symbol.
349 dnl # This will cause a failure at link time which we'd rather know about
350 dnl # at compile time.
351 dnl #
352 dnl # Since we plan to pursue making mutex_lock_nested() a non-GPL symbol
353 dnl # with the upstream community we add a check to detect this case.
354 dnl #
355 AC_DEFUN([ZFS_AC_KERNEL_CONFIG_DEBUG_LOCK_ALLOC], [
356
357         ZFS_LINUX_CONFIG([DEBUG_LOCK_ALLOC], [
358                 AC_MSG_CHECKING([whether mutex_lock() is GPL-only])
359                 tmp_flags="$EXTRA_KCFLAGS"
360                 ZFS_LINUX_TRY_COMPILE([
361                         #include <linux/module.h>
362                         #include <linux/mutex.h>
363
364                         MODULE_LICENSE("$ZFS_META_LICENSE");
365                 ],[
366                         struct mutex lock;
367
368                         mutex_init(&lock);
369                         mutex_lock(&lock);
370                         mutex_unlock(&lock);
371                 ],[
372                         AC_MSG_RESULT(no)
373                 ],[
374                         AC_MSG_RESULT(yes)
375                         AC_MSG_ERROR([
376         *** Kernel built with CONFIG_DEBUG_LOCK_ALLOC which is incompatible
377         *** with the CDDL license.  You must rebuild your kernel without
378         *** this option enabled.])
379                 ])
380                 EXTRA_KCFLAGS="$tmp_flags"
381         ], [])
382 ])
383
384 dnl #
385 dnl # ZFS_LINUX_CONFTEST
386 dnl #
387 AC_DEFUN([ZFS_LINUX_CONFTEST], [
388 cat confdefs.h - <<_ACEOF >conftest.c
389 $1
390 _ACEOF
391 ])
392
393 dnl #
394 dnl # ZFS_LANG_PROGRAM(C)([PROLOGUE], [BODY])
395 dnl #
396 m4_define([ZFS_LANG_PROGRAM], [
397 $1
398 int
399 main (void)
400 {
401 dnl Do *not* indent the following line: there may be CPP directives.
402 dnl Don't move the `;' right after for the same reason.
403 $2
404   ;
405   return 0;
406 }
407 ])
408
409 dnl #
410 dnl # ZFS_LINUX_COMPILE_IFELSE / like AC_COMPILE_IFELSE
411 dnl #
412 AC_DEFUN([ZFS_LINUX_COMPILE_IFELSE], [
413         m4_ifvaln([$1], [ZFS_LINUX_CONFTEST([$1])])
414         rm -Rf build && mkdir -p build
415         echo "obj-m := conftest.o" >build/Makefile
416         AS_IF(
417                 [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])],
418                 [$4],
419                 [_AC_MSG_LOG_CONFTEST m4_ifvaln([$5],[$5])]
420         )
421         rm -Rf build
422 ])
423
424 dnl #
425 dnl # ZFS_LINUX_TRY_COMPILE like AC_TRY_COMPILE
426 dnl #
427 AC_DEFUN([ZFS_LINUX_TRY_COMPILE],
428         [ZFS_LINUX_COMPILE_IFELSE(
429         [AC_LANG_SOURCE([ZFS_LANG_PROGRAM([[$1]], [[$2]])])],
430         [modules],
431         [test -s build/conftest.o],
432         [$3], [$4])
433 ])
434
435 dnl #
436 dnl # ZFS_LINUX_CONFIG
437 dnl #
438 AC_DEFUN([ZFS_LINUX_CONFIG],
439         [AC_MSG_CHECKING([whether Linux was built with CONFIG_$1])
440         ZFS_LINUX_TRY_COMPILE([
441                 #include <linux/module.h>
442         ],[
443                 #ifndef CONFIG_$1
444                 #error CONFIG_$1 not #defined
445                 #endif
446         ],[
447                 AC_MSG_RESULT([yes])
448                 $2
449         ],[
450                 AC_MSG_RESULT([no])
451                 $3
452         ])
453 ])
454
455 dnl #
456 dnl # ZFS_CHECK_SYMBOL_EXPORT
457 dnl # check symbol exported or not
458 dnl #
459 AC_DEFUN([ZFS_CHECK_SYMBOL_EXPORT],
460         [AC_MSG_CHECKING([whether symbol $1 is exported])
461         grep -q -E '[[[:space:]]]$1[[[:space:]]]' \
462                 $LINUX_OBJ/$LINUX_SYMBOLS 2>/dev/null
463         rc=$?
464         AS_IF([test $rc -ne 0], [
465                 export=0
466                 for file in $2; do
467                         grep -q -E "EXPORT_SYMBOL.*($1)" "$LINUX/$file" 2>/dev/null
468                         rc=$?
469                         AS_IF([test $rc -eq 0], [
470                                 export=1
471                                 break;
472                         ])
473                 done
474                 AS_IF([test $export -eq 0], [
475                         AC_MSG_RESULT([no])
476                         $4
477                 ], [
478                         AC_MSG_RESULT([yes])
479                         $3
480                 ])
481         ], [
482                 AC_MSG_RESULT([yes])
483                 $3
484         ])
485 ])