2006-12-11 15:23:07

by Silviu Craciunas

[permalink] [raw]
Subject: get device from file struct

quick question for the gurus.. is it possible to determine the hardware
device from a file struct during read/write system call. For example in
fs/read_write.c when doing a vfs_read.

thanks


2006-12-11 18:34:24

by Brice Goglin

[permalink] [raw]
Subject: Re: get device from file struct

Silviu Craciunas wrote:
> quick question for the gurus.. is it possible to determine the hardware
> device from a file struct during read/write system call. For example in
> fs/read_write.c when doing a vfs_read.
>

file->f_dentry->d_inode gives you the inode. If the inode is on top of a
block device, inode->i_bdev gives you a struct block_device that might
help you.

Brice

2006-12-12 09:04:49

by Silviu Craciunas

[permalink] [raw]
Subject: Re: get device from file struct

On Mon, 2006-12-11 at 19:34 +0100, Brice Goglin wrote:
> Silviu Craciunas wrote:
> > quick question for the gurus.. is it possible to determine the hardware
> > device from a file struct during read/write system call. For example in
> > fs/read_write.c when doing a vfs_read.
> >
>
> file->f_dentry->d_inode gives you the inode. If the inode is on top of a
> block device, inode->i_bdev gives you a struct block_device that might
> help you.
>
> Brice

thanks for the reply, the block device can be determined with the major
and minor numbers , what I would be more interested in is if one can get
the net_device struct from the file struct

silviu

2006-12-13 10:38:13

by Jan Engelhardt

[permalink] [raw]
Subject: Re: get device from file struct


>thanks for the reply, the block device can be determined with the major
>and minor numbers , what I would be more interested in is if one can get
>the net_device struct from the file struct

Just how are you supposed to match files and network devices?


-`J'
--

2006-12-13 10:47:14

by Silviu Craciunas

[permalink] [raw]
Subject: Re: get device from file struct

On Wed, 2006-12-13 at 10:59 +0100, Jan Engelhardt wrote:
> >thanks for the reply, the block device can be determined with the major
> >and minor numbers , what I would be more interested in is if one can get
> >the net_device struct from the file struct
>
> Just how are you supposed to match files and network devices?
>
>
> -`J'

from the struct file you can get the struct socket and from there to the
struct sock . What I would like to find out is where the data is coming
from (read) and where it is going to(write) or if it is even possible to
find the net device out using the struct file.

Silviu

2006-12-13 11:04:23

by Jan Engelhardt

[permalink] [raw]
Subject: Re: get device from file struct


>> >thanks for the reply, the block device can be determined with the major
>> >and minor numbers , what I would be more interested in is if one can get
>> >the net_device struct from the file struct
>>
>> Just how are you supposed to match files and network devices?
>>
>
>from the struct file you can get the struct socket and from there to the
>struct sock .

That only applies when using PF_LOCAL sockets.

>What I would like to find out is where the data is coming
>from (read) and where it is going to(write) or if it is even possible to
>find the net device out using the struct file.

I really don't get what you want.

Suppose a daemon reads from a socket (PF_INET), then there is a file descriptor
to sockfs (look into /proc/$$/fd/). Well, then you may be able to get the
struct file for that socket, but it does not connect to a regular file
(S_IFREG) at all.


-`J'
--

2006-12-13 12:29:36

by Silviu Craciunas

[permalink] [raw]
Subject: Re: get device from file struct

On Wed, 2006-12-13 at 12:03 +0100, Jan Engelhardt wrote:
> >> >thanks for the reply, the block device can be determined with the major
> >> >and minor numbers , what I would be more interested in is if one can get
> >> >the net_device struct from the file struct
> >>
> >> Just how are you supposed to match files and network devices?
> >>
> >
> >from the struct file you can get the struct socket and from there to the
> >struct sock .
>
> That only applies when using PF_LOCAL sockets.
>
> >What I would like to find out is where the data is coming
> >from (read) and where it is going to(write) or if it is even possible to
> >find the net device out using the struct file.
>
> I really don't get what you want.
>
> Suppose a daemon reads from a socket (PF_INET), then there is a file descriptor
> to sockfs (look into /proc/$$/fd/). Well, then you may be able to get the
> struct file for that socket, but it does not connect to a regular file
> (S_IFREG) at all.
>
>
> -`J'

