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