学习笔记 --- LINUX MTD子系统框架分析

2023-12-12 00:58

本文主要是介绍学习笔记 --- LINUX MTD子系统框架分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MTD(memory technology device):内存技术设备,是linux用于描述ROM,NAND,NOR等设备的子系统的抽象,MTD设备可以按块读写也可以按字节读写,也就是说MTD设备既可以是块设备也可以是字符设备,块设备(mtdblackx)操作针对文件系统,字符设备(mtdx)操作主要针对格式化等操作的测试用。

一个MTD设备的描述为mtd_info这个结构体,填充好这个结构体,然后调用add_mtd_device添加mtd设备函数就可以注册MTD设备了。在内核的源代码中,mtdram.c是一个很简单的例子,他把一块内存空间模拟了一个MTD device,研究MTD的框架,从这个mtdram.c开始:

/** mtdram - a test mtd device* Author: Alexander Larsson <alex@cendio.se>** Copyright (c) 1999 Alexander Larsson <alex@cendio.se>* Copyright (c) 2005 Joern Engel <joern@wh.fh-wedel.de>** This code is GPL**/#include <linux/module.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/mtd/compatmac.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/mtdram.h>static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
static unsigned long erase_size = CONFIG_MTDRAM_ERASE_SIZE;
#define MTDRAM_TOTAL_SIZE (total_size * 1024)
#define MTDRAM_ERASE_SIZE (erase_size * 1024)#ifdef MODULE
module_param(total_size, ulong, 0);
MODULE_PARM_DESC(total_size, "Total device size in KiB");
module_param(erase_size, ulong, 0);
MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
#endif// We could store these in the mtd structure, but we only support 1 device..
static struct mtd_info *mtd_info;static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
{if (instr->addr + instr->len > mtd->size)return -EINVAL;memset((char *)mtd->priv + instr->addr, 0xff, instr->len);instr->state = MTD_ERASE_DONE;mtd_erase_callback(instr);return 0;
}static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,size_t *retlen, void **virt, resource_size_t *phys)
{if (from + len > mtd->size)return -EINVAL;/* can we return a physical address with this driver? */if (phys)return -EINVAL;*virt = mtd->priv + from;*retlen = len;return 0;
}static void ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
{
}/** Allow NOMMU mmap() to directly map the device (if not NULL)* - return the address to which the offset maps* - return -ENOSYS to indicate refusal to do the mapping*/
static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,unsigned long len,unsigned long offset,unsigned long flags)
{return (unsigned long) mtd->priv + offset;
}static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,size_t *retlen, u_char *buf)
{if (from + len > mtd->size)return -EINVAL;memcpy(buf, mtd->priv + from, len);*retlen = len;return 0;
}static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,size_t *retlen, const u_char *buf)
{if (to + len > mtd->size)return -EINVAL;memcpy((char *)mtd->priv + to, buf, len);*retlen = len;return 0;
}static void __exit cleanup_mtdram(void)
{if (mtd_info) {del_mtd_device(mtd_info);vfree(mtd_info->priv);kfree(mtd_info);}
}int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,unsigned long size, char *name)
{memset(mtd, 0, sizeof(*mtd));/* Setup the MTD structure */mtd->name = name;mtd->type = MTD_RAM;mtd->flags = MTD_CAP_RAM;mtd->size = size;mtd->writesize = 1;mtd->erasesize = MTDRAM_ERASE_SIZE;mtd->priv = mapped_address;mtd->owner = THIS_MODULE;mtd->erase = ram_erase;mtd->point = ram_point;mtd->unpoint = ram_unpoint;mtd->get_unmapped_area = ram_get_unmapped_area;mtd->read = ram_read;mtd->write = ram_write;if (add_mtd_device(mtd)) {     //填充好mtd_info之后注册这个mtd设备return -EIO;}return 0;
}static int __init init_mtdram(void)
{void *addr;int err;if (!total_size)return -EINVAL;/* Allocate some memory */mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);   //分配mtd_infoif (!mtd_info)return -ENOMEM;addr = vmalloc(MTDRAM_TOTAL_SIZE);  //开辟一段内存用来模拟MTD设备if (!addr) {kfree(mtd_info);mtd_info = NULL;return -ENOMEM;}err = mtdram_init_device(mtd_info, addr, MTDRAM_TOTAL_SIZE, "mtdram test device"); if (err) {vfree(addr);kfree(mtd_info);mtd_info = NULL;return err;}memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);return err;
}module_init(init_mtdram);
module_exit(cleanup_mtdram);MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alexander Larsson <alexl@redhat.com>");
MODULE_DESCRIPTION("Simulated MTD driver for testing");
这是一个很简单的mtd设备驱动,以一块ram来模拟一个mtd设备,从上面代码可以看到,一个mtd设备的注册就是先填充好一个mtd_info,这个结构体提供设备的硬件信息以及读写擦除等操作方法。注册这个结构体就完成了一个mtd设备的添加。这里对应的是ram设备,读写是直接操作内存的,如果是nandflash和norflash设备,对读写擦除等操作函数比较复杂,但是最终mtd的设备都要调用add_mtd_device这个函数来注册,下面来分析到底这个函数做了什么来注册一个mtd设备的:

