0

I am using vfs_write(...) to write a file in kernel module. If an error occurs while writing file I want the changes to get reverted and file should be restored to its original state. Currently I am thinking of maintaining a temp file which contains data of file to be written and in case of faliure replace the temp file with original file but its too much work in kernel module.

I am stuck here anyone know any work around this ?

Morten Kristensen
  • 7,412
  • 4
  • 32
  • 52
user3367692
  • 11
  • 1
  • 3

1 Answers1

0

It might be too much work, but it sounds like the right solution.

Alternatively, handle the getting data from the kernel userspace (e.g. read it from a file in /proc/ or similar), and make this atomically replace the file concerned.

abligh
  • 24,573
  • 4
  • 47
  • 84
  • How to exactly read/write in /proc/ ? and any other suggestions you can think of ? as writing a temp files sounds too much work in kernel module... – user3367692 Mar 01 '14 at 16:56
  • To put a custom file in `/proc`, see for example: http://stackoverflow.com/questions/588848/how-to-create-proc-entry-under-proc-driver . You can read this file in userspace and atomically replace the file on the disk. Or perhaps just having it in `/proc` is enough? You could for instance symlink the file on disk to the `/proc` file. – abligh Mar 01 '14 at 16:58
  • Hey, can you provide me code for how to link a proc file with a given file (Replace given file with proc file after process completion) – user3367692 Mar 02 '14 at 00:39
  • Really? `ln -sf /proc/my/proc/file /file/on/disk/somewhere`. Changes in `/proc` are by their nature atomic. – abligh Mar 02 '14 at 07:29
  • Dude i was talking about code for kernel module... not in terminal. – user3367692 Mar 02 '14 at 18:15
  • Indeed. And I am telling you that you could do what you want by creating a file in `/proc` in your module and creating a symlink to that file in `/proc`. The creation of the symlink need not be in the kernel module because it would be created once, when your kernel module is first installed. – abligh Mar 02 '14 at 19:10
  • 1
    And if you *really* want to make the symlink from kernel space, it's the `sys_symlink` call I think, which you need to wrap with `set_fs` like this: http://www.linuxjournal.com/node/8110 though please do read Greg's comments that say *'don't do this'* – abligh Mar 02 '14 at 21:22
  • when I call sys_symlink it says. Warning: sys_symlink symbol undefined. Any idea why is this ? – user3367692 Mar 02 '14 at 21:28
  • Yeah the function is being imported from syscalls.h but it gives me a warning when i make, not an error. And it gives an error when i do insmod. It says undefined symbol in sys_symlink in function.ko – user3367692 Mar 02 '14 at 21:34
  • 1
    The symbol is probably not exported for modules then, either put it in the kernel, add an export symbol to the kernel, or use one of the other dubious ways to get around this problem. Linux makes this sort of thing hard for a reason. Why is creating the link once in user space so bad? – abligh Mar 02 '14 at 21:37
  • Can you tell how to add export symbol to the kernel ? I have been given to do this task so i have no option :) – user3367692 Mar 02 '14 at 22:13
  • 1
    Add an `EXPORT_SYMBOL` macro. However I would suggest you divert your energies either to re-understanding what your task is or advocating a different approach. – abligh Mar 03 '14 at 00:24