git: d3efbe0132b2 - main - cdevpriv(9): add iterator

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sat, 23 Mar 2024 06:59:15 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=d3efbe0132b24e8660df836905cda7662f85a154

commit d3efbe0132b24e8660df836905cda7662f85a154
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-03-22 02:58:00 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-03-23 06:59:00 +0000

    cdevpriv(9): add iterator
    
    Reviewed by:    christos
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D44469
---
 sys/fs/devfs/devfs_vnops.c | 20 ++++++++++++++++++++
 sys/sys/conf.h             |  2 ++
 2 files changed, 22 insertions(+)

diff --git a/sys/fs/devfs/devfs_vnops.c b/sys/fs/devfs/devfs_vnops.c
index 3b8a624fe620..a35f6dbf9520 100644
--- a/sys/fs/devfs/devfs_vnops.c
+++ b/sys/fs/devfs/devfs_vnops.c
@@ -177,6 +177,26 @@ devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
 	return (error);
 }
 
+int
+devfs_foreach_cdevpriv(struct cdev *dev, int (*cb)(void *data, void *arg),
+    void *arg)
+{
+	struct cdev_priv *cdp;
+	struct cdev_privdata *p;
+	int error;
+
+	cdp = cdev2priv(dev);
+	error = 0;
+	mtx_lock(&cdevpriv_mtx);
+	LIST_FOREACH(p, &cdp->cdp_fdpriv, cdpd_list) {
+		error = cb(p->cdpd_data, arg);
+		if (error != 0)
+			break;
+	}
+	mtx_unlock(&cdevpriv_mtx);
+	return (error);
+}
+
 void
 devfs_destroy_cdevpriv(struct cdev_privdata *p)
 {
diff --git a/sys/sys/conf.h b/sys/sys/conf.h
index d7a33ed10afa..f34fd43cfe24 100644
--- a/sys/sys/conf.h
+++ b/sys/sys/conf.h
@@ -319,6 +319,8 @@ typedef void d_priv_dtor_t(void *data);
 int	devfs_get_cdevpriv(void **datap);
 int	devfs_set_cdevpriv(void *priv, d_priv_dtor_t *dtr);
 void	devfs_clear_cdevpriv(void);
+int	devfs_foreach_cdevpriv(struct cdev *dev,
+	    int (*cb)(void *data, void *arg), void *arg);
 
 ino_t	devfs_alloc_cdp_inode(void);
 void	devfs_free_cdp_inode(ino_t ino);