简体   繁体   English

在内核模块中查找目录

[英]lookup a directory in kernel module

I am writing a stackable file system which would rename unlinked files to a particular folder say abc by default. 我正在编写一个可堆叠的文件系统,默认情况下会将未链接的文件重命名为一个特定的文件夹,例如abc。 So as any file /xyz is unlinked its renamed to /abc/xyz. 因此,当任何文件/ xyz取消链接时,其重命名为/ abc / xyz。 I want to do this by overriding the unlink function of stackable file system. 我想通过重写可堆叠文件系统的取消链接功能来做到这一点。 I am using wrapfs so I am modifying wrapfs_unlink for this. 我正在使用wrapfs,所以我为此修改了wrapfs_unlink。 I have dentry of the unlinked file also I have inode of parent directory , now I need to have inode of /abc and dentry of /abc/xyz to call vfs_rename instead of vfs_unlink. 我有未链接文件的dentry,也有父目录的inode,现在我需要/ abc的inode和/ abc / xyz的dentry来调用vfs_rename而不是vfs_unlink。 I could find the dentry and vfsmount for the / so I have a dentry for / but I don't know how to get the dentry/inode of /abc I know I can get inode from dentry but I cannot get dentry also. 我可以找到/的dentry和vfsmount,所以我有一个for / dentry,但我不知道如何获取/ abc的dentry / inode,我知道我可以从dentry获取inode,但我也无法获得dentry。 I tried using lookup_one_len /abc is created but still it returns a negative inode , also I tried to use vfs_path_lookup to find the directory /abc it also returns an error. 我尝试使用lookup_one_len / abc创建,但仍返回负的inode,我也尝试使用vfs_path_lookup查找目录/ abc,它也返回错误。 Am I using wrong functions? 我使用了错误的功能吗? Or these methods see cache only not the actual directory structure ? 还是这些方法只看到缓存而不看到实际的目录结构? Please help. 请帮忙。

You can use the following code to move an object into the trash at unlink. 您可以使用以下代码在取消链接时将对象移至垃圾箱。

static int move_to_trash(struct dentry * trash, struct dentry * object)
{
    int result;
    char name[32];
    struct dentry * de;

    sprintf(name, "XX-%lu", object->d_inode->i_ino);

    de = d_alloc_name(trash, name);
    if (!de)
        return -ENOMEM;

    trash->d_inode->i_op->lookup(trash->d_inode, de, NULL);

    mutex_lock(&trash->d_inode->i_mutex);
    result = trash->d_inode->i_op->link(object, trash->d_inode, de);
    mutex_unlock(&trash->d_inode->i_mutex);

    dput(de);

    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM