[libbsd 08/22] devfs: Do not use FreeBSD file descriptors

Sebastian Huber sebastian.huber at embedded-brains.de
Fri Jun 24 06:33:36 UTC 2022


Revert commit 6514d561587fd1527fe6a26cb43e6b5742c8c779 in
"rtemsbsd/sys/fs/devfs/devfs_devs.c".  Using the FreeBSD file descriptors for
the device file system is unnecessary.

Update #4475.
---
 freebsd/sys/fs/devfs/devfs_int.h   |   3 +-
 freebsd/sys/fs/devfs/devfs_vnops.c |   4 +-
 rtemsbsd/sys/fs/devfs/devfs_devs.c | 181 ++++++++---------------------
 3 files changed, 53 insertions(+), 135 deletions(-)

diff --git a/freebsd/sys/fs/devfs/devfs_int.h b/freebsd/sys/fs/devfs/devfs_int.h
index e3991cbd..51064ed1 100644
--- a/freebsd/sys/fs/devfs/devfs_int.h
+++ b/freebsd/sys/fs/devfs/devfs_int.h
@@ -95,7 +95,8 @@ extern struct sx clone_drain_lock;
 extern struct mtx cdevpriv_mtx;
 extern TAILQ_HEAD(cdev_priv_list, cdev_priv) cdevp_list;
 #ifdef __rtems__
-void devfs_fpdrop(struct file *);
+struct rtems_libio_tt;
+void devfs_fpdrop(struct rtems_libio_tt *);
 #endif /* __rtems__ */
 
 #endif /* _KERNEL */
diff --git a/freebsd/sys/fs/devfs/devfs_vnops.c b/freebsd/sys/fs/devfs/devfs_vnops.c
index f1027e6f..9ca39259 100644
--- a/freebsd/sys/fs/devfs/devfs_vnops.c
+++ b/freebsd/sys/fs/devfs/devfs_vnops.c
@@ -209,10 +209,12 @@ devfs_destroy_cdevpriv(struct cdev_privdata *p)
 
 #ifndef __rtems__
 static void
+devfs_fpdrop(struct file *fp)
 #else /* __rtems__ */
+#define	f_cdevpriv data1
 void
+devfs_fpdrop(rtems_libio_t *fp)
 #endif /* __rtems__ */
-devfs_fpdrop(struct file *fp)
 {
 	struct cdev_privdata *p;
 
diff --git a/rtemsbsd/sys/fs/devfs/devfs_devs.c b/rtemsbsd/sys/fs/devfs/devfs_devs.c
index 39a38d62..c105661b 100644
--- a/rtemsbsd/sys/fs/devfs/devfs_devs.c
+++ b/rtemsbsd/sys/fs/devfs/devfs_devs.c
@@ -35,7 +35,6 @@
 #include <sys/conf.h>
 #include <sys/kernel.h>
 #include <sys/file.h>
-#include <sys/filedesc.h>
 #include <sys/malloc.h>
 #include <sys/proc.h>
 #include <sys/poll.h>
@@ -43,8 +42,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <machine/rtems-bsd-libio.h>
-
 #include <fs/devfs/devfs_int.h>
 
 #include <machine/pcpu.h>
@@ -76,24 +73,15 @@ devfs_imfs_open(rtems_libio_t *iop, const char *path, int oflag, mode_t mode)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_null();
-	struct file *fp = NULL;
-	struct cdevsw *dsw = NULL;
 	struct file *fpop;
+	struct cdevsw *dsw;
 	int error, ref;
-	int fd = -1;
 
 	if (td != NULL) {
 		if (cdev == NULL) {
 			error = ENXIO;
 			goto err;
 		}
-		error = falloc(td, &fp, &fd, oflag);
-		if (error != 0)
-			goto err;
-		finit(fp, FREAD | FWRITE, DTYPE_DEV, NULL, NULL);
-		rtems_libio_iop_hold(iop);
-		rtems_bsd_libio_iop_set_bsd_descriptor(iop, fd);
-		rtems_bsd_libio_iop_set_bsd_file(iop, fp);
 		if (cdev->si_flags & SI_ALIAS) {
 			cdev = cdev->si_parent;
 		}
@@ -103,26 +91,19 @@ devfs_imfs_open(rtems_libio_t *iop, const char *path, int oflag, mode_t mode)
 			goto err;
 		}
 		fpop = td->td_fpop;
-		curthread->td_fpop = fp;
-		fp->f_cdevpriv = NULL;
+		curthread->td_fpop = (struct file *)iop;
+		iop->data1 = NULL;
 		error = dsw->d_open(cdev, oflag + 1, 0, td);
 		/* Clean up any cdevpriv upon error. */
 		if (error != 0)
 			devfs_clear_cdevpriv();
 		curthread->td_fpop = fpop;
+		dev_relthread(cdev, ref);
 	} else {
 		error = ENOMEM;
 	}
 
 err:
