git: c1aa97cf79a6 - stable/14 - file: Add foffset_lock_pair()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 15 Apr 2025 02:25:36 UTC
The branch stable/14 has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=c1aa97cf79a6fd40eb89bcbd4faebdc5656a6279
commit c1aa97cf79a6fd40eb89bcbd4faebdc5656a6279
Author: Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-03-31 01:25:16 +0000
Commit: Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-04-15 02:25:24 +0000
file: Add foffset_lock_pair()
This will be used in kern_copy_file_range(), which needs to lock two
offsets.
Reviewed by: kib, rmacklem
MFC after: 2 weeks
Differential Revision: https://reviews.freebsd.org/D49440
(cherry picked from commit 12ecb0fe0afda8c051605045e446371ddd34741f)
---
sys/kern/vfs_vnops.c | 20 ++++++++++++++++++++
sys/sys/file.h | 2 ++
2 files changed, 22 insertions(+)
diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c
index c28d6e66853f..c447ceea1634 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -894,6 +894,26 @@ foffset_read(struct file *fp)
}
#endif
+void
+foffset_lock_pair(struct file *fp1, off_t *off1p, struct file *fp2, off_t *off2p,
+ int flags)
+{
+ KASSERT(fp1 != fp2, ("foffset_lock_pair: fp1 == fp2"));
+
+ /* Lock in a consistent order to avoid deadlock. */
+ if ((uintptr_t)fp1 > (uintptr_t)fp2) {
+ struct file *tmpfp;
+ off_t *tmpoffp;
+
+ tmpfp = fp1, fp1 = fp2, fp2 = tmpfp;
+ tmpoffp = off1p, off1p = off2p, off2p = tmpoffp;
+ }
+ if (fp1 != NULL)
+ *off1p = foffset_lock(fp1, flags);
+ if (fp2 != NULL)
+ *off2p = foffset_lock(fp2, flags);
+}
+
void
foffset_lock_uio(struct file *fp, struct uio *uio, int flags)
{
diff --git a/sys/sys/file.h b/sys/sys/file.h
index bef21d39e641..07f6bbd5bcae 100644
--- a/sys/sys/file.h
+++ b/sys/sys/file.h
@@ -85,6 +85,8 @@ struct ucred;
#define FOF_NEXTOFF_W 0x08 /* Also update f_nextoff[UIO_WRITE] */
#define FOF_NOUPDATE 0x10 /* Do not update f_offset */
off_t foffset_lock(struct file *fp, int flags);
+void foffset_lock_pair(struct file *fp1, off_t *off1p, struct file *fp2,
+ off_t *off2p, int flags);
void foffset_lock_uio(struct file *fp, struct uio *uio, int flags);
void foffset_unlock(struct file *fp, off_t val, int flags);
void foffset_unlock_uio(struct file *fp, struct uio *uio, int flags);