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