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