Getting an error while trying to access struct file private_data - linux

The problem:
I have a pointer to struct file called flip, an int called cmd and an unsigned long called arg.
The private_data field in struct file points
the private_data structure is defined as follows:
typedef struct {
unsigned char key;
int read_state;
} my_private_data;
and ioctl function is defined as follows:
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;
}
However, when I try to compile the code, I get the following 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.
What can I do to fix this?
Here's the open method:
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;
}

The struct file member .private_data is defined as a void *, so filp->private_data itself has no members. If you want to store data items to your structure, you could access them with a local of that type; eg, within 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;

Related

how to get struct device's private data in the Linux's chrdev ->open() function?

I'm a beginnig learner of linux driver, so far I've studied how to write an basic char device driver and platform driver. I'm pricticing on the led example, I want to improve it from basic char device driver model to platform driver model.
In the earlier practice, I define a global int gpio_num[MAX_LED_NUM] array to keep the led's gpio. It's easy because I can indentify the led by device's minor number, and operate the corresponding led by referencing gpio_num[minor];
But in this improvement, I don't want to use global array to keep led's gpio because I can't predict how many leds on the board. So I malloc a structure for every platform device in the probe function to keep their own gpio, and call device_create() to create device node. It seems good so far because how many leds are there, there are how many structures and device nodes in the /dev directory, and there is no global variables.
In order to seperate led operation functions from char device driver, I define the led operaions functions in the platform driver part(driver.c) and pass the function sets to the char device driver part(leds.c). for example , I define the alpha_led_init(struct drv_priv *priv) in the driver.c, and call it from char device's open function(in leds.c) .
in order to call alpha_led_init(struct drv_priv *priv), the open function needs the parameter *priv(the private data of platform device which contains led_gpio). I've pass the private data to the char device by using device_create()'s third parameter. But how can I get it from the open function ? I can't get the struct device *pdev in the open function, so I can't call dev_get_drvdata(pdev) to get the platform device's private data , so there's no way to call alpha_led_init(struct drv_priv *priv).
Is my program model very bad? Any good way to pass platform device's private data to char device ? Any help or advice would be appreciating.
Below is my practicing code, for simplicity, some header files're omitted.
alpha_led.h
#ifndef __ALPHA_LED_H__
#define __ALPHA_LED_H__
#define LED_OFF (1)
#define LED_ON (0)
#define LED_MAX_NUM (10)
struct drv_priv
{
int led_gpio;
};
struct alpha_led_operations
{
int inited;
int (*alpha_led_init)(struct drv_priv *pdev);
};
#endif
driver.c
#include "alpha_led.h"
static int led_count;
static int alpha_led_init(struct drv_priv *priv)
{
int err;
char name[64];
if(!priv)
return -1;
memset(name, 0, sizeof(name));
snprintf(name, sizeof(name), "alpha-led-pin-%d", priv->led_gpio);
err = gpio_request(priv->led_gpio, name);
if(err)
return -1;
err = gpio_direction_output(priv->led_gpio, LED_OFF);
if(err) {
gpio_free(priv->led_gpio);
return -1;
}
return 0;
}
static int alpha_led_probe(struct platform_device *pdev)
{
int err, gpio;
const char *status = NULL;
struct drv_priv *priv = NULL;
struct device_node *np = pdev->dev.of_node;
if(!np)
return -1;
err = of_property_read_string(np, "status", &status);
if(err || (strcmp(status, "okay") != 0))
return -1;
gpio = of_get_named_gpio(np, "led-gpio", 0);
if(gpio < 0)
return -1;
// I malloc a drv_priv structure for every platform device to keep their private data
priv = devm_kzalloc(&pdev->dev, sizeof(struct drv_priv), GFP_KERNEL);
if(!priv)
return -ENOMEM;
platform_set_drvdata(pdev, priv);
// for every platform device, the gpio number is their private data.
priv->led_gpio = gpio;
// I call self-defined function in leds.c to create device node in /dev directory
// and pass the platform device's private data(priv) to the device_create()
return create_led_device_node(led_count++, np->name, priv);
}
static int alpha_led_remove(struct platform_device *pdev)
{
// get the platform device's private data
struct drv_priv *priv = platform_get_drvdata(pdev);
gpio_free(priv->led_gpio);
}
static const struct of_device_id alpha_led_of_match[] = {
{ .compatible = "alientek-alpha,led" },
{}
};
static struct platform_driver alpha_led_driver = {
.probe = alpha_led_probe,
.remove = alpha_led_remove,
.driver = {
.name = "alpha-led",
.of_match_table = alpha_led_of_match,
}
};
static int __init platform_driver_led_init(void)
{
int rc;
struct alpha_led_operations *ops;
rc = platform_driver_register(&alpha_led_driver);
// pass the lower led control functions to leds.c
ops = get_alpha_led_ops();
ops->alpha_led_init = alpha_led_init;
ops->inited = 1;
return 0;
}
static void __exit platform_driver_led_exit(void)
{
platform_driver_unregister(&alpha_led_driver);
}
module_init(platform_driver_led_init);
module_exit(platform_driver_led_exit);
MODULE_AUTHOR("David");
MODULE_LICENSE("GPL");
leds.c
#include "alpha_led.h"
#define LED_DEV_NAME ("alpha-led")
#define LED_CLASS_NAME ("alpha-led-class")
static int led_major;
static struct cdev led_cdev;
static struct class *led_class;
static int led_open(struct inode *inode, struct file *filp);
static int led_close(struct inode *inode, struct file *filp);
static struct alpha_led_operations alpha_led_ops;
static const struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_close,
};
static int led_open(struct inode *inode, struct file *filp)
{
int err, minor;
if(!inode || !filp)
return -1;
if(!alpha_led_ops.inited)
return -1;
if(!alpha_led_ops.alpha_led_init)
return -1;
//Question: here I want to call alpha_led_init(struct drv_priv *priv) defined in the driver.c
//But how can I get the parameter priv ? I know I have set it in the device_create(), but how can I fetch it here?
//Or Am writing a very bad platform driver model?
return alpha_led_ops.alpha_led_init(...);
}
static int led_close(struct inode *inode, struct file *filp)
{
return 0;
}
static int __init chrdev_led_init(void)
{
dev_t devid;
int i, rc, major, minor;
struct device *pdev;
if (led_major) {
devid = MKDEV(led_major, 0);
rc = register_chrdev_region(devid, LED_MAX_NUM, LED_DEV_NAME);
} else {
rc = alloc_chrdev_region(&devid, 0, LED_MAX_NUM, LED_DEV_NAME);
led_major = MAJOR(devid);
}
if(rc < 0)
goto chrdev_failed;
cdev_init(&led_cdev, &led_fops);
rc = cdev_add(&led_cdev, devid, LED_MAX_NUM);
if(rc < 0)
goto cdev_failed;
led_class = class_create(THIS_MODULE, LED_CLASS_NAME);
if(IS_ERR(led_class))
goto class_failed;
return 0;
class_failed:
cdev_del(&led_cdev);
cdev_failed:
unregister_chrdev_region(devid, LED_MAX_NUM);
chrdev_failed:
return -1;
}
static void __exit chrdev_led_exit(void)
{
class_destroy(led_class);
cdev_del(&led_cdev);
unregister_chrdev_region(MKDEV(led_major, 0), LED_MAX_NUM);
}
int create_led_device_node(int minor, const char *name, void *priv)
{
struct device *dev = NULL;
if(minor >= LED_MAX_NUM)
return NULL;
//device_create take the platform device's private data(priv) as it's own private data.
if(name)
dev = device_create(led_class, NULL, MKDEV(led_major, minor), priv, "%s", name);
else
dev = device_create(led_class, NULL, MKDEV(led_major, minor), priv, "led-%d", minor);
if(!dev)
return -1;
return 0;
}
void destroy_led_device_node(int minor)
{
device_destroy(led_class, MKDEV(led_major, minor));
}
struct alpha_led_operations * get_alpha_led_ops(void)
{
return &alpha_led_ops;
}
EXPORT_SYMBOL(create_led_device_node);
EXPORT_SYMBOL(destroy_led_device_node);
EXPORT_SYMBOL(get_alpha_led_ops);
module_init(chrdev_led_init);
module_exit(chrdev_led_exit);
MODULE_AUTHOR("David");
MODULE_LICENSE("GPL");

