Linux Kernel development-Virtual File System

Source: Internet
Author: User
Document directory
  • Universal File System Interface
  • Super block object

The Virtual File System (VFS) provides interfaces related to the file system for the user space. the user program can perform read and write operations on different file systems on different media through standard UNIX file system calls.

Universal File System Interface

VFS allows you to directly use open (), read (), and write () without considering the specific file system and actual physical media. Standard System calls can also be performed between different media and file systems. VFS is responsible for coordination between different media and different file systems, and provides a common access method to the interface.

This general interface can be used to operate all types of file systems because the kernel establishes an abstraction layer on top of its underlying file system. This abstraction layer provides a general file system model that supports various file systems. VFS defines the basic data structures and interfaces supported by all file systems, and the actual file system implements these basic interfaces. The actual File System Code hides the implementation details under the unified interface and data structure, so in the VFS layer and other parts of the kernel, all file systems are the same.

VFS has four main object models:

  • Super block object: an installed file system;
  • Index Node object: indicates a file;
  • Directory item object: a component of the path;
  • File object: indicates a file. Note that the directory is also a file.

Each object model kernel defines the corresponding operation object and describes the methods that the kernel can use for this object.

Super block object

Each file system must implement a super block to store the information of a specific file system. It usually corresponds to a file system super block or a file system control block stored in a specific disk sector. For non-disk-based file systems, Super blocks are created and saved in the memory during use.

The super block is represented by the struct super_block struct:

1400 struct super_block {1401 struct list_head s_list;/* keep this first pointer to the super block linked list */1402 dev_t s_dev;/* search index; _ not _ kdev_t device identifier */1403 unsigned char s_dirt;/* modify (dirty) identifier */1404 unsigned char s_blocksize_bits; /* block size unit bits */1405 unsigned long s_blocksize;/* block size unit bytes */1406 loff_t s_maxbytes;/* max file size */1407 struct file_system_type * s_type; 1408 const struct super_operations * s_op;/× super block method ×/1409 const struct dquot_operations * dq_op;/× disk quota method ×/1410 const struct quotactl_ops * s_qcop; /× quota control method ×/1411 const struct export_operations * s_export_op;/× export method ×/1412 unsigned long s_flags; 1413 unsigned long s_magic; 1414 struct dentry * s_root; 1415 struct rw_semaphore s_umount; 1416 struct mutex s_lock; 1417 int s_count; 1418 atomic_t s_active; 1419 # ifdef config_security1420 void * s_security; 1421 # endif1422 const struct xattr_handler ** s_xattr; 14231424 struct list_head s_inodes;/* All inodes */1425 struct s_s_anon;/* anonymous dentries for (NFS) Exporting */1426 # ifdef config_smp1427 struct list_head _ percpu * s_files; 1428 # else1429 struct list_head s_files; 1430 # endif1431/* s_dentry_lru, s_nr_dentry_unused protected by dcache. c LRU locks */1432 struct list_head s_dentry_lru;/* unused dentry LRU */1433 int s_nr_dentry_unused; /* # Of dentry on LRU */14341435/* s_inode_lru_lock protects s_inode_lru and latency */1436 spinlock_t s_inode_lru_lock ____ rows; 1437 struct list_head s_inode_lru; /* unused inode LRU */1438 int s_nr_inodes_unused;/* # Of inodes on LRU */14391440 struct block_device * s_bdev; 1441 struct backing_dev_info * s_bdi; 1442 struct mtd_info * s_mtd; 1443 struct list_head s_instances; 1444 struct quota_info s_dquot;/* diskquota specific options */14451446 int s_frozen; 1447 wait_queue_head_t s_wait_unfrozen; 14481449 char s_id [32]; /* informational name */1450 u8 s_uuid [16];/* UUID */14511452 void * s_fs_info;/* filesystem private info */1453 fmode_t s_mode; 14541455/* granularity of C/M/atime in ns.1456 cannot be worse than a second */1457 u32 s_time_gran; 14581459/* 1460 * the next field is for VFS * only *. no filesystems have any business1461 * even looking at it. you had been warned.1462 */1463 struct mutex s_vfs_rename_mutex;/* kludge */14641465/* 1466 * filesystem subtype. if non-empty the filesystem type field1467 * In/proc/mounts will be "type. subtype "1468 */1469 char * s_subtype; 14701471/* 1472 * saved mount options for lazy filesystems using1473 * generic_show_options () 1474 */1475 char _ RCU * s_options; 1476 const struct dentry_operations * s_d_op;/* default d_op for dentries */14771478/* 1479 * saved pool identifier for cleancache (-1 means none) 1480 */1481 int cleancache_poolid; 14821483 struct shrinker s_shrink;/* per-Sb shrinker handle */1484 };

The s_op in the super block object defines the super block operation function table, which is represented by the super_operations struct. Each of these statements defines a function pointer for an operation:
1658 struct super_operations {1659 struct inode * (* alloc_inode) (struct super_block * SB); 1660 void (* destroy_inode) (struct inode *); 16611662 void (* dirty_inode) (struct inode *, int flags); 1663 int (* write_inode) (struct inode *, struct writeback_control * WBC); 1664 int (* drop_inode) (struct inode *); 1665 void (* evict_inode) (struct inode *); 1666 void (* put_super) (struct super_block *); 1667 void (* write_super) (struct super_block *); 1668 int (* sync_fs) (struct super_block * Sb, int wait); 1669 int (* freeze_fs) (struct super_block *); 1670 int (* unfreeze_fs) (struct super_block *); 1671 int (* statfs) (struct dentry *, struct kstatfs *); 1672 int (* remount_fs) (struct super_block *, int *, char *); 1673 void (* umount_begin) (struct super_block *); 16741675 int (* show_options) (struct seq_file *, struct vfsmount *); 1676 int (* show_devname) (struct seq_file *, struct vfsmount *); 1677 int (* show_path) (struct seq_file *, struct vfsmount *); 1678 int (* show_stats) (struct seq_file *, struct vfsmount *); 1679 # ifdef config_quota1680 ssize_t (* quota_read) (struct super_block *, Int, char *, size_t, loff_t); 1681 ssize_t (* quota_write) (struct super_block *, Int, const char *, size_t, loff_t); 1682 # endif1683 int (* bdev_try_to_free_page) (struct super_block *, struct page *, gfp_t); 1684 int (* nr_cached_objects) (struct super_block *); 1685 void (* free_cached_objects) (struct super_block *, INT); 1686 };

In a specific file system, there is a pointer to the super block structure, which will pass the operation function implementation of the file system to the struct. The preceding operation tables do not need to be fully implemented either. The file system can set unnecessary function pointers to null. In addition, the above functions are called in the context of the process and can be blocked if necessary.

Index Node object
The index Node object contains all the information required by the kernel for Operating Files And Directories. This information can be directly read from the disk index node.
An index node is represented by an inode struct. An index node represents a file in the file system. It can also be a special file such as a device or pipe.
744/* 745 * keep mostly read-only and often accessed (especially for 746 * the RCU path lookup and 'STAT' data) fields at the beginning 747 * of the 'struct inode' 748 */749 struct inode {750 umode_t I _mode; 751 unsigned short I _opflags; 752 uid_t I _uid; 753 gid_t I _gid; 754 unsigned int I _flags; 755 756 # ifdef limit 757 struct posix_acl * I _acl; 758 struct posix_acl * handle; 759 # endif 760 761 construst inode_operations * I _op; 762 struct super_block * I _sb; 763 struct address_space * I _mapping; 764 765 # ifdef config_security 766 void * I _security; 767 # endif 768 769/* Stat data, not accessed from path walking */770 unsigned long I _ino; 771/* 772 * filesystems may only read I _nlink directly. they shall use the 773 * Following funwing for modification: 774*775 * (set | clear | Inc | drop) _ nlink 776 * inode _ (INC | dec) _ link_count 777 */778 Union {779 const unsigned int I _nlink; 780 unsigned int _ I _nlink; 781}; 782 dev_t I _rdev; 783 struct timespec I _atime; 784 struct timespec I _mtime; 785 struct timespec I _ctime; 786 spinlock_t I _lock;/* I _blocks, I _bytes, maybe I _size */787 unsigned short I _bytes; 788 blkcnt_t I _blocks; 789 loff_t I _size; 790 791 # ifdef _ limit 792 seqcount_t I _size_seqcount; 793 # endif 794 795/* MISC */796 unsigned long I _state; 797 struct mutex I _mutex; 798 799 unsigned long dirtied_when; /* jiffies of first dirtying */800 801 struct hlist_node I _hash; 802 struct list_head I _wb_list;/* backing Dev Io list */803 struct list_head I _lru; /* inode LRU list */804 struct list_head I _sb_list; 805 Union {806 struct list_head I _dentry; 807 struct rcu_head I _rcu; 808}; 809 atomic_t I _count; 810 unsigned int I _blkbits; 811 u64 I _version; 812 atomic_t I _dio_count; 813 atomic_t I _writecount; 814 const struct file_operations * I _fop;/* former-> I _op-> limit */815 struct file_lock * I _flock; 816 struct address_space I _data; 817 # ifdef config_quota 818 struct dquot * I _dquot [maxquotas]; 819 # endif 820 struct list_head I _devices; 821 Union {822 struct pipe_inode_info * I _pipe; 823 struct block_device * I _bdev; 824 struct cdev * I _cdev; 825}; 826 827 _ u32 I _generation; 828 829 # ifdef config_fsno1_830 _ u32 I _fsnotify_mask; /* all events this inode cares about */831 struct hlist_head I _fsnotify_marks; 832 # endif 833 834 # ifdef config_ima 835 atomic_t I _readcount; /* struct files open ro */836 # endif 837 void * I _private;/* FS or device private pointer */838}; 839

Here, I _op defines all the operation methods of the index Node object:
1613struct inode_operations {
1614 struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameidata *);
1615 void * (*follow_link) (struct dentry *, struct nameidata *);
1616 int (*permission) (struct inode *, int);
1617 struct posix_acl * (*get_acl)(struct inode *, int);
1618
1619 int (*readlink) (struct dentry *, char __user *,int);
1620 void (*put_link) (struct dentry *, struct nameidata *, void *);
1621
1622 int (*create) (struct inode *,struct dentry *,int, struct nameidata *);
1623 int (*link) (struct dentry *,struct inode *,struct dentry *);
1624 int (*unlink) (struct inode *,struct dentry *);
1625 int (*symlink) (struct inode *,struct dentry *,const char *);
1626 int (*mkdir) (struct inode *,struct dentry *,int);
1627 int (*rmdir) (struct inode *,struct dentry *);
1628 int (*mknod) (struct inode *,struct dentry *,int,dev_t);
1629 int (*rename) (struct inode *, struct dentry *,
1630 struct inode *, struct dentry *);
1631 void (*truncate) (struct inode *);
1632 int (*setattr) (struct dentry *, struct iattr *);
1633 int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
1634 int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
1635 ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
1636 ssize_t (*listxattr) (struct dentry *, char *, size_t);
1637 int (*removexattr) (struct dentry *, const char *);
1638 void (*truncate_range)(struct inode *, loff_t, loff_t);
1639 int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
1640 u64 len);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.