内核直接读取硬盘固定扇区的代码
时间:2023-05-31 05:05:33 点击:388

  块缓存 struct buffer_head 用来保存从磁盘读取到的数据,而 struct page 是文件的缓存,在文件层面上的数据会缓存到page里,所以内核里直接读取某个固定的扇区可以利用 struct buffer_head,读取的速度会快一些;以下是实现的代码:

  #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(“%sn“,usage);

  return -1;

  }

#p#副标题#e#

  printk(“read diskn“);

  bdev = open_bdev_excl(devn,0x8000,NULL);

  if(IS_ERR(bdev))

  {

  printk(“open failedn“);

  return PTR_ERR(bdev);

  }

  size = bdev_hardsect_size(bdev);

  printk(“size = %dn“,size);

  if(set_blocksize(bdev,set_size)){

  printk(“set block size errorn“);

  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(“exitn“);

  close_bdev_excl(bdev);

  }

  module_init(init_read);

  module_exit(exit_read);

最新游戏更多

最新软件更多

  • 玩家推荐
  • 游戏攻略

金钼软件下载站 Copyright(C) 2008- 601958.cn All Rights Reserved!

闽ICP备2023004188号| 免责声明