Skip to content

Commit

Permalink
zfs_debug: specific variant for userspace
Browse files Browse the repository at this point in the history
Just nice and simple, with room to grow.

Reviewed by: Brian Behlendorf <[email protected]>
Reviewed-by: Alexander Motin <[email protected]>
Signed-off-by: Rob Norris <[email protected]>
Sponsored-by: https://despairlabs.com/sponsor/
Closes #16492
  • Loading branch information
robn authored and behlendorf committed Sep 19, 2024
1 parent c22d56e commit e8ede2b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 82 deletions.
4 changes: 2 additions & 2 deletions lib/libzpool/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ dist_libzpool_la_SOURCES = \
%D%/taskq.c \
%D%/util.c \
%D%/vdev_label_os.c \
%D%/zfs_racct.c
%D%/zfs_racct.c \
%D%/zfs_debug.c

nodist_libzpool_la_SOURCES = \
module/lua/lapi.c \
Expand Down Expand Up @@ -46,7 +47,6 @@ nodist_libzpool_la_SOURCES = \
module/lua/lzio.c \
\
module/os/linux/zfs/vdev_file.c \
module/os/linux/zfs/zfs_debug.c \
module/os/linux/zfs/zio_crypt.c \
\
module/zcommon/cityhash.c \
Expand Down
12 changes: 9 additions & 3 deletions lib/libzpool/vdev_label_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@
#include <sys/vdev_impl.h>

/*
* Check if the reserved boot area is in-use.
* Check if the reserved boot area is in-use. This is called from
* spa_vdev_attach() when adding a device to a raidz vdev, to ensure that the
* reserved area is available as scratch space for raidz expansion.
*
* This function always returns 0, as there are no known external uses
* of the reserved area on Linux.
* This function currently always returns 0. On Linux, there are no known
* external uses of the reserved area. On FreeBSD, the reserved boot area is
* used when booting to a ZFS root from an MBR partition.
*
* Currently nothing using libzpool can add a disk to a pool, so this does
* nothing.
*/
int
vdev_check_boot_reserve(spa_t *spa, vdev_t *childvd)
Expand Down
106 changes: 106 additions & 0 deletions lib/libzpool/zfs_debug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or https://opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2014 by Delphix. All rights reserved.
* Copyright (c) 2024, Rob Norris <[email protected]>
*/

#include <sys/zfs_context.h>

typedef struct zfs_dbgmsg {
list_node_t zdm_node;
uint64_t zdm_timestamp;
uint_t zdm_size;
char zdm_msg[]; /* variable length allocation */
} zfs_dbgmsg_t;

static list_t zfs_dbgmsgs;
static kmutex_t zfs_dbgmsgs_lock;

int zfs_dbgmsg_enable = B_TRUE;

void
zfs_dbgmsg_init(void)
{
list_create(&zfs_dbgmsgs, sizeof (zfs_dbgmsg_t),
offsetof(zfs_dbgmsg_t, zdm_node));
mutex_init(&zfs_dbgmsgs_lock, NULL, MUTEX_DEFAULT, NULL);
}

void
zfs_dbgmsg_fini(void)
{
zfs_dbgmsg_t *zdm;
while ((zdm = list_remove_head(&zfs_dbgmsgs)))
umem_free(zdm, zdm->zdm_size);
mutex_destroy(&zfs_dbgmsgs_lock);
}

void
__set_error(const char *file, const char *func, int line, int err)
{
if (zfs_flags & ZFS_DEBUG_SET_ERROR)
__dprintf(B_FALSE, file, func, line, "error %lu",
(ulong_t)err);
}

void
__zfs_dbgmsg(char *buf)
{
uint_t size = sizeof (zfs_dbgmsg_t) + strlen(buf) + 1;
zfs_dbgmsg_t *zdm = umem_zalloc(size, KM_SLEEP);
zdm->zdm_size = size;
zdm->zdm_timestamp = gethrestime_sec();
strcpy(zdm->zdm_msg, buf);

mutex_enter(&zfs_dbgmsgs_lock);
list_insert_tail(&zfs_dbgmsgs, zdm);
mutex_exit(&zfs_dbgmsgs_lock);
}

void
zfs_dbgmsg_print(int fd, const char *tag)
{
ssize_t ret __attribute__((unused));

mutex_enter(&zfs_dbgmsgs_lock);

/*
* We use write() in this function instead of printf()
* so it is safe to call from a signal handler.
*/
ret = write(fd, "ZFS_DBGMSG(", 11);
ret = write(fd, tag, strlen(tag));
ret = write(fd, ") START:\n", 9);

for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs); zdm != NULL;
zdm = list_next(&zfs_dbgmsgs, zdm)) {
ret = write(fd, zdm->zdm_msg, strlen(zdm->zdm_msg));
ret = write(fd, "\n", 1);
}

