get vfsmount from dentry
Hi,
I'm trying to develop a fmon (file monitor) patch. For that I'm trying to find complete pathname of the file/directory being changed. If you have dentry of a file and a corresponding vfsmount struct, I can find out its entire pathname using following code. Which works exactly the way I want.
static char * mirror_path (struct dentry *dentry, struct vfsmount *vfsmnt, char *buffer, int buflen)
{
char * end = buffer+buflen;
char * retval;
int namelen;
*--end = '\0';
buflen--;
if (buflen < 1)
goto Elong;
/* Get '/' right */
retval = end-1;
*retval = '/';
for (;;) {
struct dentry * parent;
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
/* Global root? */
spin_lock(&vfsmount_lock);
if (vfsmnt->mnt_parent == vfsmnt) {
spin_unlock(&vfsmount_lock);
break;
}
dentry = vfsmnt->mnt_mountpoint;
vfsmnt = vfsmnt->mnt_parent;
spin_unlock(&vfsmount_lock);
continue;
}
parent = dentry->d_parent;
prefetch(parent);
namelen = dentry->d_name.len;
buflen -= namelen + 1;
if (buflen < 0)
goto Elong;
end -= namelen;
memcpy(end, dentry->d_name.name, namelen);
*--end = '/';
retval = end;
dentry = parent;
}
namelen = dentry->d_name.len;
buflen -= namelen;
if (buflen < 0)
goto Elong;
retval -= namelen-1; /* hit the slash */
memcpy(retval, dentry->d_name.name, namelen);
return retval;
Elong:
return NULL;
}Now the only problem is in many functions in the filesystem code, I have access only to the dentry and not to its vfsmount. I can do one thing I can change the function definition and pass corresponding vfsmount as a parameter to the function, but this will lead to lot of changes.
Is there any way by which I can found the vfsmount structure of a file represented by dentry structure?
Regards,
- Ashish
Quoted 7 lines · Ashish Khurange
Hi,
I'm trying to develop a fmon (file monitor) patch. For that I'm trying to find complete pathname of the file/directory being changed. If you have dentry of a file and a corresponding vfsmount struct, I can find out its entire pathname using following code. Which works exactly the way I want.
Quoted 7 lines
Now the only problem is in many functions in the filesystem code, I have access only to the dentry and not to its vfsmount. I can do one thing I can change the function definition and pass corresponding vfsmount as a parameter to the function, but this will lead to lot of changes.
Is there any way by which I can found the vfsmount structure of a file represented by dentry structure?
Which one?
There is no such thing as pathname of a file. It can be present in any number of places, including 0. Moreover, the pathname that makes sense for one process doesn't have to resolve to the same file (or even resolve at all) for others.
So what are you really trying to achieve?
Quoted 4 lines · Al Viro
Which one?
The name used by a process to access the file.
Quoted 2 lines
There is no such thing as pathname of a file.
Please do not confuse me in jargon of words. I may not be clear in terminology.
Quoted 6 lines
Moreover, the pathname that makes sense
for one process doesn't have to resolve to the same file (or even resolve at all) for others.
Yes thats it, the pathname that is used by a process to access a file, I want to that pathname.
Quoted 3 lines
So what are you really trying to achieve?
To get that pathname along with dentry of the file I need the vfsmount of that file also. Is there any way to get vfsmount of a file, when you have its dentry with you.
- Ashish
Quoted 2 lines · Ashish Khurange
Yes thats it, the pathname that is used by a process to access a file, I want to that pathname.
Which process?
Quoted 6 lines
Quoted 1 line
So what are you really trying to achieve?
To get that pathname along with dentry of the file I need the vfsmount of that file also.
Is there any way to get vfsmount of a file, when you have its dentry with you.
No. Simply because there may be many pathnames giving the same dentry.
mkdir /tmp/1
mkdir /tmp/2
mkdir /tmp/3
mount -t ramfs none /tmp/1
mount --bind /tmp/1 /tmp/2
mount --bind /tmp/1 /tmp/3
touch /tmp/1/foo
umount /tmp/1
Now /tmp/2/foo and /tmp/3/foo have exact same dentry. They do have different vfsmounts. In other words, dentry alone is simply not enough to find the pathname. Moreover, if you do
mkdir /tmp/1
mount -t ramfs none /tmp/1
exec >/tmp/1/foo
umount -l /tmp/1
echo a
you will have a written into file that has no pathname. I.e. there is no such pathname that opening it at that point would result in that file. At all.
It's the same situation as with getting pathname by inode - there may be many (hardlinks), there may be none (unlinked but still open).
In other words, the answer to your question above is "no, there is no way to find vfsmount by dentry and that is in principle impossible". So unless you can give higher-level description of what you want to get, you are out of luck.
Quoted 4 lines · Al Viro
On Sun, Mar 12, 2006 at 10:50:51PM +0530, Ashish Khurange wrote:
Quoted 8 lines
Quoted 3 lines
Yes thats it, the pathname that is used by a process to access a file, I want to that pathname.
Which process?
come on ...
As per my knowledge in vfs layer file is either accessed by pathname or file structure pointer. In file struct you have both dentry of the file and vfsmount. When a file is accessed by a name first path look up happens and its nameidata is created which holds both dentry and vfsmount. In may function calls only dentry is passed as an argument. Now I want the path name of the file (forget what I want, as term pathname is not clear between us). For some reason, I need vfsmount of the file as well.
Regards,
- Ashish
Quoted 8 lines · Ashish Khurange
As per my knowledge in vfs layer file is either accessed by pathname or file structure pointer. In file struct you have both dentry of the file and vfsmount. When a file is accessed by a name first path look up happens and its nameidata is created which holds both dentry and vfsmount. In may function calls only dentry is passed as an argument. Now I want the path name of the file (forget what I want, as term pathname is not clear between us). For some reason, I need vfsmount of the file as well.
And I am telling you that dentry alone is not enough to find vfsmount. So unless the thing you need it for can't be done in other way, you are out of luck. See the previous mail for the reasons...
Again, if for some reason you really need to find vfsmount inside a function that gets only dentry, you are stuck. Without knowing _why_ you need to find it and need to do that inside such function, there's nothing I (or anybody else) can do - as far as I know nobody here is a telepath.
Quoted 10 lines · Ashish Khurange
Quoted 1 line
come on ...
As per my knowledge in vfs layer file is either accessed by pathname or file structure pointer. In file struct you have both dentry of the file and vfsmount. When a file is accessed by a name first path look up happens and its nameidata is created which holds both dentry and vfsmount. In may function calls only dentry is passed as an argument. Now I want the path name of the file (forget what I want, as term pathname is not clear between us). For some reason, I need vfsmount of the file as well.
you don't get it.
case a.
1. fd1 = open("/tmp/123")
2. mount bind "/tmp/456" -> "/tmp/123"
3. fd2 = open("/tmp/123")
both have the same exact "same" path, but 2 separate dentry.
case b.
1. fd1 = open("/tmp/123")
2. mount bind "/tmp/123" -> "/tmp/456"
3. fd2 = open("/tmp/456")
both have different paths, but will share the same dentry.
linux (and possibly other modern OSs) let us muck with the file system namespace without modifying the underlying file systems.
The question is, what are you trying to do? Why can't you use a "struct file" (which will have the relevant vfsmnt). If you can't say, then its hard for people to help you, most people here want to solve something "right", and without knowing the real problem don't feel its worth giving a half ass solution.
Quoted 9 lines · Shaya Potter
The question is, what are you trying to do? Why can't you use a "struct file" (which will have the relevant vfsmnt). If you can't say, then its hard for people to help you, most people here want to solve something "right", and without knowing the real problem don't feel its worth giving a half ass solution.
I'm trying to build file monitor patch. I want to detect all the changes happening to file system. For this I need to insert my code in various functions in fs/open.c, fs/ nami.c.
Functions like vfs_mkdir, vfs_rmdir, notify_change, sys_write etc.
Whenever a change to file system happens I want to track that change with the name of of the file and the change happened to it.
The function which I send in my previous mail can be used to get the full pathname of the file being changed (pathname : lets say the name with which file is accessed). Now for this function I need both dentry and vfsmount struct of the file. In few functions (like sys_fchmod) I have file struct as an argument, which provides me both vfsmount and dentry, hence I can find out pathname of the file being changed.
Where as in few functions (like vfs_mkdir) I have only dentry and inode of the directory, which are not sufficient to find the pathname.
I hope I am clear now.
- Ashish
Quoted 2 lines · Ashish Khurange
Is there any way by which I can found the vfsmount structure of a file represented by dentry structure?
No, it's not possible, because one dentry can refer to multiple paths which have different vfsmount structures.
-- Jamie
Quoted 7 lines · Ashish Khurange
In few functions (like
sys_fchmod) I have file struct as an argument, which provides me both vfsmount and dentry, hence I can find out pathname of the file being changed.
Where as in few functions (likevfs_mkdir) I have only dentry and inode of the directory, which are not sufficient to find the pathname.I hope I am clear now.
mkdir /tmp/1
mkdir /tmp/2
mkdir /tmp/3
mount --bind /tmp/2 /tmp/1
mkdir /tmp/1/a
umount /tmp/1
mount --bind /tmp/3 /tmp/1
mkdir /tmp/1/a
umount /tmp/1
What output do you want to get from the last two mkdir() calls, assuming you do have vfsmounts? What is this output useful for?
Quoted 10 lines · Al Viro
mkdir
/tmp/1
mkdir/tmp/2
mkdir/tmp/3
mount --bind/tmp/2/tmp/1
mkdir/tmp/1/a
umount/tmp/1
mount --bind/tmp/3/tmp/1
mkdir/tmp/1/a
umount/tmp/1
Quoted 4 lines
What output do you want to get from the last two
mkdir()calls, assuming you do have vfsmounts? What is this output useful for?
Well, forget about this particualr case.
My final aim is to build online file system mirroring tool for home directories. And lets assuming that no other user have superuser privilage to use mount or make symbolic links to directories. I want entire pathname of changed file to replicate that change on the corresponding file in the mirror site.
Quoted 16 lines · Ashish Khurange
Quoted 10 lines · Al Viro
mkdir
/tmp/1
mkdir/tmp/2
mkdir/tmp/3
mount --bind/tmp/2/tmp/1
mkdir/tmp/1/a
umount/tmp/1
mount --bind/tmp/3/tmp/1
mkdir/tmp/1/a
umount/tmp/1Quoted 4 lines
What output do you want to get from the last two
mkdir()calls, assuming you do have vfsmounts? What is this output useful for?
Quoted 6 lines
Well, forget about this particualr case.
My final aim is to build online file system mirroring tool for home directories. And lets assuming that no other user have superuser privilage to use mount or make symbolic links to directories. I want entire pathname of changed file to replicate that change on the corresponding file in the mirror site.
look at inotify. beagle monitors entire home directory via inotify.
Quoted 15 lines · Ashish Khurange
Quoted 10 lines · Al Viro
mkdir
/tmp/1
mkdir/tmp/2
mkdir/tmp/3
mount --bind/tmp/2/tmp/1
mkdir/tmp/1/a
umount/tmp/1
mount --bind/tmp/3/tmp/1
mkdir/tmp/1/a
umount/tmp/1Quoted 3 lines
What output do you want to get from the last two
mkdir()calls, assuming you do have vfsmounts? What is this output useful for?
Quoted 6 lines
Well, forget about this particualr case.
My final aim is to build online file system mirroring tool for home directories. And lets assuming that no other user have superuser privilage to use mount or make symbolic links to directories. I want entire pathname of changed file to replicate that change on the corresponding file in the mirror site.
And why do you need abvolute paths for that? You only need paths relative to root of that particular filesystem -- and you can get those without vfsmount.
Signature · 1 line
Jan 'Bulb' Hudec <[email protected]>