Skip to content

Commit 7466e09

Browse files
robnbehlendorf
authored andcommitted
Linux 6.8 compat: implement strlcpy fallback
Linux has removed strlcpy in favour of strscpy. This implements a fallback implementation of strlcpy for this case. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Rob Norris <[email protected]> Sponsored-by: https://despairlabs.com/sponsor/ Closes #15805
1 parent ce782d0 commit 7466e09

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed

config/kernel-strlcpy.m4

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
dnl #
2+
dnl # 6.8.x replaced strlcpy with strscpy. Check for both so we can provide
3+
dnl # appropriate fallbacks.
4+
dnl #
5+
AC_DEFUN([ZFS_AC_KERNEL_SRC_STRLCPY], [
6+
ZFS_LINUX_TEST_SRC([kernel_has_strlcpy], [
7+
#include <linux/string.h>
8+
], [
9+
const char *src = "goodbye";
10+
char dst[32];
11+
size_t len;
12+
len = strlcpy(dst, src, sizeof (dst));
13+
])
14+
])
15+
16+
AC_DEFUN([ZFS_AC_KERNEL_SRC_STRSCPY], [
17+
ZFS_LINUX_TEST_SRC([kernel_has_strscpy], [
18+
#include <linux/string.h>
19+
], [
20+
const char *src = "goodbye";
21+
char dst[32];
22+
ssize_t len;
23+
len = strscpy(dst, src, sizeof (dst));
24+
])
25+
])
26+
27+
AC_DEFUN([ZFS_AC_KERNEL_STRLCPY], [
28+
AC_MSG_CHECKING([whether strlcpy() exists])
29+
ZFS_LINUX_TEST_RESULT([kernel_has_strlcpy], [
30+
AC_MSG_RESULT([yes])
31+
AC_DEFINE(HAVE_KERNEL_STRLCPY, 1,
32+
[strlcpy() exists])
33+
], [
34+
AC_MSG_RESULT([no])
35+
])
36+
])
37+
38+
AC_DEFUN([ZFS_AC_KERNEL_STRSCPY], [
39+
AC_MSG_CHECKING([whether strscpy() exists])
40+
ZFS_LINUX_TEST_RESULT([kernel_has_strscpy], [
41+
AC_MSG_RESULT([yes])
42+
AC_DEFINE(HAVE_KERNEL_STRSCPY, 1,
43+
[strscpy() exists])
44+
], [
45+
AC_MSG_RESULT([no])
46+
])
47+
])

config/kernel.m4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ AC_DEFUN([ZFS_AC_KERNEL_TEST_SRC], [
149149
ZFS_AC_KERNEL_SRC_SYSFS
150150
ZFS_AC_KERNEL_SRC_SET_SPECIAL_STATE
151151
ZFS_AC_KERNEL_SRC_STANDALONE_LINUX_STDARG
152+
ZFS_AC_KERNEL_SRC_STRLCPY
153+
ZFS_AC_KERNEL_SRC_STRSCPY
152154
ZFS_AC_KERNEL_SRC_PAGEMAP_FOLIO_WAIT_BIT
153155
ZFS_AC_KERNEL_SRC_ADD_DISK
154156
ZFS_AC_KERNEL_SRC_KTHREAD
@@ -294,6 +296,8 @@ AC_DEFUN([ZFS_AC_KERNEL_TEST_RESULT], [
294296
ZFS_AC_KERNEL_SYSFS
295297
ZFS_AC_KERNEL_SET_SPECIAL_STATE
296298
ZFS_AC_KERNEL_STANDALONE_LINUX_STDARG
299+
ZFS_AC_KERNEL_STRLCPY
300+
ZFS_AC_KERNEL_STRSCPY
297301
ZFS_AC_KERNEL_PAGEMAP_FOLIO_WAIT_BIT
298302
ZFS_AC_KERNEL_ADD_DISK
299303
ZFS_AC_KERNEL_KTHREAD

include/os/linux/spl/sys/string.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1+
/*
2+
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3+
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
4+
* Written by Brian Behlendorf <[email protected]>.
5+
* UCRL-CODE-235197
6+
*
7+
* This file is part of the SPL, Solaris Porting Layer.
8+
*
9+
* The SPL is free software; you can redistribute it and/or modify it
10+
* under the terms of the GNU General Public License as published by the
11+
* Free Software Foundation; either version 2 of the License, or (at your
12+
* option) any later version.
13+
*
14+
* The SPL is distributed in the hope that it will be useful, but WITHOUT
15+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17+
* for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License along
20+
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
#ifndef _SPL_STRING_H
24+
#define _SPL_STRING_H
25+
126
#include <linux/string.h>
27+
28+
/* Fallbacks for kernel missing strlcpy */
29+
#ifndef HAVE_KERNEL_STRLCPY
30+
31+
#if defined(HAVE_KERNEL_STRSCPY)
32+
/*
33+
* strscpy is strlcpy, but returns an error on truncation. strlcpy is defined
34+
* to return strlen(src), so detect error and override it.
35+
*/
36+
static inline size_t
37+
strlcpy(char *dest, const char *src, size_t size)
38+
{
39+
ssize_t ret = strscpy(dest, src, size);
40+
if (likely(ret > 0))
41+
return ((size_t)ret);
42+
return (strlen(src));
43+
}
44+
#else
45+
#error "no strlcpy fallback available"
46+
#endif
47+
48+
#endif /* HAVE_KERNEL_STRLCPY */
49+
50+
#endif /* _SPL_STRING_H */

module/os/linux/spl/spl-kmem-cache.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <sys/timer.h>
2929
#include <sys/vmem.h>
3030
#include <sys/wait.h>
31+
#include <sys/string.h>
3132
#include <linux/slab.h>
3233
#include <linux/swap.h>
3334
#include <linux/prefetch.h>

module/os/linux/spl/spl-kstat.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <sys/vmem.h>
3333
#include <sys/cmn_err.h>
3434
#include <sys/sysmacros.h>
35+
#include <sys/string.h>
3536

3637
static kmutex_t kstat_module_lock;
3738
static struct list_head kstat_module_list;

module/os/linux/spl/spl-thread.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <sys/thread.h>
2727
#include <sys/kmem.h>
2828
#include <sys/tsd.h>
29+
#include <sys/string.h>
2930

3031
/*
3132
* Thread interfaces

module/os/linux/spl/spl-zone.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/file.h>
3131
#include <linux/magic.h>
3232
#include <sys/zone.h>
33+
#include <sys/string.h>
3334

3435
#if defined(CONFIG_USER_NS)
3536
#include <linux/statfs.h>

0 commit comments

Comments
 (0)