時(shí)間:2015-06-28 00:00:00 來(lái)源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評(píng)論(0)
塊緩存 struct buffer_head 用來(lái)保存從磁盤(pán)讀取到的數(shù)據(jù),而 struct page 是文件的緩存,在文件層面上的數(shù)據(jù)會(huì)緩存到page里,所以內(nèi)核里直接讀取某個(gè)固定的扇區(qū)可以利用 struct buffer_head,讀取的速度會(huì)快一些;以下是實(shí)現(xiàn)的代碼:
#include <linux/module.h>
#include <linux/init.h>
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int set_size = 512;
static int nr = -1;
static char *devn = "/dev/sda";
module_param(set_size,int,S_IRUGO);
MODULE_Parm_DESC(set_size,"how many bytes you want to read,not more than 4096");
module_param(nr,long,S_IRUGO);
MODULE_Parm_DESC(nr,"which sectors you want to read");
module_param(devn,charp,S_IRUGO);
MODULE_Parm_DESC(devn,"which device");
MODULE_LICENSE("GPL");
static struct block_device *bdev;
static char *usage = "You can change the value:set_size nr devn";
static int __init init_read(void)
{
struct buffer_head *bh = NULL;
int size;
if(nr == -1)
{
printk("Using this programm,you need set \"nr\"\n");
printk("%s\n",usage);
return -1;
}
#p#副標(biāo)題#e#
printk("read disk\n");
bdev = open_bdev_excl(devn,0x8000,NULL);
if(IS_ERR(bdev))
{
printk("open failed\n");
return PTR_ERR(bdev);
}
size = bdev_hardsect_size(bdev);
printk("size = %d\n",size);
if(set_blocksize(bdev,set_size)){
printk("set block size error\n");
return -1;
}
/* read disk */
bh = __bread(bdev,nr,set_size);
if(bh == NULL)
return 0;
//if you want to modify the disk contents,do this operations
memset(bh->b_data,0x30,set_size);
set_buffer_uptodate(bh);
/* write disk */
mark_buffer_dirty(bh);
if(bh)
brelse(bh);
return 0;
}
static void __exit exit_read(void)
{
printk("exit\n");
close_bdev_excl(bdev);
}
module_init(init_read);
module_exit(exit_read);
關(guān)鍵詞標(biāo)簽:硬盤(pán),內(nèi)核
相關(guān)閱讀
熱門(mén)文章 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程 Tomcat9.0如何安裝_Tomcat9.0環(huán)境變量配置方法 多種操作系統(tǒng)NTP客戶端配置 Linux操作系統(tǒng)修改IP
人氣排行 Linux下獲取CPUID、硬盤(pán)序列號(hào)與MAC地址 dmidecode命令查看內(nèi)存型號(hào) linux tc實(shí)現(xiàn)ip流量限制 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程 linux下解壓rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 關(guān)機(jī)、重啟、注銷 命令 查看linux服務(wù)器硬盤(pán)IO讀寫(xiě)負(fù)載