Opening Files

Files must be opened before reading or writing. In the view of the application, this is done by the open function of the standard library, which returns a file descriptor.23 The function uses the identically named open system call, which invokes the sys_open function in fs/open.c. The associated code flow diagram is shown in Figure 8-11.

As a first step, force_o_largefile checks if the flag o_largefile should always be set irregardless of which flags were passed from userland. This is the case if the word size of the underlying processor is not 32 bits, that is, a 64-bit system. Such systems use 64-bit indexing, and large files are thus the only sensible default on them. The proper work of opening the file is then delegated to do_sys_open.

In the kernel, each opened file is represented by a file descriptor that acts as a position index for a process-specific array (task_struct->files->fd_array). This array contains an instance of the abovementioned file structure with all necessary file information for each opened file. For this reason, get_unused_fd_flags is first invoked to find a used file descriptor.

Because a string with the name of the file is used as a system call parameter, the main problem is to find the matching inode. The procedure described above does this.

23It would also be possible to use openat, which opens a file relative to a directory. The mechanisms are, however, more or less identical.

Figure 8-11: Code flow diagram for sys_open.

do_filp_open finds the file inode with the support of two helper functions.

1. open_namei invokes the path_lookup function to find the inode and performs several additional checks (e.g., to ascertain whether the application is trying to open a directory as if it were a regular file). If a new filesystem entry needs to be created, the function also applies the current default settings for the permission bits as stored in the process's umask (current->fs->umask).

2. nameidata_to_filp initializes the readahead structure, places the newly generated file instance on the s_files list of the superblock (see Section 8.4.1), and invokes the open function in the file_operations structure of the underlying filesystem.

fd_install must then install the file instance in files->fd from the task structure of the process before control is transferred back to the user process to which the file descriptor is returned.

Continue reading here: Read

Was this article helpful?

0 0