From: Gabriel Goller <g.goller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 1/2] process_locker: use ofd locking
Date: Mon, 4 Dec 2023 14:18:19 +0100 [thread overview]
Message-ID: <20231204131820.204423-2-g.goller@proxmox.com> (raw)
In-Reply-To: <20231204131820.204423-1-g.goller@proxmox.com>
Instead of using normal fcntl locking, use the OFD (open file
descriptor) flags [0].
This blocks the file on the file-descriptor level, not on the
process-level.
This solves two problems:
- If a process closes any file descriptor referring to a file,
then all of the process's locks on that file are released,
regardless of the file descriptor(s) on which the locks were
obtained. This is bad: it means that a process can lose its
locks on a file such as /etc/passwd or /etc/mtab when for some
reason a library function decides to open, read, and close the
same file.
- The threads in a process share locks. In other words, a
multithreaded program can't use record locking to ensure that
threads don't simultaneously access the same region of a file.
(Copied from [0])
[0]: https://man7.org/linux/man-pages/man2/fcntl.2.html
Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
---
proxmox-sys/src/process_locker.rs | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/proxmox-sys/src/process_locker.rs b/proxmox-sys/src/process_locker.rs
index 4874da8..ac2a8a2 100644
--- a/proxmox-sys/src/process_locker.rs
+++ b/proxmox-sys/src/process_locker.rs
@@ -1,7 +1,7 @@
//! Inter-process reader-writer lock builder.
//!
//! This implementation uses fcntl record locks with non-blocking
-//! F_SETLK command (never blocks).
+//! F_OFD_SETLK command (never blocks).
//!
//! We maintain a map of shared locks with time stamps, so you can get
//! the timestamp for the oldest open lock with
@@ -13,8 +13,6 @@ use std::sync::{Arc, Mutex};
use anyhow::{bail, Error};
-// fixme: use F_OFD_ locks when implemented with nix::fcntl
-
// Note: flock lock conversion is not atomic, so we need to use fcntl
/// Inter-process reader-writer lock
@@ -53,9 +51,10 @@ impl Drop for ProcessLockSharedGuard {
l_pid: 0,
};
- if let Err(err) =
- nix::fcntl::fcntl(data.file.as_raw_fd(), nix::fcntl::FcntlArg::F_SETLKW(&op))
- {
+ if let Err(err) = nix::fcntl::fcntl(
+ data.file.as_raw_fd(),
+ nix::fcntl::FcntlArg::F_OFD_SETLKW(&op),
+ ) {
panic!("unable to drop writer lock - {}", err);
}
}
@@ -93,9 +92,10 @@ impl Drop for ProcessLockExclusiveGuard {
l_pid: 0,
};
- if let Err(err) =
- nix::fcntl::fcntl(data.file.as_raw_fd(), nix::fcntl::FcntlArg::F_SETLKW(&op))
- {
+ if let Err(err) = nix::fcntl::fcntl(
+ data.file.as_raw_fd(),
+ nix::fcntl::FcntlArg::F_OFD_SETLKW(&op),
+ ) {
panic!("unable to drop exclusive lock - {}", err);
}
@@ -132,7 +132,7 @@ impl ProcessLocker {
l_pid: 0,
};
- nix::fcntl::fcntl(file.as_raw_fd(), nix::fcntl::FcntlArg::F_SETLK(&op))?;
+ nix::fcntl::fcntl(file.as_raw_fd(), nix::fcntl::FcntlArg::F_OFD_SETLK(&op))?;
Ok(())
}
--
2.39.2
next prev parent reply other threads:[~2023-12-04 13:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-04 13:18 [pbs-devel] [PATCH proxmox{, -backup} 0/2] Move ProcessLocker to tmpfs Gabriel Goller
2023-12-04 13:18 ` Gabriel Goller [this message]
2023-12-04 13:18 ` [pbs-devel] [PATCH proxmox-backup 2/2] datastore: store datastore lock on tmpfs Gabriel Goller
2023-12-06 13:29 ` [pbs-devel] [PATCH proxmox{, -backup} 0/2] Move ProcessLocker to tmpfs Gabriel Goller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231204131820.204423-2-g.goller@proxmox.com \
--to=g.goller@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox