Bern University of Applied Sciences Engineering and Information Technology Module I-7410 Advanced Linux Virtual File System By Franz Meyer Version 1.4 April 2012 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Processes - Topics Layering Model Kernel Subsystems Virtual File System in Linux Kernel (version 2.6.35.9) Operation on Processes: Creation and Termination Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-2 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux The Linux Kernel 2.6 Layering Model User1 User2 User3 UserN Layer User System Call Interface File Process Memory Management Management Systems Device Control Networking Scheduling Multitasking Virtual Memory Block Devices Character Devices Network Drivers CPUs Memory Disks CDs Consoles Network interfaces Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Kernel Subsystem Kernel Kernel Features Hardware Slide-3 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (1) Process Management Creating and destroying processes Inter process communication and synchronization Processor scheduling Memory Management Provides virtual memory (virtual address space) Allocation/Liberation of memory blocks File Systems Most important view of the kernel by users/devices Structuring data on unstructured carrier devices Multiples files system types supported (ext2/3,reiser,fat32,etc.) Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-4 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (2) File Subsystem more detailed The implementation consists of three major layers File System Interface: contains file system relevant system calls like open, close, read, write, etc. The Virtual File System: Separates generic functions from their implementation by defining a clean virtual interface. This allows several concrete file system implementations to coexist on the same machine. Provides a mechanism for representing a file across a network (NFS). The concrete file system implementation or the remote file system protocol. File System Interface Virtual File System Interface Local File System Type 1 Local File System Type 2 Carrier Device Kernel RAM for pseudo FS Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-5 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (3) File System Organization Directory: can contain files and directories Directory Entry: a component in the path to a file or directory File: A collection of data that is described by meta-data Directories are files of type directory Meta Data: information about file systems and files Inode: meta data of a particular file or directory Superblock: contains information about a file system on a disk or disk partition Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-6 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (4) Virtual File System “Objects” Although the kernel is written in C object-oriented techniques can be used Data structures that contain data fields and function pointers to operate on these data fields. There are four primary object types in the VFS Superblock: represents a specific mounted file system super_operations: the superblock's methods Inode: represents a specific file inode_operations: the inode's methods Dentry: represents a directory entry, a single component in the path. Note that a directory is a file. dentry_operations: the dentry's methods File: represents an open file associated with a process file_operations: the methods that a process can invoke on an open file in the form of a file system call Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-7 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (5) Virtual File System “Objects”, cont. In the driver part of the course you will see inode objects and file objects. Of course, the glue between those objects is the dentry object. The inode object Recall that an inode in a Unix file system represents all information about a file or directory When a process opens a file the kernel finds the corresponding inode on the disk an loads it into the inode cache and adds some control information struct inode { from <linux/fs.h>, some fields deleted struct hlist_node i_hash; /* hash list struct list_head i_list; /* inode list */ struct list_head i_dentry; /* directroy entry list */ unsigned long i_ino; /* i­number */ atomic_t i_count; /* reference counter */ unsigned int i_nlink; /* numer of hard links */ uid_t i_uid; /* user id of owner */ gid_t i_gid; /* group id of owner */ ...... Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-8 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (6) Virtual File System “Objects”, inode object, cont. struct inode { cont .... const struct inode_operations i_op; /*file operations*/ const struct file_operations *i_fop;/*default file ops*/ .... union { /* link to device type */ struct pipe_inode_info *i_pipe; /*pipe special file*/ struct block_device *i_bdev; /*block special file*/ struct cdev *i_cdev; /*character spec file*/ }; .... void *i_private; /*fs private pointer*/ }; Virtual File System “Objects”, inode operations see struct inode_operation {...}; Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-9 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (7) Virtual File System “Objects”, cont. The file object Recall that a file object in a Unix file system is the in-memory representation of a file opened by a process The file object is created in response to the open() system call and destroyed in the close() system call. If multiple processes open and manipulate a file the kernel will create an instance of a file object for each process The file object points back to the dentry object that in turn points back to the inode object. struct file { from <linux/fs.h>, some fields deleted struct path f_path; /*contains dentry*/ struct file_operation *f_op; /*file operation pnters*/ atomic_t f_count; /*reference counter*/ unsigned int f_flags; /*specified open flags*/ loff_t i_pos; /*file position*/ void *private_data; /*driver hook*/ ... Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-10 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (8) Virtual File System “Objects”, cont. The file operations struct file_operations { from <linux/fs.h>, some fields del struct module *owner loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); ... int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *, fl_owner_t id); ... /* more file operations */ }; Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-11 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (8) Virtual File System “Objects”, cont. The dentry object is a specific component in a path: a file, a directory, a mount point, etc. struct dentry { from <linux/dcache.h>, some fields del atomic_t d_count; /*reference counter*/ unsigned int d_flags; /*dentry flags*/ spinlock_t d_lock; /*per dentry lock*/ int d_mounted; /*is it a mount point?*/ struct inode *d_inode; /*associated inode*/ ... struct qstr d_name; struct list_head d_subdirs; /*our children */ struct list_head d_alias; /*inode alias list */ unsigned long d_time; /*used by d_revalidate */ const struct dentry_operations *d_op; struct super_block *d_sb; /*root of the dentry tree */ unsigned char d_iname[DNAME_INLINE_LEN_MIN]; /*small names*/ }; Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-12 Berne University of Applied Sciences School of Information Technology HTI Module: Advanced Linux Kernel Subsystem Linux Kernel 2.6 (9) Virtual File System “Objects”, cont. The dentry cache File to Inode: Find the inode instance (struct inode) that corresponds with a given instance of an open file (stuct file). Recall that an inode object is a system wide representation of an open file, while the file object is the per process representation of an open file, and the glue between these objects is a dentry (directory entry) object. struct dentry *dentry; struct file *file struct inode *inode inode = file­>f_path.dentry­>d_inode; Introduction to Virtual File System V1.0 © 2012 by franz.meyer@bfh.ch Slide-13