in fs/read_write.c, the vfs_read function does:

file->f_op->read(file, buf, count, pos);

after this call is it possible to determine where the
data is coming from? e.g., the first hard disk, a pipe
or from a socket. If it is a socket we are interested
from which device (eth0, eth1, lo, ...) the data was received.

silviu

2006-12-13 19:20:46

by Jan Engelhardt

[permalink] [raw]
Subject: Re: get device from file struct


>in fs/read_write.c, the vfs_read function does:
>
>file->f_op->read(file, buf, count, pos);
>
>after this call is it possible to determine where the
>data is coming from?
>e.g., the first hard disk, a pipe or from a socket.

For hard disks:
file->f_dentry->d_inode->d_sb->s_bdev gives you the block device
in case it is a non-virtual filesystem.

For pipes/sockets I do not know of a may to go from a filp to a
struct sock or struct socket.

>If it is a socket we are interested
>from which device (eth0, eth1, lo, ...) the data was received.

I do not think that is possible either.


-`J'
--

2006-12-14 01:04:44

by Stephen Hemminger

[permalink] [raw]
Subject: Re: get device from file struct

On Wed, 13 Dec 2006 20:19:51 +0100 (MET)
Jan Engelhardt <[email protected]> wrote:

>
> >in fs/read_write.c, the vfs_read function does:
> >
> >file->f_op->read(file, buf, count, pos);
> >
> >after this call is it possible to determine where the
> >data is coming from?
> >e.g., the first hard disk, a pipe or from a socket.
>
> For hard disks:
> file->f_dentry->d_inode->d_sb->s_bdev gives you the block device
> in case it is a non-virtual filesystem.
>
> For pipes/sockets I do not know of a may to go from a filp to a
> struct sock or struct socket.
>
> >If it is a socket we are interested
> >from which device (eth0, eth1, lo, ...) the data was received.
>
> I do not think that is possible either.

The connection between file and network device is through many
layers and there is no direct binding. It could be 0 to N interfaces
and even be data dependent.

--
Stephen Hemminger <[email protected]>

2006-12-14 13:03:59

by Silviu Craciunas

[permalink] [raw]
Subject: Re: get device from file struct

On Wed, 2006-12-13 at 15:53 -0800, Stephen Hemminger wrote:
> The connection between file and network device is through many
> layers and there is no direct binding. It could be 0 to N interfaces
> and even be data dependent.

you mean protocol dependent? yes,it goes trough the layer of the vfs but
I thought there may be a way to get to the struct net_device from the
struct file. You have the dev_base which contains all devices and in the
net_device there is the ifindex which is the unique identifier. I guess
there is no way to know the ifindex directly from a socket struct.

thanks
silviu

2006-12-14 18:46:56

by Stephen Hemminger

[permalink] [raw]
Subject: Re: get device from file struct

On Thu, 14 Dec 2006 14:03:28 +0100
Silviu Craciunas <[email protected]> wrote:

> On Wed, 2006-12-13 at 15:53 -0800, Stephen Hemminger wrote:
> > The connection between file and network device is through many
> > layers and there is no direct binding. It could be 0 to N interfaces
> > and even be data dependent.
>
> you mean protocol dependent? yes,it goes trough the layer of the vfs but
> I thought there may be a way to get to the struct net_device from the
> struct file. You have the dev_base which contains all devices and in the
> net_device there is the ifindex which is the unique identifier. I guess
> there is no way to know the ifindex directly from a socket struct.
>
> thanks
> silviu
>

You can have things like netfilter and traffic classifiers that look
at packet.

--
Stephen Hemminger <[email protected]>