Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between these two calls: read vs read_iter

I am exploring linux kernel code and in vfs_read function, I see these two calls. Can someone explain the difference between the two?

like image 608
Divija Gogineni Avatar asked Sep 05 '25 07:09

Divija Gogineni


1 Answers

So I assume you're referring to this snippet of code from vfs_read():

if (file->f_op->read)
    return file->f_op->read(file, buf, count, pos);
else if (file->f_op->read_iter)
    return new_sync_read(file, buf, count, pos);

Not sure how much background knowledge you have on this, so apologies if I write things you already know. read and read_iter act as function pointers within a f_op (file operations struct). File operations is a general struct containing a ton of different function pointers/declarations that can be used to manipulate anything considered a file. read and read_iter are two pointers in that struct.

In reality, we can't know exactly what read or read_iter are doing unless we know for sure what function is being pointed to for this particular file that is being read. So the more general question is, when do you use the read pointer and when should you use the read_iter pointer?

The best answer I've seen comes from a document in the Linux kernel:

``read``
called by read(2) and related system calls

``read_iter``
possibly asynchronous read with iov_iter as destination

The use of the word "possibly" tells you that these function pointers could in theory be used for whatever, so if you want further details you'd have to look at the specific functions called for a specific file you're interested in.

like image 70
wxz Avatar answered Sep 07 '25 21:09

wxz