Reading and Writing a File
The section "The read( ) and write( ) System
Calls" in Chapter 12
described how the read( )
and
write( )
system calls are
implemented. The corresponding service routines end up invoking the file
object’s read
and write
methods, which may be
filesystem-dependent. For disk-based filesystems, these methods locate
the physical blocks that contain the data being accessed and activate
the block device driver to start the data transfer.
Reading a file is page-based: the kernel always transfers whole
pages of data at once. If a process issues a read( )
system call to get a few bytes, and
that data is not already in RAM, the kernel allocates a new page frame,
fills the page with the suitable portion of the file, adds the page to
the page cache, and finally copies the requested bytes into the process
address space. For most filesystems, reading a page of data from a file
is just a matter of finding what blocks on disk contain the requested
data. Once this is done, the kernel fills the pages by submitting the
proper I/O operations to the generic block layer. In practice, the
read
method of all disk-based
filesystems is implemented by a common function named generic_file_read( )
.
Write operations on disk-based files are slightly more complicated to handle, because the file size could increase, and therefore the kernel might allocate some physical blocks on the disk. Of course, how this is precisely done depends on the filesystem type. However, many disk-based filesystems ...
Get Understanding the Linux Kernel, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.