Linux kernel research: my virtual file system (linux)

Source: Internet
Author: User
Article title: linux kernel research: my virtual file system (linux ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Hello. c
Code:
  
# Include "hello. h"
  
Struct inode * hello_get_inode (struct super_block *, int, struct hello_dir_entry *);
  
Int hello_readdir (struct file * filp, void * dirent, filldir_t filldir)
{
Printk ("hello_readdir \ n ");
Struct hello_dir_entry * de;
Unsigned int ino;
Int I;
Struct inode * inode = filp-> f_dentry-> d_inode;
  
Ino = inode-> I _ino;
De = (struct hello_dir_entry *) inode-> u. generic_ip;
If (! De)
Return-EINVAL;
I = filp-> f_pos;
Switch (I ){
Case 0:
If (filldir (dirent, ".", 1, I, ino, DT_DIR) <0)
Return 0;
I ++;
Filp-> f_pos ++;
/* Fall through */
Case 1:
If (filldir (dirent, "..", 2, I,
Filp-> f_dentry-> d_parent-> d_inode-> I _ino,
DT_DIR) <0)
Return 0;
I ++;
Filp-> f_pos ++;
/* Fall through */
Default:
De = de-> subdir;
I-= 2;
For (;;){
If (! De)
Return 1;
If (! I)
Break;
De = de-> next;
I --;
}
  
Do {
If (filldir (dirent, de-> name, de-> namelen, filp-> f_pos,
De-> low_ino, de-> mode> 12) <0)
Return 0;
Filp-> f_pos ++;
De = de-> next;
} While (de );
}
Return 1;
}
  
Int hello_d_revalidate (struct dentry * res, int I) {printk ("d_revalidate \ n"); return 0 ;}
Int hello_d_hash (struct dentry * res, struct qstr * name) {printk ("d_hash \ n"); return 0 ;}
Int hello_d_compare (struct dentry * res, struct qstr * name, struct qstr * old)
{Printk ("d_compare \ n"); return 0 ;}
Int hello_d_delete (struct dentry * res) {printk ("d_delete \ n"); return 0 ;}
Void hello_d_release (struct dentry * res) {printk ("d_release \ n ");}
Void hello_d_iput (struct dentry * res, struct inode * inode) {printk ("d_iput \ n ");}
  
Struct dentry_operations hello_lookup_dops = {
/* D_revalidate: hello_d_revalidate,
D_hash: hello_d_hash,
D_compare: hello_d_compare ,*/
D_delete: hello_d_delete,
D_release: hello_d_release,
/* D_iput: hello_d_iput */
};
  
Struct dentry * hello_lookup (struct inode * dir, struct dentry * dentry)
{
Struct inode * inode;
Struct hello_dir_entry * de;
Int error;
  
Error =-ENOENT;
Inode = NULL;
De = (struct hello_dir_entry *) dir-> u. generic_ip;
If (de ){
For (de = de-> subdir; de = de-> next ){
If (! De |! De-> low_ino)
Continue;
If (de-> namelen! = Dentry-> d_name.len)
Continue;
If (! Memcmp (dentry-> d_name.name, de-> name, de-> namelen )){
Int ino = de-> low_ino;
Error =-EINVAL;
Inode = hello_get_inode (dir-> I _sb, ino, de );
Break;
}
}
}
  
If (inode ){
Dentry-> d_op = & hello_lookup_dops;
D_add (dentry, inode );
Return NULL;
}
Return ERR_PTR (error );
}
/*************************************** **************************************** *****************************/
Static struct inode_operations hello_root_inode_operations = {
Lookup: hello_lookup,
};
  
Static struct file_operations hello_file_operations = {
Readdir: hello_readdir,
};
  
Struct hello_dir_entry hello_root = {
Low_ino: HELLO_ROOT_INO,
Namelen: 5,
Name: "/hello ",
Mode: S_IFDIR | S_IRUGO | S_IXUGO,
Nlink: 2,
Hello_iops: & hello_root_inode_operations,
Hello_fops: & hello_file_operations,
Parent: & hello_root,
};
  
Struct inode * hello_get_inode (struct super_block * sb, int ino,
Struct hello_dir_entry * de)
{
Printk ("hello_get_inode \ n ");
Struct inode * inode;
  
De_get (de );
Inode = iget (sb, ino );
If (! Inode)
Goto out_fail;
Inode-> u. generic_ip = (void *) de;
If (de ){
If (de-> mode ){
Inode-> I _mode = de-> mode;
Inode-> I _uid = de-> uid;
Inode-> I _gid = de-> gid;
}
If (de-> size)
Inode-> I _size = de-> size;
If (de-> nlink)
Inode-> I _nlink = de-> nlink;
If (de-> owner)
_ MOD_INC_USE_COUNT (de-> owner );
If (de-> hello_iops)
Inode-> I _op = de-> hello_iops;
If (de-> hello_fops)
Inode-> I _fop = de-> hello_fops;
}
  
Out:
Return inode;
  
Out_fail:
De_put (de );
Goto out;
}
  
/*************************************** **************************************** ****************************/
  
Void d_instantiate (struct dentry * entry, struct inode * inode)
{
Printk ("d_instantiate \ n ");
If (! List_empty (& entry-> d_alias) BUG ();
Spin_lock (& dcache_lock );
If (inode)
List_add (& entry-> d_alias, & inode-> I _dentry );
Entry-> d_inode = inode;
Spin_unlock (& dcache_lock );
}
  
Struct dentry * d_alloc_root (struct inode * root_inode)
{
Struct dentry * res = NULL;
Printk ("d_alloc_root \ n ");
If (root_inode ){
Res = d_alloc (NULL, & (const struct qstr) {"/", 1, 0 });
If (res ){
Res-> d_sb = root_inode-> I _sb;
Res-> d_parent = res;
D_instantiate (res, root_inode );
}
}
Return res;
}
  
Void force_delete (struct inode * inode)
{
Printk ("force_delete \ n ");
Struct hello_dir_entry * de = inode-> u. generic_ip;
    
If (atomic_read (& inode-> I _count) = 1)
Inode-> I _nlink = 0;
If (atomic_dec_and_test (& de-> count ))
Printk ("hello_root.count: % d \ n", atomic_read (& de-> count ));
}
  
Static void hello_delete_inode (struct inode * inode)
{
Printk ("hello_delete_inode \ n ");
Struct hello_dir_entry * de = inode-> u. generic_ip;
Inode-> I _state = I _CLEAR;
/* If (de ){
If (de-> owner)
_ MOD_DEC_USE_COUNT (de-> owner );
De_put (de );
}*/
}
  
Static void hello_read_inode (struct inode * inode)
{
Printk ("hello_read_inode \ n ");
Inode-> I _mtime = inode-> I _atime = inode-> I _ctime = CURRENT_TIME;
}
  
Static int hello_statfs (struct super_block * sb, struct statfs * buf)
{
Printk ("hello_statfs \ n ");
Return 0;
}
  
Void hello_write_super (struct super_block * s)
{
Printk ("write_super \ n ");
}
  
Static struct super_operations hello_sops = {
Read_inode: hello_read_inode,
Put_inode: force_delete,
Delete_inode: hello_delete_inode,
Write_super: hello_w
Related Article

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.