ret = write(fd, "ZFS_DBGMSG(", 11);
ret = write(fd, tag, strlen(tag));
ret = write(fd, ") END\n", 6);

mutex_exit(&zfs_dbgmsgs_lock);
}
38 changes: 1 addition & 37 deletions module/os/freebsd/zfs/zfs_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,11 @@ zfs_dbgmsg_fini(void)
{
if (zfs_dbgmsg_kstat)
kstat_delete(zfs_dbgmsg_kstat);
/*
* TODO - decide how to make this permanent
*/
#ifdef _KERNEL

mutex_enter(&zfs_dbgmsgs_lock);
zfs_dbgmsg_purge(0);
mutex_exit(&zfs_dbgmsgs_lock);
mutex_destroy(&zfs_dbgmsgs_lock);
#endif
}

void
Expand Down Expand Up @@ -184,7 +180,6 @@ __set_error(const char *file, const char *func, int line, int err)
__dprintf(B_FALSE, file, func, line, "error %lu", (ulong_t)err);
}

#ifdef _KERNEL
void
__dprintf(boolean_t dprint, const char *file, const char *func,
int line, const char *fmt, ...)
Expand Down Expand Up @@ -236,37 +231,6 @@ __dprintf(boolean_t dprint, const char *file, const char *func,
kmem_free(buf, size);
}

#else

void
zfs_dbgmsg_print(int fd, const char *tag)
{
ssize_t ret __attribute__((unused));

/*
* We use write() in this function instead of printf()
* so it is safe to call from a signal handler.
*/
ret = write(fd, "ZFS_DBGMSG(", 11);
ret = write(fd, tag, strlen(tag));
ret = write(fd, ") START:\n", 9);

mutex_enter(&zfs_dbgmsgs_lock);

for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs); zdm != NULL;
zdm = list_next(&zfs_dbgmsgs, zdm))
ret = write(fd, zdm->zdm_msg, strlen(zdm->zdm_msg));
ret = write(fd, "\n", 1);
}

ret = write(fd, "ZFS_DBGMSG(", 11);
ret = write(fd, tag, strlen(tag));
ret = write(fd, ") END\n", 6);

mutex_exit(&zfs_dbgmsgs_lock);
}
#endif /* _KERNEL */

ZFS_MODULE_PARAM(zfs, zfs_, dbgmsg_enable, INT, ZMOD_RW,
"Enable ZFS debug message log");

Expand Down
40 changes: 0 additions & 40 deletions module/os/linux/zfs/zfs_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ zfs_dbgmsg_fini(void)
procfs_list_uninstall(&zfs_dbgmsgs);
zfs_dbgmsg_purge(0);

/*
* TODO - decide how to make this permanent
*/
#ifdef _KERNEL
procfs_list_destroy(&zfs_dbgmsgs);
#endif
}

void
Expand Down Expand Up @@ -148,8 +143,6 @@ __zfs_dbgmsg(char *buf)
mutex_exit(&zfs_dbgmsgs.pl_lock);
}

#ifdef _KERNEL

void
__dprintf(boolean_t dprint, const char *file, const char *func,
int line, const char *fmt, ...)
Expand Down Expand Up @@ -218,43 +211,10 @@ __dprintf(boolean_t dprint, const char *file, const char *func,
kmem_free(buf, size);
}

#else

void
zfs_dbgmsg_print(int fd, const char *tag)
{
ssize_t ret __attribute__((unused));

mutex_enter(&zfs_dbgmsgs.pl_lock);

/*
* We use write() in this function instead of printf()
* so it is safe to call from a signal handler.
*/
ret = write(fd, "ZFS_DBGMSG(", 11);
ret = write(fd, tag, strlen(tag));
ret = write(fd, ") START:\n", 9);

for (zfs_dbgmsg_t *zdm = list_head(&zfs_dbgmsgs.pl_list); zdm != NULL;
zdm = list_next(&zfs_dbgmsgs.pl_list, zdm)) {
ret = write(fd, zdm->zdm_msg, strlen(zdm->zdm_msg));
ret = write(fd, "\n", 1);
}

ret = write(fd, "ZFS_DBGMSG(", 11);
ret = write(fd, tag, strlen(tag));
ret = write(fd, ") END\n", 6);

mutex_exit(&zfs_dbgmsgs.pl_lock);
}
#endif /* _KERNEL */

#ifdef _KERNEL
module_param(zfs_dbgmsg_enable, int, 0644);
MODULE_PARM_DESC(zfs_dbgmsg_enable, "Enable ZFS debug message log");

/* BEGIN CSTYLED */
module_param(zfs_dbgmsg_maxsize, uint, 0644);
/* END CSTYLED */
MODULE_PARM_DESC(zfs_dbgmsg_maxsize, "Maximum ZFS debug log size");
#endif

0 comments on commit e8ede2b

Please sign in to comment.