磁盘格式化程序

/*mkfs.db.c*/ 

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <mntent.h>
#include <getopt.h>

#include "rt.h"


#ifndef BLKGETSIZE
#define BLKGETSIZE _IO(0x12,96)
#endif

#ifndef __GNUC__
#error "needs gcc for the bitop-__asm__'s"
#endif

#define IPOINT_BUFFER_SIZE (RT_IPOINT_BLOCKS*RT_BLOCK_SIZE)
#define ZONEMAP_BUFFER_SIZE (RT_CLUSTERMAP_BLOCKS*RT_BLOCK_SIZE)
#define RT_IPOINTS_PER_BLOCK ((RT_BLOCK_SIZE)/RT_IPOINT_SIZE)
#define RT_IPOINTS_COUNT (RT_IPOINTS_PER_BLOCK*RT_IPOINT_BLOCKS)

#define TOTAL_CLUSTERS  ((unsigned long)((ZONES-RT_IPOINT_BLOCKS-RT_CLUSTERMAP_BLOCKS-2)/RT_BLOCKS_PER_CLUSTER))
#define RT_CLUSTERMAP_BITS ((unsigned long)(RT_CLUSTERMAP_BLOCKS*RT_BLOCK_SIZE*8))
#define RT_CLUSTER_COUNT ((TOTAL_CLUSTERS<RT_CLUSTERMAP_BITS)?TOTAL_CLUSTERS:RT_CLUSTERMAP_BITS)

static char *program_name = "mkfs";
static char *device_name = NULL;
static long BLOCKS = 0;
static int DEV = -1;
static int magic = RT_SUPER_MAGIC;

static char boot_block_buffer[512];
static char super_block_buffer[RT_BLOCK_SIZE];
static char *ipoint_buffer = NULL;

#define Super (*(struct rt_super_block *)super_block_buffer) 
#define Point (*(struct rt_ipoint *)ipoint_buffer)
#define IPOINTS ((unsigned long)Super.s_ipoints_count)
#define ZONES ((unsigned long)Super.s_total_blocks_count)
#define FIRSTZONE ((unsigned long)Super.s_cluster_start_position)
#define MAGIC  (Super.s_magic)

static char *ipoint_map;
static char *zone_map;

static void die(char *str){
 fprintf(stderr,"%s:",program_name);
 fprintf(stderr,str,device_name);
 fprintf(stderr,"\n"); 
 exit(8);
}

static long valid_offset(int fd,int offset){
 char ch;
 if(lseek(fd,offset,0)<0)
  return 0;
 if(read(fd,&ch,1)<1)
  return 0;
 return 1;
}

static unsigned long count_blocks(int fd){
 int high,low;
 low=0;
 for(high=1;valid_offset(fd,high);high*=2)
  low=high;
 while(low<high-1){

  const int mid=(low+high)/2;
  if(valid_offset(fd,mid))
   low=mid;
  else
   high=mid;
 }
 valid_offset(fd,0);
 return (low+1);
}

static unsigned long get_size(const char *file){

 int fd;
 unsigned long size;
 fd=open(file,O_RDWR);
 if(fd<0){
  perror(file);
  exit(1);
 }
 if(ioctl(fd,BLKGETSIZE,&size)>=0){
  
  close(fd);
  return (size*512);
 }
 size=count_blocks(fd);
 close(fd);
 return size;
}

static void write_tables(void){
 
 if(lseek(DEV,0,SEEK_SET))//SEEK_SET代表从文件起始位置开始偏移
  die("seek to boot block failed in write_tables");
 if(512!=write(DEV,boot_block_buffer,512))
  die("unable to clear boot sector");
 if(RT_BLOCK_SIZE!=lseek(DEV,RT_BLOCK_SIZE,SEEK_SET))
  die("seek failed in write_tables");
 if(RT_BLOCK_SIZE!=write(DEV,super_block_buffer,RT_BLOCK_SIZE))
  die("unable to write super-block");
 if(IPOINT_BUFFER_SIZE!=write(DEV,ipoint_buffer,IPOINT_BUFFER_SIZE))
  die("unable to write inodes");
 if(ZONEMAP_BUFFER_SIZE!=write(DEV,zone_map,ZONEMAP_BUFFER_SIZE))
  die("unable to write inodes");

}

