Saturday, July 29, 2006

Linux Kernel Programming Points to Remember

1 ) An 'inode' describes a file. A file can have several names (or no name at all), but it has a unique inode. A 'dentry' (directory entry) describes a name of a file: the inode plus the pathname. A 'vfsmount' describes the filesystem we are in.

2 ) INVOKING SYSTEM CALL FROM LINUX KERNEL - System calls defined in
can be used but the system call checks whether the provided buffer is a valid address or not. During normal operations, an address that lies in the user address range (0 - 3GB for standard kernel configuration) is considered valid, and an address that lies in the kernel address space (3GB - 4GB) is not.
If the system call is invoked from the kernel space, the system call will fail because the virtual address of our destination buffer will be in the kernel space.
The field 'addr_limit' in the 'task_struct' structure is used to define the highest virtual address that is considered valid, the macro get_fs() and set_fs() can be used to read & write this value.

Example:

# mm_segment_t fs;
# fs = get_fs(); /* save previous value */
# set_fs(KERNEL_DS); /* use kernel limit */

# /* Invoke system call */
# set_fs(fs);

It's important to restore original "fs" before returning to the user space. Otherwise, the user program that executed this code will retain permission to overwrite kernel memory

3 ) A 'struct nameidata' represents the result of a lookup.

4 ) A 'struct address_space' gives the mapping between the blocks in a file and blocks on disk.

5 ) Directories contain file names and the number of the corresponding inodes.

6 ) Name to inode is possible by doing the lookup but inode to name is not possible.

7 ) All hard links to a file live in the same filesystem but this is not true for synbolic links.

8 ) The filesystem keeps track of the number of hard links to a file, but not of the number of symbolic links.

9 ) There can be only one hardlink to a directory.

No comments: