diff options
author | Mateusz Guzik <mjg@FreeBSD.org> | 2021-01-12 16:05:27 +0000 |
---|---|---|
committer | Mateusz Guzik <mjg@FreeBSD.org> | 2021-01-13 02:27:03 +0000 |
commit | 530b699a62ad0f1e5718825d60ddb1ec9b214489 (patch) | |
tree | 584bb20248937af08fbd58f2a2388dbc2403bed8 /sys | |
parent | 4faa375cdd4e36d3a5b7fc46ee9dd8079a5e26f7 (diff) | |
download | src-530b699a62ad0f1e5718825d60ddb1ec9b214489.tar.gz src-530b699a62ad0f1e5718825d60ddb1ec9b214489.zip |
fd: add finstall_refed
Can be used to consume an already existing reference and consequently
avoid atomic ops.
Diffstat (limited to 'sys')
-rw-r--r-- | sys/kern/kern_descrip.c | 28 | ||||
-rw-r--r-- | sys/sys/filedesc.h | 2 |
2 files changed, 22 insertions, 8 deletions
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 237d15fb5387..407280d79850 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -2059,7 +2059,7 @@ _finstall(struct filedesc *fdp, struct file *fp, int fd, int flags, } int -finstall(struct thread *td, struct file *fp, int *fd, int flags, +finstall_refed(struct thread *td, struct file *fp, int *fd, int flags, struct filecaps *fcaps) { struct filedesc *fdp = td->td_proc->p_fd; @@ -2067,18 +2067,30 @@ finstall(struct thread *td, struct file *fp, int *fd, int flags, MPASS(fd != NULL); - if (!fhold(fp)) - return (EBADF); FILEDESC_XLOCK(fdp); error = fdalloc(td, 0, fd); + if (__predict_true(error == 0)) { + _finstall(fdp, fp, *fd, flags, fcaps); + } + FILEDESC_XUNLOCK(fdp); + return (error); +} + +int +finstall(struct thread *td, struct file *fp, int *fd, int flags, + struct filecaps *fcaps) +{ + int error; + + MPASS(fd != NULL); + + if (!fhold(fp)) + return (EBADF); + error = finstall_refed(td, fp, fd, flags, fcaps); if (__predict_false(error != 0)) { - FILEDESC_XUNLOCK(fdp); fdrop(fp, td); - return (error); } - _finstall(fdp, fp, *fd, flags, fcaps); - FILEDESC_XUNLOCK(fdp); - return (0); + return (error); } /* diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h index 132aa6c1de03..f0cae3ed6911 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -234,6 +234,8 @@ void _finstall(struct filedesc *fdp, struct file *fp, int fd, int flags, struct filecaps *fcaps); int finstall(struct thread *td, struct file *fp, int *resultfd, int flags, struct filecaps *fcaps); +int finstall_refed(struct thread *td, struct file *fp, int *resultfd, int flags, + struct filecaps *fcaps); int fdalloc(struct thread *td, int minfd, int *result); int fdallocn(struct thread *td, int minfd, int *fds, int n); int fdcheckstd(struct thread *td); |