Linux内核4.14版本——mtd子系统(5)——块mtd设备(mtdblkdevs.c)_风雨兼程8023的博客-程序员宝宝_linux mtd子系统

技术标签: c语言  运维  linux  Linux mtd子系统  

1. mtd块设备结构定义

2. register_mtd_blktrans初始化

3. add_mtd_blktrans_dev

4. mtd_blktrans_request

5. mtd_blktrans_work

6. blktrans_open


1. mtd块设备结构定义

static const struct block_device_operations mtd_block_ops = {
	.owner		= THIS_MODULE,
	.open		= blktrans_open,
	.release	= blktrans_release,
	.ioctl		= blktrans_ioctl,
	.getgeo		= blktrans_getgeo,
};

2. register_mtd_blktrans初始化

static struct mtd_notifier blktrans_notifier = {
	.add = blktrans_notify_add,
	.remove = blktrans_notify_remove,
};

int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
{
	struct mtd_info *mtd;
	int ret;

	/* Register the notifier if/when the first device type is
	   registered, to prevent the link/init ordering from fucking
	   us over. */
	// 为每个设备注册注册notifier
	if (!blktrans_notifier.list.next)
		register_mtd_user(&blktrans_notifier);   


	mutex_lock(&mtd_table_mutex);

	// 注册mtd块设备
	ret = register_blkdev(tr->major, tr->name);
	if (ret < 0) {
		printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
		       tr->name, tr->major, ret);
		mutex_unlock(&mtd_table_mutex);
		return ret;
	}

	if (ret)
		tr->major = ret;

	tr->blkshift = ffs(tr->blksize) - 1;

	INIT_LIST_HEAD(&tr->devs);
	list_add(&tr->list, &blktrans_majors);

	mtd_for_each_device(mtd)
		if (mtd->type != MTD_ABSENT)
			tr->add_mtd(tr, mtd);

	mutex_unlock(&mtd_table_mutex);
	return 0;
}

      最终会调用struct mtd_blktrans_ops *tr结构体里面的add_mtd函数,前文分析到,其最终会调用add_mtd_blktrans_dev函数。

3. add_mtd_blktrans_dev

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;
	int ret;

	if (mutex_trylock(&mtd_table_mutex)) {
		mutex_unlock(&mtd_table_mutex);
		BUG();
	}

	mutex_lock(&blktrans_ref_mutex);
	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 */
			mutex_unlock(&blktrans_ref_mutex);
			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;
	}

	ret = -EBUSY;
	if (new->devnum == -1)
		new->devnum = last_devnum+1;

	/* Check that the device and any partitions will get valid
	 * minor numbers and that the disk naming code below can cope
	 * with this number. */
	if (new->devnum > (MINORMASK >> tr->part_bits) ||
	    (tr->part_bits && new->devnum >= 27 * 26)) {
		mutex_unlock(&blktrans_ref_mutex);
		goto error1;
	}

	list_add_tail(&new->list, &tr->devs);
 added:
	mutex_unlock(&blktrans_ref_mutex);

	mutex_init(&new->lock);
	kref_init(&new->ref);
	if (!tr->writesect)
		new->readonly = 1;

	/* Create gendisk */
	ret = -ENOMEM;
	gd = alloc_disk(1 << tr->part_bits);

	if (!gd)
		goto error2;

	new->disk = gd;
	gd->private_data = new;
	gd->major = tr->major;
	gd->first_minor = (new->devnum) << tr->part_bits;
	gd->fops = &mtd_block_ops;

	if (tr->part_bits)
		if (new->devnum < 26)
			snprintf(gd->disk_name, sizeof(gd->disk_name),
				 "%s%c", tr->name, 'a' + new->devnum);
		else
			snprintf(gd->disk_name, sizeof(gd->disk_name),
				 "%s%c%c", tr->name,
				 'a' - 1 + new->devnum / 26,
				 'a' + new->devnum % 26);
	else
		snprintf(gd->disk_name, sizeof(gd->disk_name),
			 "%s%d", tr->name, new->devnum);

	set_capacity(gd, ((u64)new->size * tr->blksize) >> 9);

	/* Create the request queue */
	spin_lock_init(&new->queue_lock);         //初始化访问请求队列的自旋锁
	//初始化请求队列
	new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);

	if (!new->rq)
		goto error3;

	if (tr->flush)
		blk_queue_write_cache(new->rq, true, false);

	new->rq->queuedata = new; 
	//设置MTD块设备硬盘扇区的大小
	blk_queue_logical_block_size(new->rq, tr->blksize);

	blk_queue_bounce_limit(new->rq, BLK_BOUNCE_HIGH);
	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, new->rq);
	queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, new->rq);

	if (tr->discard) {
		queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, new->rq);
		blk_queue_max_discard_sectors(new->rq, UINT_MAX);
	}

	gd->queue = new->rq;

	/* Create processing workqueue */
	new->wq = alloc_workqueue("%s%d", 0, 0,
				  tr->name, new->mtd->index);
	if (!new->wq)
		goto error4;
	//初始化处理请求队列的的线程
	INIT_WORK(&new->work, mtd_blktrans_work);

	if (new->readonly)
		set_disk_ro(gd, 1);

	device_add_disk(&new->mtd->dev, gd);

	if (new->disk_attributes) {
		ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
					new->disk_attributes);
		WARN_ON(ret);
	}
	return 0;