-	if (dsw != NULL)
-		dev_relthread(cdev, ref);
-	if (td != NULL && fp != NULL) {
-		if (error != 0)
-			fdclose(td, fp, fd);
-		else
-			fdrop(fp, td);
-	}
 	return rtems_bsd_error_to_status_and_errno(error);
 }
 
@@ -132,9 +113,8 @@ devfs_imfs_close(rtems_libio_t *iop)
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_null();
 	int flags = rtems_libio_to_fcntl_flags(iop->flags);
-	struct file *fp = NULL;
-	struct cdevsw *dsw = NULL;
 	struct file *fpop;
+	struct cdevsw *dsw;
 	int error, ref;
 
 	if (td != NULL) {
@@ -145,31 +125,23 @@ devfs_imfs_close(rtems_libio_t *iop)
 		if (cdev->si_flags & SI_ALIAS) {
 			cdev = cdev->si_parent;
 		}
-		fp = rtems_bsd_libio_iop_to_file_hold(iop, td);
-		if (fp == NULL) {
-			error = EBADF;
-			goto err;
-		}
 		dsw = dev_refthread(cdev, &ref);
 		if (dsw == NULL) {
 			error = ENXIO;
 			goto err;
 		}
 		fpop = td->td_fpop;
-		curthread->td_fpop = fp;
+		curthread->td_fpop = (struct file *)iop;
 		error = dsw->d_close(cdev, flags, 0, td);
 		curthread->td_fpop = fpop;
-		if (fp->f_cdevpriv != NULL)
-			devfs_fpdrop(fp);
+		dev_relthread(cdev, ref);
+		if (iop->data1 != NULL)
+			devfs_fpdrop(iop);
 	} else {
 		error = ENOMEM;
 	}
 
 err:
-	if (dsw != NULL)
-		dev_relthread(cdev, ref);
-	if (td != NULL && fp != NULL)
-		fdrop(fp, td);
 	return rtems_bsd_error_to_status_and_errno(error);
 }
 
@@ -188,9 +160,8 @@ devfs_imfs_readv(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
 		.uio_rw = UIO_READ,
 		.uio_td = td
 	};
-	struct file *fp = NULL;
-	struct cdevsw *dsw = NULL;
 	struct file *fpop;
