git: 17e0a1c6e9e4 - stable/13 - file: Move code to share fdtol structs into kern_descrip.c

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 11 Aug 2022 14:36:27 UTC
The branch stable/13 has been updated by markj:

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

commit 17e0a1c6e9e47b7fc1ab4f6144d7dcf792c3eaec
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-08-04 13:39:25 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-08-11 14:36:11 +0000

    file: Move code to share fdtol structs into kern_descrip.c
    
    This ensures the filedesc-to-leader code is consistently encapsulated in
    kern_descrip.c.
    
    No functional change intended.
    
    Reviewed by:    kib
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit d07675a9352efce5e997e987080b3f98bbfdac96)
---
 sys/kern/kern_descrip.c | 12 +++++++++++-
 sys/kern/kern_fork.c    |  5 +----
 sys/sys/filedesc.h      |  3 +++
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index 9d73417b7a3d..ff999cc82f97 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -4239,7 +4239,8 @@ mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
 }
 
 struct filedesc_to_leader *
-filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
+filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp,
+    struct proc *leader)
 {
 	struct filedesc_to_leader *fdtol;
 
@@ -4263,6 +4264,15 @@ filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, s
 	return (fdtol);
 }
 
+struct filedesc_to_leader *
+filedesc_to_leader_share(struct filedesc_to_leader *fdtol, struct filedesc *fdp)
+{
+	FILEDESC_XLOCK(fdp);
+	fdtol->fdl_refcount++;
+	FILEDESC_XUNLOCK(fdp);
+	return (fdtol);
+}
+
 static int
 sysctl_kern_proc_nfds(SYSCTL_HANDLER_ARGS)
 {
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 32866aa26fba..90fc027b2979 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -451,10 +451,7 @@ do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread *
 			 * Shared file descriptor table, and shared
 			 * process leaders.
 			 */
-			fdtol = p1->p_fdtol;
-			FILEDESC_XLOCK(p1->p_fd);
-			fdtol->fdl_refcount++;
-			FILEDESC_XUNLOCK(p1->p_fd);
+			fdtol = filedesc_to_leader_share(p1->p_fdtol, p1->p_fd);
 		} else {
 			/*
 			 * Shared file descriptor table, and different
diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h
index 9ae7b543e4a1..345576438c67 100644
--- a/sys/sys/filedesc.h
+++ b/sys/sys/filedesc.h
@@ -263,6 +263,9 @@ struct	filedesc *fdshare(struct filedesc *fdp);
 struct filedesc_to_leader *
 	filedesc_to_leader_alloc(struct filedesc_to_leader *old,
 	    struct filedesc *fdp, struct proc *leader);
+struct filedesc_to_leader *
+	filedesc_to_leader_share(struct filedesc_to_leader *fdtol,
+	    struct filedesc *fdp);
 int	getvnode(struct thread *td, int fd, cap_rights_t *rightsp,
 	    struct file **fpp);
 int	getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,