error4:
	blk_cleanup_queue(new->rq);
error3:
	put_disk(new->disk);
error2:
	list_del(&new->list);
error1:
	return ret;
}

    这块就是创建块设备了,关于块设备可以参考以前的文章。

4. mtd_blktrans_request

      上面代码中,调用blk_init_queue(mtd_blktrans_request, ...设置请求队列的处理全程为为mtd_blktrans_request,它的代码如下。

static void mtd_blktrans_request(struct request_queue *rq)
{
	struct mtd_blktrans_dev *dev;
	struct request *req = NULL;

	dev = rq->queuedata;

	if (!dev)
		while ((req = blk_fetch_request(rq)) != NULL)
			__blk_end_request_all(req, BLK_STS_IOERR);
	else
		queue_work(dev->wq, &dev->work);
}

      这个函数好像什么也没有做,最终都是mtd_blktrans_work在做。INIT_WORK的作用,可以参考改文章

INIT_WORK(&new->work, mtd_blktrans_work);

5. mtd_blktrans_work

static void mtd_blktrans_work(struct work_struct *work)
{
	struct mtd_blktrans_dev *dev =
		container_of(work, struct mtd_blktrans_dev, work);
	struct mtd_blktrans_ops *tr = dev->tr;
	struct request_queue *rq = dev->rq;
	struct request *req = NULL;
	int background_done = 0;

	spin_lock_irq(rq->queue_lock);

	while (1) {
		blk_status_t res;

		dev->bg_stop = false;
		if (!req && !(req = blk_fetch_request(rq))) {
			if (tr->background && !background_done) {
				spin_unlock_irq(rq->queue_lock);
				mutex_lock(&dev->lock);
				tr->background(dev);
				mutex_unlock(&dev->lock);
				spin_lock_irq(rq->queue_lock);
				/*
				 * Do background processing just once per idle
				 * period.
				 */
				background_done = !dev->bg_stop;
				continue;
			}
			break;
		}

		spin_unlock_irq(rq->queue_lock);

		mutex_lock(&dev->lock);
		res = do_blktrans_request(dev->tr, dev, req);
		mutex_unlock(&dev->lock);

		spin_lock_irq(rq->queue_lock);

		if (!__blk_end_request_cur(req, res))
			req = NULL;

		background_done = 0;
	}

	spin_unlock_irq(rq->queue_lock);
}

6. blktrans_open

static int blktrans_open(struct block_device *bdev, fmode_t mode)
{
	struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
	int ret = 0;

	if (!dev)
		return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/

	mutex_lock(&mtd_table_mutex);
	mutex_lock(&dev->lock);

	if (dev->open)
		goto unlock;

	kref_get(&dev->ref);
	__module_get(dev->tr->owner);

	if (!dev->mtd)
		goto unlock;

	if (dev->tr->open) {
		ret = dev->tr->open(dev);
		if (ret)
			goto error_put;
	}

	ret = __get_mtd_device(dev->mtd);
	if (ret)
		goto error_release;
	dev->file_mode = mode;

unlock:
	dev->open++;
	mutex_unlock(&dev->lock);
	mutex_unlock(&mtd_table_mutex);
	blktrans_dev_put(dev);
	return ret;

error_release:
	if (dev->tr->release)
		dev->tr->release(dev);
error_put:
	module_put(dev->tr->owner);
	kref_put(&dev->ref, blktrans_dev_release);
	mutex_unlock(&dev->lock);
	mutex_unlock(&mtd_table_mutex);
	blktrans_dev_put(dev);
	return ret;
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yangguoyu8023/article/details/122453687

智能推荐

java jni demo_简单JNI使用demo_许传志的博客-程序员宝宝

1 #include "com_example_ndkdemo_JniClient.h"2 #include 3 #include 45 //引入log头文件6 #include 7 //log标签8 #define TAG "jniCLient"9 //定义info信息10 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG,__...

bzoj2594 [Wc2006]水管局长数据加强版 离线+LCT维护边权_weixin_30621711的博客-程序员宝宝

LCT维护边权势把每条边都当成一个点赋予点权,原来的点当然也保留,如果求和点权赋为0,求max点权赋为-INF,如果都要求,那就弄两三个点权。需要注意的是,LCT的link操作必须换根,如果不把x设为根而直接pre[x]=y的话,结果并不是连接x和y而是连接x的splay的根和y。在弹飞绵羊的题中之所以不用换根是因为x本来就是根。#include&lt;bits/stdc++.h&...

php 5.2 post,php – Laravel 5.2:POST请求总是返回“405 Method Not Allowed”_青州重楼上的博客-程序员宝宝

所以我正在用Laravel 5.2开发一个API,我面临着一个重要的问题.我有一个UserController将管理我的应用程序的用户.这是我的routes.php文件:Route::group(array('prefix' =&gt; 'api/v1'), function() {Route::post('user', '[email protected]');});我的UserContr...

时间序列数据处理与解释_天地一沙鸥GW的博客-程序员宝宝_真实变异性 时间序列数据

简介STL:Seasonal-Trend decomposition procedures based on LoessLoess(locally weighted regression,局部加权回归)此方法是由Cleveland于1990年在论文《STL: A seasonal-trend decomposition procedure based on loess》中提出。以上图片来源自snowdroptulip博客。特点STL有一个简单的设计,它包含了loess平滑法的一系列应用;

特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver_weixin_34114823的博客-程序员宝宝

Best Solver Problem's Link Mean:给出x和M,求:(5+2√6)^(1+2x)的值。x&lt;2^32,M&lt;=46337. analyse:这题需要用到高中的数学知识点:特征根法求递推数列通项公式。方法是这样的:对于这题的解法:记λ1=5+2√6,λ2=5...

随便推点

C语言结构体求矩形周长和面积_JHpku的博客-程序员宝宝

**C语言结构体求矩形周长和面积**代码如下图:运行结果如下:总结:要想将这道题做出来必须掌握结构体的基本结构定义,对结构体变量的初始化还有基本函数的调用

sparkstreaming reduceByKeyAndWindow 高效的同时带来的问题_周小科的博客-程序员宝宝

reduceByKeyAndWindow 有一个重载reduceByKeyAndWindow(func,invFunc,windowLength,slideInterval,[numTasks]),可以实现高效计算。它的原理是维护一个中间状态(结果),然后对增量(减量)进行计算。但同时也随之带来两个问题1、必须维护中间状态,所以使用这个方法时,必须同时使用checkpiont。2、因为有中间结果的存在,所以当某个key已经完全滑出窗口后,这个key依然还会在中间结果中维护随着时间的推移,如果

com.sun.jna.Native maven导入_imHanweihu的博客-程序员宝宝_com.sun.jna

&lt;dependency&gt; &lt;groupId&gt;com.sun.jna&lt;/groupId&gt; &lt;artifactId&gt;jna&lt;/artifactId&gt; &lt;version&gt;3.0.9&lt;/version&gt;&lt;/dependency&gt;

计算机管理无用怎么办,厉害:腾讯计算机管理器无法卸载,该怎么办_士节的博客-程序员宝宝

hapy123您好,如果已经安装了更新的版本,则需要在安装前清理注册表。可能存在未知错误,请尝试重新启动。其他原因分析1、腾讯计算机管理器是否为最新版本(您可以查看官方网站进行比较)2、下载时可能有问题。下载的管家缺少某个文件,无法安装。建议您删除安装软件包,然后重新下载已被其他防病毒软件阻止3、。建议关闭其他安全保护软件后再安装管家,以确保管家的正常运行,并充分保护您的计算机和QQ的安全。如何关...

将Matlab计算结果输出至Excel中_那只卷毛的博客-程序员宝宝

Matlab计算结果导入Excel1. 初级篇 在利用Matlab绘图时,其绘图比较繁琐,且图像调整较为复杂。未解决这一问题,我们可将Matlab计算的结果导出,然后利用Origin等专业绘图软件进行绘图。下面将介绍如何将Matlab计算结果导出至Excel中。 首先,我们可以利用Matlab中的 xlswrite( ) 函数,其语法格式如下:xlswrite('filename',..._1671465600

推荐文章

热门文章

相关标签