O'Reilly logo

Understanding the Linux Kernel by Marco Cesati, Daniel P. Bovet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required