2002-11-14 13:36:57

by Chandrasekhar

[permalink] [raw]
Subject: Path Name to kdev_t

Hi,

In one of the part of my driver module , I have a path name to a device file
(for eg:- /dev/hda1) .Now if I want to obtain the associated major number
and minor number i.e. device ID(kdev_t) of this file what would be the
procedure?

Thanks and Regards
Chandrasekhar


2002-11-14 15:14:11

by Gianni Tedesco

[permalink] [raw]
Subject: Re: Path Name to kdev_t

On Thu, 2002-11-14 at 13:49, chandrasekhar.nagaraj wrote:
> In one of the part of my driver module , I have a path name to a device file
> (for eg:- /dev/hda1) .Now if I want to obtain the associated major number
> and minor number i.e. device ID(kdev_t) of this file what would be the
> procedure?

You need to lookup the inode from the path using namei() or something
then it's just a field in the inode.

Check out fs/namei.c and related headers for more details.

--
// Gianni Tedesco (gianni at ecsc dot co dot uk)
lynx --source http://www.scaramanga.co.uk/gianni-at-ecsc.asc | gpg --import
8646BE7D: 6D9F 2287 870E A2C9 8F60 3A3C 91B5 7669 8646 BE7D


Attachments:
signature.asc (232.00 B)
This is a digitally signed message part

2002-11-14 15:24:43

by Steven Dake

[permalink] [raw]
Subject: Re: Path Name to kdev_t

You would want to find a struct file * that represents the device (look
at fs/open.c in the sys_open calls, it shows how to convert a filename
to a file *) then look at file->f_dentry->d_inode->i_bdev->bd_dev. This
is the major/minor dev_t for the device.

Hope this helps.
Thanks
-steve
chandrasekhar.nagaraj wrote:

>Hi,
>
>In one of the part of my driver module , I have a path name to a device file
>(for eg:- /dev/hda1) .Now if I want to obtain the associated major number
>and minor number i.e. device ID(kdev_t) of this file what would be the
>procedure?
>
>Thanks and Regards
>Chandrasekhar
>
>-
>To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>the body of a message to [email protected]
>More majordomo info at http://vger.kernel.org/majordomo-info.html
>Please read the FAQ at http://www.tux.org/lkml/
>
>
>
>
>

2002-11-14 17:48:45

by Irfan Hamid

[permalink] [raw]
Subject: Re: Path Name to kdev_t

in all functions of the driver where you will need the kdev_t (e.g.: the VFS
layer hooks) you will receive either a struct inode* and/or the struct file*
of the device file. the i_rdev member of struct inode is defined as the
kdev_t of the particular device.

hope this helps.

regards,
irfan.

On Thursday 14 November 2002 01:49 pm, chandrasekhar.nagaraj wrote:
> Hi,
>
> In one of the part of my driver module , I have a path name to a device
> file (for eg:- /dev/hda1) .Now if I want to obtain the associated major
> number and minor number i.e. device ID(kdev_t) of this file what would be
> the procedure?
>
> Thanks and Regards
> Chandrasekhar
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/

2002-11-15 16:09:35

by Joe Thornber

[permalink] [raw]
Subject: Re: Path Name to kdev_t

On Thu, Nov 14, 2002 at 07:19:16PM +0530, chandrasekhar.nagaraj wrote:
> Hi,
>
> In one of the part of my driver module , I have a path name to a device file
> (for eg:- /dev/hda1) .Now if I want to obtain the associated major number
> and minor number i.e. device ID(kdev_t) of this file what would be the
> procedure?

I think this should be standard function, I'm sure lots of people are
duplicating this code. For 2.4 kernels:

/*
* Convert a device path to a kdev_t.
*/
static int lookup_device(const char *path, kdev_t *dev)
{
int r;
struct nameidata nd;
struct inode *inode;

if (!path_init(path, LOOKUP_FOLLOW, &nd))
return 0;

if ((r = path_walk(path, &nd)))
goto out;

inode = nd.dentry->d_inode;
if (!inode) {
r = -ENOENT;
goto out;
}

if (!S_ISBLK(inode->i_mode)) {
r = -EINVAL;
goto out;
}

*dev = inode->i_rdev;

out:
path_release(&nd);
return r;
}

2002-11-15 16:21:58

by Christoph Hellwig

[permalink] [raw]
Subject: Re: Path Name to kdev_t

On Fri, Nov 15, 2002 at 04:15:36PM +0000, Joe Thornber wrote:
> On Thu, Nov 14, 2002 at 07:19:16PM +0530, chandrasekhar.nagaraj wrote:
> > Hi,
> >
> > In one of the part of my driver module , I have a path name to a device file
> > (for eg:- /dev/hda1) .Now if I want to obtain the associated major number
> > and minor number i.e. device ID(kdev_t) of this file what would be the
> > procedure?
>
> I think this should be standard function, I'm sure lots of people are
> duplicating this code. For 2.4 kernels:
>
> /*
> * Convert a device path to a kdev_t.
> */
> static int lookup_device(const char *path, kdev_t *dev)
> {
> int r;
> struct nameidata nd;
> struct inode *inode;
>
> if (!path_init(path, LOOKUP_FOLLOW, &nd))
> return 0;

missing LOOKUP_POSITIVE

>
> if ((r = path_walk(path, &nd)))
> goto out;
>
> inode = nd.dentry->d_inode;
> if (!inode) {
> r = -ENOENT;
> goto out;
> }
>
> if (!S_ISBLK(inode->i_mode)) {
> r = -EINVAL;
> goto out;
> }

shouldb be -ENOTBLK

new check here:

if (nd.mnt->mnt_flags & MNT_NODEV)
r = -EACCES;

>
> *dev = inode->i_rdev;
>
> out:
> path_release(&nd);
> return r;

I also think that this doesn not make much sense, you really want
name to properly opened struct block_device * instead, i.e. the firsdt halve
of get_sb_bdev()

2002-11-15 18:03:20

by Alexander Viro

[permalink] [raw]
Subject: Re: Path Name to kdev_t



On Fri, 15 Nov 2002, Joe Thornber wrote:

> On Thu, Nov 14, 2002 at 07:19:16PM +0530, chandrasekhar.nagaraj wrote:
> > Hi,
> >
> > In one of the part of my driver module , I have a path name to a device file
> > (for eg:- /dev/hda1) .Now if I want to obtain the associated major number
> > and minor number i.e. device ID(kdev_t) of this file what would be the
> > procedure?
>
> I think this should be standard function, I'm sure lots of people are
> duplicating this code. For 2.4 kernels:

No, it really shouldn't. You should _NOT_ mess with kdev_t - use real
objects instead.