# Problem with reading a file in kernel module



## walden (Jul 25, 2011)

Hi all,

I'm current working on a kernel module that needs to read a data file at load time, but I don't know how to do that. I know reading or writing files from a kernel module is not a good idea, but that file contains one million number that was pre-calculated in userland using complex math formula. That's too difficult to implement in kernel space.

I had done this in Linux successfully. Now I need to implement it in FreeBSD, and is stuck on this issue. Does FreeBSD provide some mechanisms to do file I/O in kernel module?

Thanks a lot.

walden


----------



## gordon@ (Jul 25, 2011)

Why not just go ahead and add it to the build of the kernel module and build it in?


----------



## walden (Jul 26, 2011)

Because there are 1 million 64 bit integer, totally 8M size. It's too big, so I don't want to build it into the module.

I find some useful routines in /usr/src/sys/cddl/compat/opensolaris/kern/opensolaris_kobj.c:


```
struct _buf *kobj_open_file(const char *path);
int kobj_get_filesize(struct _buf *file, uint64_t *size);
int kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off);
void kobj_close_file(struct _buf *file);
```

I will have a try.

Thanks.


----------

