# vnode



## ariarat (Jul 22, 2012)

Hi

I want to writing/reading files in kernel space. Can anyone explain me use of vn_rdwr()? I am beginner. Please show me sample code.

Thanks


----------



## Maelstorm (Jul 23, 2012)

Most of the system calls that userland programs use can also be used in kernel mode.  The functions pointed to by the system call tree are wrapper functions for the actual kernel routines.  You really haven't said much about what you are running this from, so I am going to assume that you are running this from a KLM.  I think you are going about it the wrong way.  Instead of using such a low-level function call, use one of the higher level calls and have the kernel manage everything for you.  If you don't need to reinvent the wheel, then don't.  Your best bet would be to take a look at /usr/src/sys/kern/vfs_syscalls.c at around line 1030 and work from there.

If you are wondering how I even found that, I used the following command:

[CMD=""]grep -Rn sys_open * | more[/CMD] from /usr/src/sys directory.

There are certain caviates when dealing with kernel source code.  First of all, at least those that I have looked at, all the system call functions start with sys_  So the open system call would be sys_open.  Those are generally wrapper functions that call kernel functions.  Those functions start with kern_.  So the kernel open function name is kern_open.  Then you go from there.  The same technique can be applied to all system calls.  After all, it's the system calls which allows programs to interact with the kernel.

Furthermore, using a similar command line, I found out what the UIO_USERSPACE is.  It's part of an enumerated data type that is defined in /usr/src/sys/sys/uio.h  Looking at what's available, you can tell it that the name is in user space, kernel space, or is already part of the object that you are working with.

Might want to do a google search.  I found LOTS of information about it.  The terms that I used were "freebsd read file in kernel mode" (without the quotes).


----------



## ariarat (Jul 23, 2012)

Thanks Maelstorm,
I want to read/write files inside a kernel module. Can you show any simple example?


----------



## Maelstorm (Jul 23, 2012)

I've never really done it myself, but it shouldn't be much different than doing it in userland with the exception that you are calling routines from kernel space.  The routine that you propose using requires ALOT of setup.  This is from /usr/src/sys/kern/vfs_vnops.c starting at line 364:


```
/*
 * Package up an I/O request on a vnode into a uio and do it.
 */
int
vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
    aresid, td)
        enum uio_rw rw;
        struct vnode *vp;
        void *base;
        int len;
        off_t offset;
        enum uio_seg segflg;
        int ioflg;
        struct ucred *active_cred;
        struct ucred *file_cred;
        int *aresid;
        struct thread *td;
```

The ucred stuff deals with credentials which means that this routine checks the credentials of the user against the credentials of the file in question.  The file also needs to have a vnode associated with it, which means that the file must already be open.  Looking at that function, you would be duplicating a fair amount of the kernel code to make that function happy and not cause a panic.

The functions that I would use are as follows:

kern_open   open(2) vfs_syscalls.c
kern_readv  read(2) sys_general.c
kern_writev write(2) sys_general.c
kern_close  close(2) kern_descrip.c

The logical progression would be to open the file and get a file descriptor, read/write some data, then close the file.  I included links to the man pages of the relative system calls because the parameters used such as flags, buffers, errors, and such are the same when calling the kernel mode functions (If you look at the source, the kernel mode functions do require some extra arguments, like struct thread (given) and if buffers are in kernel memory or user memory, etc.  Since I haven't done this myself, this is the best that I can give you.  You will need to do the rest on your own. :\


----------



## ariarat (Jul 23, 2012)

Thank you so much ... I am going for test.


----------

