Skip to content

struct file_operations

Purpose

struct file_operations defines how a character device responds to user-space file operations.

Common Callbacks

static const struct file_operations mydev_fops = {
    .owner = THIS_MODULE,
    .open = mydev_open,
    .read = mydev_read,
    .write = mydev_write,
    .poll = mydev_poll,
    .unlocked_ioctl = mydev_ioctl,
    .fasync = mydev_fasync,
    .release = mydev_release,
};

Common Fields

Field Purpose
.open Initialize per-open state
.read Copy data or events to user space
.write Accept user-space commands or data
.poll Report readiness to poll / epoll
.unlocked_ioctl Handle control commands
.fasync Register or unregister asynchronous listeners for SIGIO notification
.release Cleanup per-open state

.poll() vs .fasync()

Drivers commonly implement both .poll() and .fasync() to support different user-space I/O models.

Callback Purpose
.poll() Reports the current device readiness for poll() and epoll()
.fasync() Registers asynchronous listeners for SIGIO notification

A typical driver event updates the device state first, then notifies all supported mechanisms:

Device Event
data_ready = true
 ┌───────────────┬────────────────┐
 ▼               ▼                ▼
wake_up()   kill_fasync()     .poll()

The driver does not choose the notification model. Applications decide whether to use blocking I/O, poll(), epoll(), or SIGIO.