[PATCH 4/5] erofs-utils: mount: gracefully exit when `erofsmount_nbd()` encounts an error

zhaoyifan (H) zhaoyifan28 at huawei.com
Fri Dec 26 18:43:42 AEDT 2025


On 2025/12/26 14:49, Gao Xiang wrote:
>
>
> On 2025/12/23 18:04, Yifan Zhao wrote:
>> If the main process of `erofsmount_nbd()` encounters an error after the
>> nbd device has been successfully set up, it fails to disconnect it
>> before exiting, resulting in the subprocess not being cleaned up and
>> keeping its connection with NBD device.
>>
>> This patch resolves the issue by disconnecting NBD device before exiting
>> on error.
>>
>> Signed-off-by: Yifan Zhao <zhaoyifan28 at huawei.com>
>> ---
>> Note:
>> - I believe directly killing the child process is unsafe, as it may 
>> leave
>> in-flight NBD requests from the kernel unhandled, causing soft lockup.
>> - And I believe using nbdpath here is safe, as the child process 
>> maintains
>> the NBD device connection throughout, preventing concurrent access by 
>> other
>> actors.
>
> What if the child process is already exited earlier, and the current NBD
> device is reused for others?
>
> How about keeping the previous nbdfd for ioctl interfaces instead to
> avoid nbd device reuse.
>
OK, I will try this way.


Thanks,

Yifan

> Thanks,
> Gao Xiang
>
>>
>>   mount/main.c | 17 +++++++++++++++--
>>   1 file changed, 15 insertions(+), 2 deletions(-)
>>
>> diff --git a/mount/main.c b/mount/main.c
>> index 2a21979..d2d4815 100644
>> --- a/mount/main.c
>> +++ b/mount/main.c
>> @@ -1287,10 +1287,23 @@ static int erofsmount_nbd(struct 
>> erofs_nbd_source *source,
>>         if (!err) {
>>           err = mount(nbdpath, mountpoint, fstype, flags, options);
>> -        if (err < 0)
>> +        if (err < 0) {
>>               err = -errno;
>> +            if (msg.is_netlink) {
>> +                erofs_nbd_nl_disconnect(msg.nbdnum);
>> +            } else {
>> +                int nbdfd;
>> +
>> +                nbdfd = open(nbdpath, O_RDWR);
>> +                if (nbdfd > 0) {
>> +                    erofs_nbd_disconnect(nbdfd);
>> +                    close(nbdfd);
>> +                }
>> +            }
>> +            return err;
>> +        }
>>   -        if (!err && msg.is_netlink) {
>> +        if (msg.is_netlink) {
>>               id = erofs_nbd_get_identifier(msg.nbdnum);
>>                 err = IS_ERR(id) ? PTR_ERR(id) :
>


More information about the Linux-erofs mailing list