ext2 filesystem

5 views
Skip to first unread message

Lokesh

unread,
Feb 10, 2009, 7:31:23 PM2/10/09
to linuxgurus
Hello,

I am trying to build a table in the ext2 filesystem. The table needs
to be populated when the filesystem is created with data that resides
in a regular file. I am trying to read in the file during filesystem
setup, but am unable to do so. Many web references mention how this is
a bad idea or how to read from /proc. Does anyone know how to do a
regular file read from the kernel?

Thanks,

Jagadeesh

-----------------------------------------------
Message edited by
Lokesh
Moderator - Linuxgurus

Chaitanya

unread,
Feb 11, 2009, 5:13:50 AM2/11/09
to linuxgurus
As the web references tell you, its a really bad idea.
There is no protection, and its almost a raw i/o operation you're
doing.

If you still want to do it.
This is a simple file open, and read 1 byte of data.

<code>

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <asm/uaccess.h>

int init_module(void)
{
char c[1];
struct file *op;
op = filp_open("/root/abc", O_RDONLY, 0);
if(op == NULL) {
printk("error, opening\n");
return -1;
} else {
mm_segment_t fs;
fs = get_fs();
set_fs(get_ds());
printk("ok, continue\n");
int ret;
ret = op->f_op->read(op, c, 1, &op->f_pos);
set_fs(fs);
printk("%d\n", ret);
printk("%s\n", c);
return 0;
}
}

void cleanup_module(void)
{
//Nothing to do here
}

MODULE_LICENSE("GPL");

<end_of_code>

I tested it on a slightly modified 2.6.25 (fedora 9)

Assumptions :
1) You know how to get this module compiled.
2) There is a file called "abc" under your /root directory and it has
atleast 1 byte of data.

If you noticed the main function is "filp_open"
get_fs and set_fs are for the fs specific info.

This program can read from any filesystem, even cdroms!
If you're looking for something really specific to EXT then I suggest
you go a layer below this(as in below the VFS).
Reply all
Reply to author
Forward
0 new messages