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