git: 03c44354fc9e - stable/13 - loader: zfs reader should only store devdesc in f_devdata

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:12:11 UTC
The branch stable/13 has been updated by imp:

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

commit 03c44354fc9e82f89d6e323d5f20ab953854812d
Author:     Toomas Soome <tsoome@FreeBSD.org>
AuthorDate: 2022-08-14 21:49:50 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:32 +0000

    loader: zfs reader should only store devdesc in f_devdata
    
    Use d_opendata for device specific data.
    
    PR:             265825
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D36202
    
    (cherry picked from commit d98de7440507aea1648c8f4bc302bf88c0eb9458)
---
 stand/libsa/zfs/zfs.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

diff --git a/stand/libsa/zfs/zfs.c b/stand/libsa/zfs/zfs.c
index b525858ffc3c..bee243352f78 100644
--- a/stand/libsa/zfs/zfs.c
+++ b/stand/libsa/zfs/zfs.c
@@ -108,7 +108,8 @@ struct zfs_be_entry {
 static int
 zfs_open(const char *upath, struct open_file *f)
 {
-	struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
+	struct devdesc *dev = f->f_devdata;
+	struct zfsmount *mount = dev->d_opendata;
 	struct file *fp;
 	int rc;
 
@@ -149,7 +150,8 @@ zfs_close(struct open_file *f)
 static int
 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid	/* out */)
 {
-	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
+	struct devdesc *dev = f->f_devdata;
+	const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa;
 	struct file *fp = (struct file *)f->f_fsdata;
 	struct stat sb;
 	size_t n;
@@ -213,7 +215,8 @@ zfs_seek(struct open_file *f, off_t offset, int where)
 static int
 zfs_stat(struct open_file *f, struct stat *sb)
 {
-	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
+	struct devdesc *dev = f->f_devdata;
+	const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa;
 	struct file *fp = (struct file *)f->f_fsdata;
 
 	return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
@@ -222,7 +225,8 @@ zfs_stat(struct open_file *f, struct stat *sb)
 static int
 zfs_readdir(struct open_file *f, struct dirent *d)
 {
-	const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
+	struct devdesc *dev = f->f_devdata;
+	const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa;
 	struct file *fp = (struct file *)f->f_fsdata;
 	mzap_ent_phys_t mze;
 	struct stat sb;
@@ -1586,8 +1590,7 @@ zfs_dev_open(struct open_file *f, ...)
 		rv = zfs_mount(devformat(&dev->dd), NULL, (void **)&mount);
 
 	if (rv == 0) {
-		f->f_devdata = mount;
-		free(dev);
+		dev->dd.d_opendata = mount;
 	}
 	return (rv);
 }
@@ -1595,25 +1598,18 @@ zfs_dev_open(struct open_file *f, ...)
 static int
 zfs_dev_close(struct open_file *f)
 {
+	struct devdesc *dev;
 	struct zfsmount	*mnt, *mount;
 
-	mnt = f->f_devdata;
+	dev = f->f_devdata;
+	mnt = dev->d_opendata;
 
 	STAILQ_FOREACH(mount, &zfsmount, next) {
 		if (mnt->spa->spa_guid == mount->spa->spa_guid)
 			break;
 	}
 
-	/*
-	 * devclose() will free f->f_devdata, but since we do have
-	 * pointer to zfsmount structure in f->f_devdata, and
-	 * zfs_unmount() will also free the zfsmount structure,
-	 * we will get double free. To prevent double free,
-	 * we must set f_devdata to NULL there.
-	 */
-	if (mount != NULL)
-		f->f_devdata = NULL;
-
+	/* XXX */
 	return (0);
 }