4

I want to get the filesystem mount point for a file system in my kernel module, which is a stackable filesystem. Eg. if /home/ab/abc is a file and /home is mounted on a different filesystem I want to have a dentry or path struct to /home. I want to do this in a module without modifying kernel code. e.g. there is a file in /home/user/ and now I want to know in which partition this file is. For example this file might be in the partition same as "/" or this file might be in another partition mounted on /home or /home/user.

gaurav
  • 872
  • 2
  • 10
  • 25

2 Answers2

7

You can get the list of file systems from current->namespace. By iterating current->namespace->list (items being struct vfsmount) you can get all mounted file systems. vfsmount->mnt_mountpoint is the directory entry you want.

You can follow the code that prints /proc/mounts (e.g. base.c/mountstats_open, namespace.c/m_start) to get more details (e.g. some locking is needed).

I don't know if you can do it in a kernel module, however.

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
ugoren
  • 16,023
  • 3
  • 35
  • 65
  • Thanks for your reply. Actually I wanted to know how to know the mountpoint of a particular file. I have edited the question please if you could take a look at it. – gaurav Jan 04 '12 at 17:14
  • @gaurav please realize that there could be multiple mount points for any given inode. – Ahmed Masud May 16 '18 at 14:29
1

If someone still needs to "dereference" given path to its mount point there is follow_up function which does exactly this thing :) It operates on the struct path and changes it accordingly to its .mnt entry. Maybe someone will find it handy!

Edit: I've used it in the 5.6 kernel version. I don't know if it was always available (<5.6)

Community
  • 1
  • 1
Radoj11
  • 31
  • 4