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