Skip to contentSkip to conversation

Reading from file in module fails

DISCUSSION Libor Vanek 3 participants 6 messages May 3, 2004

lore
Share & tools
lore
#0 lore raw

Hi,
I'm writing module which needs to read from file. I've got this simple sample code:

char buffer[4096];
ssize_t read;
file *f;
f = filp_open("/some/file",O_RDONLY | O_LARGEFILE,0); f->f_pos = 0;
read = vfs_read(f,(char __user *) buffer,4096,&f->f_pos);

but here read value is "-14" (-EINVAL?) Does anybody has idea what's wrong?

It seems that file is opened OK (I've tested:

if (f->f_op->read) {
	read = f->f_op->read(file, buf, count, pos);
}

and result was the same - so I assume that file is opened OK and structure "file f" is filled correctly.

I need to copy files (yes - I know that kernel shouldn't do this but I REALLY need) and there is nothing like "sys_copy" and "sys_sendfile" is not exported (which seems strange to me but there is nothing like EXPORT_SYMBOL(sys_sendfile) in fs/read_write.c

Thanks,
Libor Vanek

#1 lore raw
Quoted 2 lines · Libor Vanek

I need to copy files (yes - I know that kernel shouldn't do this but I REALLY need).

This is ugly, but it should do the trick:

int rv;
char *argv[4] = {"/bin/cp", "/tmp/foo", "/tmp/bar", NULL}; char *envp[3] = {"HOME=/", "PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL};

rv = call_usermodehelper(argv[0], argv, envp, 1);

if(rv < 0) {
	/* error handling */
}

Called from kernel, done in userspace. And if you want to access an SQL database from kernel tomorrow, it's just a matter of changing the usermode helper.

(BTW, if you need to copy files from kernel, it's usually a sign of bad design)

Erik

Signature · 2 lines
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
#2 lore raw
Quoted 15 lines
Quoted 2 lines

I need to copy files (yes - I know that kernel shouldn't do this but I REALLY need).

This is ugly, but it should do the trick:

int rv;
char *argv[4] = {"/bin/cp", "/tmp/foo", "/tmp/bar", NULL}; char *envp[3] = {"HOME=/", "PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL};

rv = call_usermodehelper(argv[0], argv, envp, 1);

if(rv < 0) {
	/* error handling */
}
Quoted 6 lines

Called from kernel, done in userspace. And if you want to access an SQL database from kernel tomorrow, it's just a matter of changing the usermode helper.

(BTW, if you need to copy files from kernel, it's usually a sign of bad design)

Geez - that's REALLY ugly :-) But for testing I can use it.

It's not bad design - what I'm doing is writing snapshots for VFS as my diploma thesis. And I need to create copy of file before it's changed (copy-on-write). There is no other way how to do it in kernel-space (and user-space solutions like using LUFS are really slow)

Thx,
Libor

#3 lore raw
Quoted 8 lines · Libor Vanek
Quoted 2 lines

(BTW, if you need to copy files from kernel, it's usually a sign of bad design)

It's not bad design - what I'm doing is writing snapshots for VFS as my diploma thesis. And I need to create copy of file before it's changed (copy-on-write). There is no other way how to do it in kernel-space (and user-space solutions like using LUFS are really slow)

Have a look at the cowlinks (copy-on-write links) thread from last month, it might do the trick.

Erik

Signature · 2 lines
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
author
#4 lore raw
Quoted 10 lines · Erik Mouw
Quoted 7 lines · Libor Vanek
Quoted 2 lines

(BTW, if you need to copy files from kernel, it's usually a sign of bad design)

It's not bad design - what I'm doing is writing snapshots for VFS as my diploma thesis. And I need to create copy of file before it's changed (copy-on-write). There is no other way how to do it in kernel-space (and user-space solutions like using LUFS are really slow)

Have a look at the cowlinks (copy-on-write links) thread from last month, it might do the trick.

Hmmm, that seems to have several serious disadvantages:
- it's not filesystem/user-space SW independent (requires modifying them - as far as I understand how it's done)
- needs to "touch" all existing files/dirs (maybe I'm wrong but from very first look it seems to me that I need to setup "cow" flag on each file/dir I want to "cow") - my approach should be "atomic" even when I need to setup snapshot on directory containing 100s thousands of files/dirs (and that's an average server I'm thinking of!)

Nevertheless - thanks for a tip.

Libor

#5 lore raw
In reply to Libor Vanek

On Mon, 2004-05-03 13:43:16 +0200, Libor Vanek <[email protected]> wrote in message <20040503114316.GA22732@Loki>:

Quoted 1 line

It's not bad design - what I'm doing is writing snapshots for VFS as my diploma thesis. And I need to create copy of file before it's changed (copy-on-write). There is no other way how to do it in kernel-space (and user-space solutions like using LUFS are really slow)

That can all be done in userspace.

$ export LD_PRELOAD=libcopyfilesbeforemodify.so

You just need to program a library that provides all functions that modify files (eg. write, open with O_CREAT, ...) and you're done - 100% in userspace.

MfG, JBG

Signature · 4 lines
   Jan-Benedict Glaw       [email protected]    . +49-172-7608481
   "Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg
    fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!
   ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));