ceph存储 字符设备驱动示例x86Linux平台

1、驱动头文件:

#ifndef __ICFS_MOD_CMM_H__
#define __ICFS_MOD_CMM_H__

extern int icfs_cmd_init(void);
extern void icfs_cmd_exit(void);

extern int icfs_ksym_init(void);
extern void icfs_ksym_exit(void);

extern int icfs_stat_init(void);
extern void icfs_stat_exit(void);

#endif

2、驱动C文件:

#include <linux/ctype.h>
#include <asm/uaccess.h>
#include <linux/mm.h>
#include <asm/io.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("inspur icfs develop group");

#define ICFS_DEBUG_DEV    "ICFS_DEBUG_DEV"

#define STATISTICS_BUF_SIZE  256
//add by zyl for debug
#define MAJOR_NUM 200 //主设备号

static struct class *pstICFSDebugDrv_class;
static struct device *pstICFSSDebugDrv_class_dev;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;

extern char *icfs_kernel_version;
extern int icfs_stat_start(void);
extern int icfs_stat_stop(void);


#if ICFS_KERN_VERSION <=2006032000
int icfs_debug_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
#else
long icfs_debug_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
#endif
{
    char buf[STATISTICS_BUF_SIZE];
    unsigned long ret;
   
 printk(KERN_ALERT" icfs_debug_ioctl arg %lu\n", arg);
 
 ret = copy_from_user(buf, (struct ioctl_data *)arg, STATISTICS_BUF_SIZE);
 switch (cmd)
 {
  case 0:
   printk(KERN_ALERT"icfs_debug_ioctl case 0: \n");
   break;
  case 2:
   if(NULL != icfs_kernel_version)
   {
    printk(KERN_ALERT"version zhuo: %s\n", icfs_kernel_version);
   }
   else
   {
    printk(KERN_ALERT"icfs_kernel_version Failed!\n");
   }
   break;
  case 3:
   if(icfs_stat_start())
   {
    printk(KERN_ALERT"icfs_stat_start Failed!\n");
   }
   else
   {
    printk(KERN_ALERT"icfs_stat_start Success!\n");
   }
   break;
  case 4:
   if(icfs_stat_stop())
   {
    printk(KERN_ALERT"icfs_stat_stop Failed\n");
   }
   else
   {
    printk(KERN_ALERT"icfs_stat_stop Success!\n");
   }
   break;
  default:
   printk(KERN_ALERT"icfs_debug_ioctl default: \n");
 }
 //strcpy((char*)arg, "hello world,too\n");
 //printk(KERN_ALERT"%s", buf);
 //copy_to_user((void *)arg, buf, 100);
 
 return 0;

}

struct file_operations icfs_debug_fops = {
#if ICFS_KERN_VERSION <=2006032000
  .ioctl = icfs_debug_ioctl,
#else
  .unlocked_ioctl = icfs_debug_ioctl,
#endif
};

int icfs_cmd_init(void)
{
 int ret;
 
 ret = register_chrdev(MAJOR_NUM,ICFS_DEBUG_DEV, &icfs_debug_fops);
 if(ret)
 {
  printk(KERN_ALERT"icfs_debug_dev register failure\n");
  return ret;
 }
 else
 {
     pstICFSDebugDrv_class = class_create(THIS_MODULE, ICFS_DEBUG_DEV);
  
  pstICFSSDebugDrv_class_dev = device_create(pstICFSDebugDrv_class,
                                          NULL,
                                          MKDEV(MAJOR_NUM, 0),
                                          NULL,
                                          "icfs_debug_dev");

  printk(KERN_ALERT"icfs_debug_dev register success!\n");
 }
 return 0;
}

void icfs_cmd_exit(void)
{
 unregister_chrdev(MAJOR_NUM, ICFS_DEBUG_DEV);
 device_unregister(pstICFSSDebugDrv_class_dev);
 class_destroy(pstICFSDebugDrv_class);

 return;
}

