Skip to content

ufat: a writing call can fail with ImageBusy right after another one for the same block device #43

Open

Description

@brauner

For a method that writes, the service claims a passed block device
exclusively, the way a mount claims it. Device::from_passed() reopens
the descriptor with O_EXCL (src/fat/device.rs:229-245, since
cc11436 ("host: vet descriptors handed over by other processes")),
and EBUSY from that reopen becomes ImageBusy
(src/bin/ufat/service.rs:577-589). docs/varlink.md:646-648 says that
the device is claimed while a writing call runs.

ufat/src/fat/device.rs

Lines 229 to 245 in 49e4987

pub fn from_passed(fd: OwnedFd, access: Access) -> Result<Device> {
let md = sys::fstat(fd.as_fd())?;
let file = if access == Access::ReadWrite && md.file_type().is_block_device() {
let flags = sys::O_RDWR | sys::O_EXCL | sys::O_NONBLOCK | sys::O_NOCTTY;
File::from(sys::reopen(fd.as_fd(), flags).map_err(|e| {
let hint = if e.raw_os_error() == Some(EBUSY) {
", it is in use (mounted?)"
} else {
""
};
Error::from(e).context(format!("failed to claim the block device{}", hint))
})?)
} else {
File::from(fd)
};
Device::from_file(file)
}

In the kernel that claim belongs to the struct file the reopen made.
blkdev_open() makes the file the holder, bd_may_claim() refuses any
other holder, and only blkdev_release() gives the claim up. __fput()
calls that once the last reference to the file is gone (block/fops.c,
block/bdev.c and fs/file_table.c, as of v7.4-rc3). When the service
closes the file at the end of a call, that normally happens before
close() returns. But a process that looks at the service's descriptors
holds a reference to the file for a moment. Reading /proc/PID/fdinfo/N
or the link /proc/PID/fd/N, as lsof does, takes one (seq_show() and
proc_fd_link() in fs/proc/fd.c). If the service closes the file right
then, the reader puts the last reference, and the claim only goes away
once the reader returns to user space.

So of two writing calls in a row for the same block device, on one
connection or not, the second one can fail with ImageBusy although
nothing has the device mounted or claimed. The client can't tell that
from a mounted device.

This is from reading the source, ours and the kernel's, and wasn't
reproduced. #28 ran into the same window with BSD locks: a test that
listed /proc/self/fd held the last reference to the images of other
tests for a moment, and their next open failed with EBUSY.

Note that the command line has the same window between two runs for the
same image, for the claim of a block device and for the BSD lock it
takes on every image (Device::open_pinned() and Device::lock_and_wrap(),
device.rs:143-175).

What we want first is a test that shows it, say a thread that reads
/proc/PID/fdinfo of the service in a loop while a client makes writing
calls for one loop device back to back. Then there are two ways out:

  • Retry the reopen for a short while when it fails with EBUSY. A claim
    that is about to go away is gone after a short wait, while a mounted
    device keeps failing. A call for a mounted device then takes as long
    as the retries.
  • Document that ImageBusy can come right after a writing call for the
    same device, and that a client may retry.

Line numbers are at 49e4987.

Activity

  1. added
    bug
    on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions