So, the problem is: I've got a block device, for example, /dev/sdd1, containing a filesystem, e.g. EXT3 or XFS, mounted under /mnt/testdisk.
There's also a file, /mnt/testdisk/somefile.bin.
What I want, is to get which device that file is on, in this case, "/dev/sdd1". And, to make the matters worse, I have to do it in both userspace and kernel module (It's a linux driver. It doesn't have to be portable).
In userspace: my current attempt is to open
/proc/mounts
and parse it line-by-line to find the best match for the pathname. It works, but I think there's must be a better way...
In kernel driver: I'm trying to open the file, "/mnt/testdisk/somefile.bin", using filp_open from linux/fs.h header.
struct file *testfile;
struct inode *inode;
testfile = filp_open("/mnt/testdisk/somefile.bin", (O_RDWR | O_LARGEFILE), OP_FLAGS);
inode = testfile->f_mapping->host;
however, for some reason
inode->i_bdev == NULL
so I can't extract block device path from that :(
I know that theoretically it's a bad thing to open files from kernelspace, but yeah, whatever, don't care.
So, for each case, what's the best way to solve that conundrum?