gpt4 book ai didi

c - 在文件创建期间,struct file_operations 和 struct file 何时连接?

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:31 25 4
gpt4 key购买 nike

我正在跟踪 open() 系统调用,以查明 struct file_operationsstruct file 在创建文件期间何时连接.

主要路径如下:

sys_open -> do_sys_open -> do_filp_open -> nameidata_to_filp -> __dentry_open

在 __dentry_open 中

 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
int flags, struct file *f,
int (*open)(struct inode *, struct file *),
const struct cred *cred)
{
struct inode *inode;
int error;

f->f_flags = flags;
f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
FMODE_PREAD | FMODE_PWRITE;
inode = dentry->d_inode;
if (f->f_mode & FMODE_WRITE) {
error = __get_file_write_access(inode, mnt);
if (error)
goto cleanup_file;
if (!special_file(inode->i_mode))
file_take_write(f);
}

f->f_mapping = inode->i_mapping;
f->f_path.dentry = dentry;
f->f_path.mnt = mnt;
f->f_pos = 0;
f->f_op = fops_get(inode->i_fop);//I think it is here that they get connected
file_move(f, &inode->i_sb->s_files);

error = security_dentry_open(f);

...

但是 inode 中的 i_fop 何时以及在哪个函数中被初始化?

最佳答案

你见过this吗?和 this :

The open(2) system call is implemented in fs/open.c:sys_open function and the real work is done by fs/open.c:filp_open() function, which is split into two parts:

open_namei(): fills in the nameidata structure containing the dentry and vfsmount structures. dentry_open(): given a dentry and vfsmount, this function allocates a new struct file and links them together; it also invokes the filesystem specific f_op->open() method which was set in inode->i_fop when inode was read in open_namei() (which provided inode via dentry->d_inode).

它实际上是在path_walk 函数中设置的(如果文件存在):

path_walk(const char *name, struct nameidata *nd) {
/* ... */
/* if . or .. then special, otherwise: */
dentry = cached_lookup(nd->dentry, &this);
/* ... */
if (!dentry)
dentry = real_lookup(nd->dentry, &this);

dentry 在其d_inode 成员中包含inode 信息。所以初始化 inode 在 open_namei 函数中(或下面的某个地方),在 dentry_open 之前。只需跟踪 dentry 结构。

关于c - 在文件创建期间,struct file_operations 和 struct file 何时连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9625393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com