]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - config/spl-build.m4
Merge branch 'cleanup'
[FreeBSD/FreeBSD.git] / config / spl-build.m4
1 ###############################################################################
2 # Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 # Copyright (C) 2007 The Regents of the University of California.
4 # Written by Brian Behlendorf <behlendorf1@llnl.gov>.
5 ###############################################################################
6 # SPL_AC_CONFIG_KERNEL: Default SPL kernel configuration.
7 ###############################################################################
8
9 AC_DEFUN([SPL_AC_CONFIG_KERNEL], [
10         SPL_AC_KERNEL
11
12         if test "${LINUX_OBJ}" != "${LINUX}"; then
13                 KERNELMAKE_PARAMS="$KERNELMAKE_PARAMS O=$LINUX_OBJ"
14         fi
15         AC_SUBST(KERNELMAKE_PARAMS)
16
17         KERNELCPPFLAGS="$KERNELCPPFLAGS -Wstrict-prototypes"
18         AC_SUBST(KERNELCPPFLAGS)
19
20         SPL_AC_DEBUG
21         SPL_AC_DEBUG_LOG
22         SPL_AC_DEBUG_KMEM
23         SPL_AC_DEBUG_KMEM_TRACKING
24         SPL_AC_TEST_MODULE
25         SPL_AC_ATOMIC_SPINLOCK
26         SPL_AC_SHRINKER_CALLBACK
27         SPL_AC_CTL_NAME
28         SPL_AC_PDE_DATA
29         SPL_AC_MUTEX_OWNER
30         SPL_AC_MUTEX_OWNER_TASK_STRUCT
31         SPL_AC_SET_FS_PWD_WITH_CONST
32         SPL_AC_2ARGS_VFS_UNLINK
33         SPL_AC_4ARGS_VFS_RENAME
34         SPL_AC_2ARGS_VFS_FSYNC
35         SPL_AC_INODE_TRUNCATE_RANGE
36         SPL_AC_FS_STRUCT_SPINLOCK
37         SPL_AC_KUIDGID_T
38         SPL_AC_PUT_TASK_STRUCT
39         SPL_AC_EXPORTED_RWSEM_IS_LOCKED
40         SPL_AC_KERNEL_FALLOCATE
41         SPL_AC_CONFIG_ZLIB_INFLATE
42         SPL_AC_CONFIG_ZLIB_DEFLATE
43         SPL_AC_2ARGS_ZLIB_DEFLATE_WORKSPACESIZE
44         SPL_AC_SHRINK_CONTROL_STRUCT
45         SPL_AC_RWSEM_SPINLOCK_IS_RAW
46         SPL_AC_SCHED_RT_HEADER
47         SPL_AC_2ARGS_VFS_GETATTR
48         SPL_AC_USLEEP_RANGE
49         SPL_AC_KMEM_CACHE_ALLOCFLAGS
50         SPL_AC_WAIT_ON_BIT
51 ])
52
53 AC_DEFUN([SPL_AC_MODULE_SYMVERS], [
54         modpost=$LINUX/scripts/Makefile.modpost
55         AC_MSG_CHECKING([kernel file name for module symbols])
56         if test "x$enable_linux_builtin" != xyes -a -f "$modpost"; then
57                 if grep -q Modules.symvers $modpost; then
58                         LINUX_SYMBOLS=Modules.symvers
59                 else
60                         LINUX_SYMBOLS=Module.symvers
61                 fi
62
63                 if ! test -f "$LINUX_OBJ/$LINUX_SYMBOLS"; then
64                         AC_MSG_ERROR([
65         *** Please make sure the kernel devel package for your distribution
66         *** is installed.  If you are building with a custom kernel, make sure the
67         *** kernel is configured, built, and the '--with-linux=PATH' configure
68         *** option refers to the location of the kernel source.])
69                 fi
70         else
71                 LINUX_SYMBOLS=NONE
72         fi
73         AC_MSG_RESULT($LINUX_SYMBOLS)
74         AC_SUBST(LINUX_SYMBOLS)
75 ])
76
77 AC_DEFUN([SPL_AC_KERNEL], [
78         AC_ARG_WITH([linux],
79                 AS_HELP_STRING([--with-linux=PATH],
80                 [Path to kernel source]),
81                 [kernelsrc="$withval"])
82
83         AC_ARG_WITH([linux-obj],
84                 AS_HELP_STRING([--with-linux-obj=PATH],
85                 [Path to kernel build objects]),
86                 [kernelbuild="$withval"])
87
88         AC_MSG_CHECKING([kernel source directory])
89         if test -z "$kernelsrc"; then
90                 if test -e "/lib/modules/$(uname -r)/source"; then
91                         headersdir="/lib/modules/$(uname -r)/source"
92                         sourcelink=$(readlink -f "$headersdir")
93                 elif test -e "/lib/modules/$(uname -r)/build"; then
94                         headersdir="/lib/modules/$(uname -r)/build"
95                         sourcelink=$(readlink -f "$headersdir")
96                 else
97                         sourcelink=$(ls -1d /usr/src/kernels/* \
98                                      /usr/src/linux-* \
99                                      2>/dev/null | grep -v obj | tail -1)
100                 fi
101
102                 if test -n "$sourcelink" && test -e ${sourcelink}; then
103                         kernelsrc=`readlink -f ${sourcelink}`
104                 else
105                         kernelsrc="[Not found]"
106                 fi
107         else
108                 if test "$kernelsrc" = "NONE"; then
109                         kernsrcver=NONE
110                 fi
111         fi
112
113         AC_MSG_RESULT([$kernelsrc])
114         if test ! -d "$kernelsrc"; then
115                 AC_MSG_ERROR([
116         *** Please make sure the kernel devel package for your distribution
117         *** is installed and 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
121         AC_MSG_CHECKING([kernel build directory])
122         if test -z "$kernelbuild"; then
123                 if test -e "/lib/modules/$(uname -r)/build"; then
124                         kernelbuild=`readlink -f /lib/modules/$(uname -r)/build`
125                 elif test -d ${kernelsrc}-obj/${target_cpu}/${target_cpu}; then
126                         kernelbuild=${kernelsrc}-obj/${target_cpu}/${target_cpu}
127                 elif test -d ${kernelsrc}-obj/${target_cpu}/default; then
128                         kernelbuild=${kernelsrc}-obj/${target_cpu}/default
129                 elif test -d `dirname ${kernelsrc}`/build-${target_cpu}; then
130                         kernelbuild=`dirname ${kernelsrc}`/build-${target_cpu}
131                 else
132                         kernelbuild=${kernelsrc}
133                 fi
134         fi
135         AC_MSG_RESULT([$kernelbuild])
136
137         AC_MSG_CHECKING([kernel source version])
138         utsrelease1=$kernelbuild/include/linux/version.h
139         utsrelease2=$kernelbuild/include/linux/utsrelease.h
140         utsrelease3=$kernelbuild/include/generated/utsrelease.h
141         if test -r $utsrelease1 && fgrep -q UTS_RELEASE $utsrelease1; then
142                 utsrelease=linux/version.h
143         elif test -r $utsrelease2 && fgrep -q UTS_RELEASE $utsrelease2; then
144                 utsrelease=linux/utsrelease.h
145         elif test -r $utsrelease3 && fgrep -q UTS_RELEASE $utsrelease3; then
146                 utsrelease=generated/utsrelease.h
147         fi
148
149         if test "$utsrelease"; then
150                 kernsrcver=`(echo "#include <$utsrelease>";
151                              echo "kernsrcver=UTS_RELEASE") | 
152                              cpp -I $kernelbuild/include |
153                              grep "^kernsrcver=" | cut -d \" -f 2`
154
155                 if test -z "$kernsrcver"; then
156                         AC_MSG_RESULT([Not found])
157                         AC_MSG_ERROR([*** Cannot determine kernel version.])
158                 fi
159         else
160                 AC_MSG_RESULT([Not found])
161                 if test "x$enable_linux_builtin" != xyes; then
162                         AC_MSG_ERROR([*** Cannot find UTS_RELEASE definition.])
163                 else
164                         AC_MSG_ERROR([
165         *** Cannot find UTS_RELEASE definition.
166         *** Please run 'make prepare' inside the kernel source tree.])
167                 fi
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         SPL_AC_MODULE_SYMVERS
181 ])
182
183 dnl #
184 dnl # Default SPL user configuration
185 dnl #
186 AC_DEFUN([SPL_AC_CONFIG_USER], [])
187
188 dnl #
189 dnl # Check for rpm+rpmbuild to build RPM packages.  If these tools
190 dnl # are missing, it is non-fatal, but you will not be able to build
191 dnl # RPM packages and will be warned if you try too.
192 dnl #
193 dnl # By default, the generic spec file will be used because it requires
194 dnl # minimal dependencies.  Distribution specific spec files can be
195 dnl # placed under the 'rpm/<distribution>' directory and enabled using
196 dnl # the --with-spec=<distribution> configure option.
197 dnl #
198 AC_DEFUN([SPL_AC_RPM], [
199         RPM=rpm
200         RPMBUILD=rpmbuild
201
202         AC_MSG_CHECKING([whether $RPM is available])
203         AS_IF([tmp=$($RPM --version 2>/dev/null)], [
204                 RPM_VERSION=$(echo $tmp | $AWK '/RPM/ { print $[3] }')
205                 HAVE_RPM=yes
206                 AC_MSG_RESULT([$HAVE_RPM ($RPM_VERSION)])
207         ],[
208                 HAVE_RPM=no
209                 AC_MSG_RESULT([$HAVE_RPM])
210         ])
211
212         AC_MSG_CHECKING([whether $RPMBUILD is available])
213         AS_IF([tmp=$($RPMBUILD --version 2>/dev/null)], [
214                 RPMBUILD_VERSION=$(echo $tmp | $AWK '/RPM/ { print $[3] }')
215                 HAVE_RPMBUILD=yes
216                 AC_MSG_RESULT([$HAVE_RPMBUILD ($RPMBUILD_VERSION)])
217         ],[
218                 HAVE_RPMBUILD=no
219                 AC_MSG_RESULT([$HAVE_RPMBUILD])
220         ])
221
222         RPM_DEFINE_COMMON='--define "$(DEBUG_SPL) 1" --define "$(DEBUG_LOG) 1" --define "$(DEBUG_KMEM) 1" --define "$(DEBUG_KMEM_TRACKING) 1"'
223         RPM_DEFINE_UTIL=
224         RPM_DEFINE_KMOD='--define "kernels $(LINUX_VERSION)"'
225         RPM_DEFINE_DKMS=
226
227         SRPM_DEFINE_COMMON='--define "build_src_rpm 1"'
228         SRPM_DEFINE_UTIL=
229         SRPM_DEFINE_KMOD=
230         SRPM_DEFINE_DKMS=
231
232         RPM_SPEC_DIR="rpm/generic"
233         AC_ARG_WITH([spec],
234                 AS_HELP_STRING([--with-spec=SPEC],
235                 [Spec files 'generic|fedora']),
236                 [RPM_SPEC_DIR="rpm/$withval"])
237
238         AC_MSG_CHECKING([whether spec files are available])
239         AC_MSG_RESULT([yes ($RPM_SPEC_DIR/*.spec.in)])
240
241         AC_SUBST(HAVE_RPM)
242         AC_SUBST(RPM)
243         AC_SUBST(RPM_VERSION)
244
245         AC_SUBST(HAVE_RPMBUILD)
246         AC_SUBST(RPMBUILD)
247         AC_SUBST(RPMBUILD_VERSION)
248
249         AC_SUBST(RPM_SPEC_DIR)
250         AC_SUBST(RPM_DEFINE_UTIL)
251         AC_SUBST(RPM_DEFINE_KMOD)
252         AC_SUBST(RPM_DEFINE_DKMS)
253         AC_SUBST(RPM_DEFINE_COMMON)
254         AC_SUBST(SRPM_DEFINE_UTIL)
255         AC_SUBST(SRPM_DEFINE_KMOD)
256         AC_SUBST(SRPM_DEFINE_DKMS)
257         AC_SUBST(SRPM_DEFINE_COMMON)
258 ])
259
260 dnl #
261 dnl # Check for dpkg+dpkg-buildpackage to build DEB packages.  If these
262 dnl # tools are missing it is non-fatal but you will not be able to build
263 dnl # DEB packages and will be warned if you try too.
264 dnl #
265 AC_DEFUN([SPL_AC_DPKG], [
266         DPKG=dpkg
267         DPKGBUILD=dpkg-buildpackage
268
269         AC_MSG_CHECKING([whether $DPKG is available])
270         AS_IF([tmp=$($DPKG --version 2>/dev/null)], [
271                 DPKG_VERSION=$(echo $tmp | $AWK '/Debian/ { print $[7] }')
272                 HAVE_DPKG=yes
273                 AC_MSG_RESULT([$HAVE_DPKG ($DPKG_VERSION)])
274         ],[
275                 HAVE_DPKG=no
276                 AC_MSG_RESULT([$HAVE_DPKG])
277         ])
278
279         AC_MSG_CHECKING([whether $DPKGBUILD is available])
280         AS_IF([tmp=$($DPKGBUILD --version 2>/dev/null)], [
281                 DPKGBUILD_VERSION=$(echo $tmp | \
282                     $AWK '/Debian/ { print $[4] }' | cut -f-4 -d'.')
283                 HAVE_DPKGBUILD=yes
284                 AC_MSG_RESULT([$HAVE_DPKGBUILD ($DPKGBUILD_VERSION)])
285         ],[
286                 HAVE_DPKGBUILD=no
287                 AC_MSG_RESULT([$HAVE_DPKGBUILD])
288         ])
289
290         AC_SUBST(HAVE_DPKG)
291         AC_SUBST(DPKG)
292         AC_SUBST(DPKG_VERSION)
293
294         AC_SUBST(HAVE_DPKGBUILD)
295         AC_SUBST(DPKGBUILD)
296         AC_SUBST(DPKGBUILD_VERSION)
297 ])
298
299 dnl #
300 dnl # Until native packaging for various different packing systems
301 dnl # can be added the least we can do is attempt to use alien to
302 dnl # convert the RPM packages to the needed package type.  This is
303 dnl # a hack but so far it has worked reasonable well.
304 dnl #
305 AC_DEFUN([SPL_AC_ALIEN], [
306         ALIEN=alien
307
308         AC_MSG_CHECKING([whether $ALIEN is available])
309         AS_IF([tmp=$($ALIEN --version 2>/dev/null)], [
310                 ALIEN_VERSION=$(echo $tmp | $AWK '{ print $[3] }')
311                 HAVE_ALIEN=yes
312                 AC_MSG_RESULT([$HAVE_ALIEN ($ALIEN_VERSION)])
313         ],[
314                 HAVE_ALIEN=no
315                 AC_MSG_RESULT([$HAVE_ALIEN])
316         ])
317
318         AC_SUBST(HAVE_ALIEN)
319         AC_SUBST(ALIEN)
320         AC_SUBST(ALIEN_VERSION)
321 ])
322
323 dnl #
324 dnl # Using the VENDOR tag from config.guess set the default
325 dnl # package type for 'make pkg': (rpm | deb | tgz)
326 dnl #
327 AC_DEFUN([SPL_AC_DEFAULT_PACKAGE], [
328         AC_MSG_CHECKING([linux distribution])
329         if test -f /etc/toss-release ; then
330                 VENDOR=toss ;
331         elif test -f /etc/fedora-release ; then
332                 VENDOR=fedora ;
333         elif test -f /etc/redhat-release ; then
334                 VENDOR=redhat ;
335         elif test -f /etc/gentoo-release ; then
336                 VENDOR=gentoo ;
337         elif test -f /etc/arch-release ; then
338                 VENDOR=arch ;
339         elif test -f /etc/SuSE-release ; then
340                 VENDOR=sles ;
341         elif test -f /etc/slackware-version ; then
342                 VENDOR=slackware ;
343         elif test -f /etc/lunar.release ; then
344                 VENDOR=lunar ;
345         elif test -f /etc/lsb-release ; then
346                 VENDOR=ubuntu ;
347         elif test -f /etc/debian_version ; then
348                 VENDOR=debian ;
349         else
350                 VENDOR= ;
351         fi
352         AC_MSG_RESULT([$VENDOR])
353         AC_SUBST(VENDOR)
354
355         AC_MSG_CHECKING([default package type])
356         case "$VENDOR" in
357                 toss)       DEFAULT_PACKAGE=rpm  ;;
358                 redhat)     DEFAULT_PACKAGE=rpm  ;;
359                 fedora)     DEFAULT_PACKAGE=rpm  ;;
360                 gentoo)     DEFAULT_PACKAGE=tgz  ;;
361                 arch)       DEFAULT_PACKAGE=tgz  ;;
362                 sles)       DEFAULT_PACKAGE=rpm  ;;
363                 slackware)  DEFAULT_PACKAGE=tgz  ;;
364                 lunar)      DEFAULT_PACKAGE=tgz  ;;
365                 ubuntu)     DEFAULT_PACKAGE=deb  ;;
366                 debian)     DEFAULT_PACKAGE=deb  ;;
367                 *)          DEFAULT_PACKAGE=rpm  ;;
368         esac
369
370         AC_MSG_RESULT([$DEFAULT_PACKAGE])
371         AC_SUBST(DEFAULT_PACKAGE)
372 ])
373
374 dnl #
375 dnl # Default SPL user configuration
376 dnl #
377 AC_DEFUN([SPL_AC_PACKAGE], [
378         SPL_AC_DEFAULT_PACKAGE
379         SPL_AC_RPM
380         SPL_AC_DPKG
381         SPL_AC_ALIEN
382 ])
383
384 AC_DEFUN([SPL_AC_LICENSE], [
385         AC_MSG_CHECKING([spl author])
386         AC_MSG_RESULT([$SPL_META_AUTHOR])
387
388         AC_MSG_CHECKING([spl license])
389         AC_MSG_RESULT([$SPL_META_LICENSE])
390 ])
391
392 AC_DEFUN([SPL_AC_CONFIG], [
393         SPL_CONFIG=all
394         AC_ARG_WITH([config],
395                 AS_HELP_STRING([--with-config=CONFIG],
396                 [Config file 'kernel|user|all|srpm']),
397                 [SPL_CONFIG="$withval"])
398         AC_ARG_ENABLE([linux-builtin],
399                 [AC_HELP_STRING([--enable-linux-builtin],
400                 [Configure for builtin in-tree kernel modules @<:@default=no@:>@])],
401                 [],
402                 [enable_linux_builtin=no])
403
404         AC_MSG_CHECKING([spl config])
405         AC_MSG_RESULT([$SPL_CONFIG]);
406         AC_SUBST(SPL_CONFIG)
407
408         case "$SPL_CONFIG" in
409                 kernel) SPL_AC_CONFIG_KERNEL ;;
410                 user)   SPL_AC_CONFIG_USER   ;;
411                 all)    SPL_AC_CONFIG_KERNEL
412                         SPL_AC_CONFIG_USER   ;;
413                 srpm)                        ;;
414                 *)
415                 AC_MSG_RESULT([Error!])
416                 AC_MSG_ERROR([Bad value "$SPL_CONFIG" for --with-config,
417                              user kernel|user|all|srpm]) ;;
418         esac
419
420         AM_CONDITIONAL([CONFIG_USER],
421                        [test "$SPL_CONFIG" = user -o "$SPL_CONFIG" = all])
422         AM_CONDITIONAL([CONFIG_KERNEL],
423                        [test "$SPL_CONFIG" = kernel -o "$SPL_CONFIG" = all] &&
424                        [test "x$enable_linux_builtin" != xyes ])
425 ])
426
427 dnl #
428 dnl # Enable if the SPL should be compiled with internal debugging enabled.
429 dnl # By default this support is disabled.
430 dnl #
431 AC_DEFUN([SPL_AC_DEBUG], [
432         AC_MSG_CHECKING([whether debugging is enabled])
433         AC_ARG_ENABLE([debug],
434                 [AS_HELP_STRING([--enable-debug],
435                 [Enable generic debug support @<:@default=no@:>@])],
436                 [],
437                 [enable_debug=no])
438
439         AS_IF([test "x$enable_debug" = xyes],
440         [
441                 KERNELCPPFLAGS="${KERNELCPPFLAGS} -DDEBUG -Werror"
442                 DEBUG_CFLAGS="-DDEBUG -Werror"
443                 DEBUG_SPL="_with_debug"
444         ], [
445                 KERNELCPPFLAGS="${KERNELCPPFLAGS} -DNDEBUG"
446                 DEBUG_CFLAGS="-DNDEBUG"
447                 DEBUG_SPL="_without_debug"
448         ])
449
450         AC_SUBST(DEBUG_CFLAGS)
451         AC_SUBST(DEBUG_SPL)
452         AC_MSG_RESULT([$enable_debug])
453 ])
454
455 dnl #
456 dnl # Enabled by default it provides a basic debug log infrastructure.
457 dnl # Each subsystem registers itself with a name and logs messages
458 dnl # using predefined types.  If the debug mask it set to allow the
459 dnl # message type it will be written to the internal log.  The log
460 dnl # can be dumped to a file by echoing 1 to the 'dump' proc entry,
461 dnl # after dumping the log it must be decoded using the spl utility.
462 dnl #
463 dnl # echo 1 >/proc/sys/kernel/spl/debug/dump
464 dnl # spl /tmp/spl-log.xxx.yyy /tmp/spl-log.xxx.yyy.txt
465 dnl #
466 AC_DEFUN([SPL_AC_DEBUG_LOG], [
467         AC_ARG_ENABLE([debug-log],
468                 [AS_HELP_STRING([--enable-debug-log],
469                 [Enable basic debug logging @<:@default=yes@:>@])],
470                 [],
471                 [enable_debug_log=yes])
472
473         AS_IF([test "x$enable_debug_log" = xyes],
474         [
475                 KERNELCPPFLAGS="${KERNELCPPFLAGS} -DDEBUG_LOG"
476                 DEBUG_LOG="_with_debug_log"
477                 AC_DEFINE([DEBUG_LOG], [1],
478                 [Define to 1 to enable basic debug logging])
479         ], [
480                 DEBUG_LOG="_without_debug_log"
481         ])
482
483         AC_SUBST(DEBUG_LOG)
484         AC_MSG_CHECKING([whether basic debug logging is enabled])
485         AC_MSG_RESULT([$enable_debug_log])
486 ])
487
488 dnl #
489 dnl # Enabled by default it provides a minimal level of memory tracking.
490 dnl # A total count of bytes allocated is kept for each alloc and free.
491 dnl # Then at module unload time a report to the console will be printed
492 dnl # if memory was leaked.  Additionally, /proc/spl/kmem/slab will exist
493 dnl # and provide an easy way to inspect the kmem based slab.
494 dnl #
495 AC_DEFUN([SPL_AC_DEBUG_KMEM], [
496         AC_ARG_ENABLE([debug-kmem],
497                 [AS_HELP_STRING([--enable-debug-kmem],
498                 [Enable basic kmem accounting @<:@default=yes@:>@])],
499                 [],
500                 [enable_debug_kmem=yes])
501
502         AS_IF([test "x$enable_debug_kmem" = xyes],
503         [
504                 KERNELCPPFLAGS="${KERNELCPPFLAGS} -DDEBUG_KMEM"
505                 DEBUG_KMEM="_with_debug_kmem"
506                 AC_DEFINE([DEBUG_KMEM], [1],
507                 [Define to 1 to enable basic kmem accounting])
508         ], [
509                 DEBUG_KMEM="_without_debug_kmem"
510         ])
511
512         AC_SUBST(DEBUG_KMEM)
513         AC_MSG_CHECKING([whether basic kmem accounting is enabled])
514         AC_MSG_RESULT([$enable_debug_kmem])
515 ])
516
517 dnl #
518 dnl # Disabled by default it provides detailed memory tracking.  This
519 dnl # feature also requires --enable-debug-kmem to be set.  When enabled
520 dnl # not only will total bytes be tracked but also the location of every
521 dnl # alloc and free.  When the SPL module is unloaded a list of all leaked
522 dnl # addresses and where they were allocated will be dumped to the console.
523 dnl # Enabling this feature has a significant impact on performance but it
524 dnl # makes finding memory leaks pretty straight forward.
525 dnl #
526 AC_DEFUN([SPL_AC_DEBUG_KMEM_TRACKING], [
527         AC_ARG_ENABLE([debug-kmem-tracking],
528                 [AS_HELP_STRING([--enable-debug-kmem-tracking],
529                 [Enable detailed kmem tracking  @<:@default=no@:>@])],
530                 [],
531                 [enable_debug_kmem_tracking=no])
532
533         AS_IF([test "x$enable_debug_kmem_tracking" = xyes],
534         [
535                 KERNELCPPFLAGS="${KERNELCPPFLAGS} -DDEBUG_KMEM_TRACKING"
536                 DEBUG_KMEM_TRACKING="_with_debug_kmem_tracking"
537                 AC_DEFINE([DEBUG_KMEM_TRACKING], [1],
538                 [Define to 1 to enable detailed kmem tracking])
539         ], [
540                 DEBUG_KMEM_TRACKING="_without_debug_kmem_tracking"
541         ])
542
543         AC_SUBST(DEBUG_KMEM_TRACKING)
544         AC_MSG_CHECKING([whether detailed kmem tracking is enabled])
545         AC_MSG_RESULT([$enable_debug_kmem_tracking])
546 ])
547
548 dnl #
549 dnl # SPL_LINUX_CONFTEST
550 dnl #
551 AC_DEFUN([SPL_LINUX_CONFTEST], [
552 cat confdefs.h - <<_ACEOF >conftest.c
553 $1
554 _ACEOF
555 ])
556
557 dnl #
558 dnl # SPL_LANG_PROGRAM(C)([PROLOGUE], [BODY])
559 dnl #
560 m4_define([SPL_LANG_PROGRAM], [
561 $1
562 int
563 main (void)
564 {
565 dnl Do *not* indent the following line: there may be CPP directives.
566 dnl Don't move the `;' right after for the same reason.
567 $2
568   ;
569   return 0;
570 }
571 ])
572
573 dnl #
574 dnl # SPL_LINUX_COMPILE_IFELSE / like AC_COMPILE_IFELSE
575 dnl #
576 AC_DEFUN([SPL_LINUX_COMPILE_IFELSE], [
577         m4_ifvaln([$1], [SPL_LINUX_CONFTEST([$1])])
578         rm -Rf build && mkdir -p build && touch build/conftest.mod.c
579         echo "obj-m := conftest.o" >build/Makefile
580         modpost_flag=''
581         test "x$enable_linux_builtin" = xyes && modpost_flag='modpost=true' # fake modpost stage
582         AS_IF(
583                 [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 $modpost_flag) >/dev/null && AC_TRY_COMMAND([$3])],
584                 [$4],
585                 [_AC_MSG_LOG_CONFTEST m4_ifvaln([$5],[$5])]
586         )
587         rm -Rf build
588 ])
589
590 dnl #
591 dnl # SPL_LINUX_TRY_COMPILE like AC_TRY_COMPILE
592 dnl #
593 AC_DEFUN([SPL_LINUX_TRY_COMPILE],
594         [SPL_LINUX_COMPILE_IFELSE(
595         [AC_LANG_SOURCE([SPL_LANG_PROGRAM([[$1]], [[$2]])])],
596         [modules],
597         [test -s build/conftest.o],
598         [$3], [$4])
599 ])
600
601 dnl #
602 dnl # SPL_CHECK_SYMBOL_EXPORT
603 dnl # check symbol exported or not
604 dnl #
605 AC_DEFUN([SPL_CHECK_SYMBOL_EXPORT], [
606         grep -q -E '[[[:space:]]]$1[[[:space:]]]' \
607                 $LINUX_OBJ/Module*.symvers 2>/dev/null
608         rc=$?
609         if test $rc -ne 0; then
610                 export=0
611                 for file in $2; do
612                         grep -q -E "EXPORT_SYMBOL.*($1)" \
613                                 "$LINUX_OBJ/$file" 2>/dev/null
614                         rc=$?
615                         if test $rc -eq 0; then
616                                 export=1
617                                 break;
618                         fi
619                 done
620                 if test $export -eq 0; then :
621                         $4
622                 else :
623                         $3
624                 fi
625         else :
626                 $3
627         fi
628 ])
629
630 dnl #
631 dnl # SPL_LINUX_TRY_COMPILE_SYMBOL
632 dnl # like SPL_LINUX_TRY_COMPILE, except SPL_CHECK_SYMBOL_EXPORT
633 dnl # is called if not compiling for builtin
634 dnl #
635 AC_DEFUN([SPL_LINUX_TRY_COMPILE_SYMBOL], [
636         SPL_LINUX_TRY_COMPILE([$1], [$2], [rc=0], [rc=1])
637         if test $rc -ne 0; then :
638                 $6
639         else
640                 if test "x$enable_linux_builtin" != xyes; then
641                         SPL_CHECK_SYMBOL_EXPORT([$3], [$4], [rc=0], [rc=1])
642                 fi
643                 if test $rc -ne 0; then :
644                         $6
645                 else :
646                         $5
647                 fi
648         fi
649 ])
650
651 dnl #
652 dnl # SPL_CHECK_SYMBOL_HEADER
653 dnl # check if a symbol prototype is defined in listed headers.
654 dnl #
655 AC_DEFUN([SPL_CHECK_SYMBOL_HEADER], [
656         AC_MSG_CHECKING([whether symbol $1 exists in header])
657         header=0
658         for file in $3; do
659                 grep -q "$2" "$LINUX/$file" 2>/dev/null
660                 rc=$?
661                 if test $rc -eq 0; then
662                         header=1
663                         break;
664                 fi
665         done
666         if test $header -eq 0; then
667                 AC_MSG_RESULT([no])
668                 $5
669         else
670                 AC_MSG_RESULT([yes])
671                 $4
672         fi
673 ])
674
675 dnl #
676 dnl # SPL_CHECK_HEADER
677 dnl # check whether header exists and define HAVE_$2_HEADER
678 dnl #
679 AC_DEFUN([SPL_CHECK_HEADER],
680         [AC_MSG_CHECKING([whether header $1 exists])
681         SPL_LINUX_TRY_COMPILE([
682                 #include <$1>
683         ],[
684                 return 0;
685         ],[
686                 AC_DEFINE(HAVE_$2_HEADER, 1, [$1 exists])
687                 AC_MSG_RESULT(yes)
688                 $3
689         ],[
690                 AC_MSG_RESULT(no)
691                 $4
692         ])
693 ])
694
695 dnl #
696 dnl # Basic toolchain sanity check.  Verify that kernel modules can
697 dnl # be built and which symbols can be used.
698 dnl #
699 AC_DEFUN([SPL_AC_TEST_MODULE],
700         [AC_MSG_CHECKING([whether modules can be built])
701         SPL_LINUX_TRY_COMPILE([],[],[
702                 AC_MSG_RESULT([yes])
703         ],[
704                 AC_MSG_RESULT([no])
705                 if test "x$enable_linux_builtin" != xyes; then
706                         AC_MSG_ERROR([*** Unable to build an empty module.])
707                 else
708                         AC_MSG_ERROR([
709         *** Unable to build an empty module.
710         *** Please run 'make scripts' inside the kernel source tree.])
711                 fi
712         ])
713
714         AC_RUN_IFELSE([
715                 AC_LANG_PROGRAM([
716                         #include "$LINUX/include/linux/license.h"
717                 ], [
718                         return !license_is_gpl_compatible("$SPL_META_LICENSE");
719                 ])
720         ], [
721                 AC_DEFINE([SPL_IS_GPL_COMPATIBLE], [1],
722                     [Define to 1 if GPL-only symbols can be used])
723         ], [
724         ])
725 ])
726
727 dnl #
728 dnl # Use the atomic implemenation based on global spinlocks.  This
729 dnl # should only be needed by 32-bit kernels which do not provide
730 dnl # the atomic64_* API.  It may be optionally enabled as a fallback
731 dnl # if problems are observed with the direct mapping to the native
732 dnl # Linux atomic operations.  You may not disable atomic spinlocks
733 dnl # if you kernel does not an atomic64_* API.
734 dnl #
735 AC_DEFUN([SPL_AC_ATOMIC_SPINLOCK], [
736         AC_ARG_ENABLE([atomic-spinlocks],
737                 [AS_HELP_STRING([--enable-atomic-spinlocks],
738                 [Atomic types use spinlocks @<:@default=check@:>@])],
739                 [],
740                 [enable_atomic_spinlocks=check])
741
742         SPL_LINUX_TRY_COMPILE([
743                 #include <linux/fs.h>
744         ],[
745                 atomic64_t *ptr __attribute__ ((unused));
746         ],[
747                 have_atomic64_t=yes
748                 AC_DEFINE(HAVE_ATOMIC64_T, 1,
749                         [kernel defines atomic64_t])
750         ],[
751                 have_atomic64_t=no
752         ])
753
754         AS_IF([test "x$enable_atomic_spinlocks" = xcheck], [
755                 AS_IF([test "x$have_atomic64_t" = xyes], [
756                         enable_atomic_spinlocks=no
757                 ],[
758                         enable_atomic_spinlocks=yes
759                 ])
760         ])
761
762         AS_IF([test "x$enable_atomic_spinlocks" = xyes], [
763                 AC_DEFINE([ATOMIC_SPINLOCK], [1],
764                         [Atomic types use spinlocks])
765         ],[
766                 AS_IF([test "x$have_atomic64_t" = xno], [
767                         AC_MSG_FAILURE(
768                         [--disable-atomic-spinlocks given but required atomic64 support is unavailable])
769                 ])
770         ])
771
772         AC_MSG_CHECKING([whether atomic types use spinlocks])
773         AC_MSG_RESULT([$enable_atomic_spinlocks])
774
775         AC_MSG_CHECKING([whether kernel defines atomic64_t])
776         AC_MSG_RESULT([$have_atomic64_t])
777 ])
778
779 AC_DEFUN([SPL_AC_SHRINKER_CALLBACK],[
780         tmp_flags="$EXTRA_KCFLAGS"
781         EXTRA_KCFLAGS="-Werror"
782         dnl #
783         dnl # 2.6.23 to 2.6.34 API change
784         dnl # ->shrink(int nr_to_scan, gfp_t gfp_mask)
785         dnl #
786         AC_MSG_CHECKING([whether old 2-argument shrinker exists])
787         SPL_LINUX_TRY_COMPILE([
788                 #include <linux/mm.h>
789
790                 int shrinker_cb(int nr_to_scan, gfp_t gfp_mask);
791         ],[
792                 struct shrinker cache_shrinker = {
793                         .shrink = shrinker_cb,
794                         .seeks = DEFAULT_SEEKS,
795                 };
796                 register_shrinker(&cache_shrinker);
797         ],[
798                 AC_MSG_RESULT(yes)
799                 AC_DEFINE(HAVE_2ARGS_OLD_SHRINKER_CALLBACK, 1,
800                         [old shrinker callback wants 2 args])
801         ],[
802                 AC_MSG_RESULT(no)
803                 dnl #
804                 dnl # 2.6.35 - 2.6.39 API change
805                 dnl # ->shrink(struct shrinker *,
806                 dnl #          int nr_to_scan, gfp_t gfp_mask)
807                 dnl #
808                 AC_MSG_CHECKING([whether old 3-argument shrinker exists])
809                 SPL_LINUX_TRY_COMPILE([
810                         #include <linux/mm.h>
811
812                         int shrinker_cb(struct shrinker *, int nr_to_scan,
813                                         gfp_t gfp_mask);
814                 ],[
815                         struct shrinker cache_shrinker = {
816                                 .shrink = shrinker_cb,
817                                 .seeks = DEFAULT_SEEKS,
818                         };
819                         register_shrinker(&cache_shrinker);
820                 ],[
821                         AC_MSG_RESULT(yes)
822                         AC_DEFINE(HAVE_3ARGS_SHRINKER_CALLBACK, 1,
823                                 [old shrinker callback wants 3 args])
824                 ],[
825                         AC_MSG_RESULT(no)
826                         dnl #
827                         dnl # 3.0 - 3.11 API change
828                         dnl # ->shrink(struct shrinker *,
829                         dnl #          struct shrink_control *sc)
830                         dnl #
831                         AC_MSG_CHECKING(
832                                 [whether new 2-argument shrinker exists])
833                         SPL_LINUX_TRY_COMPILE([
834                                 #include <linux/mm.h>
835
836                                 int shrinker_cb(struct shrinker *,
837                                                 struct shrink_control *sc);
838                         ],[
839                                 struct shrinker cache_shrinker = {
840                                         .shrink = shrinker_cb,
841                                         .seeks = DEFAULT_SEEKS,
842                                 };
843                                 register_shrinker(&cache_shrinker);
844                         ],[
845                                 AC_MSG_RESULT(yes)
846                                 AC_DEFINE(HAVE_2ARGS_NEW_SHRINKER_CALLBACK, 1,
847                                         [new shrinker callback wants 2 args])
848                         ],[
849                                 AC_MSG_RESULT(no)
850                                 dnl #
851                                 dnl # 3.12 API change,
852                                 dnl # ->shrink() is logically split in to
853                                 dnl # ->count_objects() and ->scan_objects()
854                                 dnl #
855                                 AC_MSG_CHECKING(
856                                     [whether ->count_objects callback exists])
857                                 SPL_LINUX_TRY_COMPILE([
858                                         #include <linux/mm.h>
859
860                                         unsigned long shrinker_cb(
861                                                 struct shrinker *,
862                                                 struct shrink_control *sc);
863                                 ],[
864                                         struct shrinker cache_shrinker = {
865                                                 .count_objects = shrinker_cb,
866                                                 .scan_objects = shrinker_cb,
867                                                 .seeks = DEFAULT_SEEKS,
868                                         };
869                                         register_shrinker(&cache_shrinker);
870                                 ],[
871                                         AC_MSG_RESULT(yes)
872                                         AC_DEFINE(HAVE_SPLIT_SHRINKER_CALLBACK,
873                                                 1, [->count_objects exists])
874                                 ],[
875                                         AC_MSG_ERROR(error)
876                                 ])
877                         ])
878                 ])
879         ])
880         EXTRA_KCFLAGS="$tmp_flags"
881 ])
882
883 dnl #
884 dnl # 2.6.33 API change,
885 dnl # Removed .ctl_name from struct ctl_table.
886 dnl #
887 AC_DEFUN([SPL_AC_CTL_NAME], [
888         AC_MSG_CHECKING([whether struct ctl_table has ctl_name])
889         SPL_LINUX_TRY_COMPILE([
890                 #include <linux/sysctl.h>
891         ],[
892                 struct ctl_table ctl __attribute__ ((unused));
893                 ctl.ctl_name = 0;
894         ],[
895                 AC_MSG_RESULT(yes)
896                 AC_DEFINE(HAVE_CTL_NAME, 1, [struct ctl_table has ctl_name])
897         ],[
898                 AC_MSG_RESULT(no)
899         ])
900 ])
901
902 dnl #
903 dnl # 2.6.29 API change,
904 dnl # Adaptive mutexs were introduced which track the mutex owner.  The
905 dnl # mutex wrappers leverage this functionality to avoid tracking the
906 dnl # owner multipe times.
907 dnl #
908 AC_DEFUN([SPL_AC_MUTEX_OWNER], [
909         AC_MSG_CHECKING([whether struct mutex has owner])
910         SPL_LINUX_TRY_COMPILE([
911                 #include <linux/mutex.h>
912         ],[
913                 struct mutex mtx __attribute__ ((unused));
914                 mtx.owner = NULL;
915         ],[
916                 AC_MSG_RESULT(yes)
917                 AC_DEFINE(HAVE_MUTEX_OWNER, 1, [struct mutex has owner])
918         ],[
919                 AC_MSG_RESULT(no)
920         ])
921 ])
922
923 dnl #
924 dnl # 2.6.39 API change,
925 dnl # Owner type change.  A Linux mutex prior to 2.6.39 would store
926 dnl # the owner as a thread_info pointer when CONFIG_DEBUG_MUTEXES
927 dnl # was defined.  As of 2.6.39 this was changed to a task_struct
928 dnl # pointer which frankly makes a lot more sense.
929 dnl #
930 AC_DEFUN([SPL_AC_MUTEX_OWNER_TASK_STRUCT], [
931         AC_MSG_CHECKING([whether struct mutex owner is a task_struct])
932         tmp_flags="$EXTRA_KCFLAGS"
933         EXTRA_KCFLAGS="-Werror"
934         SPL_LINUX_TRY_COMPILE([
935                 #include <linux/mutex.h>
936                 #include <linux/sched.h>
937         ],[
938                 struct mutex mtx __attribute__ ((unused));
939                 mtx.owner = current;
940         ],[
941                 AC_MSG_RESULT(yes)
942                 AC_DEFINE(HAVE_MUTEX_OWNER_TASK_STRUCT, 1,
943                 [struct mutex owner is a task_struct])
944         ],[
945                 AC_MSG_RESULT(no)
946         ])
947         EXTRA_KCFLAGS="$tmp_flags"
948 ])
949
950 dnl #
951 dnl # 3.10 API change,
952 dnl # PDE is replaced by PDE_DATA
953 dnl #
954 AC_DEFUN([SPL_AC_PDE_DATA], [
955         AC_MSG_CHECKING([whether PDE_DATA() is available])
956         SPL_LINUX_TRY_COMPILE_SYMBOL([
957                 #include <linux/proc_fs.h>
958         ], [
959                 PDE_DATA(NULL);
960         ], [PDE_DATA], [], [
961                 AC_MSG_RESULT(yes)
962                 AC_DEFINE(HAVE_PDE_DATA, 1, [yes])
963         ],[
964                 AC_MSG_RESULT(no)
965         ])
966 ])
967
968 dnl #
969 dnl # 3.9 API change
970 dnl # set_fs_pwd takes const struct path *
971 dnl #
972 AC_DEFUN([SPL_AC_SET_FS_PWD_WITH_CONST],
973         tmp_flags="$EXTRA_KCFLAGS"
974         EXTRA_KCFLAGS="-Werror"
975         [AC_MSG_CHECKING([whether set_fs_pwd() requires const struct path *])
976         SPL_LINUX_TRY_COMPILE([
977                 #include <linux/spinlock.h>
978                 #include <linux/fs_struct.h>
979                 #include <linux/path.h>
980                 void (*const set_fs_pwd_func)
981                         (struct fs_struct *, const struct path *)
982                         = set_fs_pwd;
983         ],[
984                 return 0;
985         ],[
986                 AC_MSG_RESULT(yes)
987                 AC_DEFINE(HAVE_SET_FS_PWD_WITH_CONST, 1,
988                         [set_fs_pwd() needs const path *])
989         ],[
990                 SPL_LINUX_TRY_COMPILE([
991                         #include <linux/spinlock.h>
992                         #include <linux/fs_struct.h>
993                         #include <linux/path.h>
994                         void (*const set_fs_pwd_func)
995                                 (struct fs_struct *, struct path *)
996                                 = set_fs_pwd;
997                 ],[
998                         return 0;
999                 ],[
1000                         AC_MSG_RESULT(no)
1001                 ],[
1002                         AC_MSG_ERROR(unknown)
1003                 ])
1004         ])
1005         EXTRA_KCFLAGS="$tmp_flags"
1006 ])
1007
1008 dnl #
1009 dnl # 3.13 API change
1010 dnl # vfs_unlink() updated to take a third delegated_inode argument.
1011 dnl #
1012 AC_DEFUN([SPL_AC_2ARGS_VFS_UNLINK],
1013         [AC_MSG_CHECKING([whether vfs_unlink() wants 2 args])
1014         SPL_LINUX_TRY_COMPILE([
1015                 #include <linux/fs.h>
1016         ],[
1017                 vfs_unlink((struct inode *) NULL, (struct dentry *) NULL);
1018         ],[
1019                 AC_MSG_RESULT(yes)
1020                 AC_DEFINE(HAVE_2ARGS_VFS_UNLINK, 1,
1021                           [vfs_unlink() wants 2 args])
1022         ],[
1023                 AC_MSG_RESULT(no)
1024                 dnl #
1025                 dnl # Linux 3.13 API change
1026                 dnl # Added delegated inode
1027                 dnl #
1028                 AC_MSG_CHECKING([whether vfs_unlink() wants 3 args])
1029                 SPL_LINUX_TRY_COMPILE([
1030                         #include <linux/fs.h>
1031                 ],[
1032                         vfs_unlink((struct inode *) NULL,
1033                                 (struct dentry *) NULL,
1034                                 (struct inode **) NULL);
1035                 ],[
1036                         AC_MSG_RESULT(yes)
1037                         AC_DEFINE(HAVE_3ARGS_VFS_UNLINK, 1,
1038                                   [vfs_unlink() wants 3 args])
1039                 ],[
1040                         AC_MSG_ERROR(no)
1041                 ])
1042
1043         ])
1044 ])
1045
1046 dnl #
1047 dnl # 3.13 and 3.15 API changes
1048 dnl # Added delegated inode and flags argument.
1049 dnl #
1050 AC_DEFUN([SPL_AC_4ARGS_VFS_RENAME],
1051         [AC_MSG_CHECKING([whether vfs_rename() wants 4 args])
1052         SPL_LINUX_TRY_COMPILE([
1053                 #include <linux/fs.h>
1054         ],[
1055                 vfs_rename((struct inode *) NULL, (struct dentry *) NULL,
1056                         (struct inode *) NULL, (struct dentry *) NULL);
1057         ],[
1058                 AC_MSG_RESULT(yes)
1059                 AC_DEFINE(HAVE_4ARGS_VFS_RENAME, 1,
1060                           [vfs_rename() wants 4 args])
1061         ],[
1062                 AC_MSG_RESULT(no)
1063                 dnl #
1064                 dnl # Linux 3.13 API change
1065                 dnl # Added delegated inode
1066                 dnl #
1067                 AC_MSG_CHECKING([whether vfs_rename() wants 5 args])
1068                 SPL_LINUX_TRY_COMPILE([
1069                         #include <linux/fs.h>
1070                 ],[
1071                         vfs_rename((struct inode *) NULL,
1072                                 (struct dentry *) NULL,
1073                                 (struct inode *) NULL,
1074                                 (struct dentry *) NULL,
1075                                 (struct inode **) NULL);
1076                 ],[
1077                         AC_MSG_RESULT(yes)
1078                         AC_DEFINE(HAVE_5ARGS_VFS_RENAME, 1,
1079                                   [vfs_rename() wants 5 args])
1080                 ],[
1081                         AC_MSG_RESULT(no)
1082                         dnl #
1083                         dnl # Linux 3.15 API change
1084                         dnl # Added flags
1085                         dnl #
1086                         AC_MSG_CHECKING([whether vfs_rename() wants 6 args])
1087                         SPL_LINUX_TRY_COMPILE([
1088                                 #include <linux/fs.h>
1089                         ],[
1090                                 vfs_rename((struct inode *) NULL,
1091                                         (struct dentry *) NULL,
1092                                         (struct inode *) NULL,
1093                                         (struct dentry *) NULL,
1094                                         (struct inode **) NULL,
1095                                         (unsigned int) 0);
1096                         ],[
1097                                 AC_MSG_RESULT(yes)
1098                                 AC_DEFINE(HAVE_6ARGS_VFS_RENAME, 1,
1099                                           [vfs_rename() wants 6 args])
1100                         ],[
1101                                 AC_MSG_ERROR(no)
1102                         ])
1103                 ])
1104         ])
1105 ])
1106
1107 dnl #
1108 dnl # 2.6.36 API change,
1109 dnl # The 'struct fs_struct->lock' was changed from a rwlock_t to
1110 dnl # a spinlock_t to improve the fastpath performance.
1111 dnl #
1112 AC_DEFUN([SPL_AC_FS_STRUCT_SPINLOCK], [
1113         AC_MSG_CHECKING([whether struct fs_struct uses spinlock_t])
1114         tmp_flags="$EXTRA_KCFLAGS"
1115         EXTRA_KCFLAGS="-Werror"
1116         SPL_LINUX_TRY_COMPILE([
1117                 #include <linux/sched.h>
1118                 #include <linux/fs_struct.h>
1119         ],[
1120                 struct fs_struct fs;
1121                 spin_lock_init(&fs.lock);
1122         ],[
1123                 AC_MSG_RESULT(yes)
1124                 AC_DEFINE(HAVE_FS_STRUCT_SPINLOCK, 1,
1125                           [struct fs_struct uses spinlock_t])
1126         ],[
1127                 AC_MSG_RESULT(no)
1128         ])
1129         EXTRA_KCFLAGS="$tmp_flags"
1130 ])
1131
1132 dnl #
1133 dnl # User namespaces, use kuid_t in place of uid_t
1134 dnl # where available. Not strictly a user namespaces thing
1135 dnl # but it should prevent surprises
1136 dnl #
1137 AC_DEFUN([SPL_AC_KUIDGID_T], [
1138         AC_MSG_CHECKING([whether kuid_t/kgid_t is available])
1139         SPL_LINUX_TRY_COMPILE([
1140                 #include <linux/uidgid.h>
1141         ], [
1142                 kuid_t userid = KUIDT_INIT(0);
1143                 kgid_t groupid = KGIDT_INIT(0);
1144         ],[
1145                 SPL_LINUX_TRY_COMPILE([
1146                         #include <linux/uidgid.h>
1147                 ], [
1148                         kuid_t userid = 0;
1149                         kgid_t groupid = 0;
1150                 ],[
1151                         AC_MSG_RESULT(yes; optional)
1152                 ],[
1153                         AC_MSG_RESULT(yes; mandatory)
1154                         AC_DEFINE(HAVE_KUIDGID_T, 1, [kuid_t/kgid_t in use])
1155                 ])
1156         ],[
1157                 AC_MSG_RESULT(no)
1158         ])
1159 ])
1160
1161 dnl #
1162 dnl # 2.6.39 API change,
1163 dnl # __put_task_struct() was exported by the mainline kernel.
1164 dnl #
1165 AC_DEFUN([SPL_AC_PUT_TASK_STRUCT],
1166         [AC_MSG_CHECKING([whether __put_task_struct() is available])
1167         SPL_LINUX_TRY_COMPILE_SYMBOL([
1168                 #include <linux/sched.h>
1169         ], [
1170                 __put_task_struct(NULL);
1171         ], [__put_task_struct], [], [
1172                 AC_MSG_RESULT(yes)
1173                 AC_DEFINE(HAVE_PUT_TASK_STRUCT, 1,
1174                           [__put_task_struct() is available])
1175         ], [
1176                 AC_MSG_RESULT(no)
1177         ])
1178 ])
1179
1180 dnl #
1181 dnl # 2.6.35 API change,
1182 dnl # Unused 'struct dentry *' removed from vfs_fsync() prototype.
1183 dnl #
1184 AC_DEFUN([SPL_AC_2ARGS_VFS_FSYNC], [
1185         AC_MSG_CHECKING([whether vfs_fsync() wants 2 args])
1186         SPL_LINUX_TRY_COMPILE([
1187                 #include <linux/fs.h>
1188         ],[
1189                 vfs_fsync(NULL, 0);
1190         ],[
1191                 AC_MSG_RESULT(yes)
1192                 AC_DEFINE(HAVE_2ARGS_VFS_FSYNC, 1, [vfs_fsync() wants 2 args])
1193         ],[
1194                 AC_MSG_RESULT(no)
1195         ])
1196 ])
1197
1198 dnl #
1199 dnl # 3.5 API change,
1200 dnl # inode_operations.truncate_range removed
1201 dnl #
1202 AC_DEFUN([SPL_AC_INODE_TRUNCATE_RANGE], [
1203         AC_MSG_CHECKING([whether truncate_range() inode operation is available])
1204         SPL_LINUX_TRY_COMPILE([
1205                 #include <linux/fs.h>
1206         ],[
1207                 struct inode_operations ops;
1208                 ops.truncate_range = NULL;
1209         ],[
1210                 AC_MSG_RESULT(yes)
1211                 AC_DEFINE(HAVE_INODE_TRUNCATE_RANGE, 1,
1212                         [truncate_range() inode operation is available])
1213         ],[
1214                 AC_MSG_RESULT(no)
1215         ])
1216 ])
1217
1218 dnl #
1219 dnl # Linux 2.6.38 - 3.x API
1220 dnl #
1221 AC_DEFUN([SPL_AC_KERNEL_FILE_FALLOCATE], [
1222         AC_MSG_CHECKING([whether fops->fallocate() exists])
1223         SPL_LINUX_TRY_COMPILE([
1224                 #include <linux/fs.h>
1225         ],[
1226                 long (*fallocate) (struct file *, int, loff_t, loff_t) = NULL;
1227                 struct file_operations fops __attribute__ ((unused)) = {
1228                         .fallocate = fallocate,
1229                 };
1230         ],[
1231                 AC_MSG_RESULT(yes)
1232                 AC_DEFINE(HAVE_FILE_FALLOCATE, 1, [fops->fallocate() exists])
1233         ],[
1234                 AC_MSG_RESULT(no)
1235         ])
1236 ])
1237
1238 dnl #
1239 dnl # Linux 2.6.x - 2.6.37 API
1240 dnl #
1241 AC_DEFUN([SPL_AC_KERNEL_INODE_FALLOCATE], [
1242         AC_MSG_CHECKING([whether iops->fallocate() exists])
1243         SPL_LINUX_TRY_COMPILE([
1244                 #include <linux/fs.h>
1245         ],[
1246                 long (*fallocate) (struct inode *, int, loff_t, loff_t) = NULL;
1247                 struct inode_operations fops __attribute__ ((unused)) = {
1248                         .fallocate = fallocate,
1249                 };
1250         ],[
1251                 AC_MSG_RESULT(yes)
1252                 AC_DEFINE(HAVE_INODE_FALLOCATE, 1, [fops->fallocate() exists])
1253         ],[
1254                 AC_MSG_RESULT(no)
1255         ])
1256 ])
1257
1258 dnl #
1259 dnl # PaX Linux 2.6.38 - 3.x API
1260 dnl #
1261 AC_DEFUN([SPL_AC_PAX_KERNEL_FILE_FALLOCATE], [
1262         AC_MSG_CHECKING([whether fops->fallocate() exists])
1263         SPL_LINUX_TRY_COMPILE([
1264                 #include <linux/fs.h>
1265         ],[
1266                 long (*fallocate) (struct file *, int, loff_t, loff_t) = NULL;
1267                 struct file_operations_no_const fops __attribute__ ((unused)) = {
1268                         .fallocate = fallocate,
1269                 };
1270         ],[
1271                 AC_MSG_RESULT(yes)
1272                 AC_DEFINE(HAVE_FILE_FALLOCATE, 1, [fops->fallocate() exists])
1273         ],[
1274                 AC_MSG_RESULT(no)
1275         ])
1276 ])
1277
1278 dnl #
1279 dnl # The fallocate callback was moved from the inode_operations
1280 dnl # structure to the file_operations structure.
1281 dnl #
1282 AC_DEFUN([SPL_AC_KERNEL_FALLOCATE], [
1283         SPL_AC_KERNEL_FILE_FALLOCATE
1284         SPL_AC_KERNEL_INODE_FALLOCATE
1285         SPL_AC_PAX_KERNEL_FILE_FALLOCATE
1286 ])
1287
1288 dnl #
1289 dnl # 2.6.33 API change. Also backported in RHEL5 as of 2.6.18-190.el5.
1290 dnl # Earlier versions of rwsem_is_locked() were inline and had a race
1291 dnl # condition.  The fixed version is exported as a symbol.  The race
1292 dnl # condition is fixed by acquiring sem->wait_lock, so we must not
1293 dnl # call that version while holding sem->wait_lock.
1294 dnl #
1295 AC_DEFUN([SPL_AC_EXPORTED_RWSEM_IS_LOCKED],
1296         [AC_MSG_CHECKING([whether rwsem_is_locked() acquires sem->wait_lock])
1297         SPL_LINUX_TRY_COMPILE_SYMBOL([
1298                 #include <linux/rwsem.h>
1299                 int rwsem_is_locked(struct rw_semaphore *sem) { return 0; }
1300         ], [], [rwsem_is_locked], [lib/rwsem-spinlock.c], [
1301                 AC_MSG_RESULT(yes)
1302                 AC_DEFINE(RWSEM_IS_LOCKED_TAKES_WAIT_LOCK, 1,
1303                           [rwsem_is_locked() acquires sem->wait_lock])
1304         ], [
1305                 AC_MSG_RESULT(no)
1306         ])
1307 ])
1308
1309 dnl #
1310 dnl # zlib inflate compat,
1311 dnl # Verify the kernel has CONFIG_ZLIB_INFLATE support enabled.
1312 dnl #
1313 AC_DEFUN([SPL_AC_CONFIG_ZLIB_INFLATE], [
1314         AC_MSG_CHECKING([whether CONFIG_ZLIB_INFLATE is defined])
1315         SPL_LINUX_TRY_COMPILE([
1316                 #if !defined(CONFIG_ZLIB_INFLATE) && \
1317                     !defined(CONFIG_ZLIB_INFLATE_MODULE)
1318                 #error CONFIG_ZLIB_INFLATE not defined
1319                 #endif
1320         ],[ ],[
1321                 AC_MSG_RESULT([yes])
1322         ],[
1323                 AC_MSG_RESULT([no])
1324                 AC_MSG_ERROR([
1325         *** This kernel does not include the required zlib inflate support.
1326         *** Rebuild the kernel with CONFIG_ZLIB_INFLATE=y|m set.])
1327         ])
1328 ])
1329
1330 dnl #
1331 dnl # zlib deflate compat,
1332 dnl # Verify the kernel has CONFIG_ZLIB_DEFLATE support enabled.
1333 dnl #
1334 AC_DEFUN([SPL_AC_CONFIG_ZLIB_DEFLATE], [
1335         AC_MSG_CHECKING([whether CONFIG_ZLIB_DEFLATE is defined])
1336         SPL_LINUX_TRY_COMPILE([
1337                 #if !defined(CONFIG_ZLIB_DEFLATE) && \
1338                     !defined(CONFIG_ZLIB_DEFLATE_MODULE)
1339                 #error CONFIG_ZLIB_DEFLATE not defined
1340                 #endif
1341         ],[ ],[
1342                 AC_MSG_RESULT([yes])
1343         ],[
1344                 AC_MSG_RESULT([no])
1345                 AC_MSG_ERROR([
1346         *** This kernel does not include the required zlib deflate support.
1347         *** Rebuild the kernel with CONFIG_ZLIB_DEFLATE=y|m set.])
1348         ])
1349 ])
1350
1351 dnl #
1352 dnl # 2.6.39 API compat,
1353 dnl # The function zlib_deflate_workspacesize() now take 2 arguments.
1354 dnl # This was done to avoid always having to allocate the maximum size
1355 dnl # workspace (268K).  The caller can now specific the windowBits and
1356 dnl # memLevel compression parameters to get a smaller workspace.
1357 dnl #
1358 AC_DEFUN([SPL_AC_2ARGS_ZLIB_DEFLATE_WORKSPACESIZE],
1359         [AC_MSG_CHECKING([whether zlib_deflate_workspacesize() wants 2 args])
1360         SPL_LINUX_TRY_COMPILE([
1361                 #include <linux/zlib.h>
1362         ],[
1363                 return zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL);
1364         ],[
1365                 AC_MSG_RESULT(yes)
1366                 AC_DEFINE(HAVE_2ARGS_ZLIB_DEFLATE_WORKSPACESIZE, 1,
1367                           [zlib_deflate_workspacesize() wants 2 args])
1368         ],[
1369                 AC_MSG_RESULT(no)
1370         ])
1371 ])
1372
1373 dnl #
1374 dnl # 2.6.39 API change,
1375 dnl # Shrinker adjust to use common shrink_control structure.
1376 dnl #
1377 AC_DEFUN([SPL_AC_SHRINK_CONTROL_STRUCT], [
1378         AC_MSG_CHECKING([whether struct shrink_control exists])
1379         SPL_LINUX_TRY_COMPILE([
1380                 #include <linux/mm.h>
1381         ],[
1382                 struct shrink_control sc __attribute__ ((unused));
1383
1384                 sc.nr_to_scan = 0;
1385                 sc.gfp_mask = GFP_KERNEL;
1386         ],[
1387                 AC_MSG_RESULT(yes)
1388                 AC_DEFINE(HAVE_SHRINK_CONTROL_STRUCT, 1,
1389                         [struct shrink_control exists])
1390         ],[
1391                 AC_MSG_RESULT(no)
1392         ])
1393 ])
1394
1395 dnl #
1396 dnl # 3.1 API Change
1397 dnl #
1398 dnl # The rw_semaphore.wait_lock member was changed from spinlock_t to
1399 dnl # raw_spinlock_t at commit ddb6c9b58a19edcfac93ac670b066c836ff729f1.
1400 dnl #
1401 AC_DEFUN([SPL_AC_RWSEM_SPINLOCK_IS_RAW], [
1402         AC_MSG_CHECKING([whether struct rw_semaphore member wait_lock is raw])
1403         tmp_flags="$EXTRA_KCFLAGS"
1404         EXTRA_KCFLAGS="-Werror"
1405         SPL_LINUX_TRY_COMPILE([
1406                 #include <linux/rwsem.h>
1407         ],[
1408                 struct rw_semaphore dummy_semaphore __attribute__ ((unused));
1409                 raw_spinlock_t dummy_lock __attribute__ ((unused));
1410                 dummy_semaphore.wait_lock = dummy_lock;
1411         ],[
1412                 AC_MSG_RESULT(yes)
1413                 AC_DEFINE(RWSEM_SPINLOCK_IS_RAW, 1,
1414                 [struct rw_semaphore member wait_lock is raw_spinlock_t])
1415         ],[
1416                 AC_MSG_RESULT(no)
1417         ])
1418         EXTRA_KCFLAGS="$tmp_flags"
1419 ])
1420
1421 dnl #
1422 dnl # 3.9 API change,
1423 dnl # Moved things from linux/sched.h to linux/sched/rt.h
1424 dnl #
1425 AC_DEFUN([SPL_AC_SCHED_RT_HEADER],
1426         [AC_MSG_CHECKING([whether header linux/sched/rt.h exists])
1427         SPL_LINUX_TRY_COMPILE([
1428                 #include <linux/sched.h>
1429                 #include <linux/sched/rt.h>
1430         ],[
1431                 return 0;
1432         ],[
1433                 AC_DEFINE(HAVE_SCHED_RT_HEADER, 1, [linux/sched/rt.h exists])
1434                 AC_MSG_RESULT(yes)
1435         ],[
1436                 AC_MSG_RESULT(no)
1437         ])
1438 ])
1439
1440 dnl #
1441 dnl # 3.9 API change,
1442 dnl # vfs_getattr() uses 2 args
1443 dnl # It takes struct path * instead of struct vfsmount * and struct dentry *
1444 dnl #
1445 AC_DEFUN([SPL_AC_2ARGS_VFS_GETATTR], [
1446         AC_MSG_CHECKING([whether vfs_getattr() wants])
1447         SPL_LINUX_TRY_COMPILE([
1448                 #include <linux/fs.h>
1449         ],[
1450                 vfs_getattr((struct path *) NULL,
1451                         (struct kstat *)NULL);
1452         ],[
1453                 AC_MSG_RESULT(2 args)
1454                 AC_DEFINE(HAVE_2ARGS_VFS_GETATTR, 1,
1455                           [vfs_getattr wants 2 args])
1456         ],[
1457                 SPL_LINUX_TRY_COMPILE([
1458                         #include <linux/fs.h>
1459                 ],[
1460                         vfs_getattr((struct vfsmount *)NULL,
1461                                 (struct dentry *)NULL,
1462                                 (struct kstat *)NULL);
1463                 ],[
1464                         AC_MSG_RESULT(3 args)
1465                 ],[
1466                         AC_MSG_ERROR(unknown)
1467                 ])
1468         ])
1469 ])
1470
1471 dnl #
1472 dnl # 2.6.36 API compatibility.
1473 dnl # Added usleep_range timer.
1474 dnl # usleep_range is a finer precision implementation of msleep
1475 dnl # designed to be a drop-in replacement for udelay where a precise
1476 dnl # sleep / busy-wait is unnecessary.
1477 dnl #
1478 AC_DEFUN([SPL_AC_USLEEP_RANGE], [
1479         AC_MSG_CHECKING([whether usleep_range() is available])
1480         SPL_LINUX_TRY_COMPILE([
1481                 #include <linux/delay.h>
1482         ],[
1483                 usleep_range(0, 0);
1484         ],[
1485                 AC_MSG_RESULT(yes)
1486                 AC_DEFINE(HAVE_USLEEP_RANGE, 1,
1487                           [usleep_range is available])
1488         ],[
1489                 AC_MSG_RESULT(no)
1490         ])
1491 ])
1492
1493 dnl #
1494 dnl # 2.6.35 API change,
1495 dnl # The cachep->gfpflags member was renamed cachep->allocflags.  These are
1496 dnl # private allocation flags which are applied when allocating a new slab
1497 dnl # in kmem_getpages().  Unfortunately there is no public API for setting
1498 dnl # non-default flags.
1499 dnl #
1500 AC_DEFUN([SPL_AC_KMEM_CACHE_ALLOCFLAGS], [
1501         AC_MSG_CHECKING([whether struct kmem_cache has allocflags])
1502         SPL_LINUX_TRY_COMPILE([
1503                 #include <linux/slab.h>
1504         ],[
1505                 struct kmem_cache cachep __attribute__ ((unused));
1506                 cachep.allocflags = GFP_KERNEL;
1507         ],[
1508                 AC_MSG_RESULT(yes)
1509                 AC_DEFINE(HAVE_KMEM_CACHE_ALLOCFLAGS, 1,
1510                         [struct kmem_cache has allocflags])
1511         ],[
1512                 AC_MSG_RESULT(no)
1513
1514                 AC_MSG_CHECKING([whether struct kmem_cache has gfpflags])
1515                 SPL_LINUX_TRY_COMPILE([
1516                         #include <linux/slab.h>
1517                 ],[
1518                         struct kmem_cache cachep __attribute__ ((unused));
1519                         cachep.gfpflags = GFP_KERNEL;
1520                 ],[
1521                         AC_MSG_RESULT(yes)
1522                         AC_DEFINE(HAVE_KMEM_CACHE_GFPFLAGS, 1,
1523                                 [struct kmem_cache has gfpflags])
1524                 ],[
1525                         AC_MSG_RESULT(no)
1526                 ])
1527         ])
1528 ])
1529
1530 dnl #
1531 dnl # 3.17 API change,
1532 dnl # wait_on_bit() no longer requires an action argument. The former
1533 dnl # "wait_on_bit" interface required an 'action' function to be provided
1534 dnl # which does the actual waiting. There were over 20 such functions in the
1535 dnl # kernel, many of them identical, though most cases can be satisfied by one
1536 dnl # of just two functions: one which uses io_schedule() and one which just
1537 dnl # uses schedule().  This API change was made to consolidate all of those
1538 dnl # redundant wait functions.
1539 dnl #
1540 AC_DEFUN([SPL_AC_WAIT_ON_BIT], [
1541         AC_MSG_CHECKING([whether wait_on_bit() takes an action])
1542         SPL_LINUX_TRY_COMPILE([
1543                 #include <linux/wait.h>
1544         ],[
1545                 int (*action)(void *) = NULL;
1546                 wait_on_bit(NULL, 0, action, 0);
1547         ],[
1548                 AC_MSG_RESULT(yes)
1549                 AC_DEFINE(HAVE_WAIT_ON_BIT_ACTION, 1, [yes])
1550         ],[
1551                 AC_MSG_RESULT(no)
1552         ])
1553 ])