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