Why Input/Output Error occurs when use ls command in my fuse filesystem?

Why I/O Error causes when I tried ls . command in my fuse filesystem?
My filesystem has a limitation that it only allows mail address type as individual filename and it does not allows sub directory.
Now I want to display a list of file name when use ls . but it does not work.
I understood that it must implement a callback function. (Correspond function is ll_readdir in mycode)
but I have no idea what points are causes the errors.
Update:
Now I use strace command to investigate what system call raise a this error.
According to result of strace, this error caused in getdents64 syscall.
getdents64(3, 0x5611ed000540, 32768) = -1 EIO (Input/output error)
Code1 (implementation of mm:
struct mutex_map {
int counter = 2;
std::mutex _mtx;
std::unordered_map<int, std::string> _data;
std::unordered_map<std::string, int> _rev_data;
public:
int set_value(const char* value) {
std::string s = std::string(value);
std::lock_guard<std::mutex> lock(_mtx);
counter++;
_data[counter] = s;
_rev_data[s] = counter;
return counter;
}
const char* get_value(int key) { return _data[key].c_str(); }
int get_ino(const char* name) { return _rev_data[std::string(name)]; }
};
static mutex_map mm;
Code2: (sendmailfs_stat)
static int sendmailfs_stat(fuse_ino_t ino, struct stat* stbuf,
size_t name_length) {
uid_t uid = getuid();
gid_t gid = getgid();
stbuf->st_ino = ino;
if (ino == 1) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
stbuf->st_uid = uid;
stbuf->st_mode = S_IFDIR;
} else {
stbuf->st_mode = S_IFCHR | 0666;
stbuf->st_nlink = 1;
stbuf->st_size = name_length;
stbuf->st_uid = uid;
stbuf->st_gid = gid;
}
return 0;
}
Code 3: (implementation of readdir callback)
static void ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
struct fuse_file_info* fi) {
// printf("size_t=%ld, fh=%ld, ino=%ld\n", size, fi->fh, ino);
if (ino == 1) {
off_t o = 0;
size_t rest = size;
size_t res;
char* buf = (char*)calloc(1, size);
struct stat dotst;
sendmailfs_stat(ino, &dotst, strlen("."));
res = fuse_add_direntry(req, buf, rest, ".", &dotst, o);
rest -= res;
o++;
printf("%s\n", "start of loop");
uint64_t num_contain = 0;
for (auto& c : mm._data) {
const char* t = c.second.c_str();
int ino2 = mm.get_ino(t);
struct stat st;
sendmailfs_stat(ino2, &st, strlen(t));
fuse_entry_param e;
e.ino = ino2;
e.attr_timeout = 0;
sendmailfs_stat(ino2, &e.attr, strlen(t));
res = fuse_add_direntry_plus(req, buf, rest, t, &e, o);
o += 1;
rest -= res;
}
fuse_reply_buf(req, buf, size);
}
}
A bit late, but if anyone having this error stumbles upon this thread, they might want to check first whether the filesystem is mounted properly. The Input/output error from getdents64 is symptomatic of a filesystem that was unmounted, but failed for some reason (like a file was still in use when user tried the umount command), so still looks mounted, but no data can be fetched from it.
So in this case, some process could be calling umount (and failing) before you run ls, or the filesystem failed to correctly mount in the first place for some reason.

