Implementations of VFS System Calls
For the sake of brevity, we cannot discuss the implementation of all the VFS system calls listed in Table 12-1. However, it could be useful to sketch out the implementation of a few system calls, in order to show how VFS’s data structures interact.
Let’s reconsider the example proposed at the beginning of this chapter: a user issues a shell command that copies the MS-DOS file /floppy/TEST to the Ext2 file /tmp/test. The command shell invokes an external program such as cp, which we assume executes the following code fragment:
inf = open("/floppy/TEST", O_RDONLY, 0); outf = open("/tmp/test", O_WRONLY | O_CREAT | O_TRUNC, 0600); do { len = read(inf, buf, 4096); write(outf, buf, len); } while (len); close(outf); close(inf);
Actually, the code of the real cp program is more complicated, because it must also check for possible error codes returned by each system call. In our example, we focus our attention on the “normal” behavior of a copy operation.
The open( ) System Call
The open( )
system
call is serviced by the sys_open( )
function, which receives as its parameters the pathname filename
of the file to be opened, some
access mode flags flags
, and a
permission bit mask mode
if the
file must be created. If the system call succeeds, it returns a file
descriptor—that is, the index assigned to the new file in the current->files->fd
array of pointers
to file objects; otherwise, it returns -1.
In our example, open( )
is invoked twice; the first time to open ...
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.