module_init(icfs_cmd_init);
module_exit(icfs_cmd_exit);

3、Makefile:

export C_INCLUDE_PATH=/root/ceph-client-kernel/include
obj-m := icfsmod.o
KVERSION = $(shell uname -r)
KERNELDIR :=/usr/src/kernels/2.6.32-358.el6.x86_64/
ccflags-y += -DICFS_KERN_VERSION=2006032000
PWD := $(shell pwd)
icfsmod-y := icfscmm.o icfsstat.o icfscmd.o icfsksym.o
modules:
 $(MAKE) -C  $(KERNELDIR) M=$(PWD) modules
modules_install:
 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
 $(MAKE) -C /lib/modules/$(KVERSION)/build M=$(PWD) clean


4、upper_call:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>

#include "statistics.h"

struct icfs_cmd_func_t
{
 char cmd[32];
 int (*call_back_t)(int argc, char **agrv);
 char help[128];
};

int icfs_cmd_help(int, char **);
int icfs_cmd_showversion(int, char **);
int icfs_cmd_stat_start(int, char **);
int icfs_cmd_stat_stop(int, char **);


struct icfs_cmd_func_t g_stat_cmds[ICFS_CMD_COUNT] =
{
 {"-h",   icfs_cmd_help,     "help",     },
 {"-v",   icfs_cmd_showversion, "show version", },
 {"-stat_start",  icfs_cmd_stat_start,        "stat_start",         },
    {"-stat_stop",  icfs_cmd_stat_stop,  "stat_stop",    },
};

void usage()
{
  printf("-------------------------------------------------\n");
  printf(" ./ioctl statistics [parameter]\n");
  printf(" for example: \n");
  printf("   ./ioctl statistics -h\n");
  printf("--------------------------------------------------\n");
}

int icfs_cmd_help(int argc, char **agrv)
{
 int i = 0;

 for(i=0; i<ICFS_CMD_COUNT; i++)
 {
     printf("%s %s\n", g_stat_cmds[i].cmd, g_stat_cmds[i].help);
 }
 
    return 0;
}

int icfs_cmd_showversion(int argc, char **agrv)
{
 int iFd = 0;
 char acBuf[STATISTICS_BUF_SIZE];

    iFd = open("/dev/icfs_debug_dev", O_RDONLY);
    if(iFd < 0)
    {
          printf("open df error!\n");
          return -1;
    }
 
    ioctl(iFd, STATISTIC_CMD_VERSION, acBuf);
    close(iFd);
 
 return 0;
}

int icfs_cmd_stat_start(int argc, char **agrv)
{
 int iFd = 0;
 char acBuf[STATISTICS_BUF_SIZE];

    iFd = open("/dev/icfs_debug_dev", O_RDONLY);
    if(iFd < 0)
    {
          printf("open df error!\n");
          return -1;
    }
 
    ioctl(iFd, STATISTIC_CMD_STAT, acBuf);
    close(iFd);
 
 return 0;
}

int icfs_cmd_stat_stop(int argc, char **agrv)
{
 int iFd = 0;
 char acBuf[STATISTICS_BUF_SIZE];

    iFd = open("/dev/icfs_debug_dev", O_RDONLY);
    if(iFd < 0)
    {
          printf("open df error!\n");
          return -1;
    }
 
    ioctl(iFd, STATISTIC_CMD_STOP, acBuf);
    close(iFd);
 
 return 0;
}

int icfs_cmd_dispatch(int argc, char **argv)
{
 int i;

 for (i=0; i<ICFS_CMD_COUNT; i++)
 {
  if (!strcmp(argv[1], g_stat_cmds[i].cmd))
  {
   g_stat_cmds[i].call_back_t(argc, argv);
  }
 }

 return 0;
}

int main(int argc, char *argv[])
{
  int ret;

  if(argc < 2){
    usage();
    return -1;
  }

  ret = icfs_cmd_dispatch(argc, argv);
 
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值