* [PATCH] nfsrahead: enable event-driven mountinfo monitoring
@ 2026-02-11 19:01 Aaron Tomlin
2026-02-24 12:51 ` Steve Dickson
0 siblings, 1 reply; 2+ messages in thread
From: Aaron Tomlin @ 2026-02-11 19:01 UTC (permalink / raw)
To: steved, tbecker; +Cc: linux-nfs
The nfsrahead utility relies on parsing "/proc/self/mountinfo" to
correlate a device number with a specific NFS mount point. However, due
to the asynchronous nature of system initialisation, the relevant entry
in mountinfo may not be immediately available when the tool is executed.
Currently, the utility employs a naive polling mechanism, retrying the
search five times with a fixed 50ms delay (totalling 250ms). This
approach proves brittle on systems under high load or during
distinctively slow boot sequences, where the population of the mount
table may exceed this brief window. Consequently, nfsrahead fails to
configure the readahead value.
To mitigate this race condition and improve robustness, update
get_device_info() to utilise the libmount monitoring API.
The new implementation:
1. Initialises a monitor on /proc/self/mountinfo using
mnt_new_monitor().
2. Replaces the fixed polling loop with mnt_monitor_wait(),
allowing the process to sleep until the Linux kernel notifies
userspace of a change to the mount table.
3. Increases the maximum wait time to 10 seconds (MNT_NM_TIMEOUT),
significantly reducing the likelihood of a timeout failure
whilst ensuring the tool returns immediately once the mount
appears.
4. Retains the original polling logic as a fallback mechanism
should the monitor fail to initialise.
Signed-off-by: Aaron Tomlin <atomlin@atomlin.com>
---
tools/nfsrahead/main.c | 35 ++++++++++++++++++++++++++++++++++-
1 file changed, 34 insertions(+), 1 deletion(-)
diff --git a/tools/nfsrahead/main.c b/tools/nfsrahead/main.c
index b7b889ff..64953346 100644
--- a/tools/nfsrahead/main.c
+++ b/tools/nfsrahead/main.c
@@ -16,6 +16,7 @@
#define CONF_NAME "nfsrahead"
#define NFS_DEFAULT_READAHEAD 128
+#define MNT_NM_TIMEOUT 10000
/* Device information from the system */
struct device_info {
@@ -117,7 +118,39 @@ out_free_device_info:
static int get_device_info(const char *device_number, struct device_info *device_info)
{
- int ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
+ int ret;
+ struct libmnt_monitor *mn = NULL;
+ int timeout_ms = MNT_NM_TIMEOUT;
+
+ ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
+ if (ret == 0)
+ return 0;
+
+ mn = mnt_new_monitor();
+ if (!mn)
+ goto fallback;
+
+ if (mnt_monitor_enable_kernel(mn, 1) < 0) {
+ mnt_unref_monitor(mn);
+ goto fallback;
+ }
+
+ while (timeout_ms > 0) {
+ int rc = mnt_monitor_wait(mn, timeout_ms);
+ if (rc > 0) {
+ ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
+ if (ret == 0) {
+ mnt_unref_monitor(mn);
+ return 0;
+ }
+ } else {
+ break;
+ }
+ }
+ mnt_unref_monitor(mn);
+ return ret;
+
+fallback:
for (int retry_count = 0; retry_count < 5 && ret != 0; retry_count++) {
usleep(50000);
ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
--
2.51.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] nfsrahead: enable event-driven mountinfo monitoring
2026-02-11 19:01 [PATCH] nfsrahead: enable event-driven mountinfo monitoring Aaron Tomlin
@ 2026-02-24 12:51 ` Steve Dickson
0 siblings, 0 replies; 2+ messages in thread
From: Steve Dickson @ 2026-02-24 12:51 UTC (permalink / raw)
To: Aaron Tomlin, tbecker; +Cc: linux-nfs
On 2/11/26 2:01 PM, Aaron Tomlin wrote:
> The nfsrahead utility relies on parsing "/proc/self/mountinfo" to
> correlate a device number with a specific NFS mount point. However, due
> to the asynchronous nature of system initialisation, the relevant entry
> in mountinfo may not be immediately available when the tool is executed.
>
> Currently, the utility employs a naive polling mechanism, retrying the
> search five times with a fixed 50ms delay (totalling 250ms). This
> approach proves brittle on systems under high load or during
> distinctively slow boot sequences, where the population of the mount
> table may exceed this brief window. Consequently, nfsrahead fails to
> configure the readahead value.
>
> To mitigate this race condition and improve robustness, update
> get_device_info() to utilise the libmount monitoring API.
>
> The new implementation:
>
> 1. Initialises a monitor on /proc/self/mountinfo using
> mnt_new_monitor().
>
> 2. Replaces the fixed polling loop with mnt_monitor_wait(),
> allowing the process to sleep until the Linux kernel notifies
> userspace of a change to the mount table.
>
> 3. Increases the maximum wait time to 10 seconds (MNT_NM_TIMEOUT),
> significantly reducing the likelihood of a timeout failure
> whilst ensuring the tool returns immediately once the mount
> appears.
>
> 4. Retains the original polling logic as a fallback mechanism
> should the monitor fail to initialise.
>
> Signed-off-by: Aaron Tomlin <atomlin@atomlin.com>
Committed... (tag: nfs-utils-2-8-6-rc2)
steved.
> ---
> tools/nfsrahead/main.c | 35 ++++++++++++++++++++++++++++++++++-
> 1 file changed, 34 insertions(+), 1 deletion(-)
>
> diff --git a/tools/nfsrahead/main.c b/tools/nfsrahead/main.c
> index b7b889ff..64953346 100644
> --- a/tools/nfsrahead/main.c
> +++ b/tools/nfsrahead/main.c
> @@ -16,6 +16,7 @@
>
> #define CONF_NAME "nfsrahead"
> #define NFS_DEFAULT_READAHEAD 128
> +#define MNT_NM_TIMEOUT 10000
>
> /* Device information from the system */
> struct device_info {
> @@ -117,7 +118,39 @@ out_free_device_info:
>
> static int get_device_info(const char *device_number, struct device_info *device_info)
> {
> - int ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
> + int ret;
> + struct libmnt_monitor *mn = NULL;
> + int timeout_ms = MNT_NM_TIMEOUT;
> +
> + ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
> + if (ret == 0)
> + return 0;
> +
> + mn = mnt_new_monitor();
> + if (!mn)
> + goto fallback;
> +
> + if (mnt_monitor_enable_kernel(mn, 1) < 0) {
> + mnt_unref_monitor(mn);
> + goto fallback;
> + }
> +
> + while (timeout_ms > 0) {
> + int rc = mnt_monitor_wait(mn, timeout_ms);
> + if (rc > 0) {
> + ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
> + if (ret == 0) {
> + mnt_unref_monitor(mn);
> + return 0;
> + }
> + } else {
> + break;
> + }
> + }
> + mnt_unref_monitor(mn);
> + return ret;
> +
> +fallback:
> for (int retry_count = 0; retry_count < 5 && ret != 0; retry_count++) {
> usleep(50000);
> ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-02-24 12:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-11 19:01 [PATCH] nfsrahead: enable event-driven mountinfo monitoring Aaron Tomlin
2026-02-24 12:51 ` Steve Dickson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox