12.5. Implementations of VFS System Calls
For the sake of brevity, we cannot discuss the implementation of all VFS system calls listed in Table 12-1. However, it could be useful to sketch out the implementation of a few system calls, just to show how VFS's data structures interact.
Let us reconsider the example proposed at the beginning of this chapter: a user issues a shell command that copies an MS-DOS file /floppy/TEST in an Ext2 file /tmp/test. The command shell invokes an external program like 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 {
l = read(inf, buf, 4096);
write(outf, buf, l);
} while (l);
close(outf);
close(inf);
Actually, the code of the real cp program is more complicated, since it must also check for possible error codes returned by each system call. In our example, we just focus our attention on the "normal" behavior of a copy operation.
12.5.1. The open( ) System Call
The open( ) system call is serviced by the sys_open( ) function, which receives as 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 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 /floppy/TEST for ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access