diff --git a/crates/object-capacity/README.md b/crates/object-capacity/README.md index 71ab1c2900..c79ff4d53c 100644 --- a/crates/object-capacity/README.md +++ b/crates/object-capacity/README.md @@ -287,7 +287,7 @@ The configuration constants are defined in `crates/config/src/constants/capacity | `RUSTFS_CAPACITY_MAX_TIMEOUT` | `15s` (`60s` with `RUSTFS_DRIVE_TIMEOUT_PROFILE=high_latency`) | Dynamic-timeout upper bound | | `RUSTFS_CAPACITY_STALL_TIMEOUT` | `20s` | Stall-detection threshold | -On Linux, enable `RUSTFS_CAPACITY_USE_MOUNT_USAGE=true` for whole filesystems used exclusively by RustFS. The fast path requires kernel mount IDs and the process mount table. It rejects subdirectory bind mounts, multiple views of a device in the mount namespace, and mounts with child mounts. The reading estimates used capacity from allocated filesystem blocks, which can include metadata and differ from the sum of file lengths. This mode does not count files. Ineligible or failed probes fall back to directory scanning and use file samples if the scan times out. Other platforms and configurations that follow symlinks keep the default scan behavior. Restart RustFS after changing this setting. +On Linux, `RUSTFS_CAPACITY_USE_MOUNT_USAGE=true` enables direct readings on whole XFS, ext-family, and tmpfs mounts used exclusively by RustFS. The fast path requires kernel mount IDs and the process mount table. A symlink used as the drive root resolves to its target. Subdirectory bind mounts, multiple views of a device, mounts with child mounts, and pooled filesystems such as Btrfs or ZFS use directory scanning. The reading estimates used capacity from allocated filesystem blocks, which can include metadata and differ from summed file lengths. Direct readings do not count files. When a directory scan in this mode times out, it estimates usage from available file samples. Other platforms and configurations that follow symlinks within the drive use the default scan behavior. Restart RustFS after changing this setting. Explicit `RUSTFS_CAPACITY_STAT_TIMEOUT` and `RUSTFS_CAPACITY_MAX_TIMEOUT` values take precedence over the drive-timeout profile. diff --git a/crates/object-capacity/README_ZH.md b/crates/object-capacity/README_ZH.md index 27ff4b09ec..5c1b4431b6 100644 --- a/crates/object-capacity/README_ZH.md +++ b/crates/object-capacity/README_ZH.md @@ -287,7 +287,7 @@ get_capacity_manager() | `RUSTFS_CAPACITY_MAX_TIMEOUT` | `15s` | 动态超时上界 | | `RUSTFS_CAPACITY_STALL_TIMEOUT` | `20s` | 无进展 stall 判定阈值 | -在 Linux 上可为 RustFS 独占的整卷文件系统启用 `RUSTFS_CAPACITY_USE_MOUNT_USAGE=true`。快速路径依赖内核挂载 ID 和进程挂载表,会跳过子目录 bind mount、挂载命名空间内同一设备的多个视图,以及带子挂载的文件系统。此模式按已分配块估算容量,可能包含元数据,也可能与文件长度之和不同,无文件计数。挂载不符合条件或查询失败时,回退到目录扫描,超时时使用文件采样估算。其他平台及启用符号链接跟随时使用默认扫描行为。更改设置后需重启 RustFS。 +在 Linux 上设置 `RUSTFS_CAPACITY_USE_MOUNT_USAGE=true`,可直接读取 RustFS 独占的 XFS、ext 系列和 tmpfs 整卷挂载。快速路径依赖内核挂载 ID 和进程挂载表,磁盘根路径是符号链接时会解析到目标。子目录 bind mount、同一设备的多个挂载视图、带子挂载的卷,以及 Btrfs、ZFS 等共享池文件系统走目录扫描。直接读取按已分配块估算容量,可能包含元数据,也可能与文件长度之和不同,无文件计数。此模式下目录扫描超时时,使用已有的文件样本估算。其他平台或启用盘内符号链接跟随时走默认扫描。更改设置后需重启 RustFS。 ### 配置缓存注意事项 diff --git a/crates/object-capacity/src/scan.rs b/crates/object-capacity/src/scan.rs index f6b7f5b26c..0b93d2cdcf 100644 --- a/crates/object-capacity/src/scan.rs +++ b/crates/object-capacity/src/scan.rs @@ -1504,12 +1504,17 @@ mod tests { mount_usage: Some(mount_usage), ..tight_limits(Duration::ZERO) }; - let scan = scan_dir_blocking(path, &limits, &AtomicBool::new(false)).expect("read usage at a mount root"); - assert!(scan.is_estimated); - assert!(!scan.timed_out); - assert!(!scan.metadata_incomplete); - assert_eq!(scan.file_count, 0); - assert_eq!(scan.metadata_reads, 0); + let temp_dir = tempfile::TempDir::new().expect("create symlink fixture directory"); + let drive_link = temp_dir.path().join("drive"); + std::os::unix::fs::symlink(path, &drive_link).expect("link to mount root"); + for root in [path, drive_link.as_path()] { + let scan = scan_dir_blocking(root, &limits, &AtomicBool::new(false)).expect("read usage at a mount root"); + assert!(scan.is_estimated); + assert!(!scan.timed_out); + assert!(!scan.metadata_incomplete); + assert_eq!(scan.file_count, 0); + assert_eq!(scan.metadata_reads, 0); + } } #[test] diff --git a/crates/utils/src/os/linux.rs b/crates/utils/src/os/linux.rs index f6843bc221..27cefb9cc0 100644 --- a/crates/utils/src/os/linux.rs +++ b/crates/utils/src/os/linux.rs @@ -61,8 +61,9 @@ pub struct MountUsageSnapshot(OnceLock>>); impl MountUsageSnapshot { /// Return filesystem-reported used bytes for a directory at an eligible mount root. /// - /// Returns `None` for shared or partial mounts, mounts with children, or - /// when the kernel cannot identify or read the mounted filesystem. + /// Direct accounting is supported for XFS, the ext family, and tmpfs. + /// Returns `None` when the mount or its filesystem type is ineligible + /// or the kernel cannot identify or read the mounted filesystem. pub fn used_bytes(&self, path: &Path) -> Option { let directory = open(path, OFlags::PATH | OFlags::DIRECTORY | OFlags::CLOEXEC, Mode::empty()).ok()?; let stat = statx(&directory, "", AtFlags::EMPTY_PATH, StatxFlags::MNT_ID).ok()?; @@ -82,11 +83,21 @@ impl MountUsageSnapshot { return None; } // Read the same mount that was classified, even if the path was replaced. - let (_, _, used) = statfs_space_usage(&fstatfs(directory).ok()?, path).ok()?; + let stat = fstatfs(directory).ok()?; + if !supports_direct_mount_usage(stat.f_type as u64) { + return None; + } + let (_, _, used) = statfs_space_usage(&stat, path).ok()?; Some(used) } } +fn supports_direct_mount_usage(fs_type: u64) -> bool { + // Distinct mount and device IDs do not establish independent block + // accounting on filesystems that share a pool across subvolumes. + matches!(get_fs_type(fs_type), "XFS" | "EXT4" | "TMPFS") +} + fn eligible_usage_mounts(mountinfo: &str) -> Option> { let mut devices = HashMap::new(); let mut parents = HashSet::new(); @@ -514,6 +525,17 @@ mod tests { assert!(eligible_usage_mounts("1 0 no-device /").is_none()); } + #[test] + fn mount_usage_uses_only_filesystems_with_independent_block_accounting() { + assert!(supports_direct_mount_usage(0x58465342)); // XFS + assert!(supports_direct_mount_usage(0xEF53)); // ext family + assert!(supports_direct_mount_usage(0x01021994)); // tmpfs + assert!(!supports_direct_mount_usage(0x9123683E)); // Btrfs + assert!(!supports_direct_mount_usage(0x2FC12FC1)); // ZFS + assert!(!supports_direct_mount_usage(0x794C7630)); // OverlayFS + assert!(!supports_direct_mount_usage(0x6969)); // NFS + } + #[test] fn reject_relative_path_for_cross_device_validation() { let err = ensure_no_sub_mounts("relative/path", &[]).unwrap_err();