next up previous
Next: The write callback Up: Basic Device Creation Previous: The open and close


The read callback

Returning to our cat /dev/hello-world example, what happens after the open is successful? Next, cat will try to use read(2), which will get proxied by FUSD to the function do_read on line 13. This function takes some additional arguments that we didn't see in the open and close callbacks:

The semantics of the return value are the same as if the callback were being written inside the kernel itself:

The first time a read is done on a device file, the user's file pointer (*offset) is 0. In the case of this first read, a greeting message of Hello, world! is copied back to the user, as seen on line 24. The user's file pointer is then advanced. The next read therefore fails the comparison at line 20, falling straight through to return 0, or EOF.

In this simple program, we also see an example of an error return on line 22: if the user tries to do a read smaller than the length of the greeting message, the read will fail with -EINVAL. (In an actual driver, it would normally not be an error for a user to provide a smaller read buffer than the size of the available data. The right way for drivers to handle this situation is to return partial data, then move *offset forward so that the remainder is returned on the next read(). We see an example of this in Program 2.)


next up previous
Next: The write callback Up: Basic Device Creation Previous: The open and close
Jeremy Elson 2003-08-20