how to do a Non-blocking read

1,063 views
Skip to first unread message

Nafid Haque

unread,
May 31, 2012, 4:54:13 AM5/31/12
to osxfus...@googlegroups.com
I made a small file system based on OSXFUSE which is working fine at the moment, however, when I am performing a read on the FUSE device descriptor it seems to be blocking. What I mean to say is the FUSE driver seems to wait indefinitely until there is any data on the descriptor. Is there a way to make the FUSE driver work in a non-blocking manner so that my program can perform other tasks and not wait for an event on the FUSE device.

Any help would be appreciated.

Thanks

Nafid

Benjamin Fleischer

unread,
May 31, 2012, 5:10:47 AM5/31/12
to osxfus...@googlegroups.com
Hi Nafid,

When opening the FUSE device with O_NONBLOCK a read should return immediately with EAGAIN when there is no new message. I have not tried this but fcntl(fd, F_SETFL, O_NONBLOCK) should work for an already open file descriptor.

Regards,
Benjamin

Nafid Haque

unread,
May 31, 2012, 5:20:38 AM5/31/12
to osxfus...@googlegroups.com
Unfortunately this is not working for me in the OS X environment. But in Linux if I set the flag to O_NONBLOCK then it returns. Any further ideas?

Thanks for you prompt reply.

Regards

Nafid

Camilo Aguilar

unread,
May 31, 2012, 6:05:49 AM5/31/12
to osxfus...@googlegroups.com

Fuse low level API is non blocking.

Nafid Haque

unread,
May 31, 2012, 7:09:35 AM5/31/12
to osxfus...@googlegroups.com
In the fuse_device_read() function of fuse_device.c file of the OSXFUSE kext I can see the option to set the driver to perform a non-blocking read. However, doing this ( fcntl(fd, F_SETFL, O_NONBLOCK) ) is not setting the flag to O_NONBLOCK. Is there any other way to set it?

Regards

Nafid

Benjamin Fleischer

unread,
Jun 5, 2012, 7:03:56 PM6/5/12
to osxfus...@googlegroups.com
Hi Nafid,

> Unfortunately this is not working for me in the OS X environment. But in Linux if I set the flag to O_NONBLOCK then it returns. Any further ideas?

Can you share some sample code that works on Linux but does not work on OS X?

A patch, that enables the select operation on the /dev/osxfuse devices, has been submitted a few month ago. fuse_device_select() allows to check if there are any new message before calling read. This patch has not made its way in the OSXFUSE 2.x.y branch because it is not backwards compatible, meaning file systems using this function won't work on older 2.x.y releases.

The patch can be found in the master branch at https://github.com/osxfuse/kext/commit/9258035f8f33ad611ccf4d3869d82ba1890e95fd

Regards,
Benjamin

Benjamin Fleischer

unread,
Jun 5, 2012, 10:06:04 PM6/5/12
to osxfus...@googlegroups.com
There is a bug in function fuse_device_read. fuse_device_read is non-blocking if the FNONBLOCK bit is set in ioflags. But the correct bit to check for is IO_NDELAY. See commit 7ad9a43bddd4fcd3a00f9794069355837b5cbb9f. After applying the patch fcntl(...) seems to work for me.

If I'm not mistaken, non-blocking reads (from a FUSE device) cannot have worked in MacFUSE either, because this part of the code has not been touched since the MacFUSE fork. 

Let me know if this fixes the issue.

Nafid Haque

unread,
Jun 6, 2012, 3:08:04 AM6/6/12
to osxfus...@googlegroups.com
Hello Benjamin,

First of all thank you very much for looking in to the issue. I can confirm you that the problem of non-blocking read was also present in MacFuse. I will be trying the patches you mentioned and report back any issues I face.

Thanks and Regards


Nafid

On Thursday, May 31, 2012 10:54:13 AM UTC+2, Nafid Haque wrote:
On Thursday, May 31, 2012 10:54:13 AM UTC+2, Nafid Haque wrote:
On Thursday, May 31, 2012 10:54:13 AM UTC+2, Nafid Haque wrote:
Reply all
Reply to author
Forward
0 new messages