git: cde0347ca70b - stable/14 - cdevpriv(9): add iterator

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sat, 30 Mar 2024 08:32:14 UTC
The branch stable/14 has been updated by kib:

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

commit cde0347ca70b1ffc49803a1fe6c38f8b83135ddc
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-03-22 02:58:00 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-03-30 08:31:13 +0000

    cdevpriv(9): add iterator
    
    (cherry picked from commit d3efbe0132b24e8660df836905cda7662f85a154)
---
 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 b8cf8c12d32e..cda4afc8c241 100644
--- a/sys/fs/devfs/devfs_vnops.c
+++ b/sys/fs/devfs/devfs_vnops.c
@@ -179,6 +179,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 f2dfaad5aa1b..a1c71080732f 100644
--- a/sys/sys/conf.h
+++ b/sys/sys/conf.h
@@ -321,6 +321,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);