尝试访问结构文件 private_data 时出错

Getting an error while trying to access struct file private_data

问题: 我有一个指向名为 flip 的结构文件的指针,一个名为 cmd 的 int 和一个名为 arg 的 unsigned long。 struct file指向的private_data字段 private_data结构定义如下:

typedef struct {
    unsigned char key;
    int read_state;
} my_private_data;

ioctl函数定义如下:

int my_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{

    switch (cmd) {
        case CMD_CHANGE_KEY:
            filp->private_data->key = (char)cmd; 
            filp->private_data->read_state = (int)arg; 
            break;
        case CMD_SET_READ_STATE:
            filp->private_data->key = (char)cmd;
            filp->private_data->read_state = (int)arg;
            break;
        case CMD_ZERO:
            kfree(buff_caesar);
            break;
    }
    return 0;
}

然而,当我尝试编译代码时,我得到以下 warning/error:

Warning: dereferencing 'void *' pointer.
Requesting for Member 'key' in something not a struct or union.
Warning: dereferencing 'void *' pointer.
Requesting for Member 'read_state' in something not a struct or union.

我该怎么做才能解决这个问题?

打开方法如下:

int my_open(struct inode *inode, struct file *filp)
{
    int minor = MINOR(inode->i_rdev);

    
    if(minor == ONE) {
        /* Caesar*/
        filp->f_op = &fops_caesar;
    }
    else if(minor == TWO){
        /*Xor*/
        filp->f_op = &fops_xor;
    }
    else return -ENODEV;

    
    my_private_data* privateData = NULL;
    privateData = kmalloc(sizeof(my_private_date),GFP_KERNEL);
    if (privateData==NULL){
        return -1;
    }
    filp->private_data = privateData;

    return 0;
}

struct file 成员 .private_data 定义为 void *,因此 filp->private_data 本身没有成员。如果您想将数据项存储到 您的 结构中,您可以使用该类型的本地访问它们;例如,在 my_ioctl() 内:

my_private_data *info;
. . .

if (filp->private_data == NULL) {
    error handling
}
info = filp->private_data;
. . .
info->key = (char) cmd;
info->read_state = (int) arg;