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