如何从内核模块确定文件类型?
How to determine file type from kernel module?
是否有类似struct dirent* -> d_type
的内核结构包含DT_REG
、DT_DIR
、DT_SOCK
等,例如struct file
?查看它的字段,我找不到任何用于此目的的东西。
也许有人知道readdir
如何确定d_type
?我正在这里 https://github.com/lattera/glibc/blob/master/dirent/readdir.c 查看它的实现,但我不明白这里发生了什么。
Ubuntu18.04、4.15.0-45内核版本
struct inode
field i_mode
是一个位字段,可以使用标准 S_ISDIR
、S_ISREG
、S_ISLNK
等 [=26] 进行检查=]宏:
/*
* Keep mostly read-only and often accessed (especially for
* the RCU path lookup and 'stat' data) fields at the beginning
* of the 'struct inode'
*/
struct inode {
umode_t i_mode;
unsigned short i_opflags;
kuid_t i_uid;
kgid_t i_gid;
.
.
.
An example of its use in ext4 kernel code:
/*
* Test whether an inode is a fast symlink.
* A fast symlink has its symlink data stored in ext4_inode_info->i_data.
*/
int ext4_inode_is_fast_symlink(struct inode *inode)
{
if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
int ea_blocks = EXT4_I(inode)->i_file_acl ?
EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
if (ext4_has_inline_data(inode))
return 0;
return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
}
return S_ISLNK(inode->i_mode) && inode->i_size &&
(inode->i_size < EXT4_N_BLOCKS * 4);
}
请注意,您需要非常小心地遍历此类内核结构。如果您没有使用适当的锁,它们可能会从检查它们的线程下变出。
是否有类似struct dirent* -> d_type
的内核结构包含DT_REG
、DT_DIR
、DT_SOCK
等,例如struct file
?查看它的字段,我找不到任何用于此目的的东西。
也许有人知道readdir
如何确定d_type
?我正在这里 https://github.com/lattera/glibc/blob/master/dirent/readdir.c 查看它的实现,但我不明白这里发生了什么。
Ubuntu18.04、4.15.0-45内核版本
struct inode
field i_mode
是一个位字段,可以使用标准 S_ISDIR
、S_ISREG
、S_ISLNK
等 [=26] 进行检查=]宏:
/*
* Keep mostly read-only and often accessed (especially for
* the RCU path lookup and 'stat' data) fields at the beginning
* of the 'struct inode'
*/
struct inode {
umode_t i_mode;
unsigned short i_opflags;
kuid_t i_uid;
kgid_t i_gid;
.
.
.
An example of its use in ext4 kernel code:
/*
* Test whether an inode is a fast symlink.
* A fast symlink has its symlink data stored in ext4_inode_info->i_data.
*/
int ext4_inode_is_fast_symlink(struct inode *inode)
{
if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
int ea_blocks = EXT4_I(inode)->i_file_acl ?
EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
if (ext4_has_inline_data(inode))
return 0;
return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
}
return S_ISLNK(inode->i_mode) && inode->i_size &&
(inode->i_size < EXT4_N_BLOCKS * 4);
}
请注意,您需要非常小心地遍历此类内核结构。如果您没有使用适当的锁,它们可能会从检查它们的线程下变出。