int add_mtd_device(struct mtd_info *mtd)
{int i;if (!mtd->backing_dev_info) {switch (mtd->type) {case MTD_RAM:mtd->backing_dev_info = &mtd_bdi_rw_mappable;break;case MTD_ROM: mtd->backing_dev_info = &mtd_bdi_ro_mappable;break;default:mtd->backing_dev_info = &mtd_bdi_unmappable;break;}}BUG_ON(mtd->writesize == 0);mutex_lock(&mtd_table_mutex);for (i=0; i < MAX_MTD_DEVICES; i++)if (!mtd_table[i]) {struct mtd_notifier *not;mtd_table[i] = mtd;mtd->index = i;mtd->usecount = 0;if (is_power_of_2(mtd->erasesize))mtd->erasesize_shift = ffs(mtd->erasesize) - 1;elsemtd->erasesize_shift = 0;if (is_power_of_2(mtd->writesize))mtd->writesize_shift = ffs(mtd->writesize) - 1;elsemtd->writesize_shift = 0;mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;/* Some chips always power up locked. Unlock them now */if ((mtd->flags & MTD_WRITEABLE)&& (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {if (mtd->unlock(mtd, 0, mtd->size))printk(KERN_WARNING"%s: unlock failed, ""writes may not work\n",mtd->name);}/* Caller should have set dev.parent to match the* physical device.*/mtd->dev.type = &mtd_devtype;mtd->dev.class = &mtd_class;mtd->dev.devt = MTD_DEVT(i);dev_set_name(&mtd->dev, "mtd%d", i);   //设置mtd设备名称dev_set_drvdata(&mtd->dev, mtd);if (device_register(&mtd->dev) != 0) {  //注册mtd设备(会自动创建mtd设备可读写的节点)mtd_table[i] = NULL;break;}if (MTD_DEVT(i))device_create(&mtd_class, mtd->dev.parent,   //创建mtd设备只读的节点MTD_DEVT(i) + 1,NULL, "mtd%dro", i);  DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);/* No need to get a refcount on the module containingthe notifier, since we hold the mtd_table_mutex */list_for_each_entry(not, &mtd_notifiers, list)   not->add(mtd);  //把这个mtd device注册到mtdmutex_unlock(&mtd_table_mutex);/* We _know_ we aren't being removed, becauseour caller is still holding us here. So noneof this try_ nonsense, and no bitching about iteither. :) */__module_get(THIS_MODULE);return 0;}mutex_unlock(&mtd_table_mutex);return 1;
}

可以看出注册device的时候,需要调用mtd_notifiers链表里面的add函数,这个函数指针在哪里被添加进去的?

void register_mtd_user (struct mtd_notifier *new)
{int i;mutex_lock(&mtd_table_mutex);list_add(&new->list, &mtd_notifiers); //new添加到mtd_notifiers链表__module_get(THIS_MODULE);for (i=0; i< MAX_MTD_DEVICES; i++)if (mtd_table[i])new->add(mtd_table[i]);mutex_unlock(&mtd_table_mutex);
}

那么register_mtd_user是谁调用的?查看代码可以知道在mtdchar.c与mtdblock.c以及mtdblock_ro.c里面注册MTD字符设备与MTD块设备的时候调用的,所以可以知道这个链表放的就是支持mtd设备的所有驱动,都放在这个链表里面,在执行add_mtd_device里面的

list_for_each_entry(not, &mtd_notifiers, list)
not->add(mtd);

的时候,一个for循环调用不同驱动(字符,块,只读块驱动)的add函数来添加这个device到不同的驱动。这里以块设备驱动为例,如果添加到块驱动,那么:

static struct mtd_notifier blktrans_notifier = {.add = blktrans_notify_add,.remove = blktrans_notify_remove,
};
调用add就是调用:
static void blktrans_notify_add(struct mtd_info *mtd)
{struct mtd_blktrans_ops *tr;if (mtd->type == MTD_ABSENT)return;list_for_each_entry(tr, &blktrans_majors, list)tr->add_mtd(tr, mtd);
}

可以知道执行blktrans_majors链表里面的add_mtd,这个链表的元素通过下面函数添加进去:

int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
{int ret, i;/* Register the notifier if/when the first device type isregistered, to prevent the link/init ordering from fuckingus over. */if (!blktrans_notifier.list.next)register_mtd_user(&blktrans_notifier);tr->blkcore_priv = kzalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL);if (!tr->blkcore_priv)return -ENOMEM;mutex_lock(&mtd_table_mutex);ret = register_blkdev(tr->major, tr->name);if (ret) {printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",tr->name, tr->major, ret);kfree(tr->blkcore_priv);mutex_unlock(&mtd_table_mutex);return ret;}spin_lock_init(&tr->blkcore_priv->queue_lock);tr->blkcore_priv->rq = blk_init_queue(mtd_blktrans_request, &tr->blkcore_priv->queue_lock);if (!tr->blkcore_priv->rq) {unregister_blkdev(tr->major, tr->name);kfree(tr->blkcore_priv);mutex_unlock(&mtd_table_mutex);return -ENOMEM;}tr->blkcore_priv->rq->queuedata = tr;blk_queue_logical_block_size(tr->blkcore_priv->rq, tr->blksize);if (tr->discard)queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,tr->blkcore_priv->rq);tr->blkshift = ffs(tr->blksize) - 1;tr->blkcore_priv->thread = kthread_run(mtd_blktrans_thread, tr,"%sd", tr->name);if (IS_ERR(tr->blkcore_priv->thread)) {int ret = PTR_ERR(tr->blkcore_priv->thread);blk_cleanup_queue(tr->blkcore_priv->rq);unregister_blkdev(tr->major, tr->name);kfree(tr->blkcore_priv);mutex_unlock(&mtd_table_mutex);return ret;}INIT_LIST_HEAD(&tr->devs);list_add(&tr->list, &blktrans_majors);  //添加这个块驱动到链表for (i=0; i<MAX_MTD_DEVICES; i++) {  //注册块驱动的时候添加所有mtd的块设备if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT)tr->add_mtd(tr, mtd_table[i]);}mutex_unlock(&mtd_table_mutex);return 0;
}
这个函数就是注册一个块驱动,然后添加所有mtd的块设备。所以最终的核心就是add_mtd这个,他在mtd块设备里面是mtdblock_add_mtd函数,这个函数最终会调用下面函数来添加一个MTD块设备:

int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
{struct mtd_blktrans_ops *tr = new->tr;struct mtd_blktrans_dev *d;int last_devnum = -1;struct gendisk *gd;if (mutex_trylock(&mtd_table_mutex)) {mutex_unlock(&mtd_table_mutex);BUG();}list_for_each_entry(d, &tr->devs, list) {if (new->devnum == -1) {/* Use first free number */if (d->devnum != last_devnum+1) {/* Found a free devnum. Plug it in here */new->devnum = last_devnum+1;list_add_tail(&new->list, &d->list);goto added;}} else if (d->devnum == new->devnum) {/* Required number taken */return -EBUSY;} else if (d->devnum > new->devnum) {/* Required number was free */list_add_tail(&new->list, &d->list);goto added;}last_devnum = d->devnum;}if (new->devnum == -1)new->devnum = last_devnum+1;if ((new->devnum << tr->part_bits) > 256) {return -EBUSY;}list_add_tail(&new->list, &tr->devs);added:mutex_init(&new->lock);if (!tr->writesect)new->readonly = 1;gd = alloc_disk(1 << tr->part_bits);  //分配diskif (!gd) {list_del(&new->list);return -ENOMEM;}gd->major = tr->major;gd->first_minor = (new->devnum) << tr->part_bits;gd->fops = &mtd_blktrans_ops;if (tr->part_bits)if (new->devnum < 26)snprintf(gd->disk_name, sizeof(gd->disk_name),"%s%c", tr->name, 'a' + new->devnum);elsesnprintf(gd->disk_name, sizeof(gd->disk_name),"%s%c%c", tr->name,'a' - 1 + new->devnum / 26,'a' + new->devnum % 26);elsesnprintf(gd->disk_name, sizeof(gd->disk_name),"%s%d", tr->name, new->devnum);/* 2.5 has capacity in units of 512 bytes while stillhaving BLOCK_SIZE_BITS set to 10. Just to keep us amused. */set_capacity(gd, (new->size * tr->blksize) >> 9);gd->private_data = new;new->blkcore_priv = gd;gd->queue = tr->blkcore_priv->rq;gd->driverfs_dev = &new->mtd->dev;if (new->readonly)set_disk_ro(gd, 1);add_disk(gd);  //添加diskreturn 0;
}
alloc_disk与add_disk这在之前分析块设备驱动框架的时候已经分析了,这两个就是注册块设备的基本函数。而如果是mtd字符设备,就会调用字符设备的添加函数,所以add_mtd_device这个函数就是这样来添加mtd设备的。
mtd设备驱动由块设备,字符设备,只读块设备等类型:

MTD块driver(针对文件系统):

static struct mtd_blktrans_ops mtdblock_tr = {.name		= "mtdblock",.major		= 31,.part_bits	= 0,.blksize 	= 512,.open		= mtdblock_open,.flush		= mtdblock_flush,.release	= mtdblock_release,.readsect	= mtdblock_readsect,.writesect	= mtdblock_writesect,.add_mtd	= mtdblock_add_mtd,.remove_dev	= mtdblock_remove_dev,.owner		= THIS_MODULE,
};static int __init init_mtdblock(void)
{mutex_init(&mtdblks_lock);return register_mtd_blktrans(&mtdblock_tr);
}static void __exit cleanup_mtdblock(void)
{deregister_mtd_blktrans(&mtdblock_tr);
}module_init(init_mtdblock);
module_exit(cleanup_mtdblock);

MTD字符driver(支持块设备像字符设备那样去操作,一般用作测试)

static int __init init_mtdchar(void)
{int ret;ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,"mtd", &mtd_fops);	//注册字符设备,捆绑mtd_fopsif (ret < 0) {pr_notice("Can't allocate major number %d for Memory Technology Devices.\n", MTD_CHAR_MAJOR);return ret;}ret = register_filesystem(&mtd_inodefs_type);	//注册mtd_inodefs_type文件系统if (ret) {pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);goto err_unregister_chdev;}mtd_inode_mnt = kern_mount(&mtd_inodefs_type);	//挂载mtd_inodefs_type文件系统if (IS_ERR(mtd_inode_mnt)) {ret = PTR_ERR(mtd_inode_mnt);pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);goto err_unregister_filesystem;}register_mtd_user(&mtdchar_notifier);	//注册到链表return ret;
err_unregister_filesystem:unregister_filesystem(&mtd_inodefs_type);
err_unregister_chdev:__unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");return ret;
}

MTD的框架跟输入子系统一样类似的分离框架,一边注册驱动(driver),一边注册设备(device),驱动有字符,块,只读块,系统开机会注册mtd设备的驱动同时添加已注册的设备。

MTD驱动端:这里可以注册mtd的字符与块设备,只读块驱动,分别对应内核代码的mtdblock.c与mtdchar.c还有只读的mtdblock_ro.c

MTD设备端:调用add_mtd_device注册设备


这篇关于学习笔记 --- LINUX MTD子系统框架分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/482684

相关文章

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

高效管理你的Linux系统: Debian操作系统常用命令指南

《高效管理你的Linux系统:Debian操作系统常用命令指南》在Debian操作系统中,了解和掌握常用命令对于提高工作效率和系统管理至关重要,本文将详细介绍Debian的常用命令,帮助读者更好地使... Debian是一个流行的linux发行版,它以其稳定性、强大的软件包管理和丰富的社区资源而闻名。在使用

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

Linux Mint Xia 22.1重磅发布: 重要更新一览

《LinuxMintXia22.1重磅发布:重要更新一览》Beta版LinuxMint“Xia”22.1发布,新版本基于Ubuntu24.04,内核版本为Linux6.8,这... linux Mint 22.1「Xia」正式发布啦!这次更新带来了诸多优化和改进,进一步巩固了 Mint 在 Linux 桌面

LinuxMint怎么安装? Linux Mint22下载安装图文教程

《LinuxMint怎么安装?LinuxMint22下载安装图文教程》LinuxMint22发布以后,有很多新功能,很多朋友想要下载并安装,该怎么操作呢?下面我们就来看看详细安装指南... linux Mint 是一款基于 Ubuntu 的流行发行版,凭借其现代、精致、易于使用的特性,深受小伙伴们所喜爱。对

Redis主从复制的原理分析

《Redis主从复制的原理分析》Redis主从复制通过将数据镜像到多个从节点,实现高可用性和扩展性,主从复制包括初次全量同步和增量同步两个阶段,为优化复制性能,可以采用AOF持久化、调整复制超时时间、... 目录Redis主从复制的原理主从复制概述配置主从复制数据同步过程复制一致性与延迟故障转移机制监控与维

什么是 Linux Mint? 适合初学者体验的桌面操作系统

《什么是LinuxMint?适合初学者体验的桌面操作系统》今天带你全面了解LinuxMint,包括它的历史、功能、版本以及独特亮点,话不多说,马上开始吧... linux Mint 是一款基于 Ubuntu 和 Debian 的知名发行版,它的用户体验非常友好,深受广大 Linux 爱好者和日常用户的青睐,

Linux(Centos7)安装Mysql/Redis/MinIO方式

《Linux(Centos7)安装Mysql/Redis/MinIO方式》文章总结:介绍了如何安装MySQL和Redis,以及如何配置它们为开机自启,还详细讲解了如何安装MinIO,包括配置Syste... 目录安装mysql安装Redis安装MinIO总结安装Mysql安装Redis搜索Red

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或