+	struct cdevsw *dsw;
 	int error, ref;
 
 	if (td != NULL) {
@@ -201,30 +172,22 @@ devfs_imfs_readv(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
 		if (cdev->si_flags & SI_ALIAS) {
 			cdev = cdev->si_parent;
 		}
-		fp = rtems_bsd_libio_iop_to_file_hold(iop, td);
-		if (fp == NULL) {
-			error = EBADF;
-			goto err;
-		}
 		dsw = dev_refthread(cdev, &ref);
 		if (dsw == NULL) {
 			error = ENXIO;
 			goto err;
 		}
 		fpop = td->td_fpop;
-		curthread->td_fpop = fp;
+		curthread->td_fpop = (struct file *)iop;
 		error = dsw->d_read(cdev, &uio,
 		    rtems_libio_to_fcntl_flags(iop->flags));
 		td->td_fpop = fpop;
+		dev_relthread(cdev, ref);
 	} else {
 		error = ENOMEM;
 	}
 
 err:
-	if (dsw != NULL)
-		dev_relthread(cdev, ref);
-	if (td != NULL && fp != NULL)
-		fdrop(fp, td);
 	if (error == 0) {
 		return (total - uio.uio_resid);
 	} else {
@@ -258,9 +221,8 @@ devfs_imfs_writev(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
 		.uio_rw = UIO_WRITE,
 		.uio_td = td
 	};
-	struct file *fp = NULL;
-	struct cdevsw *dsw = NULL;
 	struct file *fpop;
+	struct cdevsw *dsw;
 	int error, ref;
 
 	if (td != NULL) {
@@ -271,30 +233,22 @@ devfs_imfs_writev(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
 		if (cdev->si_flags & SI_ALIAS) {
 			cdev = cdev->si_parent;
 		}
-		fp = rtems_bsd_libio_iop_to_file_hold(iop, td);
-		if (fp == NULL) {
-			error = EBADF;
-			goto err;
-		}
 		dsw = dev_refthread(cdev, &ref);
 		if (dsw == NULL) {
 			error = ENXIO;
 			goto err;
 		}
 		fpop = td->td_fpop;
-		curthread->td_fpop = fp;
+		curthread->td_fpop = (struct file *)iop;
 		error = dsw->d_write(cdev, &uio,
 		    rtems_libio_to_fcntl_flags(iop->flags));
 		td->td_fpop = fpop;
+		dev_relthread(cdev, ref);
 	} else {
 		error = ENOMEM;
 	}
 
 err:
-	if (dsw != NULL)
-		dev_relthread(cdev, ref);
-	if (td != NULL && fp != NULL)
-		fdrop(fp, td);
 	if (error == 0) {
 		return (total - uio.uio_resid);
 	} else {
@@ -318,9 +272,8 @@ devfs_imfs_ioctl(rtems_libio_t *iop, ioctl_command_t request, void *buffer)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_null();
-	struct file *fp = NULL;
-	struct cdevsw *dsw = NULL;
 	struct file *fpop;
+	struct cdevsw *dsw;
 	int error, ref;
 	int flags = rtems_libio_to_fcntl_flags(iop->flags);
 
@@ -332,30 +285,22 @@ devfs_imfs_ioctl(rtems_libio_t *iop, ioctl_command_t request, void *buffer)
 		if (cdev->si_flags & SI_ALIAS) {
 			cdev = cdev->si_parent;
 		}
-		fp = rtems_bsd_libio_iop_to_file_hold(iop, td);
-		if (fp == NULL) {
-			error = EBADF;
-			goto err;
-		}
 		dsw = dev_refthread(cdev, &ref);
 		if (dsw == NULL) {
 			error = ENXIO;
 			goto err;
 		}
 		fpop = td->td_fpop;
-		curthread->td_fpop = fp;
+		curthread->td_fpop = (struct file *)iop;
 		error = dsw->d_ioctl(cdev, request, buffer, flags,
 		    td);
 		td->td_fpop = fpop;
+		dev_relthread(cdev, ref);
 	} else {
 		error = ENOMEM;
 	}
 
 err:
-	if (dsw != NULL)
-		dev_relthread(cdev, ref);
-	if (td != NULL && fp != NULL)
-		fdrop(fp, td);
 	return rtems_bsd_error_to_status_and_errno(error);
 }
 
@@ -379,42 +324,26 @@ devfs_imfs_poll(rtems_libio_t *iop, int events)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_wait_forever();
-	struct file *fp = NULL;
-	struct cdevsw *dsw = NULL;
 	struct file *fpop;
+	struct cdevsw *dsw;
 	int error, ref;
 
-	if (td != 0) {
-		if (cdev == NULL) {
-			error = POLLERR;
-			goto err;
-		}
-		if (cdev->si_flags & SI_ALIAS) {
-			cdev = cdev->si_parent;
-		}
-		fp = rtems_bsd_libio_iop_to_file_hold(iop, td);
-		if (fp == NULL) {
-			error = EBADF;
-			goto err;
-		}
-		dsw = dev_refthread(cdev, &ref);
-		if (dsw == NULL) {
-			error = POLLERR;
-			goto err;
-		}
-		fpop = td->td_fpop;
-		curthread->td_fpop = fp;
-		error = dsw->d_poll(cdev, events, td);
-		td->td_fpop = fpop;
-	} else {
-		error = ENOMEM;
+	if (cdev == NULL) {
+		return POLLERR;
 	}
+	if (cdev->si_flags & SI_ALIAS) {
+		cdev = cdev->si_parent;
+	}
+	dsw = dev_refthread(cdev, &ref);
+	if (dsw == NULL) {
+		return POLLERR;
+	}
+	fpop = td->td_fpop;
+	curthread->td_fpop = (struct file *)iop;
+	error = dsw->d_poll(cdev, events, td);
+	td->td_fpop = fpop;
+	dev_relthread(cdev, ref);
 
-err:
-	if (dsw != NULL)
-		dev_relthread(cdev, ref);
-	if (td != NULL && fp != NULL)
-		fdrop(fp, td);
 	return error;
 }
 
@@ -423,40 +352,26 @@ devfs_imfs_kqfilter(rtems_libio_t *iop, struct knote *kn)
 {
 	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
 	struct thread *td = rtems_bsd_get_curthread_or_wait_forever();
-	struct file *fp = NULL;
-	struct cdevsw *dsw = NULL;
 	struct file *fpop;
+	struct cdevsw *dsw;
 	int error, ref;
 
-	if (td != 0) {
-		if (cdev == NULL) {
-			error = EINVAL;
-		}
-		fp = rtems_bsd_libio_iop_to_file_hold(iop, td);
-		if (fp == NULL) {
-			error = EBADF;
-			goto err;
-		}
-		if (cdev->si_flags & SI_ALIAS) {
-			cdev = cdev->si_parent;
-		}
-		dsw = dev_refthread(cdev, &ref);
-		if (dsw == NULL) {
-			error = EINVAL;
-		}
-		fpop = td->td_fpop;
-		curthread->td_fpop = fp;
-		error = dsw->d_kqfilter(cdev, kn);
-		td->td_fpop = fpop;
-	} else {
-		error = ENOMEM;
+	if (cdev == NULL) {
+		return EINVAL;
+	}
+	if (cdev->si_flags & SI_ALIAS) {
+		cdev = cdev->si_parent;
 	}
+	dsw = dev_refthread(cdev, &ref);
+	if (dsw == NULL) {
+		return EINVAL;
+	}
+	fpop = td->td_fpop;
+	curthread->td_fpop = (struct file *)iop;
+	error = dsw->d_kqfilter(cdev, kn);
+	td->td_fpop = fpop;
+	dev_relthread(cdev, ref);
 
-err:
-	if (dsw != NULL)
-		dev_relthread(cdev, ref);
-	if (td != NULL && fp != NULL)
-		fdrop(fp, td);
 	return error;
 }
 
-- 
2.35.3



More information about the devel mailing list