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