1. 先獲取dentry所屬文件系統(tǒng)對應(yīng)的掛載點,基本原理是遍歷文件系統(tǒng)vfsmount樹,找到與dentry有相同超級塊的vfsmount,實現(xiàn)如下
1
extern spinlock_t *vfsmnt_lock;
2
3
static struct vfsmount* next_mnt(struct vfsmount *p, struct vfsmount *root)
4
{
5
struct list_head *next = p->mnt_mounts.next;
6
if (next == &p->mnt_mounts) {
7
while (1) {
8
if (p == root)
9
return NULL;
10
next = p->mnt_child.next;
11
if (next != &p->mnt_parent->mnt_mounts)
12
break;
13
p = p->mnt_parent;
14
}
15
}
16
return list_entry(next, struct vfsmount, mnt_child);
17
}
18
19
static struct vfsmount* get_dentry_mnt(struct dentry *dentry)
20
{
21
struct vfsmount *p, *root;
22
struct fs_struct *fs = current->fs;
23
24
read_lock(&fs->lock);
25
root = fs->root.mnt;
26
mntget(root);
27
read_unlock(&fs->lock);
28
29
spin_lock(vfsmnt_lock);
30
for(p = root; p; p = next_mnt(p,root)){
31
if(p->mnt_sb == dentry->d_sb){
32
mntget(p);
33
break;
34
}
35
}
36
spin_unlock(vfsmnt_lock);
37
38
mntput(root);
39
40
return p;
41
}
extern spinlock_t *vfsmnt_lock;2

3
static struct vfsmount* next_mnt(struct vfsmount *p, struct vfsmount *root)4
{5
struct list_head *next = p->mnt_mounts.next;6
if (next == &p->mnt_mounts) {7
while (1) {8
if (p == root)9
return NULL;10
next = p->mnt_child.next;11
if (next != &p->mnt_parent->mnt_mounts)12
break;13
p = p->mnt_parent;14
}15
}16
return list_entry(next, struct vfsmount, mnt_child);17
}18

19
static struct vfsmount* get_dentry_mnt(struct dentry *dentry)20
{21
struct vfsmount *p, *root;22
struct fs_struct *fs = current->fs; 23

24
read_lock(&fs->lock);25
root = fs->root.mnt;26
mntget(root);27
read_unlock(&fs->lock);28

29
spin_lock(vfsmnt_lock);30
for(p = root; p; p = next_mnt(p,root)){31
if(p->mnt_sb == dentry->d_sb){32
mntget(p);33
break; 34
}35
}36
spin_unlock(vfsmnt_lock);37

38
mntput(root);39
40
return p;41
}2. 再調(diào)用內(nèi)核函數(shù)d_path,接口封裝如下
1
char* get_dentry_path(struct dentry *dentry,char *buf,int len)
2
{
3
char *p = "";
4
struct vfsmount *mnt = get_dentry_mnt(dentry);
5
6
if(mnt){
7
struct path ph = {.dentry = dentry, .mnt = mnt};
8
p = d_path(&ph,buf,len);
9
if(IS_ERR(p))
10
p = "";
11
mntput(mnt);
12
}
13
14
return p;
15
}
char* get_dentry_path(struct dentry *dentry,char *buf,int len)2
{3
char *p = ""; 4
struct vfsmount *mnt = get_dentry_mnt(dentry);5
6
if(mnt){7
struct path ph = {.dentry = dentry, .mnt = mnt};8
p = d_path(&ph,buf,len);9
if(IS_ERR(p))10
p = "";11
mntput(mnt);12
}13
14
return p;15
}




