linux内核中的major()和minor()函数

时间:2021-08-17 09:04:59

Hi I'm running vfs_stat, fd is an opened file desciptor of /dev/tty0:

嗨我正在运行vfs_stat,fd是/ dev / tty0的打开文件说明符:

set_fs (KERNEL_DS);
if (vfs_fstat (fd, &stat))
{
    goto out3;
}

if (stat.mode & S_IFCHR)
    printk (KERN_INFO "opening %s (dev %d)\n", filename, stat.rdev);

And it prints:

它打印:

[ 8657.480625] opening /dev/tty0 (dev 4194304)

So now I need to retrieve major number of the device, but I couldn't find major() or minor() definition in linux kernel.

所以现在我需要检索设备的主要数量,但我在linux内核中找不到major()或minor()定义。

I found this answer but it doesn't seem to be right:

我找到了这个答案,但似乎不对:

#define major(dev) ((int)(((unsigned int) (dev) >> 8) & 0xff))
#define minor(dev) ((int)((dev) & 0xff))

Because if I do printk (KERN_INFO "opening %s (dev %d)\n", filename, major (stat.rdev)); The second field is always zero.

因为如果我做printk(KERN_INFO“打开%s(dev%d)\ n”,filename,major(stat.rdev));第二个字段始终为零。

How should I get the major number then?

那我该怎么办?

1 个解决方案

#1


1  

I found linux/include/linux/kdev_t.h which has:

我发现linux / include / linux / kdev_t.h有:

#define MINORBITS        20
#define MINORMASK        ((1U << MINORBITS) - 1)

#define MAJOR(dev)        ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev)        ((unsigned int) ((dev) & MINORMASK))
#define MKDEV(ma,mi)      (((ma) << MINORBITS) | (mi))

Which seems "better" than the code you showed, since this has more bits allocated for the major number (20 rather than 8).

这看起来比你展示的代码“更好”,因为它有更多的位分配给主要数字(20而不是8)。

#1


1  

I found linux/include/linux/kdev_t.h which has:

我发现linux / include / linux / kdev_t.h有:

#define MINORBITS        20
#define MINORMASK        ((1U << MINORBITS) - 1)

#define MAJOR(dev)        ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev)        ((unsigned int) ((dev) & MINORMASK))
#define MKDEV(ma,mi)      (((ma) << MINORBITS) | (mi))

Which seems "better" than the code you showed, since this has more bits allocated for the major number (20 rather than 8).

这看起来比你展示的代码“更好”,因为它有更多的位分配给主要数字(20而不是8)。