Module Initialization and Deinitialization

When writing a loadable kernel module, there are three different things that need to be defined:

  • A declaration giving information about the type of module
  • A function to be called when the module is loaded. This can perform any initialization functions including registering the filesystem type with the kernel.
  • A function to be called when the module is unloaded. This can clean up any remaining filesystem structures and unregister the filesystem.

The various components that are applicable to uxfs are shown in ux_inode.c on lines 1304 to 1317. The module_init() call specifies the function to be run when the module is loaded while the module_exit() function specifies the function to be called when the module is unloaded. Both of these functions perform little work other than registering and unregistering the filesystem driver respectively. The DECLARE_FSTYPE_DEV() macro is shown below:

#define DECLARE_FSTYPE(var,type,read,flags) \
struct file_system_type var = { \
        name:       type, \
        read_super: read, \
        fs_flags:   flags, \
        owner:      THIS_MODULE, \
}

#define DECLARE_FSTYPE_DEV(var,type,read) \
         DECLARE_FSTYPE(var,type,read,FS_REQUIRES_DEV)

The kernel maintains a list of all such structures, one per filesystem. The entry for uxfs is added when calling register_filesystem(). When a mount system call enters the kernel, the filesystem name passed to mount is compared with the name field of each file_system_type structure. If a match is found, the read_super ...

Get UNIX Filesystems: Evolution, Design, and Implementation 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.