static void setup_tables(void){

 unsigned int i;
 //引导区
 memset(boot_block_buffer,0,512);
 //超级块
 memset(super_block_buffer,0,RT_BLOCK_SIZE);

  Super.s_total_blocks_count = BLOCKS;
 Super.s_ipoint_start_position = RT_IPOINT_START_POSITION;
 Super.s_ipoints_count = RT_IPOINTS_COUNT;
 Super.s_ipoint_size = RT_IPOINT_SIZE;

 Super.s_cluster_map_start_position = RT_CLUSTERMAP_START_POSITION;
 Super.s_cluster_map_count = RT_CLUSTERMAP_BLOCKS;
 Super.s_cluster_start_position=RT_CLUSTER_START_POSITION;
 Super.s_cluster_size = RT_BLOCKS_PER_CLUSTER;
 Super.s_cluster_count = RT_CLUSTER_COUNT;
 Super.s_magic = magic;
 
 // 点索引区
 ipoint_buffer=malloc(IPOINT_BUFFER_SIZE);
 if(!ipoint_buffer)
  die("unable to allocate buffer for ipoints");
 memset(ipoint_buffer,0x00,IPOINT_BUFFER_SIZE);
 for(i=0;i<RT_IPOINTS_COUNT;i++)
  Point.i_latest_cluster[i]=(RT_CLUSTER_START_POSITION+RT_BLOCKS_PER_CLUSTER*i);

 //数据簇位图
 zone_map=malloc(ZONEMAP_BUFFER_SIZE);// 10*1024
 if(!zone_map)
  die("unable to allocate buffer for maps");
 memset(zone_map,0x00,ZONEMAP_BUFFER_SIZE);
 unsigned int markbytes;
 unsigned int markbits;
 markbytes = RT_IPOINTS_COUNT/8;
 markbits = RT_IPOINTS_COUNT%8;
 for(i=0;i<markbytes;i++)
  zone_map[i] |= 0xff;
 switch(markbits)
 {
  case 1:
   zone_map[markbytes] |= 0x80;
   break; 
  case 2:
   zone_map[markbytes] |= 0xC0;
   break; 
  case 3:
   zone_map[markbytes] |= 0xE0;
   break; 
  case 4:
   zone_map[markbytes] |= 0xF0;
   break; 
  case 5:
   zone_map[markbytes] |= 0xF8;
   break; 
  case 6:
   zone_map[markbytes] |= 0xFC;
   break; 
  case 7:
   zone_map[markbytes] |= 0xFE;
   break; 
  default:
   break;
 }

 printf("markbytes=%d\n",markbytes);
 printf("markbits=%d\n",markbits);

 printf("%ld ipoints\n",IPOINTS);
 printf("%ld blocks\n",ZONES);
 printf("Firstdatablock=%ld(%ld)\n",FIRSTZONE,RT_CLUSTER_START_POSITION);
 printf("Blocksize=%d\n",RT_BLOCK_SIZE);
}


int main(int argc,char **argv){
 char *tmp;
 struct stat statbuf;
 if(RT_IPOINT_SIZE*RT_IPOINTS_PER_BLOCK!=RT_BLOCK_SIZE)
  die("bad ipoint size");
 magic=RT_SUPER_MAGIC;
 device_name="/dev/sdb1";
 BLOCKS=get_size(device_name)/1024; 
 DEV=open(device_name,O_RDWR);
 if(DEV<0)
  die("unable to open %s");
 if(fstat(DEV,&statbuf)<0)
  die("unable to stat %s");
 setup_tables();
 write_tables();
 return 0;
}

 

 

/*rt.h*/

#include <linux/fs.h>
#include <linux/types.h>
#include <linux/magic.h>


#define RT_SUPER_MAGIC 0x0920
#define RT_BLOCK_SIZE 1024

#define CLUSTER_SIZE    50
#define DATAZONEBASE    16

#define RT_IPOINT_SIZE   4
#define RT_IPOINT_BLOCKS  4
#define RT_IPOINT_START_POSITION 2

#define RT_CLUSTERMAP_BLOCKS  10
#define RT_CLUSTERMAP_START_POSITION 6

#define RT_BLOCKS_PER_CLUSTER  50
#define RT_CLUSTER_START_POSITION 16

#define RT_IPOINTS_PER_BLOCK ((RT_BLOCK_SIZE)/RT_IPOINT_SIZE)
#define RT_IPOINTS_COUNT (RT_IPOINTS_PER_BLOCK*RT_IPOINT_BLOCKS)

struct rt_ipoint{
 __le32 i_latest_cluster[RT_IPOINTS_COUNT];
};

struct rt_super_block{
 __le64 s_total_blocks_count;         //the total blocks of the partation
 __le16 s_ipoint_start_position;   //the point index start position default 2
 __le16 s_ipoints_count;        //points count default 1024
 __le16 s_ipoint_num;              //point num default 0
 __u8   s_ipoint_size;             //point size default 4 Byte
 __le32 s_cluster_map_start_position;   //cluster bit map start position default 6(block) block=1024  byte;
 __le16 s_cluster_map_count;           //default 10 blocks
 __le16 s_cluster_start_position;    //cluster start position  default 16 (block)
 __u8   s_cluster_size;             //cluster size =50  blocks
 __le64 s_cluster_num;            //current cluster number
 __le64 s_cluster_count;          //clusters count
 __le64 s_create_time;      //rtfs create time
 __le64 s_write_time;       //the recent write time
 __le64 s_read_time;       //the read time
 __le16 s_magic;            //the magic of rtfs default  2336
};

 

 

/*Makefile*/

mkfs.gt:
 cc -g mkfs.db.c -o mkfs.db
clean: 
 rm -rf mkfs.db

 

运行命令:gdb mkfs.db

命令:l

            显示一页

命令:b 43

            设置断点第43行

命令:b setup_table

            设置断点函数名

命令:run

            运行mkfs.db

命令:n

            执行一条语句

命令:c

            执行到下一断点处

命令:p DEV

           打印变量DEV

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值