I need an explanation with File I/O I am recieving warnings I do not understand

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
struct string_count_struct{
char fname;
char str;
long long count;
};
void* filesearch(void* arg)
{
//get the file name
struct string_count_struct *arg_ptr = (struct string_count_struct*) arg;
int line_num = 1;
int find_result = 0;
char temp[512];
//create a file pointer
FILE *fp;
fp = fopen(arg_ptr -> fname, "r");
//dont forget error handling
if (fp == NULL){
printf("File could not be opened");
return(-1);
}
while (fgets(temp, 512, fp) != NULL) {
if ((strstr(temp, arg_ptr -> str)) != NULL) {
find_result++;
}
line_num++;
}
if(find_result = 0) {
printf("\nSorry, couldn't find a match.\n");
}
arg_ptr -> count = find_result;
//close the file
if (fp){
fclose(fp);
}
pthread_exit(0);
}
int main (int argc, char **argv)
{
if (argc < 3) {
printf("Usage: <file> <string> <arg1> <arg2>...<argN>\n", argv[0]);
exit(-1);
}
int num_args = argc - 2;
struct string_count_struct args[num_args];
//Thread Creation:
pthread_t tids[num_args];
for(int i = 0; i < num_args; i++) {
args[i].fname = atoll(argv[i + 2]);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tids[i], &attr, filesearch, &args[i]);
}
//Wait until work is completed
for (int i = 0; i < num_args; i ++){
pthread_join(tids[i], NULL);
printf("blah is blah %lld\n", args[i].count);
}
return 0;
}
Here are my warnings
root#kali:~/Desktop# gcc prog2.c -lbthread
prog2.c: In function ‘filesearch’:
prog2.c:29:13: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion]
fp = fopen(arg_ptr -> fname, "r");
^~~~~~~
In file included from prog2.c:1:0:
/usr/include/stdio.h:274:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern FILE *fopen (const char *__restrict __filename,
^~~~~
prog2.c:34:8: warning: return makes pointer from integer without a cast [-Wint-conversion]
return(-1);
^
prog2.c:38:21: warning: passing argument 2 of ‘strstr’ makes pointer from integer without a cast [-Wint-conversion]
if ((strstr(temp, arg_ptr -> str)) != NULL) {
^~~~~~~
In file included from prog2.c:4:0:
/usr/include/string.h:337:14: note: expected ‘const char *’ but argument is of type ‘char’
extern char *strstr (const char *__haystack, const char *__needle)
^~~~~~
prog2.c: In function ‘main’:
prog2.c:78:17: error: assignment of read-only member ‘fname’
args[i].fname = atoll(argv[i + 2]);
I am unsure of what I am doing wrong, these errors are preventing my program from correctly reading through the desired files and calculating the # of occurrences of a particular string that the user will select. I have fixed my error but not the warnings.
The program will take a command line argument, create a separate thread for each file to be searched through, search through each file, and then give the results. I plan on using a Mutex for further refinement, but right now I am just trying to solve my I/O issues.
Just addressing some of the warnings, I'm not at all sure if this will make the code work:
In line 29, fopen expects a filename (char *) but fname is just a char in string_count_struct. Make it a char*. In the main function you convert one of the arguments from ASCII to long long and assign it to fname, which shall later be used as a file name for fopen(). That's probably not what you want to do.
In line 34, you return -1 which is not a pointer. You declared the function to be returning a void pointer. Make it return(0) (or return(NULL)).
Same as in line 29 happens in line 38: str in the struct string_count_struct is just a char, but strstr expects a char*. Make it a char*, too.
Your "Usage" is missing a "%s" format string to actually print the argument argv[0].

How can i use skb->cb in my custom kernel modules

I build a new kernel module(2.6.32 CentOS6.5) named "xt_hello.ko", and I want to send some custom data to nflog, so I have changed skb->cb in my module, and nflog can read my data correctly.
Question: I found cb field has been used in tcp netlink and etc, can my module make some bad influence for them?
Definition of sk_buff:
struct sk_buff {
/* These two members must be first. */
struct sk_buff *next;
struct sk_buff *prev;
struct sock *sk;
ktime_t tstamp;
struct net_device *dev;
unsigned long _skb_dst;
#ifdef CONFIG_XFRM
struct sec_path *sp;
#endif
/*
* This is the control buffer. It is free to use for every
* layer. Please put your private variables there. If you
* want to keep them across layers you have to do a skb_clone()
* first. This is owned by whoever has the skb queued ATM.
*/
char cb[48];
... skip ...
Definition of cb in netlink:
#define NETLINK_CB(skb) (*(struct netlink_skb_parms*)&((skb)->cb))
#define NETLINK_CREDS(skb) (&NETLINK_CB((skb)).creds)
Core code in my kernel module:
My definition:
struct xt_ndpi_cb {
u_int16_t protocol_detected ;
u_int16_t ndpi_proto;
}xt_ndpi_cb_t;
#define NDPI_CB(skb) (*(struct xt_ndpi_cb*)&((skb)->cb))
#define NDPI_CB_RECORD(skb,entry) NDPI_CB(skb).ndpi_proto = entry.VALUEA; NDPI_CB(skb).protocol_detected = entry.VALUEB;
/*core func*/
static bool ndpi_process_packet_tg(const struct sk_buff *_skb, const struct
xt_ndpi_tginfo *info, struct nf_conn *ct) {
...skip...
if (/*condition*/)
NDPI_CB_RECORD(_skb, entry)
}
Kernel module .c:
static bool ndpi_match(const struct sk_buff *skb, struct xt_action_param *par){
bool verdict;
struct nf_conn * ct;
enum ip_conntrack_info ctinfo;
const struct xt_ndpi_protocols *info = par->matchinfo;
ct = nf_ct_get(skb, &ctinfo);
if((ct == NULL) || (skb == NULL)) {
return(false);
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
} else if (nf_ct_is_untracked(skb)) {
#else
} else if(nf_ct_is_untracked(ct)) {
#endif
return false;
}
/*change cb in this func*/
verdict = ndpi_process_packet(skb, info, ct );
return(verdict);
}
static struct xt_match ndpi_regs[] __read_mostly = {
{
.name = "ndpi",
.revision = 0,
.family = NFPROTO_IPV4,
.match = ndpi_match,
.matchsize = sizeof(struct xt_ndpi_protocols),
.me = THIS_MODULE,
}
};
Use in xt_NFLOG.c:
static unsigned int
nflog_tg(struct sk_buff *skb, const struct xt_target_param *par)
{
char buf[64+16]; /**my buf/
const struct xt_nflog_info *info = par->targinfo;
...skip...
sprintf(buf,"%s MYID=%u",info->prefix,NDPI_CB(_skb).ndpi_proto); /*NFLOG can get right cb value*/
...skip...
}

Getting error: implicit declaration of function 'proc_create'

I am getting the "implicit declaration of function 'proc_create'" error while compiling my driver module.I want to create a entry in /proc and print the number of programs which are using the module. Can you please let me know what is wrong in here?? Here is my code.
#include<linux/module.h>
#include<linux/fs.h>
#define HELLO_MAJOR 234
static int debug_enable = 0;
static int no_of_access;
module_param(debug_enable, int, 0);
MODULE_PARM_DESC(debug_enable, "Enable module debug mode.");
struct file_operations hello_fops;
struct proc_dir_entry *proc_file_entry;
<File operation functions...>
<Incremented global_counter in the file open operation.>
static int hello1_read_proc(char *buf, char **start, off_t offset,
int count, int *eof, void *data)
{
int len=0;
len += sprintf(buf+len, no_of_access);
*eof=1;
return len;
}
static int __init hello_init(void)
{
int ret;
proc_file_entry = proc_create("examples/hello1", 0,NULL, hello1_read_proc);
if(proc_file_entry == NULL)
return -ENOMEM;
printk("\nProc file entry for hello1 has been created !!!\n");
}
static void __exit hello_exit(void)
{
printk("Hello Example Exit\n");
remove_proc_entry("exmaples/hello1", NULL);
unregister_chrdev(HELLO_MAJOR,"hello1");
}
Thanks in advance.
You also need to include <linux/proc_fs.h>

Resources