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)
This commit is contained in:
Mark Johnston 2025-03-31 01:25:16 +00:00
parent e6a3962d7d
commit c1aa97cf79
2 changed files with 22 additions and 0 deletions

View file

@ -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)
{

View file

@ -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);