索引节点inode: Linux内核文件系统之(inode)

inode是Linux内核中存储文件元信息的数据结构,包括文件大小、权限、时间戳等。inode用于区分文件,每个文件都有唯一的inode号。inode表记录了文件系统所有inode号,通过inode号可以访问文件。硬链接是指向相同inode的不同文件名,删除其中一个不影响其他硬链接。软链接(符号链接)存储目标文件路径,删除源文件将导致软链接失效。文件描述符(FD)是进程对文件的抽象,每个进程有自己的FD表,系统也有全局FD表。FD、文件、进程间通过文件描述符表、打开文件表和inode表相互关联,实现文件操作。inode和FD的分离机制使得软件更新可以在不关闭软件的情况下进行,提高了系统效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

inode译成中文就是索引节点,它用来存放档案及目录的基本信息,包含时间、档名、使用者及群组等。

static int eachreg_open(struct inode *inode, struct file *file)
{
	file->private_data = inode->i_private;
	return 0;
}

static ssize_t eachreg_write(struct file *file, const char __user *ubuf,
			     size_t count, loff_t *ppos)
{


...

}

static ssize_t eachreg_read(struct file *file, char __user *ubuf,
			    size_t count, loff_t *ppos)
{

...

}

上面寄存器操作遇到inode结构体,看看下面资料。

先看一个驱动模型:

简单的debugfs模型


#include <linux/init.h>
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm-generic/uaccess.h>


static char abc_str[32] =
{ };
static char blob_inof[32] = "dragon blob wrapper\n";
static struct debugfs_blob_wrapper blob;
static u32 my_u32;
static u32 my_x32;
static u16 my_x16;
static u16 my_u16;


static struct dentry *dragon_root;
static struct dentry *dragon_sub;


static int abc_open(struct inode * inode, struct file * file)
{
file->private_data = inode->i_private;
return 0;
}


static ssize_t abc_read(struct file * file, char __user * buf, size_t size,
loff_t * offset)
{
printk("###dragon### abc_read size=%ld\n", size);
if (*offset >= 32)
{
return 0;
}
if (*offset + size > 32)
{
size = 32 - *offset;
}


if (copy_to_user(buf, abc_str + *offset, size))
{
return -EFAULT;
}


*offset += size;
return size;
}


static ssize_t abc_write(struct file * file, char __user * buf, size_t size,
loff_t * offset)
{
printk("###dragon### abc_write size=%ld\n", size);


if (*offset >= 32)
{
return 0;
}
if (*offset + size > 32)
{
size = 32 - *offset;
}


if (copy_from_user(abc_str + *offset, buf, size))
{
return -EFAULT;
}


*offset += size;
return size;
}
struct file_operations abc =
{ .owner = THIS_MODULE, .open = abc_open, .read = abc_read, .write =
abc_write, };






static int hello_init(void)
{
printk("###dragon### %s\n", __FUNCTION__);


//在根目录中创建dragon_debug文件夹
dragon_root = debugfs_create_dir("dragon_debug", NULL);
if (IS_ERR(dragon_root))
{
printk("DEBUGFS DIR create failed\n");
return -1;
}


dragon_sub = debugfs_create_dir("sub", dragon_root);
if (IS_ERR(dragon_sub))
{
debugfs_remove_recursive(dragon_root);
printk("DEBUGFS DIR create failed\n");
return -1;
}


//注册任意访问文件
struct dentry * entry = debugfs_create_file("abc", 0644, dragon_root, NULL,
&abc);
if (IS_ERR(entry))
{
debugfs_remove_recursive(dragon_root);
dragon_root = NULL;
printk("###dragon### abc create error\n");
return -1;
}




//注册只读字符串
blob.data = blob_inof;
blob.size = strlen(blob_inof);
debugfs_create_blob("blob", 0644, dragon_root, &blob);


//注册直接访问读写的整形
debugfs_create_u32("u32", 0644, dragon_root, &my_u32);
debugfs_create_u32("x32", 0644, dragon_root, &my_x32);
debugfs_create_u32("x16", 0644, dragon_root, &my_x16);
debugfs_create_u32("u16", 0644, dragon_root, &my_u16);
return 0;
}


static void hello_exit(void)
{
printk("###dragon### %s\r\n", __FUNCTION__);
debugfs_remove_recursive(dragon_root);
}


MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit); 


#include <linux/init.h>
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm-generic/uaccess.h>


static char abc_str[32] =
{ };
static char blob_inof[32] = "dragon blob wrapper\n";
static struct debugfs_blob_wrapper blob;
static u32 my_u32;
static u32 my_x32;
static u16 my_x16;
static u16 my_u16;


static struct dentry *dragon_root;
static struct dentry *dragon_sub;


static int abc_open(struct inode * inode, struct file * file)
{
file->private_data = inode->i_private;
return 0;
}


static ssize_t abc_read(struct file * file, char __user * buf, size_t size,
loff_t * offset)
{
printk("###dragon### abc_read size=%ld\n", size);
if (*offset >= 32)
{
return 0;
}
if (*offset + size > 32)
{
size = 32 - *offset;
}


if (copy_to_user(buf, abc_str + *offset, size))
{
return -EFAULT;
}


*offset += size;
return size;
}


static ssize_t abc_write(struct file * file, char __user * buf, size_t size,
loff_t * offset)
{
printk("###dragon### abc_write size=%ld\n", size);


if (*offset >= 32)
{
return 0;
}
if (*offset + size > 32)
{
size = 32 - *offset;
}


if (copy_from_user(abc_str + *offset, buf, size))
{
return -EFAULT;
}


*offset += size;
return size;
}
struct file_operations abc =
{ .owner = THIS_MODULE, .open = abc_open, .read = abc_read, .write =
abc_write, };


static int hello_init(void)
{
printk("###dragon### %s\n", __FUNCTION__);


//在根目录中创建dragon_debug文件夹
dragon_root = debugfs_create_dir("dragon_debug", NULL);
if (IS_ERR(dragon_root))
{
printk("DEBUGFS DIR create failed\n");
return -1;
}


dragon_sub = debugfs_create_dir("sub",

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值