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